bookshelf-deep-changed-plugin

1.1.0 • Public • Published

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)

Package Sidebar

Install

npm i bookshelf-deep-changed-plugin

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

8.72 kB

Total Files

4

Last publish

Collaborators

  • oronnadiv