Installations
npm install mongoose-slug-hero
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
8.10.0
NPM Version
5.6.0
Score
66.1
Supply Chain
97.8
Quality
74.9
Maintenance
25
Vulnerability
99.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (98.94%)
Shell (1.06%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
budiadiono
Download Statistics
Total Downloads
14,669
Last Day
11
Last Week
32
Last Month
61
Last Year
334
GitHub Statistics
11 Stars
57 Commits
2 Forks
4 Watching
3 Branches
3 Contributors
Bundle Size
1.87 MB
Minified
112.54 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.1.1
Package Id
mongoose-slug-hero@1.1.1
Unpacked Size
17.89 kB
Size
6.08 kB
File Count
7
NPM Version
5.6.0
Node Version
8.10.0
Total Downloads
Cumulative downloads
Total Downloads
14,669
Last day
0%
11
Compared to previous day
Last week
146.2%
32
Compared to previous week
Last month
144%
61
Compared to previous month
Last year
-27.7%
334
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Mongoose Slug Hero
Mongoose Slug Hero is a mongoose plugin to generate unique sequential slug. This plugin uses node-slug module to generate slug from targeted field. To guarantee the uniqueness, this plugin uses sequence collection to track the number of used slug -- inspired by mongoose-sequence plugin.
Generated slug stored in your collection in field named slug
.
We do respect a history
When you edit your data the slug will be automatically changed based to the new updated data.
But the old slug still alive. Thus we respect this history by storing the old slug to the
slugs
field in your target collection.
Slugs that belongs to deleted data will also not reused.
Find data by slug
This plugin automatically create static method for your schema called findBySlug
.
You can find your data by invoking this method by supplying whether the current slug or old slug.
Installation
npm install mongoose-slug-hero
Options
- doc (required): Name of slug-hero, this must be unique among collections. You can fill with the model name to make life easier.
- field (required): Name of your field that slug will generate for.
- scope (optional): Array of field names for scope keys (see example: Scoped Slug).
- slugField (optional / global): The slug field name that will be added to the collection to store generated slug. Default:
slug
. - slugsField (optional / global): The slugs field name that will be added to the collection to store slug history. Default:
slugs
. - slugOptions (optional / global): Options for node-slug. Please refer to https://github.com/dodo/node-slug#options. Default:
{lower: true}
. - counter (optional / global): The name of collection to store sequences data. Default:
_slug_ctrs
.
Global Options / Config
You can set those options above marked with global once for all in the very begining of your code.
For example you want to always use slug_counters
as your collection to store sequences, then you do this:
var slugHero = require('mongoose-slug-hero');
slugHero.config.counter = 'slug_counters'
// You can do same thing for slugField, slugsField or slugOptions
Usage Example
Simple Slug
var slugHero = require('mongoose-slug-hero'),
mongoose = require('mongoose'),
fooSchema = new mongoose.Schema({
name: String
});
fooSchema.plugin(slugHero, {doc: 'foo', field: 'name'});
var Foo = mongoose.model('Foo', fooSchema);
var foo = new Foo({ name: 'SpongeBob' });
foo.save(); // foo.slug => 'spongebob'
Next time you create another Foo
var foo = new Foo({ name: 'SpongeBob' });
foo.save(); // foo.slug => 'spongebob-2'
Scoped Slug
It is like a composite key, that slug should be unique within same referenced key. For example that each user's post should be only unique according to the user:
var postSchema = new mongoose.Schema({userId: Number, post: String});
postSchema.plugin(slugHero, {doc: 'post', field: 'post', scope:['userId']});
var Post = mongoose.model('Post', postSchema);
// create 1st post
(new Post({userId: 1, post: 'Great post ever'})).save(); // post.slug => 'great-post-ever'
// secondly with same user and same post...
(new Post({userId: 1, post: 'Great post ever'})).save(); // post.slug => 'great-post-ever-2'
// now with different user and same post...
(new Post({userId: 2, post: 'Great post ever'})).save(); // post.slug => 'great-post-ever'
// and so on...
Finding The Slug
findBySlug method returning mongoose query, except that the parameter is slug itself. So you can do mongoose query syntax like here.
Either <Model>.findBySlug(<slug_or_options>, [callbak]);
or <Model>.findBySlug(<slug_or_options>).exec([callbak]);
Example:
Post.findBySlug('great-post-ever', function(err, result) {
// 'result' is what we looking for
});
or
Post.findBySlug('great-post-ever').exec(function(err, result) {
// 'result' is what we looking for
});
For scoped slug, you can feed first parameter with an object instead of slug.
For example you want to find data belongs to userId
with value 1
:
Post.findBySlug({slug: 'great-post-ever', userId: 1}).exec(function(err, result) {
// 'result' is what we looking for
});
Generate slug to existing document
You can call ensureSlugIsExists method to generate slug into existing document. For example you have a schema and model like this:
var schema = new mongoose.Schema({
name: String
})
var MyModel = mongoose.model('MyModel', schema)
Then you release your app and of course MyModel
now filled with many data.
Now you change your mind, that you want to have a slug to your MyModel
. All you have to do is:
- Change your schema to have
mongoose-slug-hero
plugged.schema.plugin(slugHero, {doc: 'my-model', field: 'name'})
- Call ensureSlugIsExists right after model initialization:
MyModel.ensureSlugIsExists(function (error) { if (error) { throw error } console.log('success!') })
License
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
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 1/29 approved changesets -- score normalized to 0
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 2 are checked with a SAST tool
Reason
33 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-4jwp-vfvf-657p
- Warn: Project is vulnerable to: GHSA-v8w9-2789-6hhr
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-qh2h-chj9-jffq
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-hxm2-r34f-qmc5
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-mh5c-679w-hh4r
- Warn: Project is vulnerable to: GHSA-8687-vv9j-hgph
- Warn: Project is vulnerable to: GHSA-f825-f98c-gj3g
- Warn: Project is vulnerable to: GHSA-h8hf-x3f4-xwgp
- Warn: Project is vulnerable to: GHSA-9m93-w8w6-76hh
- Warn: Project is vulnerable to: GHSA-m7xq-9374-9rvx
- Warn: Project is vulnerable to: GHSA-vg7j-7cwx-8wgw
- Warn: Project is vulnerable to: GHSA-h466-j336-74wx
- Warn: Project is vulnerable to: GHSA-p92x-r36w-9395
- Warn: Project is vulnerable to: GHSA-45q2-34rf-mr94
- Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-jxqq-cqm6-pfq9
Score
1.7
/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