mongoose-originals
v2.0.1
Published
Get original value of mongoose fields
Downloads
24
Readme
Mongoose Originals
A mongoose plugin to retrieve original values
Instalation
yarn add mongoose-originals
Usage
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var originals = require('mongoose-originals');
var CustomerSchema = new Schema({
name: String,
email: String,
answers: [{ name: String }],
});
CustomerSchema.plugin(originals, { fields: ['name', 'email'] });
var Customer = mongoose.model('Customer', CustomerSchema);
var customer = new Customer({ name: 'test', email: 'example.com' });
customer.save();
customer.name = 'new name';
console.log(customer.originals.name);
Since mongoose has some limitations originals object will not be available when you create a brand new unsaved object. To work arround that, you'll need to execute the "initOriginals" method.
var customer = new Customer({ name: 'test', email: 'example.com' });
customer.initOriginals();
console.log(customer.originals.name);
You can check if the values are changed compared to the originals:
var customer = new Customer({ name: 'test', email: 'example.com' });
customer.save().then((customer) => {
console.log(customer.isChanged()); // false
customer.name = 'other';
console.log(customer.isChanged()); // true
});
License
-------
Copyright (c) 2016-2017 Enhancv
Licensed under the MIT license.