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

Commit 516abae1 by Jan Aagaard Meier

change timestampattributes to be on the model instead of instance.prototype. Rem…

…oved touchedAt, since it wasn't used anywhere, or part of any schema
1 parent 661dd1c2
Showing with 48 additions and 51 deletions
......@@ -14,7 +14,6 @@ module.exports = (function() {
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
touchedAt: 'touchedAt',
instanceMethods: {},
classMethods: {},
validate: {},
......@@ -83,11 +82,11 @@ module.exports = (function() {
// inject the node-sql methods to the dao factory in order to
// receive the syntax sugar ...
;(function() {
;(function() {
var instance = sql.define({ name: "dummy", columns: [] })
for (var methodName in instance) {
;(function(methodName) {
;(function(methodName) {
DAOFactory.prototype[methodName] = function() {
var dataset = this.dataset()
, result = dataset[methodName].apply(dataset, arguments)
......@@ -148,10 +147,6 @@ module.exports = (function() {
Utils.injectScope.call(this, this.options.defaultScope)
}
addDefaultAttributes.call(this)
addOptionalClassMethods.call(this)
findAutoIncrementField.call(this)
// DAO prototype
// WTF ... ?
this.DAO = function() {
......@@ -159,27 +154,20 @@ module.exports = (function() {
}
Util.inherits(this.DAO, DAO);
this.DAO.prototype.rawAttributes = this.rawAttributes;
this.DAO.prototype._hasPrimaryKeys = this.options.hasPrimaryKeys
this.DAO.prototype._isPrimaryKey = Utils._.memoize(function (key) {
return self.primaryKeyAttributes.indexOf(key) !== -1 && key !== 'id'
})
this.DAO.prototype._timestampAttributes = {}
this._timestampAttributes = {}
if (this.options.timestamps) {
if (this.options.createdAt) {
this.DAO.prototype._timestampAttributes.createdAt = Utils._.underscoredIf(this.options.createdAt, this.options.underscored)
this._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)
this._timestampAttributes.updatedAt = 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)
this._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._timestampAttributes)
}
this.DAO.prototype._hasReadOnlyAttributes = this.DAO.prototype._readOnlyAttributes && this.DAO.prototype._readOnlyAttributes.length
......@@ -187,6 +175,17 @@ module.exports = (function() {
return self.DAO.prototype._hasReadOnlyAttributes && self.DAO.prototype._readOnlyAttributes.indexOf(key) !== -1
})
addDefaultAttributes.call(this)
addOptionalClassMethods.call(this)
findAutoIncrementField.call(this)
this.DAO.prototype.rawAttributes = this.rawAttributes;
this.DAO.prototype._hasPrimaryKeys = this.options.hasPrimaryKeys
this.DAO.prototype._isPrimaryKey = Utils._.memoize(function (key) {
return self.primaryKeyAttributes.indexOf(key) !== -1 && key !== 'id'
})
if (this.options.instanceMethods) {
Utils._.each(this.options.instanceMethods, function(fct, name) {
self.DAO.prototype[name] = fct
......@@ -804,8 +803,8 @@ module.exports = (function() {
}
var self = this
, updatedAtAttr = this.DAO.prototype._timestampAttributes.updatedAt
, createdAtAttr = this.DAO.prototype._timestampAttributes.createdAt
, updatedAtAttr = this._timestampAttributes.updatedAt
, createdAtAttr = this._timestampAttributes.createdAt
, errors = []
, daos = records.map(function(v) { return self.build(v) })
......@@ -969,9 +968,9 @@ module.exports = (function() {
where = newWhere || where
if (self.DAO.prototype._timestampAttributes.deletedAt && options.force === false) {
if (self._timestampAttributes.deletedAt && options.force === false) {
var attrValueHash = {}
attrValueHash[self.DAO.prototype._timestampAttributes.deletedAt] = Utils.now()
attrValueHash[self._timestampAttributes.deletedAt] = Utils.now()
query = 'bulkUpdate'
args = [self.tableName, attrValueHash, where]
} else {
......@@ -1080,8 +1079,8 @@ module.exports = (function() {
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
options.type = QueryTypes.BULKUPDATE
if (self.DAO.prototype._timestampAttributes.updatedAt) {
attrValueHash[self.DAO.prototype._timestampAttributes.updatedAt] = Utils.now()
if (self._timestampAttributes.updatedAt) {
attrValueHash[self._timestampAttributes.updatedAt] = Utils.now()
}
return new Utils.CustomEventEmitter(function(emitter) {
......@@ -1232,7 +1231,7 @@ module.exports = (function() {
options = options || {}
options.where = options.where || {}
var deletedAtCol = this.DAO.prototype._timestampAttributes.deletedAt
var deletedAtCol = this._timestampAttributes.deletedAt
, quoteIdentifiedDeletedAtCol = this.QueryInterface.quoteIdentifier(deletedAtCol)
// Don't overwrite our explicit deletedAt search value if we provide one
......@@ -1284,15 +1283,14 @@ module.exports = (function() {
head = {}
}
console.log(this)
if (this.DAO.prototype._timestampAttributes.createdAt) {
tail[this.DAO.prototype._timestampAttributes.createdAt] = {type: DataTypes.DATE, allowNull: false}
if (this._timestampAttributes.createdAt) {
tail[this._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._timestampAttributes.updatedAt) {
tail[this._timestampAttributes.updatedAt] = {type: DataTypes.DATE, allowNull: false}
}
if (this.DAO.prototype._timestampAttributes.deletedAt) {
tail[this.DAO.prototype._timestampAttributes.deletedAt] = {type: DataTypes.DATE}
if (this._timestampAttributes.deletedAt) {
tail[this._timestampAttributes.deletedAt] = {type: DataTypes.DATE}
}
var existingAttributes = Utils._.clone(self.rawAttributes)
......
......@@ -17,7 +17,7 @@ module.exports = (function() {
this.__eagerlyLoadedAssociations = []
this.isNewRecord = options.isNewRecord
initValues.call(this, values, options)
initValues.call(this, values, options);
}
Utils._.extend(DAO.prototype, Mixin.prototype)
......@@ -32,7 +32,7 @@ module.exports = (function() {
Object.defineProperty(DAO.prototype, 'isDeleted', {
get: function() {
return this._timestampAttributes.deletedAt && this.dataValues[this._timestampAttributes.deletedAt] !== null
return this.Model._timestampAttributes.deletedAt && this.dataValues[this.Model._timestampAttributes.deletedAt] !== null
}
})
......@@ -286,8 +286,8 @@ module.exports = (function() {
var self = this
, values = {}
, updatedAtAttr = this._timestampAttributes.updatedAt
, createdAtAttr = this._timestampAttributes.createdAt
, updatedAtAttr = this.Model._timestampAttributes.updatedAt
, createdAtAttr = this.Model._timestampAttributes.createdAt
if (options.fields) {
if (updatedAtAttr && options.fields.indexOf(updatedAtAttr) === -1) {
......@@ -514,8 +514,8 @@ module.exports = (function() {
return emitter.emit('error', err)
}
if (self._timestampAttributes.deletedAt && options.force === false) {
self.dataValues[self._timestampAttributes.deletedAt] = new Date()
if (self.Model._timestampAttributes.deletedAt && options.force === false) {
self.dataValues[self.Model._timestampAttributes.deletedAt] = new Date()
query = self.save(options)
} else {
var identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id };
......@@ -549,7 +549,7 @@ module.exports = (function() {
})
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }
, updatedAtAttr = this._timestampAttributes.updatedAt
, updatedAtAttr = this.Model._timestampAttributes.updatedAt
, values = {}
if (countOrOptions === undefined) {
......@@ -640,7 +640,6 @@ module.exports = (function() {
}
// private
var initValues = function(values, options) {
// set id to null if not passed as value, a newly created dao has no id
var defaults = this.hasPrimaryKeys ? {} : { id: null },
......@@ -657,19 +656,19 @@ module.exports = (function() {
})
}
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.Model._timestampAttributes.createdAt && defaults[this.Model._timestampAttributes.createdAt]) {
this.dataValues[this.Model._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.createdAt]);
delete defaults[this.Model._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.Model._timestampAttributes.updatedAt && defaults[this.Model._timestampAttributes.updatedAt]) {
this.dataValues[this.Model._timestampAttributes.updatedAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.updatedAt]);
delete defaults[this.Model._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.Model._timestampAttributes.createdAt && defaults[this.Model._timestampAttributes.deletedAt]) {
this.dataValues[this.Model._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.deletedAt]);
delete defaults[this.Model._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!