0.4.0 • Published 9 years ago
mongoose-load-list v0.4.0
mongoose-load-list
Easy queries with mongoose. Provides two methods .load()
and .list()
to query collections. Because chaining a lot of methods everywhere is not efficient.
Installation
$ npm install mongoose-load-list
API
var defaults = require('mongoose-load-list');
var Post = new Schema({ ... });
Post.plugin(defaults, {
select: 'title body user created_at',
populate: [
{ path: 'user', select: 'user name' }
],
sort: {
created_at: -1
},
lean: true
});
Make sure any statics you write in your model is done via function call and not with assignment. When the statics are assigned, the .load and .list will be overridden.
Post.statics({
// static methods
})
Options
options.criteria
- default criteriaoptions.sort
- default sortoptions.select
- default fieldsoptions.limit
- default limitoptions.populate
- default populated fieldsoptions.lean
- default is false
The default options are always applied on all the .load
and .list
methods. It can be overridden like below.
Example:
var Post = mongoose.model('Post');
var options = {
select: 'title body created_at user',
criteria: {
_id: this.params.id
// more criterias
},
sort: {
title: -1
},
limit: 10,
skip: 10, // useful for pagination
populate: [
{ path: 'users', select: 'name email' }
],
lean: true, // only for list methods
}
yield Post.load(options);
.load(options)
does a .findOne
on Post collection with all the options passed
yield Post.load(options);
.list(options)
does a .find
on Post collection with all the options passed
yield Post.list(options);
License
MIT