npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-slugin

v0.1.4

Published

Unique slugs plugin for mongoose that is lightweight, concurrency safe, but also follows the normal mongoose plugin pattern.

Downloads

3

Readme

slugin

Dependency Status devDependency Status

Unique URL-friendly slugs plugin for mongoose that is lightweight, concurrency safe, but also follows the normal mongoose plugin pattern. Unlike some of the other offerings in NPM, this one also requires minimal effort on your part by using the standard mongoose plugin syntax.

based on Paul Vencill's slugin plugin

Getting started

Install the plugin with npm:

npm install mongoose-slugin

Add the plugin to your schema:

var slugin = require('mongoose-slugin');

// Your awesome schema building here

YourSchema.plugin(slugin);

That's it! Slugin overrides the built-in document.save function to catch the mongodb duplicate key error and appends (or updates) an iterator on the end of the slug to ensure the slug is unique without requiring the user to update it. By default it keys off the title property in your document, but you can override that in the options. For example if you save three blog posts all with the title 'Winter is Coming', you'll get three slugs that are different from each other by the number at the end (or lack thereof):

winter-is-coming // first one
winter-is-coming-5 // second one got a random number '5' assigned to it
winter-is-coming-8  // third one got a random number '3' added to the previously largest number '5'

Why the random numbers? Well, I decided that for a few edge cases if you had two docs try to save at the exact same time with the exact same title, you could wind up with infinite flapping as each increments by 1, so to be safe we increment by a random number between 1 and 10 instead.

API

slugin(schema,options)

In most cases you'll use the Schema syntax of YourSchema.plugin(slugin, options) instead. Options are, as one would expect, optional.

  • slugName - What property you want created on your schema to store the completed slug. Defaults to 'slug'. Also creates a pair of properties to hold the string and numeric parts of the slug for more efficient querying. Those will be {slugName}_base and {slugName}_it respectively.
  • source - The property or properties on your schema that you want to use as the source of the slug. Can be a string property name or array of properties. Defaults to 'title'.
  • modelName - The name you use to store your schema in mongoose when you call mongoose.model('modelName', schema). Defaults to pascal-casing the mongodb collection name for your model (e.g. the mongodb collection name "posts" will be interpreted as a modelName 'Posts').