
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
mongoose-l2r
Advanced tools
A REST-style link builder for mongoose. Useful if you are rolling your own REST like app.
id
and _id
npm install --save mongoose-l2r
modelInstance.toLink([array of parents], { options object });
var l2r = require('mongoose-l2r');
var PostSchema = new Schema({
....
});
PostSchema.plugin(l2r);
var Post = mongoose.model('Post', PostSchema);
var post = new Post;
... save your post ...
post.toLink(); // returns: /posts/:post_id
Assume you have a Comment
model that are comments to Post
s. You can build URLs like this:
/posts/:post_id/comments/:comment_id
.. by doing like this:
comment.toLink();
// returns: /comment/:comment_id
comment.toLink([post]);
// returns: /posts/:post_id/comments/:comment_id
Note: Only the model on which the method is invoked on needs to have the plugin installed.
You can use different paths than those based on model names like so:
comment.toLink([post], { posts_path: 'entries' });
// returns: /entries/:post_id/comments/:comment_id
By default the plugin will look for _id
and id
. If you want to use another property for id, specify it like this:
post.toLink({ id: 'uid' });
Note: You cannot specify a different id property for each model in the path.
Can also be used on ObjectId
s. Suppose our Post
instance had a comments
relation and instead of resturning popluated comments you would return URLs to them:
var PostSchema = new Schema({
comments: [Schema.Types.ObjectId]
});
post.idToLink(post.comments, 'Comment', [post]);
// returns: [ '/posts/:post_id/comments/:comment_id1', '/posts/:post_id/comments/:comment_id2', ... ]
You can also turn all relations on an object into links using:
post.relationsToLink();
In our example this would return an object map to each relation:
{
comments: [
/posts/:post_id/comments/:comment_id1,
/posts/:post_id/comments/:comment_id2
]
}
In the reverse you have to pass in which paths are the owner of the object (which the current object belongs to):
comment1.relationsToLink(['post']);
Resulting in:
{
post: /posts/:post_id/
}
This plugin attaches itself ot schema.methods
, so if you set the methods on a scehma like the following, be sure to add the plugin after setting the methods:
PostSchema.methods = {
...
};
PostSchema.plugin(l2r);
ref
option.FAQs
Mongoose REST-style link builder
We found that mongoose-l2r demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.