mongoose-originals

2.0.1 • Public • Published

Mongoose Originals

Build Status Code Climate Test Coverage

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.

Readme

Keywords

none

Package Sidebar

Install

npm i mongoose-originals

Weekly Downloads

3

Version

2.0.1

License

MIT

Last publish

Collaborators

  • ginovski
  • deepsyx
  • ikerin