1.1.0 • Published 6 years ago

bookshelf-deep-changed-plugin v1.1.0

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

bookshelf-deep-changed-plugin

NPM Version Build Status Test Coverage Dependencies devDependencies JavaScript Style Guide

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'));

License

MIT

Author

Oron Nadiv (oron@nadiv.us)