Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including objection-slugify with all npm packages installed. Try it out:

require("objection/package.json"); // objection is a peer dependency. var objectionSlugify = require("objection-slugify")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

objection-slugify v3.1.0

Automatically slugifies a source field.

Build Status Coverage Status

Slugged models for Objection.js

This plugin will automatically generate slugs for your model based on a source field and a slug field. It will also generate unique slugs by checking to see if the slug already exists in the model's table.

Installation

npm install objection-slugify --save

Usage

// Import the plugin.
const slugifyPlugin = require('objection-slugify');
const { Model } = require('objection');

// Mixin the plugin.
const slugify = slugifyPlugin({
  sourceField: 'title',
  slugField: 'slug'
});

// Create your model.
class Post extends slugify(Model) {
  // ...code
}

const post = await Post.query().insert({
  title: 'How to Fry an Egg'
});

console.log(post.slug);
// how-to-fry-an-egg

Options

sourceField (required)

The source of the slugged content.

slugField (defaults to slug)

The field to store the slug on.

update (defaults to true)

Specifies whether the slug is updated when the source field is updated.

unique (defaults to false)

Checks to see if the generated slug is unique in the table. If not, it will append a UUID to the end of the slug.

generateUniqueSuffix (Function: String)

A custom function that returns a string. Can be used to generate a custom suffix to the end of the slug. If unique is true and this is not specified, a random UUID will be appended to the slug.

slugifyOptions (defaults to { lower: true })

A set of options for the internal slugify library, options available here.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free