mongoose-monitor-fields-plugin

0.0.3 • Public • Published

mongoose-monitor-fields-plugin

build status code coverage code style styled with prettier made with lass license npm downloads

monitor and respond to changes in your documents!

Table of Contents

Install

npm:

npm install mongoose-monitor-fields-plugin

yarn:

yarn add mongoose-monitor-fields-plugin

Basic Example Usage

const { Schema } = require('mongoose');
const monitorFields = require('mongoose-monitor-fields-plugin');
 
const mySchema = new Schema({
  my_field: {
    type: String,
    // set monitor true to have the change passed to the document level callback post save
    // or define a function for a field level callback post save
    monitor: console.log
  }
});
 
mySchema.plugin(monitorFields, console.log);
 
const MyModel = model('MySchema', mySchema);
 
(async () => {
  let myDocument = new MyModel({
    my_field: 'foo'
  });
  await myDocument.save();
  myDocument.my_field = 'bar';
  await myDocument.save();
 
  // prints the following to the console
  // for the field level callback
  // {path: 'my_field', prev: 'foo', updated: 'bar'}
  // for the document level
  // [{path: 'my_field', prev: 'foo', updated: 'bar'}]
})();

In either field level or document level callbacks - this refers to the newly updated document.

Special Credit

This package was heavily influenced by mongoose-plugin-diff

Contributors

Name Website
Spencer Snyder https://spencersnyder.io

License

MIT © Spencer Snyder

Package Sidebar

Install

npm i mongoose-monitor-fields-plugin

Weekly Downloads

4

Version

0.0.3

License

MIT

Unpacked Size

8.96 kB

Total Files

12

Last publish

Collaborators

  • spence-s