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

Commit cb5f28aa by Jan Aagaard Meier

Make it possible to disable some but not all timestamp columns

1 parent 6166f333
...@@ -173,6 +173,11 @@ module.exports = (function() { ...@@ -173,6 +173,11 @@ module.exports = (function() {
updatedAt: Utils._.underscoredIf(this.options.updatedAt, this.options.underscored), updatedAt: Utils._.underscoredIf(this.options.updatedAt, this.options.underscored),
deletedAt: Utils._.underscoredIf(this.options.deletedAt, this.options.underscored) deletedAt: Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)
} }
Utils._.each(this.DAO.prototype._timestampAttributes, function (key, val) {
if (!!val) {
delete this.key
}
})
this.DAO.prototype._readOnlyAttributes = Object.keys(this.DAO.prototype._timestampAttributes) this.DAO.prototype._readOnlyAttributes = Object.keys(this.DAO.prototype._timestampAttributes)
} }
...@@ -866,11 +871,11 @@ module.exports = (function() { ...@@ -866,11 +871,11 @@ module.exports = (function() {
}) })
if (self.options.timestamps) { if (self.options.timestamps) {
if (!values[createdAtAttr]) { if (!values[createdAtAttr] && self.options.createdAt) {
values[createdAtAttr] = Utils.now(self.daoFactoryManager.sequelize.options.dialect) values[createdAtAttr] = Utils.now(self.daoFactoryManager.sequelize.options.dialect)
} }
if (!values[updatedAtAttr]) { if (!values[updatedAtAttr] && self.options.updatedAt) {
values[updatedAtAttr] = Utils.now(self.daoFactoryManager.sequelize.options.dialect) values[updatedAtAttr] = Utils.now(self.daoFactoryManager.sequelize.options.dialect)
} }
} }
...@@ -1078,7 +1083,7 @@ module.exports = (function() { ...@@ -1078,7 +1083,7 @@ module.exports = (function() {
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks) options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
options.type = QueryTypes.BULKUPDATE options.type = QueryTypes.BULKUPDATE
if (self.options.timestamps) { if (self.options.timestamps && self.options.updatedAt) {
var attr = Utils._.underscoredIf(self.options.updatedAt, self.options.underscored) var attr = Utils._.underscoredIf(self.options.updatedAt, self.options.underscored)
attrValueHash[attr] = Utils.now() attrValueHash[attr] = Utils.now()
} }
...@@ -1268,7 +1273,6 @@ module.exports = (function() { ...@@ -1268,7 +1273,6 @@ module.exports = (function() {
var addDefaultAttributes = function() { var addDefaultAttributes = function() {
var self = this var self = this
, head = {}
, tail = {} , tail = {}
, head = { , head = {
id: { id: {
...@@ -1285,11 +1289,15 @@ module.exports = (function() { ...@@ -1285,11 +1289,15 @@ module.exports = (function() {
} }
if (this.options.timestamps) { if (this.options.timestamps) {
tail[Utils._.underscoredIf(this.options.createdAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false} if (this.options.createdAt) {
tail[Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false} tail[Utils._.underscoredIf(this.options.createdAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
}
if (this.options.paranoid) if (this.options.updatedAt) {
tail[Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
}
if (this.options.paranoid) {
tail[Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)] = {type: DataTypes.DATE} tail[Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)] = {type: DataTypes.DATE}
}
} }
var existingAttributes = Utils._.clone(self.rawAttributes) var existingAttributes = Utils._.clone(self.rawAttributes)
......
...@@ -191,7 +191,9 @@ module.exports = (function() { ...@@ -191,7 +191,9 @@ module.exports = (function() {
value = new Date(value) value = new Date(value)
} }
if (originalValue !== value) this._previousDataValues[key] = originalValue if (originalValue !== value) {
this._previousDataValues[key] = originalValue
}
this.dataValues[key] = value this.dataValues[key] = value
} }
} }
...@@ -237,14 +239,14 @@ module.exports = (function() { ...@@ -237,14 +239,14 @@ module.exports = (function() {
value.forEach(function(data) { value.forEach(function(data) {
var daoInstance = include.daoFactory.build(data, { var daoInstance = include.daoFactory.build(data, {
isNewRecord: false, isNewRecord: false,
isDirty: false, isDirty: false,
include: include.include, include: include.include,
includeNames: include.includeNames, includeNames: include.includeNames,
includeMap: include.includeMap, includeMap: include.includeMap,
includeValidated: true, includeValidated: true,
raw: options.raw raw: options.raw
}) })
, isEmpty = !Utils.firstValueOfHash(daoInstance.identifiers) , isEmpty = !Utils.firstValueOfHash(daoInstance.identifiers)
if (association.isSingleAssociation) { if (association.isSingleAssociation) {
...@@ -292,11 +294,11 @@ module.exports = (function() { ...@@ -292,11 +294,11 @@ module.exports = (function() {
if (options.fields) { if (options.fields) {
if (self.__options.timestamps) { if (self.__options.timestamps) {
if (options.fields.indexOf(updatedAtAttr) === -1) { if (self.__options.updatedAt && options.fields.indexOf(updatedAtAttr) === -1) {
options.fields.push(updatedAtAttr) options.fields.push(updatedAtAttr)
} }
if (options.fields.indexOf(createdAtAttr) === -1 && this.isNewRecord === true) { if (self.__options.createdAt && options.fields.indexOf(createdAtAttr) === -1 && this.isNewRecord === true) {
options.fields.push(createdAtAttr) options.fields.push(createdAtAttr)
} }
} }
...@@ -341,16 +343,18 @@ module.exports = (function() { ...@@ -341,16 +343,18 @@ module.exports = (function() {
} }
if (self.__options.timestamps) { if (self.__options.timestamps) {
values[updatedAtAttr] = ( if (self.__options.updatedAt) {
( values[updatedAtAttr] = (
self.isNewRecord (
&& !!self.daoFactory.rawAttributes[updatedAtAttr] self.isNewRecord
&& !!self.daoFactory.rawAttributes[updatedAtAttr].defaultValue && !!self.daoFactory.rawAttributes[updatedAtAttr]
) && !!self.daoFactory.rawAttributes[updatedAtAttr].defaultValue
? self.daoFactory.rawAttributes[updatedAtAttr].defaultValue )
: Utils.now(self.sequelize.options.dialect)) ? self.daoFactory.rawAttributes[updatedAtAttr].defaultValue
: Utils.now(self.sequelize.options.dialect))
if (self.isNewRecord && !values[createdAtAttr]) { }
if (self.isNewRecord && self.__options.createdAt && !values[createdAtAttr]) {
values[createdAtAttr] = ( values[createdAtAttr] = (
( (
!!self.daoFactory.rawAttributes[createdAtAttr] !!self.daoFactory.rawAttributes[createdAtAttr]
...@@ -578,7 +582,7 @@ module.exports = (function() { ...@@ -578,7 +582,7 @@ module.exports = (function() {
} }
if (this.__options.timestamps) { if (this.__options.timestamps) {
if (!values[updatedAtAttr]) { if (!values[updatedAtAttr] && this.__options.updatedAt) {
countOrOptions.attributes[updatedAtAttr] = Utils.now(this.daoFactory.daoFactoryManager.sequelize.options.dialect) countOrOptions.attributes[updatedAtAttr] = Utils.now(this.daoFactory.daoFactoryManager.sequelize.options.dialect)
} }
} }
...@@ -664,17 +668,17 @@ module.exports = (function() { ...@@ -664,17 +668,17 @@ module.exports = (function() {
} }
if (this.__options.timestamps) { if (this.__options.timestamps) {
if (defaults[this._timestampAttributes.createdAt]) { if (this._timestampAttributes.createdAt && defaults[this._timestampAttributes.createdAt]) {
this.dataValues[this._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this._timestampAttributes.createdAt]); this.dataValues[this._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this._timestampAttributes.createdAt]);
delete defaults[this._timestampAttributes.createdAt]; delete defaults[this._timestampAttributes.createdAt];
} }
if (defaults[this._timestampAttributes.updatedAt]) { if (this._timestampAttributes.updatedAt && defaults[this._timestampAttributes.updatedAt]) {
this.dataValues[this._timestampAttributes.updatedAt] = Utils.toDefaultValue(defaults[this._timestampAttributes.updatedAt]); this.dataValues[this._timestampAttributes.updatedAt] = Utils.toDefaultValue(defaults[this._timestampAttributes.updatedAt]);
delete defaults[this._timestampAttributes.updatedAt]; delete defaults[this._timestampAttributes.updatedAt];
} }
if (defaults[this._timestampAttributes.deletedAt]) { if (this._timestampAttributes.createdAt && defaults[this._timestampAttributes.deletedAt]) {
this.dataValues[this._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this._timestampAttributes.deletedAt]); this.dataValues[this._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this._timestampAttributes.deletedAt]);
delete defaults[this._timestampAttributes.deletedAt]; delete defaults[this._timestampAttributes.deletedAt];
} }
......
...@@ -205,6 +205,30 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -205,6 +205,30 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('should allow me to disable some of the timestamp fields', function(done) {
var UpdatingUser = this.sequelize.define('UpdatingUser', {}, {
timestamps: true,
updatedAt: false,
createdAt: false,
deletedAt: 'deletedAtThisTime',
paranoid: true
})
UpdatingUser.sync({force: true}).success(function() {
UpdatingUser.create().success(function (user) {
expect(user.createdAt).not.to.exist
expect(user.false).not.to.exist // because, you know we might accidentally add a field named 'false'
user.save().success(function (user) {
expect(user.updatedAt).not.to.exist
user.destroy().success(function(user) {
expect(user.deletedAtThisTime).to.exist
done()
})
})
})
})
})
it('should allow me to override updatedAt, createdAt, and deletedAt fields with underscored being true', function(done) { it('should allow me to override updatedAt, createdAt, and deletedAt fields with underscored being true', function(done) {
var UserTable = this.sequelize.define('UserCol', { var UserTable = this.sequelize.define('UserCol', {
aNumber: Sequelize.INTEGER aNumber: Sequelize.INTEGER
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!