Skip to content

Commit

Permalink
When a table's timestamps are set to true, increment() and decrement(…
Browse files Browse the repository at this point in the history
…) functions should update the updatedAt column.
  • Loading branch information
durango committed Oct 30, 2013
1 parent 3a94a39 commit 2880122
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
15 changes: 12 additions & 3 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,10 @@ module.exports = (function() {
}

DAO.prototype.increment = function(fields, count) {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id },
values = {}
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }
, updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)
, values = {}
, options = {}

if (count === undefined) {
count = 1;
Expand All @@ -372,7 +374,14 @@ module.exports = (function() {
values = fields;
}

return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.__factory.tableName, this.__factory.options.schema), values, identifier)

if (this.__options.timestamps) {
if (!values[updatedAtAttr]) {
options[updatedAtAttr] = Utils.now(this.daoFactory.daoFactoryManager.sequelize.options.dialect)
}
}

return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.__factory.tableName, this.__factory.options.schema), values, identifier, options)
}

DAO.prototype.decrement = function (fields, count) {
Expand Down
8 changes: 7 additions & 1 deletion lib/dialects/mysql/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ module.exports = (function() {
return query
},

incrementQuery: function (tableName, attrValueHash, where) {
incrementQuery: function (tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)

var values = []
Expand All @@ -256,6 +256,12 @@ module.exports = (function() {
values.push(this.quoteIdentifier(key) + "=" + this.quoteIdentifier(key) + " + " + _value)
}

options = options || {}
for (var key in options) {
var value = options[key];
values.push(this.quoteIdentifier(key) + "=" + this.escape(value))
}

var table = this.quoteIdentifier(tableName)
values = values.join(",")
where = this.getWhereConditions(where)
Expand Down
8 changes: 7 additions & 1 deletion lib/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements)
},

incrementQuery: function(tableName, attrValueHash, where) {
incrementQuery: function(tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)

var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %> RETURNING *"
Expand All @@ -377,6 +377,12 @@ module.exports = (function() {
values.push(this.quoteIdentifier(key) + "=" + this.quoteIdentifier(key) + " + " + this.escape(value))
}

options = options || {}
for (var key in options) {
var value = options[key];
values.push(this.quoteIdentifier(key) + "=" + this.escape(value))
}

var replacements = {
table: this.quoteIdentifiers(tableName),
values: values.join(","),
Expand Down
8 changes: 7 additions & 1 deletion lib/dialects/sqlite/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements)
},

incrementQuery: function(tableName, attrValueHash, where) {
incrementQuery: function(tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)

var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
Expand All @@ -225,6 +225,12 @@ module.exports = (function() {
values.push(this.quoteIdentifier(key) + "=" + this.quoteIdentifier(key) + "+ " + this.escape(value))
}

options = options || {}
for (var key in options) {
var value = options[key];
values.push(this.quoteIdentifier(key) + "=" + this.escape(value))
}

var replacements = {
table: this.quoteIdentifier(tableName),
values: values.join(","),
Expand Down
4 changes: 2 additions & 2 deletions lib/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ module.exports = (function() {
return queryAndEmit.call(this, [sql, factory, queryOptions], 'select')
}

QueryInterface.prototype.increment = function(dao, tableName, values, identifier) {
var sql = this.QueryGenerator.incrementQuery(tableName, values, identifier);
QueryInterface.prototype.increment = function(dao, tableName, values, identifier, options) {
var sql = this.QueryGenerator.incrementQuery(tableName, values, identifier, options);
return queryAndEmit.call(this, [sql, dao], 'increment');
}

Expand Down
40 changes: 40 additions & 0 deletions test/dao.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,26 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
})

it('with timestamps set to true', function (done) {
var User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER
}, { timestamps: true })

User.sync({ force: true }).success(function() {
User.create({aNumber: 1}).success(function (user) {
var oldDate = user.updatedAt
setTimeout(function () {
user.increment('aNumber', 1).success(function() {
User.find(1).success(function (user) {
expect(user.updatedAt).to.be.afterTime(oldDate)
done()
})
})
}, 1000)
})
})
})
})

describe('decrement', function () {
Expand Down Expand Up @@ -415,6 +435,26 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
})

it('with timestamps set to true', function (done) {
var User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER
}, { timestamps: true })

User.sync({ force: true }).success(function() {
User.create({aNumber: 1}).success(function (user) {
var oldDate = user.updatedAt
setTimeout(function () {
user.decrement('aNumber', 1).success(function() {
User.find(1).success(function (user) {
expect(user.updatedAt).to.be.afterTime(oldDate)
done()
})
})
}, 1000)
})
})
})
})

describe('reload', function () {
Expand Down

0 comments on commit 2880122

Please sign in to comment.