1.0.0 • Published 6 years ago

mongoose-save4update v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

mongoose-save4update

Get the data(new/old) of update fields by save function

Build Status npm npm

API

save for update event data

  • collection the mongodb collection
  • original the original data
  • updated the update data
const mongoose = require('mongoose');
const save4update = require('mongoose-save4update');

const {Schema} = mongoose;
const schema = new Schema({
  name: String,
}, {
  timestamps: true,
});
const Book = mongoose.model('Book', schema);
schema.plugin(save4update, {
  collection: 'Book',
});
schema.on('save4update', (data) => {
  // { updated: { name: 'new name' },
  //     original: { name: 'name' },
  //     collection: 'Book' }
  console.info(data);
});

(async () => {
  const doc = await Book.findOne({});
  doc.name = 'new name';
  await doc.save();
})();