diff --git a/lib/dao.js b/lib/dao.js index 7a0c62a2..ee49bc94 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -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; diff --git a/test/default-scope.test.js b/test/default-scope.test.js index 8f9d1f6a..bb44f859 100644 --- a/test/default-scope.test.js +++ b/test/default-scope.test.js @@ -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' }