sequelize-slugify is a model plugin for Sequelize that automatically creates and updates unique slugs for your models.
So far this module has only been tested with the PostgreSQL database.
npm install sequelize-slugify
You must place a slug field on your model something like this.
slug: {
type: DataTypes.STRING,
unique: true
}
slugifyModel takes an options object as it's second parameter.
SequelizeSlugify.slugifyModel(User, {
source: ['givenName'],
overwrite: false
});
Available Options
source
- (Required) Array of field names in the model to build the slug fromsuffixSource
- (Optional) Array of field names in the model to use as the source for additional suffixes to make the slug unique (before defaulting to adding numbers to the end of the slug).overwrite
= (Default TRUE) Change the slug if the source fields change once the slug has already been built
var SequelizeSlugify = require('sequelize-slugify');
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
slug: {
type: DataTypes.STRING,
unique: true
},
emailAddress: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
givenName: {
type: DataTypes.STRING,
allowNull: false
},
familyName: {
type: DataTypes.STRING,
allowNull: false
}
});
SequelizeSlugify.slugifyModel(User, {
source: ['givenName', 'familyName']
});
return User;
};
var SequelizeSlugify = require('sequelize-slugify');
module.exports = function(sequelize, DataTypes) {
var Movie = sequelize.define('Movie', {
slug: {
type: DataTypes.STRING,
unique: true
},
title: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
year: {
type: DataTypes.INTEGER,
allowNull: false
}
});
SequelizeSlugify.slugifyModel(Movie, {
source: ['title'],
suffixSource: ['year']
});
return Movie;
};