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

Commit 5a75d260 by Jan Aagaard Meier

Refactored model.update and destroy to use promises

1 parent 1bd525d2
Showing with 38 additions and 118 deletions
...@@ -92,7 +92,9 @@ Hooks.runHooks = function() { ...@@ -92,7 +92,9 @@ Hooks.runHooks = function() {
if (!!arguments[0]) { if (!!arguments[0]) {
return reject(arguments[0]) return reject(arguments[0])
} }
if (arguments.length) {
resolveArgs = Array.prototype.slice.call(arguments, 1) resolveArgs = Array.prototype.slice.call(arguments, 1)
}
return run(hooks[tick]) return run(hooks[tick])
})) }))
...@@ -101,7 +103,9 @@ Hooks.runHooks = function() { ...@@ -101,7 +103,9 @@ Hooks.runHooks = function() {
maybePromise.spread(function () { maybePromise.spread(function () {
tick++ tick++
if (arguments.length) {
resolveArgs = Array.prototype.slice.call(arguments) resolveArgs = Array.prototype.slice.call(arguments)
}
return run(hooks[tick]) return run(hooks[tick])
}, reject) }, reject)
......
...@@ -1266,12 +1266,7 @@ module.exports = (function() { ...@@ -1266,12 +1266,7 @@ module.exports = (function() {
, query = null , query = null
, args = [] , args = []
return new Utils.CustomEventEmitter(function(emitter) { return self.runHooks(self.options.hooks.beforeBulkDestroy, where).then(function(newWhere) {
self.runHooks(self.options.hooks.beforeBulkDestroy, where, function(err, newWhere) {
if (!!err) {
return emitter.emit('error', err)
}
where = newWhere || where where = newWhere || where
if (self._timestampAttributes.deletedAt && options.force === false) { if (self._timestampAttributes.deletedAt && options.force === false) {
...@@ -1284,88 +1279,52 @@ module.exports = (function() { ...@@ -1284,88 +1279,52 @@ module.exports = (function() {
args = [self.getTableName(), where, options, self] args = [self.getTableName(), where, options, self]
} }
var runQuery = function(err, records) { var runQuery = function(records) {
if (!!err) { return self.QueryInterface[query].apply(self.QueryInterface, args).then(function(results) {
return emitter.emit('error', err)
}
query = self.QueryInterface[query].apply(self.QueryInterface, args)
query.on('sql', function(sql) {
emitter.emit('sql', sql)
})
.error(function(err) {
emitter.emit('error', err)
})
.success(function(results) {
var finished = function(err) {
if (!!err) {
return emitter.emit('error', err)
}
self.runHooks(self.options.hooks.afterBulkDestroy, where, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
emitter.emit('success', results)
})
}
if (options && options.hooks === true) { if (options && options.hooks === true) {
var tick = 0 var tick = 0
var next = function(i) { var next = function(i) {
self.runHooks(self.options.hooks.afterDestroy, records[i], function(err, newValues) { return self.runHooks(self.options.hooks.afterDestroy, records[i]).then(function(newValues) {
if (!!err) {
return finished(err)
}
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues
tick++ tick++
if (tick >= records.length) { if (tick >= records.length) {
return finished() return self.runHooks(self.options.hooks.afterBulkDestroy, where).return(results)
} }
next(tick) return next(tick)
}) })
} }
next(tick) return next(tick)
} else { } else {
finished() return self.runHooks(self.options.hooks.afterBulkDestroy, where).return(results)
} }
}) })
} }
if (options && options.hooks === true) { if (options && options.hooks === true) {
var tick = 0 var tick = 0
self.all({where: where}).error(function(err) { emitter.emit('error', err) }) return self.all({where: where}).then(function(records) {
.success(function(records) {
var next = function(i) { var next = function(i) {
self.runHooks(self.options.hooks.beforeDestroy, records[i], function(err, newValues) { return self.runHooks(self.options.hooks.beforeDestroy, records[i]).then(function(newValues) {
if (!!err) {
return runQuery(err)
}
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues
tick++ tick++
if (tick >= records.length) { if (tick >= records.length) {
return runQuery(null, records) return runQuery(records)
} }
next(tick) return next(tick)
}) })
} }
next(tick) return next(tick)
}) })
//
} else { } else {
runQuery() return runQuery()
} }
}) })
}).run()
} }
/** /**
...@@ -1383,7 +1342,6 @@ module.exports = (function() { ...@@ -1383,7 +1342,6 @@ module.exports = (function() {
*/ */
Model.prototype.update = function(attrValueHash, where, options) { Model.prototype.update = function(attrValueHash, where, options) {
var self = this var self = this
, query = null
, tick = 0 , tick = 0
options = options || {} options = options || {}
...@@ -1395,97 +1353,58 @@ module.exports = (function() { ...@@ -1395,97 +1353,58 @@ module.exports = (function() {
attrValueHash[self._timestampAttributes.updatedAt] = Utils.now() attrValueHash[self._timestampAttributes.updatedAt] = Utils.now()
} }
return new Utils.CustomEventEmitter(function(emitter) {
var runSave = function() { var runSave = function() {
self.runHooks(self.options.hooks.beforeBulkUpdate, attrValueHash, where, function(err, attributes, _where) { return self.runHooks(self.options.hooks.beforeBulkUpdate, attrValueHash, where).spread(function(attributes, _where) {
if (!!err) {
return emitter.emit('error', err)
}
where = _where || where where = _where || where
attrValueHash = attributes || attrValueHash attrValueHash = attributes || attrValueHash
var runQuery = function(err, records) { var runQuery = function(records) {
if (!!err) { return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHash, where, options, self.rawAttributes).then(function(results) {
return emitter.emit('error', err)
}
query = self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHash, where, options, self.rawAttributes)
query.on('sql', function(sql) {
emitter.emit('sql', sql)
})
.error(function(err) {
emitter.emit('error', err)
})
.success(function(results) {
var finished = function(err, records) {
if (!!err) {
return emitter.emit('error', err)
}
self.runHooks(self.options.hooks.afterBulkUpdate, attrValueHash, where, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
emitter.emit('success', records)
})
}
if (options && options.hooks === true && !!records && records.length > 0) { if (options && options.hooks === true && !!records && records.length > 0) {
var tick = 0 var tick = 0
var next = function(i) { var next = function(i) {
self.runHooks(self.options.hooks.afterUpdate, records[i], function(err, newValues) { return self.runHooks(self.options.hooks.afterUpdate, records[i]).then(function(newValues) {
if (!!err) { records[i].dataValues = (!!newValues && newValues.dataValues) ? newValues.dataValues : records[i].dataValues
return finished(err)
}
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues
tick++ tick++
if (tick >= records.length) { if (tick >= records.length) {
return finished(null, records) return self.runHooks(self.options.hooks.afterBulkUpdate, attrValueHash, where).return(records)
} }
next(tick) return next(tick)
}) })
} }
next(tick) return next(tick)
} else { } else {
finished(null, results) return self.runHooks(self.options.hooks.afterBulkUpdate, attrValueHash, where).return(results)
} }
}) })
} }
if (options.hooks === true) { if (options.hooks === true) {
self.all({where: where}).error(function(err) { emitter.emit('error', err) }) return self.all({where: where}).then(function(records) {
.success(function(records) {
if (records === null || records.length < 1) { if (records === null || records.length < 1) {
return runQuery(null) return runQuery()
} }
var next = function(i) { var next = function(i) {
self.runHooks(self.options.hooks.beforeUpdate, records[i], function(err, newValues) { return self.runHooks(self.options.hooks.beforeUpdate, records[i]).then(function(newValues) {
if (!!err) { records[i].dataValues = (!!newValues && newValues.dataValues) ? newValues.dataValues : records[i].dataValues
return runQuery(err)
}
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues
tick++ tick++
if (tick >= records.length) { if (tick >= records.length) {
return runQuery(null, records) return runQuery(records)
} }
next(tick) return next(tick)
}) })
} }
next(tick) return next(tick)
}) })
} else { } else {
runQuery() return runQuery()
} }
}) })
} }
...@@ -1497,19 +1416,16 @@ module.exports = (function() { ...@@ -1497,19 +1416,16 @@ module.exports = (function() {
var updatedFields = Object.keys(attrValueHash) var updatedFields = Object.keys(attrValueHash)
var skippedFields = Utils._.difference(Object.keys(self.attributes), updatedFields) var skippedFields = Utils._.difference(Object.keys(self.attributes), updatedFields)
build.hookValidate({skip: skippedFields}).error(function(err) { return build.hookValidate({skip: skippedFields}).then(function(attributes) {
emitter.emit('error', err)
}).success(function(attributes) {
if (!!attributes && !!attributes.dataValues) { if (!!attributes && !!attributes.dataValues) {
attrValueHash = Utils._.pick.apply(Utils._, [].concat(attributes.dataValues).concat(Object.keys(attrValueHash))) attrValueHash = Utils._.pick.apply(Utils._, [].concat(attributes.dataValues).concat(Object.keys(attrValueHash)))
} }
runSave() return runSave()
}) })
} else { } else {
runSave() return runSave()
} }
}).run()
} }
/** /**
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!