Cleanup, consistency: allow properties to be a function

Also, pass along the current instance, for any instance method as a
context (save, updateAttributes).
This commit is contained in:
Fabien Franzen 2014-09-07 14:24:06 +02:00
parent beeb7c46c9
commit ec05d0b5a8
2 changed files with 20 additions and 10 deletions

View File

@ -78,27 +78,29 @@ DataAccessObject._forDB = function (data) {
return res;
};
DataAccessObject.defaultScope = function(target, isData) {
DataAccessObject.defaultScope = function(target, inst) {
var scope = this.definition.settings.scope;
if (typeof scope === 'function') {
scope = this.definition.settings.scope.call(this, target, isData);
scope = this.definition.settings.scope.call(this, target, inst);
}
return scope;
};
DataAccessObject.applyScope = function(query) {
var scope = this.defaultScope(query, false) || {};
DataAccessObject.applyScope = function(query, inst) {
var scope = this.defaultScope(query, inst) || {};
if (typeof scope === 'object') {
mergeQuery(query, scope || {}, this.definition.settings.scoping);
}
};
DataAccessObject.applyProperties = function(data) {
DataAccessObject.applyProperties = function(data, inst) {
var properties = this.definition.settings.properties;
if (typeof properties === 'object') {
util._extend(data, properties);
} else if (typeof properties === 'function') {
util._extend(data, properties.call(this, data, inst) || {});
} else if (properties !== false) {
var scope = this.defaultScope(data, true) || {};
var scope = this.defaultScope(data, inst) || {};
if (typeof scope.where === 'object') {
setScopeValuesFromWhere(data, scope.where, this);
}
@ -183,7 +185,7 @@ DataAccessObject.create = function (data, callback) {
obj = new Model(data);
}
this.applyProperties(enforced);
this.applyProperties(enforced, obj);
obj.setAttributes(enforced);
data = obj.toObject(true);
@ -261,7 +263,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
inst = new Model(data);
}
update = inst.toObject(false);
this.applyProperties(update);
this.applyProperties(update, inst);
update = removeUndefined(update);
this.getDataSource().connector.updateOrCreate(Model.modelName, update, function (err, data) {
var obj;
@ -1160,7 +1162,7 @@ DataAccessObject.prototype.updateAttribute = function updateAttribute(name, valu
DataAccessObject.prototype.setAttributes = function setAttributes(data) {
if (typeof data !== 'object') return;
this.constructor.applyProperties(data);
this.constructor.applyProperties(data, this);
var Model = this.constructor;
var inst = this;

View File

@ -81,12 +81,20 @@ describe('default scope', function () {
memory: { collection: 'Product' }
});
var scopeFn = function(target, isData) {
// inst is only valid for instance methods
// like save, updateAttributes
var scopeFn = function(target, inst) {
return { where: { kind: this.modelName } };
};
var propertiesFn = function(target, inst) {
return { kind: this.modelName };
};
Thing = db.define('Thing', Product.definition.properties, {
base: 'Product',
properties: propertiesFn,
scope: scopeFn,
mongodb: { collection: 'Product' },
memory: { collection: 'Product' }