Objection.js modelAutomatically generate slugs for an
This plugin will automatically generate slugs for your model based on a source field and a slug field. It will ensure that the slugs are unique by checking to see if the slug already exists in the model's table. If so, it will attempt to append a number to the end of the slug.
For example, if the source field is
'How to Fry an Egg', then the slug will be
'how-to-fry-an-egg'. However, if that slug already exists in the model's table
then the slug will be
'how-to-fry-an-egg-1' (note that
-1 was appended). If
that slug also exists, then the slug would be
'how-to-fry-an-egg-2' and so
npm install objection-slug
Why this package?
This package was inspired by
objection-slugify but it's
different in the following ways:
Appends a number instead of a UUID.
Instead of attempting to append a UUID to the end of the slug, which does not look nice, this package appends a sequential number to the end of duplicate slugs.
Removed unwanted features
There are several options which aren't useful and were removed. For example, instead of changing the slug when the source field changes (which breaks any URLs based on the slug, which is very bad for SEO), this package never changes the slug after it is generated.
Handles many more unicode symbols by default, because it uses the
mollusclibrary instead of
const objectionSlug =const Model =// Create the mixinconst slug =// Create the Model and add the mixinModel// ...codeconst post = await Postconsole// how-to-fry-an-egg
slug = objectionSlug([opts])
Create a slug mixin to be used with Objection.js models. See usage example above.
The source of the slugged content.
opts.slugField (defaults to
The field to store the slug on.
MIT. Copyright (c) Feross Aboukhadijeh.