Mongoose Originals
A mongoose plugin to retrieve original values
Instalation
yarn add mongoose-originals
Usage
var mongoose = ;var Schema = mongooseSchema;var originals = ; var CustomerSchema = name: String email: String answers: name: String ; CustomerSchema;var Customer = mongoose; var customer = name: 'test' email: 'example.com' ; customer; customername = 'new name';console;
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.