- A plugin for Sequelize ORM models to automatically create and update unique slugs
- GitHub Actions CI tests
- PostgreSQL
- MySQL
- MariaDB
- SQLite
- MSSQL
- Semantic Versioning
- Supporting Node.js v10+
Sequelize Slugify is a plugin for the Sequelize ORM that automatically creates and updates unique, URL safe slugs for your database models.
Home Page: https://sequelize-slugify.nestedquotes.ca/
License: MIT License
@jarrodconnolly, would you accept a PR from me updating the README documentation to ES6 (import
, const
, etc)?
It might be a good idea to pass options.transaction
to checkSlug
. This plugin doesn't seem like it would be reliable for insertions of many records within a single transaction, since it doesn't scope the Model.find
to the transaction.
Scenario:
I have a model called Author
like so:
'use strict';
var SequalizeSlugify = require('sequelize-slugify');
module.exports = function (sequelize, DataTypes) {
var Author = sequelize.define('Author', {
slug: {
type: DataTypes.STRING,
unique: true
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
}
}, {
tableName: 'authors',
paranoid: true,
underscored: true,
classMethods: {
associate: function (models) {
Author.belongsToMany(models.Book, {as: 'BookAuthor', through: 'books_authors'});
}
}
});
SequalizeSlugify.slugifyModel(Author, {
source: ['name']
});
return Author;
};
Now I would like to do a find given an unslugified author name.
Author.FindOne({
where: {
slug: SequalizeSlugify.somethingmagical("John Doe")
});
Is your feature request related to a problem? Please describe.
Sequelize itself tests against mariadb, we should add this to our tests.
Describe the solution you'd like
Unit tests all pass.
Describe alternatives you've considered
N/A
Whenever the source column holds Unicode value, it converts into -1
.
For instance:
name: جنید عطاری
in the database, it will be
name: جنید عطاری
slug: -1
It may help you:
https://github.com/dzcpy/transliteration
The slug must also gets updated when the title is changed
Hello, I've found a problem. The hook beforeUpdate don't work for model's method update. I.e. slug's col doesn't update with name.
e.g.:
await Person.update(
{
name: name,
description: description
},
{ where: { id: id } }
)
But if I use this code:
const person = await Person.findByPk(id)
await person.update({
name: name,
description: description
})
Everything works well.
The Readme.md has a link to documentation pointing to https://sequelize-slugify.nestedquotes.ca/ But the URL is invalid.
NA
The url should point to a valid location. Or better still point to the internal doc links: for example
hi, I use like this
let slugify = require('sequelize-slugify');
slugify.slugifyModel(Nomenclatures, {
source: ['name'],
slugOptions: { lower: true },
overwrite: false,
column: 'slug'
});
In my case slug can not be null
await model.bulkCreate(json_data);
It gives me an error slug can not be null
Getting the following error:
sequelize deprecated Model.find has been deprecated, please use Model.findOne instead
Can you please update find
to findOne
?
/**
* Checks whether or not the slug is already in use.
*
* @param slug The slug to check for uniqueness.
* @return True if the slug is unique, false otherwise.
*/
var checkSlug = function (slug) {
var query = { where: {} };
query.where[slugColumn] = slug;
return Model.find(query).then(function (model) {
return model === null;
});
};
Is your feature request related to a problem? Please describe.
The unit tests are a mess :) clean them up and separate them into different files if possible.
Describe the solution you'd like
Should be easy to find the tests you want to run.
Describe alternatives you've considered
N/A
Node: v10.7.0
NPM: v6.4.0
{
...
"sequelize": "^4.38.0",
"sequelize-slugify": "^0.5.0",
...
}
Issue:
$ npm audit
=== npm audit security report ===
Manual Review
Some vulnerabilities require your attention to resolve
Visit https://go.npm.me/audit-guide for additional guidance
Moderate Regular Expression Denial of Service
Package slug
Patched in No patch available
Dependency of sequelize-slugify
Path sequelize-slugify > slug
More info https://nodesecurity.io/advisories/537
Hey guys, and thanks for building the plugin. I was wondering if there's a way to programmatically disable
sequelize-slugify if the user provide a custom slug.
Basically, I have a use-case where users are allowed to create their own custom slugs, e.g. for articles, and if one is provided, I would like this plugin to simply do nothing.
Is there a way to do this now, and if not, how can we help to implement this feature? I don't mind fiddling with the plugin and doing a PR.
Cheers
Is your feature request related to a problem? Please describe.
Sequelize itself tests against mssql, we should add this to our tests.
Describe the solution you'd like
Unit tests all pass.
Describe alternatives you've considered
N/A
Page.init(
{
id: {
type: DataTypes.UUID,
defaultValue: UUIDV4,
allowNull: false,
primaryKey: true
},
title: {
type: DataTypes.JSONB,
allowNull: false
},
_title: {
type: DataTypes.VIRTUAL,
get() {
const title: any = this.get('title');
return title[langs[0]];
}
},
},
{
sequelize,
tableName: 'pages',
modelName: 'Page'
}
);
SequelizeSlugify.slugifyModel(Page, { source: ['_title'] });
The slug
column is not getting _title
Virtual column as the documentation says.
Install sequelize-slugify
to a typescript project
Types are defined
src/posts/post.entity.ts:11:30 - error TS2307: Cannot find module 'sequelize-slugify' or its corresponding type declarations.
2021-08-17T08:13:24.667115708Z
2021-08-17T08:13:24.667126875Z 11 import SequelizeSlugify from 'sequelize-slugify';
2021-08-17T08:13:24.667267208Z ~~~~~~~~~~~~~~~~~~~
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' }
When we specify replacement
in the slugOptions
, it only replaces within the slug and not the incremental counter.
Example I used to slugify a model:
SequelizeSlugify.slugifyModel(People, {
source: ['first_name', 'last_name'],
slugOptions: {
replacement: '_'
},
overwrite: false,
column: 'identifier'
});
This will generate first_last
. If it already exist, it will generate first_last-1
.
I'd like to replace the separator for the counter to first_last_1
, first_last_2
, etc..
Hello Jarrod,
Since I've changed allowNull property of slug column to false, sequelize is throwing SequelizeValidationError: notNull Violation: MyModel.slug cannot be nullError
If we can execute handleSlugify on beforeValidate hook then the above issue will be solved.
Thank you.
Best regards,
Tanmay
I want to add a slug field to my model. I'm trying to do so with a new migration in using regenerateSlug
for all the model's instances, but I keep getting null as the slug. Looks like it's because forceGenerateSlug
is ignored when nothing changed on the model:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.