Installations
npm install mongoose-slug-generator
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>= 0.12.7
Node Version
0.12.7
NPM Version
2.11.3
Score
75.3
Supply Chain
100
Quality
79.1
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Kubide
Download Statistics
Total Downloads
430,517
Last Day
300
Last Week
1,333
Last Month
7,325
Last Year
89,318
GitHub Statistics
30 Stars
25 Commits
16 Forks
3 Watching
1 Branches
6 Contributors
Bundle Size
29.21 kB
Minified
10.79 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.4
Package Id
mongoose-slug-generator@1.0.4
Size
10.63 kB
NPM Version
2.11.3
Node Version
0.12.7
Total Downloads
Cumulative downloads
Total Downloads
430,517
Last day
-4.8%
300
Compared to previous day
Last week
-35.1%
1,333
Compared to previous week
Last month
-8.3%
7,325
Compared to previous month
Last year
-16.6%
89,318
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
mongoose-slug-generator
Mongoose plugin for creating slugs based on mongoose schema fields. For example you can create a slug based on a document's title and author's name: my-post-title-kevin-roosevelt, or unique slugs based on just the title: my-post-title-Nyiy4wW9l.
Installation
The best way to install it is using npm
1npm install mongoose-slug-generator --save
Loading
1var slug = require('mongoose-slug-generator');
Initialization
1var mongoose = require('mongoose'); 2mongoose.plugin(slug);
Usage
This plugin is based on the idea of using the mongoose schema as the way to check the use of slug fields.
The plugin checks and updates automatically the slug field with the correct slug.
Basic Usage
If you only want to create the slug based on a simple field.
1var mongoose = require('mongoose'), 2 slug = require('mongoose-slug-generator'), 3 mongoose.plugin(slug), 4 Schema = mongoose.Schema, 5 schema = new Schema({ 6 title: String, 7 slug: { type: String, slug: "title" } 8});
Multiple slug fields
You can add as many slug fields as you wish
1var mongoose = require('mongoose'), 2 slug = require('mongoose-slug-generator'), 3 mongoose.plugin(slug), 4 Schema = mongoose.Schema, 5 schema = new Schema({ 6 title: String, 7 subtitle: String, 8 slug: { type: String, slug: "title" }, 9 slug2: { type: String, slug: "title" }, 10 slug3: { type: String, slug: "subtitle" } 11});
Multiple fields to create the slug
If you want, you can use more than one field in order to create a new slug field.
1var mongoose = require('mongoose'), 2 slug = require('mongoose-slug-generator'), 3 mongoose.plugin(slug), 4 Schema = mongoose.Schema, 5 schema = new Schema({ 6 title: String, 7 subtitle: String, 8 slug: { type: String, slug: ["title", "subtitle"] } 9});
Unique slug field
To create a unique slug field, you must only add add the unique: true parameter in the path (also, this way the default mongo unique index gets created)
1var mongoose = require('mongoose'), 2 slug = require('mongoose-slug-generator'), 3 mongoose.plugin(slug), 4 Schema = mongoose.Schema, 5 schema = new Schema({ 6 title: String, 7 subtitle: String, 8 slug: { type: String, slug: ["title", "subtitle"], unique: true } 9});
If unique is set, the plugin searches in the mongo database, and if the slug already exists in the collection, it appends to the slug a separator (default: "-") and a random string (generated with the shortid module).
example random
1mongoose.model('Resource').create({ 2 title: 'Am I wrong, fallin\' in love with you!', 3 subtitle: "tell me am I wrong, well, fallin' in love with you" 4}) // slug -> 'am-i-wrong-fallin-in-love-with-you' 5 6mongoose.model('Resource').create({ 7 title: 'Am I wrong, fallin\' in love with you!', 8 subtitle: "tell me am I wrong, well, fallin' in love with you" 9}) // slug -> 'am-i-wrong-fallin-in-love-with-you-Nyiy4wW9l' 10 11mongoose.model('Resource').create({ 12 title: 'Am I wrong, fallin\' in love with you!', 13 subtitle: "tell me am I wrong, well, fallin' in love with you" 14}) // slug -> 'am-i-wrong-fallin-in-love-with-you-NJeskEPb5e'
Alternatively you can modify this behaviour and instead of appending a random string, an incremental counter will be used. For that to happen, you must use the parameter slug_padding_size specifying the total length of the counter:
example counter
1var mongoose = require('mongoose'), 2 slug = require('mongoose-slug-generator'), 3 mongoose.plugin(slug), 4 Schema = mongoose.Schema, 5 schema = new Schema({ 6 title: String, 7 subtitle: String, 8 slug: { type: String, slug: ["title", "subtitle"], slug_padding_size: 4, unique: true } 9}); 10 11mongoose.model('Resource').create({ 12 title: 'Am I wrong, fallin\' in love with you!', 13 subtitle: "tell me am I wrong, well, fallin' in love with you" 14}) // slug -> 'am-i-wrong-fallin-in-love-with-you' 15 16mongoose.model('Resource').create({ 17 title: 'Am I wrong, fallin\' in love with you!', 18 subtitle: "tell me am I wrong, well, fallin' in love with you" 19}) // slug -> 'am-i-wrong-fallin-in-love-with-you-0001' 20 21mongoose.model('Resource').create({ 22 title: 'Am I wrong, fallin\' in love with you!', 23 subtitle: "tell me am I wrong, well, fallin' in love with you" 24}) // slug -> 'am-i-wrong-fallin-in-love-with-you-0002'
Choose your own options
You can change any options adding to the plugin
1var mongoose = require('mongoose'), 2 slug = require('mongoose-slug-generator'), 3 options = { 4 separator: "-", 5 lang: "en", 6 truncate: 120 7 }, 8 mongoose.plugin(slug, options), 9 Schema = mongoose.Schema, 10 schema = new Schema({ 11 title: String, 12 subtitle: String, 13 slug: { type: String, slug: ["title", "subtitle"], unique: true } 14});
You can find more options in the speakingURL's npm page
Support
This plugin is proudly supported by Kubide
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 4/18 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 11 are checked with a SAST tool
Score
3.3
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More