mongoose-util

1.1.0 • Public • Published

mongoose-util

Small collection of opinionated helper functions for working with mongoose

VersionIncrementor class

Sets up the $inc: { versionKey: 1 } in a consistant way on an update clause.

var mongoose = require( 'mongoose' ),
    mongooseUtil = require( 'mongoose-util' );
    
var versionIncrementor = new mongooseUtil.VersionIncrementor( '_version' );
 
var userSchema = new Schema({
   firstName: String,
   lastName: String,
}, {
    versionKey: versionIncrementor.versionKey
});
 
// setup the update clause and $inc: { _version: 1 } property
var updateClause = { firstName: 'Jimmy' };
versionIncrementor.setup( updateClause );
 
var userModel = mongoose.model( 'User', userSchema );
userModel.update( {}, updateClause );

rawDoc( response ) function

Returns only the raw document, instead of the rich Model object.

var mongoose = require( 'mongoose' ),
    mongooseUtil = require( 'mongoose-util' );
 
var userSchema = new Schema({
   firstName: String,
   lastName: String,
}, {
    versionKey: versionIncrementor.versionKey
});
 
var userModel = mongoose.model( 'User', userSchema );
 
return userModel
   .findOne({})
   .then( mongooseUtil.rawDoc );

Readme

Keywords

Package Sidebar

Install

npm i mongoose-util

Weekly Downloads

0

Version

1.1.0

License

None

Last publish

Collaborators

  • jeff.ashton.ct
  • cantireinnovations