jarrodconnolly / sequelize-slugify

Sequelize Slugify is a plugin for the Sequelize ORM that automatically creates and updates unique, URL safe slugs for your database models.
https://sequelize-slugify.nestedquotes.ca/
MIT License
56 stars 27 forks source link

Enhanced suffix generation #4

Closed maxdeviant closed 8 years ago

maxdeviant commented 8 years ago

I think it would be nice to have a list of suffix fields that would be used to create uniqueness before reverting to appending numbers to the slugs.

Similar to the source field, it would take an array of fields that it would use when creating a suffix.

SequelizeSlugify.slugifyModel(Movie, {
    source: ['title'],
    suffixSource: ['year'],
    overwrite: false
});

A case where this would be useful would be for something like movies, where two movies have the same title but are released in different years. Obviously, you could just include make the source ['title', 'year'] right off the bat, but then each slug would be required to have the year in it, even if there is no collision.

Example:

// added first
{ title: 'Gladiator', year: 2000, slug: 'gladiator' }

// added second (use the suffixSource)
{ title: 'Gladiator', year: 1992, slug: 'gladiator-1992' }

// added third (fall back to default suffix)
{ title: 'Gladiator', year: 1992, slug: 'gladiator-1992-1' }
jarrodconnolly commented 8 years ago

This is a pretty interesting idea, especially with the movies use case you brought up. I suspect there could be other use cases that would benefit from this.

I will take a look at how this could be implemented in the next little while.

Pull requests are always welcome of course.

maxdeviant commented 8 years ago

I would be happy to see about sending a PR your way. Just wanted to see what you thought of the idea before doing any work on it :wink: