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

Commit 40d67cae by Sascha Depold

update updated_at on save

1 parent 2e768918
...@@ -14,8 +14,21 @@ var Model = module.exports = function(values, options) { ...@@ -14,8 +14,21 @@ var Model = module.exports = function(values, options) {
// set id to null if not passed as value // set id to null if not passed as value
// a newly created model has no id // a newly created model has no id
if(!this.hasOwnProperty('id'))
this.addAttribute('id', null) var defaults = { id: null }
if(this.options.timestamps) {
defaults[this.options.camelcase ? 'created_at' : 'createdAt'] = new Date()
defaults[this.options.camelcase ? 'updated_at' : 'updatedAt'] = new Date()
if(this.options.safeDelete)
defaults[this.options.camelcase ? 'deleted_at' : 'deletedAt'] = null
}
Utils._.map(defaults, function(value, attr) {
if(!self.hasOwnProperty(attr))
self.addAttribute(attr, value)
})
} }
Utils.addEventEmitter(Model) Utils.addEventEmitter(Model)
Utils._.map(Mixin.classMethods, function(fct, name) { Model[name] = fct }) Utils._.map(Mixin.classMethods, function(fct, name) { Model[name] = fct })
...@@ -40,6 +53,11 @@ Model.prototype.query = function() { ...@@ -40,6 +53,11 @@ Model.prototype.query = function() {
} }
Model.prototype.save = function() { Model.prototype.save = function() {
var attr = this.options.camelcase ? 'updated_at' : 'updatedAt'
if(this.hasOwnProperty(attr))
this[attr] = new Date()
if(this.isNewRecord) if(this.isNewRecord)
return this.query(QueryGenerator.insertQuery(this.definition.tableName, this.values)) return this.query(QueryGenerator.insertQuery(this.definition.tableName, this.values))
else else
......
...@@ -19,5 +19,26 @@ module.exports = { ...@@ -19,5 +19,26 @@ module.exports = {
}) })
}) })
}) })
},
'save should update the timestamp updated_at': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT })
User.sync({force: true}).on('success', function() {
var now = Date.now()
// timeout is needed, in order to check the update of the timestamp
setTimeout(function() {
var u = User.build({name: 'foo', bio: 'bar'})
, uNow = u.updatedAt
assert.eql(true, uNow.getTime() > now)
setTimeout(function() {
u.save().on('success', function() {
assert.eql(true, uNow.getTime() < u.updatedAt)
exit()
})
}, 100)
}, 100)
})
} }
} }
\ No newline at end of file
...@@ -4,7 +4,7 @@ var assert = require("assert") ...@@ -4,7 +4,7 @@ var assert = require("assert")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false}) , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
var initUsers = function(num, callback) { var initUsers = function(num, callback) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }, {timestamps:false})
, users = [] , users = []
User.sync({force: true}).on('success', function() { User.sync({force: true}).on('success', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!