weare-mongoose-slug
v0.3.1
Published
Consist of a mongoose plugin that generate unique slugs with incrementation.
Downloads
1
Maintainers
Readme
weare-mongoose-slug
Consist of a plugins that can generate unique slugs with an increment (exemple: mycoolslug-2
). It can also be used to generate unique fields (dosen't have to be an actual slug, can be a string of any kind).
This module support Mixed fields with objects containings slugs (like multilingual objects).
Installation
$ npm install weare-mongoose-slug
Usage
#!javascript
var mongooseSlug = require('weare-mongoose-slug');
Exemples
:sparkles: Simple
This will populate the slug
field with a slugified version of the title
field.
It will also add incrementation if an identical slug already exist in database.
#!javascript
var mongooseSlug = require('weare-mongoose-slug');
var MySchema = new Schema({
title: String,
slug: String // can be created now but it will be created by the plugin if it doesn't exist
});
// add slug plugin
MySchema.plugin(mongooseSlug.plugin, {
fieldFrom: 'title'
});
:scream: Complex
You can also independently use internal components to make your own complex slugs :
#!javascript
var mongooseSlug = require('weare-mongoose-slug');
var MySchema = new Schema({
title: String,
titleOfDoom: String
});
// add slug fields + generates them on save
MySchema.plugin(mongooseSlug.plugins.slug, {
fieldFrom: 'title',
fieldTo: 'strangeFieldNameForASlug',
options: {} // options passed to slug module
});
MySchema.plugin(mongooseSlug.plugins.slug, {
fieldFrom: 'titleOfDoom',
fieldTo: 'anOtherSlugBecauseOfReasons'
});
// add auto increment when slug is not unique
MySchema.plugin(mongooseSlug.plugins.autoIncrement, {
fields: ['strangeFieldNameForASlug','anOtherSlugBecauseOfReasons']
});
Changes log
0.3.*
- add handling for dotted paths (like
slug.fr
,slug.en
, etc.) - strict passing of mongoose document instance in many functions instead of simple object (generateSlug helper and more)
- clean obselete code
0.2.*
- remove the
model
option used in autoincrement plugin, therefore the plugin does not need to be added after the model is compiled - autocreate the
fieldTo
path in the schema if undefined - add
emptyOnly
for slug plugin
0.1.* - The module was born
The code was partially taken from evenko-yuleat and some things have changed while transfering it into a module :
- the method handleSlug in routes helpers has been modified to be used with a document instead of a req object
- fix string slug support
- the increment separator now consist of a format in which we inject the value
- fix support for indendent increment on slug object with multiple properties