bookshelf-deep-changed
Advanced tools
Comparing version 0.1.1 to 0.1.2
88
index.js
@@ -1,54 +0,52 @@ | ||
(function () { | ||
'use strict'; | ||
'use strict'; | ||
var Promise = require('bluebird'), | ||
_ = require('underscore'); | ||
var BPromise = require('bluebird'), | ||
_ = require('underscore'); | ||
module.exports = { | ||
deepChanged: function () { | ||
var missing = [], | ||
hasChanged = [], | ||
options; | ||
module.exports = { | ||
deepChanged: function () { | ||
var missing = [], | ||
hasChanged = [], | ||
options; | ||
_.each(arguments, function (arg, index, args) { | ||
if (!_.isString(arg)) { | ||
if (index !== args.length - 1) { | ||
throw new Error('Expecting strings only. You can pass an \'Options\' parameter as the last argument. Position: ' + index + ', Given non-string argument: ', arg); | ||
} else if (_.isObject(arg)) { | ||
options = arg; | ||
} else { | ||
throw new Error('Last argument can either be a string or an options object. Given last argument: ', arg); | ||
} | ||
return; | ||
_.each(arguments, function (arg, index, args) { | ||
if (!_.isString(arg)) { | ||
if (index !== args.length - 1) { | ||
throw new Error('Expecting strings only. You can pass an \'Options\' parameter as the last argument. Position: ' + index + ', Given non-string argument: ', arg); | ||
} else if (_.isObject(arg)) { | ||
options = arg; | ||
} else { | ||
throw new Error('Last argument can either be a string or an options object. Given last argument: ', arg); | ||
} | ||
return; | ||
} | ||
if (this.isNew() || !this.hasChanged(arg)) { | ||
hasChanged.push(false); | ||
if (this.isNew() || !this.hasChanged(arg)) { | ||
hasChanged.push(false); | ||
} else { | ||
var previousValue = this.previous(arg); | ||
if (_.isUndefined(previousValue)) { | ||
missing.push({index: index, argument: arg}); | ||
hasChanged.push(undefined); // we do not know. | ||
} else { | ||
var previousValue = this.previous(arg); | ||
if (_.isUndefined(previousValue)) { | ||
missing.push({index: index, argument: arg}); | ||
hasChanged.push(undefined); // we do not know. | ||
} else { | ||
hasChanged.push(!_.isEqual(previousValue, this.get(arg))); | ||
} | ||
hasChanged.push(!_.isEqual(previousValue, this.get(arg))); | ||
} | ||
}, this); | ||
} | ||
}, this); | ||
if (!missing.length) { | ||
return Promise.resolve(hasChanged); | ||
} | ||
return Promise.bind(this) | ||
.then(function () { | ||
return new this.constructor().query({where: {id: this.id}}).fetch(options); | ||
}).then(function (previousModel) { | ||
_.each(missing, function (item) { | ||
var index = item.index, | ||
argument = item.argument, | ||
areEqual = _.isEqual(previousModel.get(argument), this.get(argument)); | ||
hasChanged.splice(index, 1, !areEqual); | ||
}, this); | ||
}).return(hasChanged); | ||
if (!missing.length) { | ||
return BPromise.resolve(hasChanged); | ||
} | ||
}; | ||
}()); | ||
return BPromise.bind(this) | ||
.then(function () { | ||
return new this.constructor().query({where: {id: this.id}}).fetch(options); | ||
}).then(function (previousModel) { | ||
_.each(missing, function (item) { | ||
var index = item.index, | ||
argument = item.argument, | ||
areEqual = _.isEqual(previousModel.get(argument), this.get(argument)); | ||
hasChanged.splice(index, 1, !areEqual); | ||
}, this); | ||
}).return(hasChanged); | ||
} | ||
}; |
{ | ||
"name": "bookshelf-deep-changed", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Allows bookshelf models to check whether a value you are saving is different than the existing value in the database.", | ||
"main": "index.js", | ||
"engines": { | ||
"iojs": ">=1.0 <1.6", | ||
"node": ">=0.10 <0.13", | ||
"node": ">=0.10", | ||
"npm": "^2" | ||
@@ -27,4 +26,4 @@ }, | ||
"scripts": { | ||
"lint": "bin/lint", | ||
"test": "bin/test" | ||
"lint": "eslint --ignore-path .gitignore .", | ||
"test": "./bin/lint && ./bin/test" | ||
}, | ||
@@ -41,3 +40,4 @@ "dependencies": { | ||
"coveralls": "^2.11.2", | ||
"jshint": "*", | ||
"eslint": "^0.15", | ||
"eslint-plugin-nodeca": "^1", | ||
"mocha": "*", | ||
@@ -44,0 +44,0 @@ "mocha-lcov-reporter": "0.0.1", |
@@ -1,92 +0,90 @@ | ||
(function () { | ||
'use strict'; | ||
var should = require('should'), | ||
chance = require('chance')(), | ||
Promise = require('bluebird'), | ||
repository = require('./repository'), | ||
User = require('./user'), | ||
id, | ||
existingModel; | ||
'use strict'; | ||
var should = require('should'), | ||
chance = require('chance')(), | ||
BPromise = require('bluebird'), | ||
repository = require('./repository'), | ||
User = require('./user'), | ||
id, | ||
existingModel; | ||
describe('Bookshelf-deep-changed', function () { | ||
before(require('./migration')(repository)); | ||
describe('Bookshelf-deep-changed', function () { | ||
before(require('./migration')(repository)); | ||
afterEach(function () { | ||
return new User({id: id}).fetch() | ||
.then(function (model) { | ||
existingModel = model; | ||
}); | ||
}); | ||
afterEach(function () { | ||
return new User({id: id}).fetch() | ||
.then(function (model) { | ||
existingModel = model; | ||
}); | ||
}); | ||
it('Should return all false for new model', function () { | ||
it('Should return all false for new model', function () { | ||
return Promise.resolve() | ||
.then(function () { | ||
return new User().save({name: chance.word(), email: chance.email()}); | ||
}) | ||
.then(function (model) { | ||
id = model.id; | ||
should(model.get('name_changed_at')).be.equal(undefined); | ||
should(model.get('email_changed_at')).be.equal(undefined); | ||
}); | ||
}); | ||
return BPromise.resolve() | ||
.then(function () { | ||
return new User().save({name: chance.word(), email: chance.email()}); | ||
}) | ||
.then(function (model) { | ||
id = model.id; | ||
should(model.get('name_changed_at')).be.equal(undefined); | ||
should(model.get('email_changed_at')).be.equal(undefined); | ||
}); | ||
}); | ||
it('Should notice name has changed', function () { | ||
return Promise.delay(1000) | ||
.then(function () { | ||
return new User({id: id}).save({name: chance.word()}); | ||
}) | ||
.then(function (model) { | ||
should.exist(model.get('name_changed_at')); | ||
should(model.get('name_changed_at')).not.be.eql(existingModel.get('name_changed_at')); | ||
should(model.get('email_changed_at')).equal(undefined); | ||
}); | ||
}); | ||
it('Should notice name has changed', function () { | ||
return BPromise.delay(1000) | ||
.then(function () { | ||
return new User({id: id}).save({name: chance.word()}); | ||
}) | ||
.then(function (model) { | ||
should.exist(model.get('name_changed_at')); | ||
should(model.get('name_changed_at')).not.be.eql(existingModel.get('name_changed_at')); | ||
should(model.get('email_changed_at')).equal(undefined); | ||
}); | ||
}); | ||
it('Should notice name has not changed', function () { | ||
return Promise.delay(1000) | ||
.then(function () { | ||
return new User({id: id}).save({name: existingModel.get('name')}); | ||
}) | ||
.then(function (model) { | ||
should(model.get('name_changed_at')).equal(undefined); | ||
should(model.get('email_changed_at')).equal(undefined); | ||
}) | ||
.then(function () { | ||
return new User({id: id}).fetch(); | ||
}) | ||
.then(function (model) { | ||
should(model.get('name')).equal(existingModel.get('name')); | ||
should(model.get('email')).equal(existingModel.get('email')); | ||
should(model.get('name_changed_at')).be.eql(existingModel.get('name_changed_at')); | ||
should(model.get('email_changed_at')).be.eql(existingModel.get('email_changed_at')); | ||
should.exist(model.get('name_changed_at')); | ||
should.not.exist(model.get('email_changed_at')); | ||
}); | ||
}); | ||
it('Should notice name has not changed', function () { | ||
return BPromise.delay(1000) | ||
.then(function () { | ||
return new User({id: id}).save({name: existingModel.get('name')}); | ||
}) | ||
.then(function (model) { | ||
should(model.get('name_changed_at')).equal(undefined); | ||
should(model.get('email_changed_at')).equal(undefined); | ||
}) | ||
.then(function () { | ||
return new User({id: id}).fetch(); | ||
}) | ||
.then(function (model) { | ||
should(model.get('name')).equal(existingModel.get('name')); | ||
should(model.get('email')).equal(existingModel.get('email')); | ||
should(model.get('name_changed_at')).be.eql(existingModel.get('name_changed_at')); | ||
should(model.get('email_changed_at')).be.eql(existingModel.get('email_changed_at')); | ||
should.exist(model.get('name_changed_at')); | ||
should.not.exist(model.get('email_changed_at')); | ||
}); | ||
}); | ||
it('Should notice both name and email has changed', function () { | ||
return Promise.delay(1000) | ||
.then(function () { | ||
return new User({id: id}).save({name: chance.word(), email: chance.email()}); | ||
}) | ||
.then(function (model) { | ||
should.exist(model.get('name_changed_at')); | ||
should.exist(model.get('email_changed_at')); | ||
}) | ||
.then(function () { | ||
return new User({id: id}).fetch(); | ||
}) | ||
.then(function (model) { | ||
should(model.get('name')).not.equal(existingModel.get('name')); | ||
should(model.get('email')).not.equal(existingModel.get('email')); | ||
should(model.get('name_changed_at')).not.be.eql(existingModel.get('name_changed_at')); | ||
should(model.get('email_changed_at')).not.be.eql(existingModel.get('email_changed_at')); | ||
should.exist(model.get('name_changed_at')); | ||
should.exist(model.get('email_changed_at')); | ||
}); | ||
}); | ||
it('Should notice both name and email has changed', function () { | ||
return BPromise.delay(1000) | ||
.then(function () { | ||
return new User({id: id}).save({name: chance.word(), email: chance.email()}); | ||
}) | ||
.then(function (model) { | ||
should.exist(model.get('name_changed_at')); | ||
should.exist(model.get('email_changed_at')); | ||
}) | ||
.then(function () { | ||
return new User({id: id}).fetch(); | ||
}) | ||
.then(function (model) { | ||
should(model.get('name')).not.equal(existingModel.get('name')); | ||
should(model.get('email')).not.equal(existingModel.get('email')); | ||
should(model.get('name_changed_at')).not.be.eql(existingModel.get('name_changed_at')); | ||
should(model.get('email_changed_at')).not.be.eql(existingModel.get('email_changed_at')); | ||
should.exist(model.get('name_changed_at')); | ||
should.exist(model.get('email_changed_at')); | ||
}); | ||
}); | ||
}); | ||
}()); | ||
}); |
@@ -1,6 +0,5 @@ | ||
(function () { | ||
'use strict'; | ||
var _ = require('underscore'), | ||
Promise = require('bluebird'), | ||
BPromise = require('bluebird'), | ||
drops = [ | ||
@@ -14,3 +13,3 @@ 'users' | ||
return Promise.all(_.map(drops, function (val) { | ||
return BPromise.all(_.map(drops, function (val) { | ||
return schema.dropTableIfExists(val); | ||
@@ -29,2 +28,1 @@ })) | ||
}; | ||
}()); |
@@ -1,9 +0,7 @@ | ||
(function () { | ||
'use strict'; | ||
'use strict'; | ||
module.exports = require('bookshelf')(require('knex')({ | ||
//debug: true, | ||
client: 'pg', | ||
connection: process.env.DATABASE_URL || 'postgres://postgres@localhost/bookshelf_deep_changed' | ||
})); | ||
}()); | ||
module.exports = require('bookshelf')(require('knex')({ | ||
//debug: true, | ||
client: 'pg', | ||
connection: process.env.DATABASE_URL || 'postgres://postgres@localhost/bookshelf_deep_changed' | ||
})); |
@@ -1,23 +0,21 @@ | ||
(function () { | ||
'use strict'; | ||
'use strict'; | ||
var _ = require('underscore'), | ||
Repository = require('./repository'); | ||
var _ = require('underscore'), | ||
Repository = require('./repository'); | ||
module.exports = Repository.Model.extend(_.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()); | ||
} | ||
}); | ||
}); | ||
} | ||
}, require('../index'))); | ||
}()); | ||
module.exports = Repository.Model.extend(_.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()); | ||
} | ||
}); | ||
}); | ||
} | ||
}, require('../index'))); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16
11800
7
176