Skip to content

Sequelize Slugify is a plugin for the Sequelize ORM that automatically creates and updates unique, URL safe slugs for your database models.

License

Notifications You must be signed in to change notification settings

jarrodconnolly/sequelize-slugify

Repository files navigation

sequelize-slugify

Build Status npm Dependency Status Known Vulnerabilities GitHub license

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.

Installation

npm install sequelize-slugify

Requirements

You must place a slug field on your model something like this.

slug: {
    type: DataTypes.STRING,
    unique: true
}

Options

slugifyModel takes an options object as it's second parameter.

SequelizeSlugify.slugifyModel(User, {
    source: ['givenName'],
    slugOptions: { lower: true },
    overwrite: false,
    column: 'slug',
    incrementalReplacement: '-',
});

Available Options

  • source - (Required) Array of field names in the model to build the slug from.
  • suffixSource - (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).
  • slugOptions - (Default {lower: true}) Pass additional options for slug generation as defined by slug.
  • overwrite - (Default TRUE) Change the slug if the source fields change once the slug has already been built.
  • column - (Default slug) Specify which column the slug is to be stored into in the model.
  • incrementalReplacement - (Default -) Specify the separator between the slug and the duplicate count

Usage Examples

Basic Usage

import SequelizeSlugify from 'sequelize-slugify';

export default (sequelize, DataTypes) => {
    const 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;
};

Suffix Sources

import SequelizeSlugify from 'sequelize-slugify';

export default (sequelize, DataTypes) => {
    const 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;
};

Using getter functions as source

Using Sequelize getterMethods and VIRTUAL fields, you can derive slugs from any combination of model attributes using whatever custom logic you may need.

import SequelizeSlugify from 'sequelize-slugify';

export default (sequelize, DataTypes) => {
    const User = sequelize.define('User', {
        email: {
            type: DataTypes.STRING,
        },
        name: {
            type: DataTypes.STRING,
        },
        emailAsSlug: {
            type: DataTypes.VIRTUAL,
            get() {
              return this.get('email') && this.get('email').split('@')[0].replace('.', '-');
            },
        },
    }, {
        getterMethods: {
          timestamp() {
            return Date.now();
          },
        }
    });

    SequelizeSlugify.slugifyModel(User, {
        source: ['emailAsSlug'],
        suffixSource: ['timestamp'],
    });

    return User;
};

About

Sequelize Slugify is a plugin for the Sequelize ORM that automatically creates and updates unique, URL safe slugs for your database models.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project