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

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() {
updatedAt: Utils._.underscoredIf(this.options.updatedAt, 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)
}
......@@ -866,11 +871,11 @@ module.exports = (function() {
})
if (self.options.timestamps) {
if (!values[createdAtAttr]) {
if (!values[createdAtAttr] && self.options.createdAt) {
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)
}
}
......@@ -1078,7 +1083,7 @@ module.exports = (function() {
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
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)
attrValueHash[attr] = Utils.now()
}
......@@ -1268,7 +1273,6 @@ module.exports = (function() {
var addDefaultAttributes = function() {
var self = this
, head = {}
, tail = {}
, head = {
id: {
......@@ -1285,11 +1289,15 @@ module.exports = (function() {
}
if (this.options.timestamps) {
tail[Utils._.underscoredIf(this.options.createdAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
tail[Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
if (this.options.paranoid)
if (this.options.createdAt) {
tail[Utils._.underscoredIf(this.options.createdAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
}
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}
}
}
var existingAttributes = Utils._.clone(self.rawAttributes)
......
......@@ -191,7 +191,9 @@ module.exports = (function() {
value = new Date(value)
}
if (originalValue !== value) this._previousDataValues[key] = originalValue
if (originalValue !== value) {
this._previousDataValues[key] = originalValue
}
this.dataValues[key] = value
}
}
......@@ -237,14 +239,14 @@ module.exports = (function() {
value.forEach(function(data) {
var daoInstance = include.daoFactory.build(data, {
isNewRecord: false,
isDirty: false,
include: include.include,
includeNames: include.includeNames,
includeMap: include.includeMap,
includeValidated: true,
raw: options.raw
})
isNewRecord: false,
isDirty: false,
include: include.include,
includeNames: include.includeNames,
includeMap: include.includeMap,
includeValidated: true,
raw: options.raw
})
, isEmpty = !Utils.firstValueOfHash(daoInstance.identifiers)
if (association.isSingleAssociation) {
......@@ -292,11 +294,11 @@ module.exports = (function() {
if (options.fields) {
if (self.__options.timestamps) {
if (options.fields.indexOf(updatedAtAttr) === -1) {
if (self.__options.updatedAt && options.fields.indexOf(updatedAtAttr) === -1) {
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)
}
}
......@@ -341,16 +343,18 @@ module.exports = (function() {
}
if (self.__options.timestamps) {
values[updatedAtAttr] = (
(
self.isNewRecord
&& !!self.daoFactory.rawAttributes[updatedAtAttr]
&& !!self.daoFactory.rawAttributes[updatedAtAttr].defaultValue
)
? self.daoFactory.rawAttributes[updatedAtAttr].defaultValue
: Utils.now(self.sequelize.options.dialect))
if (self.isNewRecord && !values[createdAtAttr]) {
if (self.__options.updatedAt) {
values[updatedAtAttr] = (
(
self.isNewRecord
&& !!self.daoFactory.rawAttributes[updatedAtAttr]
&& !!self.daoFactory.rawAttributes[updatedAtAttr].defaultValue
)
? self.daoFactory.rawAttributes[updatedAtAttr].defaultValue
: Utils.now(self.sequelize.options.dialect))
}
if (self.isNewRecord && self.__options.createdAt && !values[createdAtAttr]) {
values[createdAtAttr] = (
(
!!self.daoFactory.rawAttributes[createdAtAttr]
......@@ -578,7 +582,7 @@ module.exports = (function() {
}
if (this.__options.timestamps) {
if (!values[updatedAtAttr]) {
if (!values[updatedAtAttr] && this.__options.updatedAt) {
countOrOptions.attributes[updatedAtAttr] = Utils.now(this.daoFactory.daoFactoryManager.sequelize.options.dialect)
}
}
......@@ -664,17 +668,17 @@ module.exports = (function() {
}
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]);
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]);
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]);
delete defaults[this._timestampAttributes.deletedAt];
}
......
......@@ -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) {
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!