Skip to content

goodeggs/mongoose-lean

Repository files navigation

mongoose-lean

Default Mongoose to use lean queries. Fatten them back up with syntactic sugar.

NPM version Build Status MIT License

Usage

First a word of warning: the current implementation patches mongoose directly, which means if any of your code or dependencies expects the default un-patched mongoose, you'll be in trouble. I'm interested in changing the implementation to opt-in to this behavior on a per-model basis, which should avoid this.

npm install mongoose-lean
var mongoose = require('mongoose');
var patchMongoose = require('mongoose-lean');
patchMongoose(mongoose);

var User = mongoose.model('User', new mongoose.Schema({name: String}));

function weighIn(bob) {
  console.log('bob is ', bob instanceof User ? 'fat' : 'lean');
}

User.create({name: 'Bob'}, function(err, bob) {
  weighIn(bob); // => bob is fat
  User.findById(bob._id, function(err, bob) {
    weighIn(bob); // => bob is lean
    User.findById(bob._id).fat().exec(function(err, bob) {
      weighIn(bob); // => bob is fat
    });
  });
});

Contributing

Please follow our Code of Conduct when contributing to this project.

$ git clone https://github.com/goodeggs/mongoose-lean && cd mongoose-lean
$ npm install
$ npm test

Module scaffold generated by generator-goodeggs-npm.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published