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

Commit 661dd1c2 by Jan Aagaard Meier

Make it possible to disable some but not all timestamp columns

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