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

Programmatically disabled the plugin if user provides a custom slug #56

Open agjs opened 2 years ago

agjs commented 2 years ago

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

jarrodconnolly commented 2 years ago

This is an interesting request, thank you for your interest.

My first thoughts are that you could probably disable overwrite and allow your code to set the slug field manually. This would tell the plugin not to overwrite the slug value if the source field changes. Depending on how your app works, it may cause issues with people who do not use custom slugs. Their slugs would not update if the source field changed unless you were to manually call regenerateSlug()

Let me know if that would be an option and I will think of other ways to solve this use case.

agjs commented 2 years ago

This is an interesting request, thank you for your interest.

My first thoughts are that you could probably disable overwrite and allow your code to set the slug field manually. This would tell the plugin not to overwrite the slug value if the source field changes. Depending on how your app works, it may cause issues with people who do not use custom slugs. Their slugs would not update if the source field changed unless you were to manually call regenerateSlug()

Let me know if that would be an option and I will think of other ways to solve this use case.

Hi, @jarrodconnolly and thanks for the quick reply.

Let me describe the use-case, as I believe for people who want to support custom slugs, like e.g. WordPress does, would want this same behaviour.

The reason why overwrite property isn't sufficient is due to the edge-case.

If we would set the overwrite to false, the slug would never be regenerated, and this is fine in the context of the custom slugs. If the custom slug, we indeed don't want the plugin to do anything. Whatsoever, slug is sometimes going to be custom, sometimes not. Sometimes a user might have a custom slug, and then at one point decide to remove it.

In my opinion, I don't think this will be possible without introducing some additional property, either that user has to send through the actual request object or something through the default plugin configuration object. This is because there would be no way for this plugin right now to figure out if something is custom or not.

Let me know if you have any ideas, I'm more then happy to help.

jarrodconnolly commented 1 year ago

Trying out an idea for this logic here: https://github.com/jarrodconnolly/sequelize-slugify/compare/main...custom-slug

Edit to add more precise detail to the change: I added a new config param: slugOverride This specifies a column that you would populate for the user's custom slug value. This value is also copied to the regular slug column on saving the model. If specified, the rest of the slug logic is bypassed. You would always read your slug value from the regular slug column.

Take a look at that diff or test that branch if possible. Let me know if this works for your use case or if something is still missing.

Thanks!