-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding dirty tracking #798
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,10 +84,15 @@ module.exports = (function() { | |
DAO.prototype.getDataValue = function(name) { | ||
return this.dataValues && this.dataValues.hasOwnProperty(name) ? this.dataValues[name] : this[name] | ||
} | ||
DAO.prototype.get = DAO.prototype.getDataValue | ||
|
||
DAO.prototype.setDataValue = function(name, value) { | ||
if (Utils.areChanged(this.dataValues[name], value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we rename that method to |
||
this.isDirty = true | ||
} | ||
this.dataValues[name] = value | ||
} | ||
DAO.prototype.set = DAO.prototype.setDataValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For real, my jaw dropped in awe at how simple yet powerful that single statement was :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is so wonderful about this? what am I missing! |
||
|
||
// if an array with field names is passed to save() | ||
// only those fields will be updated | ||
|
@@ -149,6 +154,7 @@ module.exports = (function() { | |
}).run() | ||
} | ||
else if (this.isNewRecord) { | ||
this.isDirty = false | ||
return this.QueryInterface.insert(this, this.QueryInterface.QueryGenerator.addSchema(this.__factory), values) | ||
} else { | ||
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }; | ||
|
@@ -157,6 +163,7 @@ module.exports = (function() { | |
identifier = this.__options.whereCollection; | ||
} | ||
|
||
this.isDirty = false | ||
var tableName = this.QueryInterface.QueryGenerator.addSchema(this.__factory) | ||
, query = this.QueryInterface.update(this, tableName, values, identifier) | ||
|
||
|
@@ -191,6 +198,7 @@ module.exports = (function() { | |
this[valueName] = obj.values[valueName] | ||
} | ||
} | ||
this.isDirty = false | ||
emitter.emit('success', this) | ||
}.bind(this)) | ||
}.bind(this)).run() | ||
|
@@ -224,14 +232,21 @@ module.exports = (function() { | |
readOnlyAttributes.push('updatedAt') | ||
readOnlyAttributes.push('deletedAt') | ||
|
||
var isDirty = this.isDirty | ||
Utils._.each(updates, function(value, attr) { | ||
var updateAllowed = ( | ||
(readOnlyAttributes.indexOf(attr) == -1) && | ||
(readOnlyAttributes.indexOf(Utils._.underscored(attr)) == -1) && | ||
(self.attributes.indexOf(attr) > -1) | ||
) | ||
updateAllowed && (self[attr] = value) | ||
if (updateAllowed) { | ||
if (Utils.areChanged(self[attr], value)) { | ||
isDirty = true | ||
} | ||
self[attr] = value | ||
} | ||
}) | ||
this.isDirty = isDirty | ||
} | ||
|
||
DAO.prototype.destroy = function() { | ||
|
@@ -330,7 +345,15 @@ module.exports = (function() { | |
// (the same is true for __defineSetter and 'prototype' getters) | ||
if (has !== true) { | ||
this.__defineGetter__(attribute, has.get || function() { return this.dataValues[attribute]; }); | ||
this.__defineSetter__(attribute, has.set || function(v) { this.dataValues[attribute] = v; }); | ||
this.__defineSetter__(attribute, has.set || function(v) { | ||
if (Utils.areChanged(this.dataValues[attribute], v)) { | ||
//Only dirty the object if the change is not due to id, touchedAt, createdAt or updatedAt being initiated | ||
if (this.dataValues[attribute] || (attribute != 'id' && attribute != 'touchedAt' && attribute != 'createdAt' && attribute != 'updatedAt')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll also need to check if underscore is true and if so the fields are deleted_at, created_at, etc. |
||
this.isDirty = true | ||
} | ||
} | ||
this.dataValues[attribute] = v | ||
}); | ||
} | ||
|
||
this[attribute] = value; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,6 +252,20 @@ var Utils = module.exports = { | |
isHash: function(obj) { | ||
return Utils._.isObject(obj) && !Array.isArray(obj); | ||
}, | ||
areChanged: function(attrValue, value) { | ||
//If attribute value is Date, check value as a date | ||
if (Utils._.isDate(attrValue) && !Utils._.isDate(value)) { | ||
value = new Date(value) | ||
} | ||
if (Utils._.isDate(attrValue)) { | ||
return attrValue.valueOf() !== value.valueOf() | ||
} | ||
//If both are them are empty, don't set as changed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo, should be: "...both of them...". |
||
if ((attrValue === undefined || attrValue === null || attrValue === '') && (value === undefined || value === null || value === '')) { | ||
return false | ||
} | ||
return attrValue !== value | ||
}, | ||
argsArePrimaryKeys: function(args, primaryKeys) { | ||
var result = (args.length == Object.keys(primaryKeys).length) | ||
if (result) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️