New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

monguurl

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

monguurl

Automatically generate a unique and url-friendly alias/slug and store it in Mongoose

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Monguurl

Automatically generate a unique and url-friendly alias/slug and store it in Mongoose. For example for posts it's common to generate an alias/slug from the title.

Example

Define your model like this:

var mongoose = require('mongoose'),
    monguurl = require('monguurl');

Post = new mongoose.schema({
  title: { type: String },
  slug: { type: string, index: { unique: true } }
});

Post.plugin(monguurl({
  source: 'title',
  target: 'slug'
}));

mongoose.model('Post', Post);

And then if you for example create a new document like this:

mongoose.model('Post').create({
  title: 'This is so Äwesome!'
});

The slug will be automatically generated and stored in the database:

{
  "_id": "09876543...",
  "title": "This is so Äwesome!",
  "slug": "this-is-so-awesome"
}

Create another identical document and it will be stored like this:

{
  "_id": "09876543...",
  "title": "This is so Äwesome!",
  "slug": "this-is-so-awesome-2"
}

The ending number will increase to "-100", then it will be "-100-2". This is to avoid ruining intentional numbers such as "It's over 9000" or "A day in 2013". It's unlikely enough that either such a title is repeated or that the same title is used more than a hundred times anyway.

Also note that theoretically this plugin can be used without a source/title, if such behavior is desired. Just set the target field when creating the document and any source will be ignored.

Update: It's now also possible to set a max-length for aliases. Setting monguurl({ length: 40 }) will cut the alias approximately at the space (dash) closest before the limit. If there's no space/dash, the string will be cut off at the limit anyway. The final length might be up to 6 bytes longer to accomodate for appended numbers. The default length is 0, which is unlimited.

Installation

  • npm install monguurl --save

Documentation

  • monguurl(options)
    Creates the mongoose plugin.
    • options.source (string) Path to the field which to generate the alias from.
      Default: 'title'
    • options.target (string) path to the field where the alias should be stored.
      Default: 'alias'
  • urlProof(alias)
    Method used to make a string url-friendly. If you want a different method, just replace it.

Keywords

FAQs

Package last updated on 16 May 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc