Make sure __cachedRelations is not enumerable

This commit is contained in:
Raymond Feng 2014-01-28 09:57:23 -08:00
parent 6e1900ca01
commit 93c18163c8
4 changed files with 44 additions and 27 deletions

View File

@ -1,3 +1,7 @@
var utils = require('./utils');
var isPlainObject = utils.isPlainObject;
var defineCachedRelations = utils.defineCachedRelations;
/** /**
* Include mixin for ./model.js * Include mixin for ./model.js
*/ */
@ -6,16 +10,6 @@ module.exports = Inclusion;
function Inclusion() { function Inclusion() {
} }
/*!
* Check if the argument is plain object
* @param {*) include The include value
* @returns {boolean}
*/
function isObject(include) {
return (typeof include === 'object') && (include !== null)
&& (include.constructor.name === 'Object');
}
/** /**
* Allows you to load relations of several objects and optimize numbers of requests. * Allows you to load relations of several objects and optimize numbers of requests.
* *
@ -40,7 +34,7 @@ Inclusion.include = function (objects, include, cb) {
if ( if (
!include || (Array.isArray(include) && include.length === 0) || !include || (Array.isArray(include) && include.length === 0) ||
(isObject(include) && Object.keys(include).length === 0) (isPlainObject(include) && Object.keys(include).length === 0)
) { ) {
cb(null, objects); cb(null, objects);
return; return;
@ -58,7 +52,7 @@ Inclusion.include = function (objects, include, cb) {
nbCallbacks++; nbCallbacks++;
callback(function () { callback(function () {
nbCallbacks--; nbCallbacks--;
if (nbCallbacks == 0) { if (nbCallbacks === 0) {
cb(null, objects); cb(null, objects);
} }
}); });
@ -71,7 +65,7 @@ Inclusion.include = function (objects, include, cb) {
if (typeof ij === 'string') { if (typeof ij === 'string') {
ij = [ij]; ij = [ij];
} }
if (isObject(ij)) { if (isPlainObject(ij)) {
var newIj = []; var newIj = [];
for (var key in ij) { for (var key in ij) {
var obj = {}; var obj = {};
@ -87,7 +81,7 @@ Inclusion.include = function (objects, include, cb) {
var relations = self.relations; var relations = self.relations;
var relationName, subInclude; var relationName, subInclude;
if (isObject(include)) { if (isPlainObject(include)) {
relationName = Object.keys(include)[0]; relationName = Object.keys(include)[0];
subInclude = include[relationName]; subInclude = include[relationName];
} else { } else {
@ -100,7 +94,7 @@ Inclusion.include = function (objects, include, cb) {
return function () { return function () {
cb(new Error('Relation "' + relationName + '" is not defined for ' cb(new Error('Relation "' + relationName + '" is not defined for '
+ self.modelName + ' model')); + self.modelName + ' model'));
} };
} }
var req = {'where': {}}; var req = {'where': {}};
@ -138,9 +132,7 @@ Inclusion.include = function (objects, include, cb) {
delete keysToBeProcessed[objsIncluded[i][relation.keyTo]]; delete keysToBeProcessed[objsIncluded[i][relation.keyTo]];
objectsFrom = objsByKeys[relation.keyFrom][objsIncluded[i][relation.keyTo]]; objectsFrom = objsByKeys[relation.keyFrom][objsIncluded[i][relation.keyTo]];
for (j = 0; j < objectsFrom.length; j++) { for (j = 0; j < objectsFrom.length; j++) {
if (!objectsFrom[j].__cachedRelations) { defineCachedRelations(objectsFrom[j]);
objectsFrom[j].__cachedRelations = {};
}
if (relation.multiple) { if (relation.multiple) {
if (!objectsFrom[j].__cachedRelations[relationName]) { if (!objectsFrom[j].__cachedRelations[relationName]) {
objectsFrom[j].__cachedRelations[relationName] = []; objectsFrom[j].__cachedRelations[relationName] = [];
@ -156,9 +148,7 @@ Inclusion.include = function (objects, include, cb) {
for (var key in keysToBeProcessed) { for (var key in keysToBeProcessed) {
objectsFrom = objsByKeys[relation.keyFrom][key]; objectsFrom = objsByKeys[relation.keyFrom][key];
for (j = 0; j < objectsFrom.length; j++) { for (j = 0; j < objectsFrom.length; j++) {
if (!objectsFrom[j].__cachedRelations) { defineCachedRelations(objectsFrom[j]);
objectsFrom[j].__cachedRelations = {};
}
objectsFrom[j].__cachedRelations[relationName] = objectsFrom[j].__cachedRelations[relationName] =
relation.multiple ? [] : null; relation.multiple ? [] : null;
} }

View File

@ -76,8 +76,8 @@ ModelBaseClass.prototype._initProperties = function (data, applySetters) {
value: {} value: {}
}); });
if (data['__cachedRelations']) { if (data.__cachedRelations) {
this.__cachedRelations = data['__cachedRelations']; this.__cachedRelations = data.__cachedRelations;
} }
// Check if the strict option is set to false for the model // Check if the strict option is set to false for the model

View File

@ -1,3 +1,5 @@
var utils = require('./utils');
var defineCachedRelations = utils.defineCachedRelations;
/** /**
* Module exports * Module exports
*/ */
@ -54,14 +56,12 @@ function defineScope(cls, targetClass, name, params, methods) {
throw new Error('Method can be only called with one or two arguments'); throw new Error('Method can be only called with one or two arguments');
} }
if (!this.__cachedRelations || (typeof this.__cachedRelations[name] == 'undefined') || actualRefresh) { if (!this.__cachedRelations || (this.__cachedRelations[name] === undefined) || actualRefresh) {
var self = this; var self = this;
var params = mergeParams(actualCond, caller._scope); var params = mergeParams(actualCond, caller._scope);
return targetClass.find(params, function (err, data) { return targetClass.find(params, function (err, data) {
if (!err && saveOnCache) { if (!err && saveOnCache) {
if (!self.__cachedRelations) { defineCachedRelations(self);
self.__cachedRelations = {};
}
self.__cachedRelations[name] = data; self.__cachedRelations[name] = data;
} }
cb(err, data); cb(err, data);

View File

@ -4,6 +4,8 @@ exports.selectFields = selectFields;
exports.removeUndefined = removeUndefined; exports.removeUndefined = removeUndefined;
exports.parseSettings = parseSettings; exports.parseSettings = parseSettings;
exports.mergeSettings = mergeSettings; exports.mergeSettings = mergeSettings;
exports.isPlainObject = isPlainObject;
exports.defineCachedRelations = defineCachedRelations;
var traverse = require('traverse'); var traverse = require('traverse');
@ -176,4 +178,29 @@ function mergeSettings(target, src) {
} }
return dst; return dst;
}
/**
* Define an non-enumerable __cachedRelations property
* @param {Object} obj The obj to receive the __cachedRelations
*/
function defineCachedRelations(obj) {
if (!obj.__cachedRelations) {
Object.defineProperty(obj, '__cachedRelations', {
writable: true,
enumerable: false,
configurable: true,
value: {}
});
}
}
/**
* Check if the argument is plain object
* @param {*) obj The obj value
* @returns {boolean}
*/
function isPlainObject(obj) {
return (typeof obj === 'object') && (obj !== null)
&& (obj.constructor === Object);
} }