bookshelf-deep-changed-plugin
v1.1.0
Published
Allows bookshelf models to check whether a value you save is different than the existing value in the database.
Downloads
6
Maintainers
Readme
bookshelf-deep-changed-plugin
Allows bookshelf models to check whether a value you save is different than the existing value in the database.
Installation:
$ npm install --save bookshelf-deep-changed-plugin
Usage:
module.exports = Repository.Model.extend({
tableName: 'users',
initialize: function () {
this.on('updating', function (model, attrs, options) {
return this
.deepChanged('name', 'email', options)
.then(function (hasDeepChanged) {
if (hasDeepChanged[0]) {
this.set('name_changed_at', new Date());
}
if (hasDeepChanged[1]) {
this.set('email_changed_at', new Date());
}
});
});
}
});
Do not forget to add bookshelf-deep-changed-plugin
to the list of bookshelf's plugins when you require bookshelf:
const knex = require('knex')({
/// knex initialization
})
const bookshelf = require('bookshelf')(knex);
bookshelf.plugin(require('bookshelf-deep-changed-plugin'));