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

Commit 0e8d9886 by Jan Aagaard Meier

Merge pull request #930 from durango/defaulttimestamps

This commit allows you to define a defaultValue for timestamp fields.
2 parents 99e3183c 780abde3
......@@ -599,8 +599,13 @@ module.exports = (function() {
})
if (self.options.timestamps) {
values[createdAtAttr] = Utils.now()
values[updatedAtAttr] = Utils.now()
if (!values[createdAtAttr]) {
values[createdAtAttr] = Utils.now(self.daoFactoryManager.sequelize.options.dialect)
}
if (!values[updatedAtAttr]) {
values[updatedAtAttr] = Utils.now(self.daoFactoryManager.sequelize.options.dialect)
}
}
records.push(values)
......
......@@ -165,7 +165,7 @@ module.exports = (function() {
}
if (this.__options.timestamps && this.dataValues.hasOwnProperty(updatedAtAttr)) {
this.dataValues[updatedAtAttr] = values[updatedAtAttr] = Utils.now(this.sequelize.options.dialect)
this.dataValues[updatedAtAttr] = values[updatedAtAttr] = (this.isNewRecord && !!this.daoFactory.rawAttributes[updatedAtAttr] && !!this.daoFactory.rawAttributes[updatedAtAttr].defaultValue ? this.daoFactory.rawAttributes[updatedAtAttr].defaultValue : Utils.now(this.sequelize.options.dialect))
}
if (this.isNewRecord) {
......@@ -419,10 +419,15 @@ module.exports = (function() {
}
if (this.__options.timestamps) {
if (!this.defaultValues[Utils._.underscoredIf(this.__options.createdAt, this.__options.underscored)]) {
defaults[Utils._.underscoredIf(this.__options.createdAt, this.__options.underscored)] = Utils.now(this.sequelize.options.dialect)
}
if (!this.defaultValues[Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)]) {
defaults[Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)] = Utils.now(this.sequelize.options.dialect)
}
if (this.__options.paranoid) {
if (this.__options.paranoid && !this.defaultValues[Utils._.underscoredIf(this.__options.deletedAt, this.__options.underscored)]) {
defaults[Utils._.underscoredIf(this.__options.deletedAt, this.__options.underscored)] = null
}
}
......
......@@ -125,6 +125,39 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
done()
})
it('should allow me to set a default value for createdAt and updatedAt', function(done) {
var UserTable = this.sequelize.define('UserCol', {
aNumber: Sequelize.INTEGER,
createdAt: {
type: Sequelize.DATE,
defaultValue: moment('2012-01-01').toDate()
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: moment('2012-01-02').toDate()
}
}, { timestamps: true })
UserTable.sync({ force: true }).success(function() {
UserTable.create({aNumber: 5}).success(function(user) {
UserTable.bulkCreate([
{aNumber: 10},
{aNumber: 12}
]).success(function() {
UserTable.all({where: {aNumber: { gte: 10 }}}).success(function(users) {
expect(moment(user.createdAt).format('YYYY-MM-DD')).to.equal('2012-01-01')
expect(moment(user.updatedAt).format('YYYY-MM-DD')).to.equal('2012-01-02')
users.forEach(function(u) {
expect(moment(u.createdAt).format('YYYY-MM-DD')).to.equal('2012-01-01')
expect(moment(u.updatedAt).format('YYYY-MM-DD')).to.equal('2012-01-02')
})
done()
})
})
})
})
})
it('should allow me to override updatedAt, createdAt, and deletedAt fields', function(done) {
var UserTable = this.sequelize.define('UserCol', {
aNumber: Sequelize.INTEGER
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!