diff --git a/lib/dao-factory.js b/lib/dao-factory.js index f8023a48ffc5..d15b09750de8 100644 --- a/lib/dao-factory.js +++ b/lib/dao-factory.js @@ -255,12 +255,12 @@ module.exports = (function() { } DAOFactory.prototype.build = function(values, options) { - options = options || {} + options = options || {isNewRecord: true} var self = this - , instance = new this.DAO(values, this.options) + , instance = new this.DAO(values, this.options, options.isNewRecord) - instance.isNewRecord = options.hasOwnProperty('isNewRecord') ? options.isNewRecord : true + instance.isNewRecord = options.isNewRecord return instance } diff --git a/lib/dao.js b/lib/dao.js index 323997231677..c4e658c07096 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -4,13 +4,13 @@ var Utils = require("./utils") , DataTypes = require("./data-types") module.exports = (function() { - var DAO = function(values, options) { + var DAO = function(values, options, isNewRecord) { var self = this; this.__options = options; this.hasPrimaryKeys = options.hasPrimaryKeys; this.selectedValues = values; - initAttributes.call(this, values) + initAttributes.call(this, values, isNewRecord) if (this.hasDefaultValues) { Utils._.each(this.defaultValues, function (value, name) { @@ -271,7 +271,7 @@ module.exports = (function() { // private - var initAttributes = function(values) { + var initAttributes = function(values, isNewRecord) { // add all passed values to the dao and store the attribute names in this.attributes for (var key in values) { if (values.hasOwnProperty(key)) { @@ -283,7 +283,7 @@ module.exports = (function() { // a newly created dao has no id var defaults = this.hasPrimaryKeys ? {} : { id: null } - if (this.__options.timestamps) { + if (this.__options.timestamps && isNewRecord) { defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date() defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date() diff --git a/spec/dao.spec.js b/spec/dao.spec.js index 293f06894ade..9ac974592b36 100644 --- a/spec/dao.spec.js +++ b/spec/dao.spec.js @@ -148,5 +148,25 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() { }) }) }) + + it("returns the timestamps if no attributes have been specified", function(done) { + this.User.create({ username: 'fnord' }).success(function() { + this.User.findAll().success(function(users) { + expect(users[0].createdAt).toBeDefined() + done() + }.bind(this)) + }.bind(this)) + }) + + it("does not return the timestamps if the username attribute has been specified", function(done) { + this.User.create({ username: 'fnord' }).success(function() { + this.User.findAll({ attributes: ['username'] }).success(function(users) { + expect(users[0].createdAt).not.toBeDefined() + expect(users[0].username).toBeDefined() + + done() + }.bind(this)) + }.bind(this)) + }) }) })