Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

mongoose-load-list

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-load-list

easy queries with mongoose

latest
Source
npmnpm
Version
0.4.0
Version published
Maintainers
1
Created
Source

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 criteria
  • options.sort - default sort
  • options.select - default fields
  • options.limit - default limit
  • options.populate - default populated fields
  • options.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

Keywords

load

FAQs

Package last updated on 27 Jan 2015

Did you know?

Socket

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.

Install

Related posts