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

Commit 661dd1c2 by Jan Aagaard Meier

Make it possible to disable some but not all timestamp columns

1 parent cb5f28aa
Showing with 35 additions and 52 deletions
...@@ -167,17 +167,17 @@ module.exports = (function() { ...@@ -167,17 +167,17 @@ module.exports = (function() {
return self.primaryKeyAttributes.indexOf(key) !== -1 && key !== 'id' return self.primaryKeyAttributes.indexOf(key) !== -1 && key !== 'id'
}) })
this.DAO.prototype._timestampAttributes = {}
if (this.options.timestamps) { if (this.options.timestamps) {
this.DAO.prototype._timestampAttributes = { if (this.options.createdAt) {
createdAt: Utils._.underscoredIf(this.options.createdAt, this.options.underscored), this.DAO.prototype._timestampAttributes.createdAt = Utils._.underscoredIf(this.options.createdAt, this.options.underscored)
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 (this.options.updatedAt) {
if (!!val) { this.DAO.prototype._timestampAttributes.updateAt = Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)
delete this.key }
if (this.options.paranoid && this.options.deletedAt) {
this.DAO.prototype._timestampAttributes.deletedAt = Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)
} }
})
this.DAO.prototype._readOnlyAttributes = Object.keys(this.DAO.prototype._timestampAttributes) this.DAO.prototype._readOnlyAttributes = Object.keys(this.DAO.prototype._timestampAttributes)
} }
...@@ -804,8 +804,8 @@ module.exports = (function() { ...@@ -804,8 +804,8 @@ module.exports = (function() {
} }
var self = this var self = this
, updatedAtAttr = Utils._.underscoredIf(self.options.updatedAt, self.options.underscored) , updatedAtAttr = this.DAO.prototype._timestampAttributes.updatedAt
, createdAtAttr = Utils._.underscoredIf(self.options.createdAt, self.options.underscored) , createdAtAttr = this.DAO.prototype._timestampAttributes.createdAt
, errors = [] , errors = []
, daos = records.map(function(v) { return self.build(v) }) , daos = records.map(function(v) { return self.build(v) })
...@@ -870,15 +870,13 @@ module.exports = (function() { ...@@ -870,15 +870,13 @@ module.exports = (function() {
values[field] = dao.dataValues[field] values[field] = dao.dataValues[field]
}) })
if (self.options.timestamps) { if (createdAtAttr && !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] && self.options.updatedAt) { if (updatedAtAttr && !values[updatedAtAttr]) {
values[updatedAtAttr] = Utils.now(self.daoFactoryManager.sequelize.options.dialect) values[updatedAtAttr] = Utils.now(self.daoFactoryManager.sequelize.options.dialect)
} }
}
records.push(values) records.push(values)
}) })
...@@ -971,10 +969,9 @@ module.exports = (function() { ...@@ -971,10 +969,9 @@ module.exports = (function() {
where = newWhere || where where = newWhere || where
if (self.options.timestamps && self.options.paranoid && options.force === false) { if (self.DAO.prototype._timestampAttributes.deletedAt && options.force === false) {
var attr = Utils._.underscoredIf(self.options.deletedAt, self.options.underscored)
var attrValueHash = {} var attrValueHash = {}
attrValueHash[attr] = Utils.now() attrValueHash[self.DAO.prototype._timestampAttributes.deletedAt] = Utils.now()
query = 'bulkUpdate' query = 'bulkUpdate'
args = [self.tableName, attrValueHash, where] args = [self.tableName, attrValueHash, where]
} else { } else {
...@@ -1083,9 +1080,8 @@ module.exports = (function() { ...@@ -1083,9 +1080,8 @@ 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 && self.options.updatedAt) { if (self.DAO.prototype._timestampAttributes.updatedAt) {
var attr = Utils._.underscoredIf(self.options.updatedAt, self.options.underscored) attrValueHash[self.DAO.prototype._timestampAttributes.updatedAt] = Utils.now()
attrValueHash[attr] = Utils.now()
} }
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
...@@ -1236,7 +1232,7 @@ module.exports = (function() { ...@@ -1236,7 +1232,7 @@ module.exports = (function() {
options = options || {} options = options || {}
options.where = options.where || {} options.where = options.where || {}
var deletedAtCol = Utils._.underscoredIf(this.options.deletedAt, this.options.underscored) var deletedAtCol = this.DAO.prototype._timestampAttributes.deletedAt
, quoteIdentifiedDeletedAtCol = this.QueryInterface.quoteIdentifier(deletedAtCol) , quoteIdentifiedDeletedAtCol = this.QueryInterface.quoteIdentifier(deletedAtCol)
// Don't overwrite our explicit deletedAt search value if we provide one // Don't overwrite our explicit deletedAt search value if we provide one
...@@ -1288,16 +1284,15 @@ module.exports = (function() { ...@@ -1288,16 +1284,15 @@ module.exports = (function() {
head = {} head = {}
} }
if (this.options.timestamps) { console.log(this)
if (this.options.createdAt) { if (this.DAO.prototype._timestampAttributes.createdAt) {
tail[Utils._.underscoredIf(this.options.createdAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false} tail[this.DAO.prototype._timestampAttributes.createdAt] = {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) { if (this.DAO.prototype._timestampAttributes.updatedAt) {
tail[Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)] = {type: DataTypes.DATE} tail[this.DAO.prototype._timestampAttributes.updatedAt] = {type: DataTypes.DATE, allowNull: false}
} }
if (this.DAO.prototype._timestampAttributes.deletedAt) {
tail[this.DAO.prototype._timestampAttributes.deletedAt] = {type: DataTypes.DATE}
} }
var existingAttributes = Utils._.clone(self.rawAttributes) var existingAttributes = Utils._.clone(self.rawAttributes)
......
...@@ -32,10 +32,7 @@ module.exports = (function() { ...@@ -32,10 +32,7 @@ module.exports = (function() {
Object.defineProperty(DAO.prototype, 'isDeleted', { Object.defineProperty(DAO.prototype, 'isDeleted', {
get: function() { get: function() {
var result = this.__options.timestamps && this.__options.paranoid return this._timestampAttributes.deletedAt && this.dataValues[this._timestampAttributes.deletedAt] !== null
result = result && this.dataValues[Utils._.underscoredIf(this.__options.deletedAt, this.__options.underscored)] !== null
return result
} }
}) })
...@@ -289,20 +286,18 @@ module.exports = (function() { ...@@ -289,20 +286,18 @@ module.exports = (function() {
var self = this var self = this
, values = {} , values = {}
, updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored) , updatedAtAttr = this._timestampAttributes.updatedAt
, createdAtAttr = Utils._.underscoredIf(this.__options.createdAt, this.__options.underscored) , createdAtAttr = this._timestampAttributes.createdAt
if (options.fields) { if (options.fields) {
if (self.__options.timestamps) { if (updatedAtAttr && options.fields.indexOf(updatedAtAttr) === -1) {
if (self.__options.updatedAt && options.fields.indexOf(updatedAtAttr) === -1) {
options.fields.push(updatedAtAttr) options.fields.push(updatedAtAttr)
} }
if (self.__options.createdAt && options.fields.indexOf(createdAtAttr) === -1 && this.isNewRecord === true) { if (createdAtAttr && options.fields.indexOf(createdAtAttr) === -1 && this.isNewRecord === true) {
options.fields.push(createdAtAttr) options.fields.push(createdAtAttr)
} }
} }
}
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
self.hookValidate().error(function(err) { self.hookValidate().error(function(err) {
...@@ -342,8 +337,7 @@ module.exports = (function() { ...@@ -342,8 +337,7 @@ module.exports = (function() {
} }
} }
if (self.__options.timestamps) { if (updatedAtAttr) {
if (self.__options.updatedAt) {
values[updatedAtAttr] = ( values[updatedAtAttr] = (
( (
self.isNewRecord self.isNewRecord
...@@ -354,7 +348,7 @@ module.exports = (function() { ...@@ -354,7 +348,7 @@ module.exports = (function() {
: Utils.now(self.sequelize.options.dialect)) : Utils.now(self.sequelize.options.dialect))
} }
if (self.isNewRecord && self.__options.createdAt && !values[createdAtAttr]) { if (self.isNewRecord && createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = ( values[createdAtAttr] = (
( (
!!self.daoFactory.rawAttributes[createdAtAttr] !!self.daoFactory.rawAttributes[createdAtAttr]
...@@ -363,7 +357,6 @@ module.exports = (function() { ...@@ -363,7 +357,6 @@ module.exports = (function() {
? self.daoFactory.rawAttributes[createdAtAttr].defaultValue ? self.daoFactory.rawAttributes[createdAtAttr].defaultValue
: values[updatedAtAttr]) : values[updatedAtAttr])
} }
}
var query = null var query = null
, args = [] , args = []
...@@ -521,9 +514,8 @@ module.exports = (function() { ...@@ -521,9 +514,8 @@ module.exports = (function() {
return emitter.emit('error', err) return emitter.emit('error', err)
} }
if (self.__options.timestamps && self.__options.paranoid && options.force === false) { if (self._timestampAttributes.deletedAt && options.force === false) {
var attr = Utils._.underscoredIf(self.__options.deletedAt, self.__options.underscored) self.dataValues[self._timestampAttributes.deletedAt] = new Date()
self.dataValues[attr] = new Date()
query = self.save(options) query = self.save(options)
} else { } else {
var identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id }; var identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id };
...@@ -557,7 +549,7 @@ module.exports = (function() { ...@@ -557,7 +549,7 @@ module.exports = (function() {
}) })
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id } var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }
, updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored) , updatedAtAttr = this._timestampAttributes.updatedAt
, values = {} , values = {}
if (countOrOptions === undefined) { if (countOrOptions === undefined) {
...@@ -581,11 +573,9 @@ module.exports = (function() { ...@@ -581,11 +573,9 @@ module.exports = (function() {
values = fields values = fields
} }
if (this.__options.timestamps) { if (updatedAtAttr && !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)
} }
}
return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.__factory.tableName, this.__factory.options.schema), values, identifier, countOrOptions) return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.__factory.tableName, this.__factory.options.schema), values, identifier, countOrOptions)
} }
...@@ -667,7 +657,6 @@ module.exports = (function() { ...@@ -667,7 +657,6 @@ module.exports = (function() {
}) })
} }
if (this.__options.timestamps) {
if (this._timestampAttributes.createdAt && 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];
...@@ -683,7 +672,6 @@ module.exports = (function() { ...@@ -683,7 +672,6 @@ module.exports = (function() {
delete defaults[this._timestampAttributes.deletedAt]; delete defaults[this._timestampAttributes.deletedAt];
} }
} }
}
if (Object.keys(defaults).length) { if (Object.keys(defaults).length) {
for (key in defaults) { for (key in defaults) {
if (!values.hasOwnProperty(key)) { if (!values.hasOwnProperty(key)) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!