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

Commit 1b9b44cd by sdepold

set created at if field list is passed to save method

1 parent d9b280a5
Showing with 17 additions and 4 deletions
...@@ -84,10 +84,22 @@ module.exports = (function() { ...@@ -84,10 +84,22 @@ module.exports = (function() {
// if an array with field names is passed to save() // if an array with field names is passed to save()
// only those fields will be updated // only those fields will be updated
DAO.prototype.save = function(fields) { DAO.prototype.save = function(fields) {
var self = this var self = this
, values = fields ? {} : this.values; , values = fields ? {} : this.values
, updatedAtAttr = this.__options.underscored ? 'updated_at' : 'updatedAt'
, createdAtAttr = this.__options.underscored ? 'created_at' : 'createdAt'
if(fields) { if(fields) {
if(self.__options.timestamps) {
if(fields.indexOf(updatedAtAttr) === -1) {
fields.push(updatedAtAttr)
}
if(fields.indexOf(createdAtAttr) === -1) {
fields.push(createdAtAttr)
}
}
fields.forEach(function(field) { fields.forEach(function(field) {
if(self.values[field]) { if(self.values[field]) {
values[field] = self.values[field] values[field] = self.values[field]
...@@ -95,7 +107,6 @@ module.exports = (function() { ...@@ -95,7 +107,6 @@ module.exports = (function() {
}); });
} }
var updatedAtAttr = this.__options.underscored ? 'updated_at' : 'updatedAt'
if(this.__options.timestamps && this.hasOwnProperty(updatedAtAttr)) { if(this.__options.timestamps && this.hasOwnProperty(updatedAtAttr)) {
var now = new Date() var now = new Date()
...@@ -189,6 +200,7 @@ module.exports = (function() { ...@@ -189,6 +200,7 @@ module.exports = (function() {
var self = this var self = this
var readOnlyAttributes = Utils._.keys(this.__factory.primaryKeys) var readOnlyAttributes = Utils._.keys(this.__factory.primaryKeys)
readOnlyAttributes.push('id') readOnlyAttributes.push('id')
readOnlyAttributes.push('createdAt') readOnlyAttributes.push('createdAt')
readOnlyAttributes.push('updatedAt') readOnlyAttributes.push('updatedAt')
...@@ -264,8 +276,9 @@ module.exports = (function() { ...@@ -264,8 +276,9 @@ module.exports = (function() {
defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date() defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date()
defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date() defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date()
if(this.__options.paranoid) if(this.__options.paranoid) {
defaults[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = null defaults[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = null
}
} }
Utils._.map(defaults, function(value, attr) { Utils._.map(defaults, function(value, attr) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!