Gathering detailed insights and metrics for mongoose-slug-hero
Gathering detailed insights and metrics for mongoose-slug-hero
npm install mongoose-slug-hero
Typescript
Module System
Node Version
NPM Version
JavaScript (98.94%)
Shell (1.06%)
Total Downloads
14,669
Last Day
11
Last Week
32
Last Month
61
Last Year
334
11 Stars
57 Commits
2 Forks
4 Watching
3 Branches
3 Contributors
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
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
.
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.
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.
npm install mongoose-slug-hero
slug
.slugs
.{lower: true}
._slug_ctrs
.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
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'
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...
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
});
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:
mongoose-slug-hero
plugged.
schema.plugin(slugHero, {doc: 'my-model', field: 'name'})
MyModel.ensureSlugIsExists(function (error) {
if (error) {
throw error
}
console.log('success!')
})
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
33 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-27
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