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

Commit d546f80d by Sascha Depold

Dao#save and DaoFactory#create can handle an object as first param

1 parent 4f6a4426
Showing with 18 additions and 8 deletions
......@@ -535,6 +535,10 @@ module.exports = (function() {
Utils.validateParameter(values, Object, { optional: true })
Utils.validateParameter(fieldsOrOptions, Object, { deprecated: Array, optional: true })
if (fieldsOrOptions instanceof Array) {
fieldsOrOptions = { fields: fieldsOrOptions }
}
return this.build(values).save(fieldsOrOptions)
}
......
......@@ -96,26 +96,32 @@ module.exports = (function() {
// if an array with field names is passed to save()
// only those fields will be updated
DAO.prototype.save = function(fields, options) {
DAO.prototype.save = function(fieldsOrOptions, options) {
var self = this
, values = fields ? {} : this.dataValues
, values = fieldsOrOptions ? {} : this.dataValues
, updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)
, createdAtAttr = Utils._.underscoredIf(this.__options.createdAt, this.__options.underscored)
if (fields) {
if (fieldsOrOptions instanceof Array) {
fieldsOrOptions = { fields: fieldsOrOptions }
}
options = Utils._.extend({}, options, fieldsOrOptions)
if (options.fields) {
if (self.__options.timestamps) {
if (fields.indexOf(updatedAtAttr) === -1) {
fields.push(updatedAtAttr)
if (options.fields.indexOf(updatedAtAttr) === -1) {
options.fields.push(updatedAtAttr)
}
if (fields.indexOf(createdAtAttr) === -1 && this.isNewRecord === true) {
fields.push(createdAtAttr)
if (options.fields.indexOf(createdAtAttr) === -1 && this.isNewRecord === true) {
options.fields.push(createdAtAttr)
}
}
var tmpVals = self.dataValues
fields.forEach(function(field) {
options.fields.forEach(function(field) {
if (tmpVals[field] !== undefined) {
values[field] = tmpVals[field]
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!