不要怂,就是干,撸起袖子干!

Commit df180435 by Mick Hansen

Merge pull request #1841 from overlookmotel/hooks2

Changes to hooks
2 parents 8df7e100 015aa1a5
......@@ -455,7 +455,7 @@ module.exports = (function() {
if (Object(association.through) === association.through) {
// Create the related model instance
return association.target.create(values, fieldsOrOptions).then(function(newAssociatedObject) {
return instance[association.accessors.add](newAssociatedObject, options).return (newAssociatedObject);
return instance[association.accessors.add](newAssociatedObject, options).return(newAssociatedObject);
});
} else {
values[association.identifier] = instance.get(association.source.primaryKeyAttribute);
......
......@@ -174,7 +174,7 @@ InstanceValidator.prototype.hookValidate = function() {
});
}).then(function() {
return self.modelInstance.Model.runHooks('afterValidate', self.modelInstance);
}).return (self.modelInstance);
}).return(self.modelInstance);
};
/**
......@@ -223,7 +223,7 @@ InstanceValidator.prototype._customValidators = function() {
var valprom = self._invokeCustomValidator(validator, validatorType)
// errors are handled in settling, stub this
.catch (noop);
.catch(noop);
validators.push(valprom);
});
......@@ -302,10 +302,10 @@ InstanceValidator.prototype._invokeCustomValidator = Promise.method(function(val
validatorFunction = Promise.promisify(validator.bind(this.modelInstance));
}
return validatorFunction()
.catch (this._pushError.bind(this, false, errorKey));
.catch(this._pushError.bind(this, false, errorKey));
} else {
return Promise.try(validator.bind(this.modelInstance, invokeArgs))
.catch (this._pushError.bind(this, false, errorKey));
.catch(this._pushError.bind(this, false, errorKey));
}
});
......
......@@ -4,6 +4,7 @@ var Utils = require('./utils')
, Mixin = require('./associations/mixin')
, InstanceValidator = require('./instance-validator')
, DataTypes = require('./data-types')
, Promise = require("./promise")
, _ = require('lodash')
, defaultsOptions = { raw: true };
......@@ -415,7 +416,9 @@ module.exports = (function() {
fieldsOrOptions = { fields: fieldsOrOptions };
}
options = Utils._.extend({}, options, fieldsOrOptions);
options = Utils._.extend({
hooks: true
}, options, fieldsOrOptions);
if (!options.fields) {
options.fields = Object.keys(this.Model.attributes);
......@@ -450,8 +453,11 @@ module.exports = (function() {
options.fields.push(createdAtAttr);
}
return self.hookValidate({
skip: _.difference(Object.keys(self.rawAttributes), options.fields)
return Promise.try(function() {
// Validate
if (options.hooks) {
return self.hookValidate({skip: _.difference(Object.keys(self.rawAttributes), options.fields)});
}
}).then(function() {
options.fields.forEach(function(field) {
if (self.dataValues[field] !== undefined) {
......@@ -488,7 +494,8 @@ module.exports = (function() {
&& !!self.Model.rawAttributes[updatedAtAttr].defaultValue
)
? self.Model.rawAttributes[updatedAtAttr].defaultValue
: Utils.now(self.sequelize.options.dialect));
: Utils.now(self.sequelize.options.dialect)
);
}
if (self.isNewRecord && createdAtAttr && !values[createdAtAttr]) {
......@@ -498,7 +505,8 @@ module.exports = (function() {
&& !!self.Model.rawAttributes[createdAtAttr].defaultValue
)
? self.Model.rawAttributes[createdAtAttr].defaultValue
: Utils.now(self.sequelize.options.dialect));
: Utils.now(self.sequelize.options.dialect)
);
}
var query = null
......@@ -534,9 +542,11 @@ module.exports = (function() {
// Add the values to the Instance
self.dataValues = _.extend(self.dataValues, values);
return Promise.try(function() {
// Run before hook
if (options.hooks) {
return self.Model.runHooks('before' + hook, self).then(function() {
// dataValues might have changed inside the hook, rebuild
// the values hash
// dataValues might have changed inside the hook, rebuild the values hash
values = {};
options.fields.forEach(function(attr) {
......@@ -552,8 +562,10 @@ module.exports = (function() {
});
args[2] = values;
return self.QueryInterface[query].apply(self.QueryInterface, args).catch (function(err) {
});
}
}).then(function() {
return self.QueryInterface[query].apply(self.QueryInterface, args).catch(function(err) {
if (!!self.__options.uniqueKeys && err.code && self.QueryInterface.QueryGenerator.uniqueConstraintMapping.code === err.code) {
var fields = self.QueryInterface.QueryGenerator.uniqueConstraintMapping.map(err.toString());
......@@ -568,15 +580,20 @@ module.exports = (function() {
}
throw err;
}).then(function(result) {
}).tap(function(result) {
// Transfer database generated values (defaults, autoincrement, etc)
values = _.extend(values, result.dataValues);
// Ensure new values are on Instance, and reset previousDataValues
result.dataValues = _.extend(result.dataValues, values);
result._previousDataValues = _.clone(result.dataValues);
return self.Model.runHooks('after' + hook, result).return (result);
}).tap(function(result) {
// Run before hook
if (options.hooks) {
return self.Model.runHooks('after' + hook, result);
}
}).then(function(result) {
return result;
});
});
});
......@@ -604,7 +621,7 @@ module.exports = (function() {
include: this.options.include || null
}, options).then(function(reload) {
self.set(reload.dataValues, {raw: true, reset: true});
}).return (self);
}).return(self);
};
/*
......@@ -660,24 +677,37 @@ module.exports = (function() {
* @return {Promise<undefined>}
*/
Instance.prototype.destroy = function(options) {
options = options || {};
options.force = options.force === undefined ? false : Boolean(options.force);
options = Utils._.extend({
hooks: true,
force: false
}, options || {});
var self = this;
// This semi awkward syntax where we can't return the chain directly but have to return the last .then() call is to allow sql proxying
return self.Model.runHooks(self.Model.options.hooks.beforeDestroy, self).then(function() {
return Promise.try(function() {
// Run before hook
if (options.hooks) {
return self.Model.runHooks('beforeDestroy', self);
}
}).then(function() {
var identifier;
if (self.Model._timestampAttributes.deletedAt && options.force === false) {
self.dataValues[self.Model._timestampAttributes.deletedAt] = new Date();
options.hooks = false;
return self.save(options);
} else {
identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id };
return self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.Model), identifier, options);
}
}).then(function(results) {
return self.Model.runHooks(self.Model.options.hooks.afterDestroy, self).return (results);
}).tap(function(result) {
// Run after hook
if (options.hooks) {
return self.Model.runHooks('afterDestroy', self);
}
}).then(function(result) {
return result;
});
};
......
......@@ -8,6 +8,7 @@ var Utils = require('./utils')
, sql = require('sql')
, SqlString = require('./sql-string')
, Transaction = require('./transaction')
, Promise = require("./promise")
, QueryTypes = require('./query-types');
module.exports = (function() {
......@@ -387,10 +388,10 @@ module.exports = (function() {
if (options.force) {
return self.drop(options).then(function() {
return doQuery().return (self);
return doQuery().return(self);
});
} else {
return doQuery().return (this);
return doQuery().return(this);
}
};
......@@ -708,7 +709,7 @@ module.exports = (function() {
// no options defined?
// return an emitter which emits null
if ([null, undefined].indexOf(options) !== -1) {
return Utils.Promise.resolve(null);
return Promise.resolve(null);
}
var primaryKeys = this.primaryKeys
......@@ -1046,11 +1047,11 @@ module.exports = (function() {
var build = self.build(params);
return build.hookValidate({skip: Object.keys(params)}).then(function() {
return Utils.Promise.resolve([build, true]);
return Promise.resolve([build, true]);
});
}
return Utils.Promise.resolve([instance, false]);
return Promise.resolve([instance, false]);
});
};
......@@ -1090,11 +1091,11 @@ module.exports = (function() {
}
return self.create(values, options).then(function(instance) {
return Utils.Promise.resolve([instance, true]);
return Promise.resolve([instance, true]);
});
}
return Utils.Promise.resolve([instance, false]);
return Promise.resolve([instance, false]);
});
};
......@@ -1109,7 +1110,8 @@ module.exports = (function() {
* @param {Object} [options]
* @param {Array} [options.fields] Fields to insert (defaults to all fields)
* @param {Boolean} [options.validate=false] Should each row be subject to validation before it is inserted. The whole insert will fail if one row fails validation
* @param {Boolean} [options.hooks=false] Run before / after create hooks for each individual Instance? BulkCreate hooks will still be run.
* @param {Boolean} [options.hooks=true] Run before / after bulk create hooks?
* @param {Boolean} [options.individualHooks=false] Run before / after create hooks for each individual Instance? BulkCreate hooks will still be run if options.hooks is true.
* @param {Boolean} [options.ignoreDuplicates=false] Ignore duplicate values for primary keys? (not supported by postgres)
*
* @return {Promise<Array<Instance>>}
......@@ -1119,16 +1121,15 @@ module.exports = (function() {
Utils.validateParameter(options, 'undefined', { deprecated: Object, optional: true, index: 3, method: 'Model#bulkCreate' });
if (!records.length) {
return new Utils.Promise(function(resolve) {
resolve([]);
});
return Promise.resolve([]);
}
options = Utils._.extend({
validate: false,
hooks: false,
hooks: true,
individualHooks: false,
ignoreDuplicates: false
}, options ||  {});
}, options || {});
if (fieldsOrOptions instanceof Array) {
options.fields = fieldsOrOptions;
......@@ -1138,87 +1139,79 @@ module.exports = (function() {
}
if (this.sequelize.options.dialect === 'postgres' && options.ignoreDuplicates) {
return Utils.Promise.reject(new Error('Postgres does not support the \'ignoreDuplicates\' option.'));
return Promise.reject(new Error('Postgres does not support the \'ignoreDuplicates\' option.'));
}
var self = this
, updatedAtAttr = this._timestampAttributes.updatedAt
, createdAtAttr = this._timestampAttributes.createdAt
, errors = []
, daoPromises = []
, daos = records.map(function(values) {
return self.build(values, {
isNewRecord: true
});
});
if (options.validate && options.fields.length) {
var skippedFields = Utils._.difference(Object.keys(self.attributes), options.fields);
}
, updatedAtAttr = this._timestampAttributes.updatedAt
, now = Utils.now(self.modelManager.sequelize.options.dialect);
var runAfterCreate = function() {
return self.runHooks('afterBulkCreate', daos, options.fields).spread(function(newRecords) {
return new self.sequelize.Promise.resolve(newRecords || daos);
// build DAOs
var daos = records.map(function(values) {
return self.build(values, {isNewRecord: true});
});
};
return self.runHooks('beforeBulkCreate', daos, options.fields).spread(function(newRecords, newFields) {
daos = newRecords || daos;
options.fields = newFields || options.fields;
var runHook = function(dao) {
if (options.hooks === false) {
var values = options.fields.length > 0 ? {} : dao.dataValues;
return Promise.try(function() {
// Run before hook
if (options.hooks) {
return self.runHooks('beforeBulkCreate', daos, options.fields).spread(function(_daos, _fields) {
daos = _daos || daos;
options.fields = _fields || options.fields;
});
}
}).then(function() {
daos.forEach(function(dao) {
// Filter dataValues by options.fields
var values = {};
options.fields.forEach(function(field) {
values[field] = dao.dataValues[field];
});
// set createdAt/updatedAt attributes
if (createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = Utils.now(self.modelManager.sequelize.options.dialect);
values[createdAtAttr] = now;
}
if (updatedAtAttr && !values[updatedAtAttr]) {
values[updatedAtAttr] = Utils.now(self.modelManager.sequelize.options.dialect);
values[updatedAtAttr] = now;
}
records.push(values);
return values;
}
return self.runHooks('beforeCreate', dao).spread(function(newValues) {
dao = newValues || dao;
return dao.save({ transaction: options.transaction }).then(function() {
return self.runHooks('afterCreate', dao);
});
dao.dataValues = values;
});
};
var runValidation = function(dao) {
if (options.validate === false) {
return dao;
}
// Validate
if (options.validate) {
var skippedFields = Utils._.difference(Object.keys(self.attributes), options.fields);
var fn = options.hooks === true ? 'hookValidate' : 'validate';
var errors = [];
return Promise.map(daos, function(dao) {
var fn = options.individualHooks ? 'hookValidate' : 'validate';
return dao[fn]({skip: skippedFields}).then(function(err) {
if (!!err) {
errors.push({record: dao, errors: err});
}
});
};
records = [];
daos.forEach(function(dao) {
daoPromises.push(runValidation(dao));
daoPromises.push(runHook(dao));
}).then(function() {
if (errors.length) {
return Promise.reject(errors);
}
});
}
}).then(function() {
if (options.individualHooks) {
// Create each dao individually
return Promise.map(daos, function(dao) {
return dao.save({transaction: options.transaction});
}).then(function(_daos) {
daos = _daos;
});
} else {
// Create all in one query
// Recreate records from daos to represent any changes made in hooks or validation
records = daos.map(function(dao) {
return dao.dataValues;
});
return self.sequelize.Promise.all(daoPromises).then(function() {
if (errors.length) {
// Validation or hooks failed
return self.sequelize.Promise.reject(errors);
} else if (records.length) {
// Map field names
records.forEach(function(values) {
for (var attr in values) {
......@@ -1241,12 +1234,17 @@ module.exports = (function() {
}
// Insert all records at once
return self.QueryInterface.bulkInsert(self.getTableName(), records, options, attributes).then(runAfterCreate);
} else {
// Records were already saved while running create / update hooks
return runAfterCreate();
return self.QueryInterface.bulkInsert(self.getTableName(), records, options, attributes);
}
}).then(function() {
// Run after hook
if (options.hooks) {
return self.runHooks('afterBulkCreate', daos, options.fields).spread(function(_daos) {
if (_daos) daos = _daos;
});
}
}).then(function() {
return daos;
});
};
......@@ -1255,79 +1253,66 @@ module.exports = (function() {
*
* @param {Object} [where] Options to describe the scope of the search.
* @param {Object} [options]
* @param {Boolean} [options.hooks] If set to true, destroy will find all records within the where parameter and will execute before-/ after bulkDestroy hooks on each row
* @param {Boolean} [options.hooks=true] Run before / after bulk destroy hooks?
* @param {Boolean} [options.individualHooks=false] If set to true, destroy will find all records within the where parameter and will execute before / after bulkDestroy hooks on each row
* @param {Number} [options.limit] How many rows to delete
* @param {Boolean} [options.truncate] If set to true, dialects that support it will use TRUNCATE instead of DELETE FROM. If a table is truncated the where and limit options are ignored
*
* @return {Promise<undefined>}
*/
Model.prototype.destroy = function(where, options) {
options = options || {};
options.force = options.force === undefined ? false : Boolean(options.force);
options = Utils._.extend({
hooks: true,
individualHooks: false,
force: false
}, options || {});
options.type = QueryTypes.BULKDELETE;
var self = this
, query = null
, args = [];
return self.runHooks(self.options.hooks.beforeBulkDestroy, where).then(function(newWhere) {
where = newWhere || where;
, daos;
if (self._timestampAttributes.deletedAt && options.force === false) {
var attrValueHash = {};
attrValueHash[self._timestampAttributes.deletedAt] = Utils.now();
query = 'bulkUpdate';
args = [self.getTableName(), attrValueHash, where, self];
} else {
query = 'bulkDelete';
args = [self.getTableName(), where, options, self];
}
var runQuery = function(records) {
return self.QueryInterface[query].apply(self.QueryInterface, args).then(function(results) {
if (options && options.hooks === true) {
var tick = 0;
var next = function(i) {
return self.runHooks(self.options.hooks.afterDestroy, records[i]).then(function(newValues) {
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues;
tick++;
if (tick >= records.length) {
return self.runHooks(self.options.hooks.afterBulkDestroy, where).return (results);
return Promise.try(function() {
// Run before hook
if (options.hooks) {
return self.runHooks('beforeBulkDestroy', where).spread(function(_where) {
where = _where || where;
});
}
return next(tick);
}).then(function() {
// Get daos and run beforeDestroy hook on each record individually
if (options.individualHooks) {
return self.all({where: where}, {transaction: options.transaction}).map(function(dao) {
return self.runHooks('beforeDestroy', dao).spread(function(_dao) {
return _dao || dao;
});
};
return next(tick);
}).then(function(_daos) {
daos = _daos;
});
}
}).then(function() {
// Run delete query (or update if paranoid)
if (self._timestampAttributes.deletedAt && !options.force) {
var attrValueHash = {};
attrValueHash[self._timestampAttributes.deletedAt] = Utils.now(self.modelManager.sequelize.options.dialect);
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHash, where, options, self.rawAttributes);
} else {
return self.runHooks(self.options.hooks.afterBulkDestroy, where).return (results);
return self.QueryInterface.bulkDelete(self.getTableName(), where, options, self);
}
}).tap(function() {
// Run afterDestroy hook on each record individually
if (options.individualHooks) {
return Promise.map(daos, function(dao) {
return self.runHooks('afterDestroy', dao);
});
};
if (options && options.hooks === true) {
var tick = 0;
return self.all({where: where}).then(function(records) {
var next = function(i) {
return self.runHooks(self.options.hooks.beforeDestroy, records[i]).then(function(newValues) {
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues;
tick++;
if (tick >= records.length) {
return runQuery(records);
}
return next(tick);
});
};
return next(tick);
});
} else {
return runQuery();
}).tap(function() {
// Run after hook
if (options.hooks) {
return self.runHooks('afterBulkDestroy', where);
}
}).then(function(affectedRows) {
return affectedRows;
});
};
......@@ -1338,98 +1323,146 @@ module.exports = (function() {
* @param {Object where Options to describe the scope of the search. Note that these options are not wrapped in a { where: ... } is in find / findAll calls etc. This is probably due to change in 2.0
* @param {Object} [options]
* @param {Boolean} [options.validate=true] Should each row be subject to validation before it is inserted. The whole insert will fail if one row fails validation
* @param {Boolean} [options.hooks=false] Run before / after bulkUpdate hooks?
* @param {Boolean} [options.hooks=true] Run before / after bulk update hooks?
* @param {Boolean} [options.individualHooks=false] Run before / after update hooks?
* @param {Number} [options.limit] How many rows to update (only for mysql and mariadb)
* @deprecated The syntax is due for change, in order to make `where` more consistent with the rest of the API
*
* @return {Promise}
*/
Model.prototype.update = function(attrValueHash, where, options) {
var self = this
, tick = 0;
var self = this;
options = Utils._.extend({
validate: true,
hooks: true,
individualHooks: false,
force: false
}, options || {});
options = options || {};
options.validate = options.validate === undefined ? true : Boolean(options.validate);
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks);
options.type = QueryTypes.BULKUPDATE;
if (self._timestampAttributes.updatedAt) {
attrValueHash[self._timestampAttributes.updatedAt] = Utils.now();
attrValueHash[self._timestampAttributes.updatedAt] = Utils.now(self.modelManager.sequelize.options.dialect);
}
var runSave = function() {
return self.runHooks(self.options.hooks.beforeBulkUpdate, attrValueHash, where).spread(function(attributes, _where) {
where = _where || where;
attrValueHash = attributes || attrValueHash;
var daos
, attrValueHashUse;
var runQuery = function(records) {
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHash, where, options, self.rawAttributes).then(function(results) {
if (options && options.hooks === true && !!records && records.length > 0) {
var tick = 0;
var next = function(i) {
return self.runHooks(self.options.hooks.afterUpdate, records[i]).then(function(newValues) {
records[i].dataValues = (!!newValues && newValues.dataValues) ? newValues.dataValues : records[i].dataValues;
tick++;
return Promise.try(function() {
// Validate
if (options.validate) {
var build = self.build(attrValueHash);
if (tick >= records.length) {
return self.runHooks(self.options.hooks.afterBulkUpdate, attrValueHash, where).return (records);
}
// We want to skip validations for all other fields
var skippedFields = Utils._.difference(Object.keys(self.attributes), Object.keys(attrValueHash));
return next(tick);
return build.hookValidate({skip: skippedFields}).then(function(attributes) {
if (attributes && attributes.dataValues) {
attrValueHash = Utils._.pick.apply(Utils._, [].concat(attributes.dataValues).concat(Object.keys(attrValueHash)));
}
});
};
return next(tick);
} else {
return self.runHooks(self.options.hooks.afterBulkUpdate, attrValueHash, where).return (results);
}
}).then(function() {
// Run before hook
if (options.hooks) {
return self.runHooks('beforeBulkUpdate', attrValueHash, where).spread(function(_attrValueHash, _where) {
where = _where || where;
attrValueHash = _attrValueHash || attrValueHash;
});
};
}
}).then(function() {
attrValueHashUse = attrValueHash;
if (options.hooks === true) {
return self.all({where: where}).then(function(records) {
if (records === null || records.length < 1) {
return runQuery();
// Get daos and run beforeDestroy hook on each record individually
if (options.individualHooks) {
return self.all({where: where}, {transaction: options.transaction}).then(function(_daos) {
daos = _daos;
if (!daos.length) {
return [];
}
var next = function(i) {
return self.runHooks(self.options.hooks.beforeUpdate, records[i]).then(function(newValues) {
records[i].dataValues = (!!newValues && newValues.dataValues) ? newValues.dataValues : records[i].dataValues;
tick++;
// Run beforeUpdate hooks on each record and check whether beforeUpdate hook changes values uniformly
// i.e. whether they change values for each record in the same way
var changedValues
, different = false;
if (tick >= records.length) {
return runQuery(records);
}
return Promise.map(daos, function(dao) {
// Record updates in dao's dataValues
Utils._.extend(dao.dataValues, attrValueHash);
return next(tick);
});
};
// Run beforeUpdate hook
return self.runHooks('beforeUpdate', dao).spread(function(_dao) {
dao = _dao || dao;
return next(tick);
});
} else {
return runQuery();
if (!different) {
var thisChangedValues = {};
Utils._.forIn(dao.dataValues, function(newValue, attr) {
if (newValue !== dao._previousDataValues[attr]) {
thisChangedValues[attr] = newValue;
}
});
};
if (options.validate === true) {
var build = self.build(attrValueHash);
if (!changedValues) {
changedValues = thisChangedValues;
} else {
different = !Utils._.isEqual(changedValues, thisChangedValues);
}
}
// We want to skip validations for all other fields
var updatedFields = Object.keys(attrValueHash);
var skippedFields = Utils._.difference(Object.keys(self.attributes), updatedFields);
return dao;
});
}).then(function(_daos) {
daos = _daos;
return build.hookValidate({skip: skippedFields}).then(function(attributes) {
if (!!attributes && !!attributes.dataValues) {
attrValueHash = Utils._.pick.apply(Utils._, [].concat(attributes.dataValues).concat(Object.keys(attrValueHash)));
if (!different) {
// Hooks do not change values or change them uniformly
if (Object.keys(changedValues).length) {
// Hooks change values - record changes in attrValueHashUse so they are executed
attrValueHashUse = changedValues;
}
return;
} else {
// Hooks change values in a different way for each record
// Do not run original query but save each record individually
return Promise.map(daos, function(dao) {
return dao.save({transaction: options.transaction, hooks: false});
}).tap(function(_daos) {
daos = _daos;
});
}
});
});
}
}).then(function(results) {
if (results) {
// Update already done row-by-row - exit
return [results.length, results];
}
return runSave();
// Run query to update all rows
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHashUse, where, options, self.rawAttributes).then(function(affectedRows) {
return [affectedRows];
});
}).tap(function(result) {
if (options.individualHooks) {
return Promise.map(daos, function(dao) {
return self.runHooks('afterUpdate', dao).spread(function(_dao) {
return _dao || dao;
});
}).then(function(_daos) {
result[1] = daos = _daos;
});
} else {
return runSave();
}
}).tap(function() {
// Run after hook
if (options.hooks) {
return self.runHooks('afterBulkUpdate', attrValueHash, where);
}
}).then(function(result) {
// Return result in form [affectedRows, daos] (daos missed off if options.individualHooks != true)
return result;
});
};
/**
......
......@@ -594,7 +594,7 @@ module.exports = (function() {
* @return {Promise}
*/
Sequelize.prototype.authenticate = function() {
return this.query('SELECT 1+1 AS result', null, { raw: true, plain: true }).return ().catch (function(err) {
return this.query('SELECT 1+1 AS result', null, { raw: true, plain: true }).return().catch(function(err) {
throw new Error(err);
});
};
......
......@@ -791,15 +791,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
, done = _.after(2, _done)
this.User.bulkCreate(data).success(function() {
self.User.update({username: 'Bill'}, {secretValue: '42'}).done(function(err, affectedRows) {
expect(err).not.to.be.ok
self.User.update({username: 'Bill'}, {secretValue: '42'}).spread(function(affectedRows) {
expect(affectedRows).to.equal(2)
done()
})
self.User.update({username: 'Bill'}, {secretValue: '44'}).done(function(err, affectedRows) {
expect(err).not.to.be.ok
self.User.update({username: 'Bill'}, {secretValue: '44'}).spread(function(affectedRows) {
expect(affectedRows).to.equal(0)
done()
......@@ -815,8 +813,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
{ username: 'Peter', secretValue: '42' }]
this.User.bulkCreate(data).success(function () {
self.User.update({secretValue: '43'}, {username: 'Peter'}, {limit: 1}).done(function (err, affectedRows) {
expect(err).not.to.be.ok
self.User.update({secretValue: '43'}, {username: 'Peter'}, {limit: 1}).spread(function(affectedRows) {
expect(affectedRows).to.equal(1)
done()
})
......
......@@ -103,7 +103,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
this.User.bulkCreate([
{username: 'Bob', mood: 'cold'},
{username: 'Tobi', mood: 'hot'}
], { fields: [], hooks: true }).success(function(bulkUsers) {
], { fields: [], individualHooks: true }).success(function(bulkUsers) {
expect(beforeBulkCreate).to.be.true
expect(afterBulkCreate).to.be.true
expect(bulkUsers).to.be.instanceof(Array)
......@@ -130,7 +130,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
return this.User.bulkCreate([
{username: 'Bob', mood: 'cold'},
{username: 'Tobi', mood: 'hot'}
], { fields: [], hooks: false }).success(function(bulkUsers) {
], { fields: [], individualHooks: false }).success(function(bulkUsers) {
return self.User.all().success(function(users) {
expect(users[0].mood).to.equal(null)
expect(users[1].mood).to.equal(null)
......@@ -288,7 +288,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
this.User.bulkCreate([
{username: 'Bob', mood: 'cold'},
{username: 'Tobi', mood: 'hot'}
], { hooks: true }).success(function(bulkUsers) {
], { individualHooks: true }).success(function(bulkUsers) {
expect(beforeBulkCreate).to.be.true
expect(afterBulkCreate).to.be.true
expect(bulkUsers).to.be.instanceof(Array)
......@@ -4343,7 +4343,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
})
describe('with the {hooks: true} option', function() {
describe('with the {individualHooks: true} option', function() {
beforeEach(function(done) {
this.User = this.sequelize.define('User', {
username: {
......@@ -4389,7 +4389,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
fn()
})
this.User.bulkCreate([{aNumber: 5}, {aNumber: 7}, {aNumber: 3}], { fields: ['aNumber'], hooks: true }).success(function(records) {
this.User.bulkCreate([{aNumber: 5}, {aNumber: 7}, {aNumber: 3}], { fields: ['aNumber'], individualHooks: true }).success(function(records) {
records.forEach(function(record) {
expect(record.username).to.equal('User' + record.id)
expect(record.beforeHookTest).to.be.true
......@@ -4423,7 +4423,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
fn()
})
this.User.bulkCreate([{aNumber: 5}, {aNumber: 7}, {aNumber: 3}], { fields: ['aNumber'], hooks: true }).error(function(err) {
this.User.bulkCreate([{aNumber: 5}, {aNumber: 7}, {aNumber: 3}], { fields: ['aNumber'], individualHooks: true }).error(function(err) {
expect(err).to.equal('You shall not pass!')
expect(beforeBulkCreate).to.be.true
expect(afterBulkCreate).to.be.false
......@@ -5254,7 +5254,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
})
describe('with the {hooks: true} option', function() {
describe('with the {individualHooks: true} option', function() {
beforeEach(function(done) {
this.User = this.sequelize.define('User', {
username: {
......@@ -5301,11 +5301,10 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
fn()
})
this.User.bulkCreate([
{aNumber: 1}, {aNumber: 1}, {aNumber: 1}
]).success(function() {
self.User.update({aNumber: 10}, {aNumber: 1}, {hooks: true}).success(function(records) {
self.User.update({aNumber: 10}, {aNumber: 1}, {individualHooks: true}).spread(function(affectedRows, records) {
records.forEach(function(record) {
expect(record.username).to.equal('User' + record.id)
expect(record.beforeHookTest).to.be.true
......@@ -5342,7 +5341,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
this.User.bulkCreate([{aNumber: 1}, {aNumber: 1}, {aNumber: 1}], { fields: ['aNumber'] }).success(function() {
self.User.update({aNumber: 10}, {aNumber: 1}, {hooks: true}).error(function(err) {
self.User.update({aNumber: 10}, {aNumber: 1}, {individualHooks: true}).error(function(err) {
expect(err).to.equal('You shall not pass!')
expect(beforeBulk).to.be.true
expect(afterBulk).to.be.false
......@@ -6038,7 +6037,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
})
describe('with the {hooks: true} option', function() {
describe('with the {individualHooks: true} option', function() {
beforeEach(function(done) {
this.User = this.sequelize.define('User', {
username: {
......@@ -6091,7 +6090,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
this.User.bulkCreate([
{aNumber: 1}, {aNumber: 1}, {aNumber: 1}
]).success(function() {
self.User.destroy({aNumber: 1}, {hooks: true}).success(function() {
self.User.destroy({aNumber: 1}, {individualHooks: true}).success(function() {
expect(beforeBulk).to.be.true
expect(afterBulk).to.be.true
expect(beforeHook).to.be.true
......@@ -6129,7 +6128,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
this.User.bulkCreate([{aNumber: 1}, {aNumber: 1}, {aNumber: 1}], { fields: ['aNumber'] }).success(function() {
self.User.destroy({aNumber: 1}, {hooks: true}).error(function(err) {
self.User.destroy({aNumber: 1}, {individualHooks: true}).error(function(err) {
expect(err).to.equal('You shall not pass!')
expect(beforeBulk).to.be.true
expect(beforeHook).to.be.true
......@@ -7243,7 +7242,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
return this.User.bulkCreate([
{username: 'Bob', mood: 'cold'},
{username: 'Tobi', mood: 'hot'}
], { fields: [], hooks: false }).success(function(bulkUsers) {
], { fields: [], individualHooks: false }).success(function(bulkUsers) {
return self.User.all().success(function(users) {
expect(users[0].mood).to.equal(null)
expect(users[1].mood).to.equal(null)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!