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

Commit edef6294 by Jochem Maas

WIP

1 parent f54644e6
Showing with 34 additions and 24 deletions
......@@ -7,6 +7,7 @@ module.exports = (function() {
var DAO = function(values, options, isNewRecord) {
var self = this
this.dataValues = []
this.__options = options
this.hasPrimaryKeys = options.hasPrimaryKeys
this.selectedValues = values
......@@ -16,18 +17,9 @@ module.exports = (function() {
if (this.hasDefaultValues) {
Utils._.each(this.defaultValues, function (value, name) {
if (typeof self[name] === 'undefined') {
self.addAttribute(name, value());
}
self.addAttribute(name, value());
})
}
if (this.booleanValues.length) {
this.booleanValues.forEach(function (name) {
//transform integer 0,1 into boolean
self[name] = !!self[name];
});
}
}
Utils._.extend(DAO.prototype, Mixin.prototype)
......@@ -42,7 +34,7 @@ module.exports = (function() {
Object.defineProperty(DAO.prototype, 'isDeleted', {
get: function() {
var result = this.__options.timestamps && this.__options.paranoid
result = result && this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] !== null
result = result && this.dataValues[this.__options.underscored ? 'deleted_at' : 'deletedAt'] !== null
return result
}
......@@ -54,7 +46,7 @@ module.exports = (function() {
, self = this
this.attributes.concat(this.__eagerlyLoadedAssociations).forEach(function(attr) {
result[attr] = self[attr]
result[attr] = self.dataValues[attr]
})
return result
......@@ -67,7 +59,7 @@ module.exports = (function() {
, self = this
Utils._.each(this.__factory.primaryKeys, function(_, attr) {
result[attr] = self[attr]
result[attr] = self.dataValues[attr]
})
return result
......@@ -85,7 +77,7 @@ module.exports = (function() {
}
primaryKeys.forEach(function(identifier) {
result[identifier] = self[identifier]
result[identifier] = self.dataValues[identifier]
})
return result
......@@ -111,9 +103,11 @@ module.exports = (function() {
}
}
var tmpVals = self.values
fields.forEach(function(field) {
if (self.values[field] !== undefined) {
values[field] = self.values[field]
if (tmpVals[field] !== undefined) {
values[field] = tmpVals[field]
}
})
}
......@@ -132,7 +126,7 @@ module.exports = (function() {
}
if (this.__options.timestamps && this.hasOwnProperty(updatedAtAttr)) {
this[updatedAtAttr] = values[updatedAtAttr] = Utils.now()
this.dataValues[updatedAtAttr] = values[updatedAtAttr] = Utils.now()
}
if (this.isNewRecord) {
......@@ -277,7 +271,7 @@ module.exports = (function() {
DAO.prototype.destroy = function() {
if (this.__options.timestamps && this.__options.paranoid) {
var attr = this.__options.underscored ? 'deleted_at' : 'deletedAt'
this[attr] = new Date()
this.dataValues[attr] = new Date()
return this.save()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
......@@ -338,7 +332,27 @@ module.exports = (function() {
}
DAO.prototype.addAttribute = function(attribute, value) {
this[attribute] = value
if (typeof this.dataValues[attribute] !== 'undefined')
return;
var def = {},
predef = Object.getOwnPropertyDescriptor(this, attribute),
isBool = this.booleanValues.length && this.booleanValues.indexOf(attribute) !== -1;
if (isBool) // transform integer 0,1 into boolean
value = !!value;
var hasnot = function(which) {
return !predef || (!predef.hasOwnProperty('value') && !predef.hasOwnProperty(which));
};
if (hasnot('get')) def.get = function() { return this.dataValues[attribute]; };
if (hasnot('set')) def.set = function(v) { this.dataValues[attribute] = v; };
if (Utils._.size(def))
Object.defineProperty(this, attribute, def);
this.dataValues[attribute] = value;
}
DAO.prototype.setValidators = function(attribute, validators) {
......@@ -374,11 +388,7 @@ module.exports = (function() {
if (Utils._.size(defaults)) {
for (var attr in defaults) {
var value = defaults[attr]
if (!this.hasOwnProperty(attr)) {
this.addAttribute(attr, Utils.toDefaultValue(value))
}
this.addAttribute(attr, Utils.toDefaultValue(defaults[attr]))
}
}
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!