bookshelf-deep-changed
v1.0.0
Published
Allows bookshelf models to check whether a value you are saving is different than the existing value in the database.
Downloads
51
Readme
bookshelf-deep-changed
Allows bookshelf models to check whether a value you are saving is different than the existing value in the database.
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());
}
});
});
}
});
You need to add bookshelf-deep-changed to the list of bookshelf's plugins when you require bookshelf:
var knex = require('knex')({
/// knex initialization
}),
bookshelf = require('bookshelf')(knex);
bookshelf.plugin(require('bookshelf-deep-changed'));