Giter Club home page Giter Club logo

objection-slug's Introduction

objection-slug travis npm downloads javascript style guide

Automatically generate slugs for an Objection.js model

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).

And if that slug also exists, then the slug would be 'how-to-fry-an-egg-2' and so on...

Install

npm install objection-slug

Why this package?

This package was inspired by objection-slugify but it's different in the following ways:

  1. 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.

  2. 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.

  3. Handles many more unicode symbols by default, because it uses the mollusc library instead of slugify.

Usage

const objectionSlug = require('objection-slug')
const { Model } = require('objection')

// Create the mixin
const slug = objectionSlug({
  sourceField: 'title',
  slugField: 'slug'
})

// Create the Model and add the mixin
class Post extends slug(Model) {
  // ...code
}

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

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

API

slug = objectionSlug([opts])

Create a slug mixin to be used with Objection.js models. See usage example above.

opts.sourceField (required)

The source of the slugged content.

opts.slugField (defaults to 'slug')

The field to store the slug on.

License

MIT. Copyright (c) Feross Aboukhadijeh.

objection-slug's People

Contributors

feross avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.