Skip to content
This repository has been archived by the owner on Jul 23, 2021. It is now read-only.

goodeggs/mongoose-document-versioner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mongoose Document Versioner

This module sets a document version key that is incremented any time you save or update a document. This is useful for detecting version conflicts.

Increments version on the following pre hooks:

  • findOneAndUpdate
  • updateOne
  • updateMany
  • update
  • save

Note: Model.findByIdAndUpdate() calls the findOneAndUpdate hook.

Usage

const versionerPlugin = require('mongoose-document-versioner');

const schema = new mongoose.Schema({ /* .... */ });
schema.plugin(versionerPlugin);
const Model = mongoose.model('Model', schema);

/* EXAMPLES */

Model.update({attr: 'foo'}, {attr: 'bar'}, {multi: true});
/*
 * mongo query:
 * model.update(
 *   { attr: 'foo' },
 *   { '$inc': { version: 1 }, '$set': { attr: 'bar' } },
 *   { multi: true }
 * )
 */

Model.findOneAndUpdate({attr: 'foo'}, {attr: 'bar'});
/*
 * mongo query:
 * model.findAndModify(
 *   { attr: 'foo' },
 *   { '$inc': { version: 1 }, '$set': { attr: 'bar' } },
 *   { new: false, upsert: false }
 * )
 */

About

Force version to increment, any time you update a mongoose document

Resources

Stars

Watchers

Forks

Packages

No packages published