Promote the included relations as properties

This commit is contained in:
Raymond Feng 2014-01-27 15:56:04 -08:00
parent 08fb43adb2
commit 1339250c8f
2 changed files with 41 additions and 19 deletions

View File

@ -542,11 +542,21 @@ DataAccessObject.find = function find(params, cb) {
obj._initProperties(d, false, params.fields); obj._initProperties(d, false, params.fields);
if (params && params.include && params.collect) { if (params && params.include) {
data[i] = obj.__cachedRelations[params.collect]; // Try to normalize the include
} else { var includes = params.include;
data[i] = obj; if(typeof includes === 'string') {
includes = [includes];
} else if(typeof includes === 'object') {
includes = Object.keys(includes);
}
includes.forEach(function (inc) {
// Promote the included model as a direct property
obj.__data[inc] = obj.__cachedRelations[inc];
});
delete obj.__data.__cachedRelations;
} }
data[i] = obj;
}); });
if (data && data.countBeforeLimit) { if (data && data.countBeforeLimit) {
data.countBeforeLimit = data.countBeforeLimit; data.countBeforeLimit = data.countBeforeLimit;

View File

@ -6,6 +6,16 @@ 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.
* *
@ -29,8 +39,8 @@ Inclusion.include = function (objects, include, cb) {
var self = this; var self = this;
if ( if (
(include.constructor.name == 'Array' && include.length == 0) || !include || (Array.isArray(include) && include.length === 0) ||
(include.constructor.name == 'Object' && Object.keys(include).length == 0) (isObject(include) && Object.keys(include).length === 0)
) { ) {
cb(null, objects); cb(null, objects);
return; return;
@ -61,7 +71,7 @@ Inclusion.include = function (objects, include, cb) {
if (typeof ij === 'string') { if (typeof ij === 'string') {
ij = [ij]; ij = [ij];
} }
if (ij.constructor.name === 'Object') { if (isObject(ij)) {
var newIj = []; var newIj = [];
for (var key in ij) { for (var key in ij) {
var obj = {}; var obj = {};
@ -76,12 +86,13 @@ Inclusion.include = function (objects, include, cb) {
function processIncludeItem(objs, include, keyVals, objsByKeys) { function processIncludeItem(objs, include, keyVals, objsByKeys) {
var relations = self.relations; var relations = self.relations;
if (include.constructor.name === 'Object') { var relationName, subInclude;
var relationName = Object.keys(include)[0]; if (isObject(include)) {
var subInclude = include[relationName]; relationName = Object.keys(include)[0];
subInclude = include[relationName];
} else { } else {
var relationName = include; relationName = include;
var subInclude = []; subInclude = [];
} }
var relation = relations[relationName]; var relation = relations[relationName];
@ -117,15 +128,16 @@ Inclusion.include = function (objects, include, cb) {
} }
} }
req['where'][relation.keyTo] = {inq: inValues}; req.where[relation.keyTo] = {inq: inValues};
req['include'] = subInclude; req.include = subInclude;
return function (cb) { return function (cb) {
relation.modelTo.find(req, function (err, objsIncluded) { relation.modelTo.find(req, function (err, objsIncluded) {
var objectsFrom, j;
for (var i = 0; i < objsIncluded.length; i++) { for (var i = 0; i < objsIncluded.length; i++) {
delete keysToBeProcessed[objsIncluded[i][relation.keyTo]]; delete keysToBeProcessed[objsIncluded[i][relation.keyTo]];
var objectsFrom = objsByKeys[relation.keyFrom][objsIncluded[i][relation.keyTo]]; objectsFrom = objsByKeys[relation.keyFrom][objsIncluded[i][relation.keyTo]];
for (var j = 0; j < objectsFrom.length; j++) { for (j = 0; j < objectsFrom.length; j++) {
if (!objectsFrom[j].__cachedRelations) { if (!objectsFrom[j].__cachedRelations) {
objectsFrom[j].__cachedRelations = {}; objectsFrom[j].__cachedRelations = {};
} }
@ -142,8 +154,8 @@ Inclusion.include = function (objects, include, cb) {
// No relation have been found for these keys // No relation have been found for these keys
for (var key in keysToBeProcessed) { for (var key in keysToBeProcessed) {
var objectsFrom = objsByKeys[relation.keyFrom][key]; objectsFrom = objsByKeys[relation.keyFrom][key];
for (var j = 0; j < objectsFrom.length; j++) { for (j = 0; j < objectsFrom.length; j++) {
if (!objectsFrom[j].__cachedRelations) { if (!objectsFrom[j].__cachedRelations) {
objectsFrom[j].__cachedRelations = {}; objectsFrom[j].__cachedRelations = {};
} }
@ -158,5 +170,5 @@ Inclusion.include = function (objects, include, cb) {
return null; return null;
} }
} };