Promote the included relations as properties
This commit is contained in:
parent
08fb43adb2
commit
1339250c8f
18
lib/dao.js
18
lib/dao.js
|
@ -542,11 +542,21 @@ DataAccessObject.find = function find(params, cb) {
|
|||
|
||||
obj._initProperties(d, false, params.fields);
|
||||
|
||||
if (params && params.include && params.collect) {
|
||||
data[i] = obj.__cachedRelations[params.collect];
|
||||
} else {
|
||||
data[i] = obj;
|
||||
if (params && params.include) {
|
||||
// Try to normalize the include
|
||||
var includes = params.include;
|
||||
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) {
|
||||
data.countBeforeLimit = data.countBeforeLimit;
|
||||
|
|
|
@ -6,6 +6,16 @@ module.exports = 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.
|
||||
*
|
||||
|
@ -29,8 +39,8 @@ Inclusion.include = function (objects, include, cb) {
|
|||
var self = this;
|
||||
|
||||
if (
|
||||
(include.constructor.name == 'Array' && include.length == 0) ||
|
||||
(include.constructor.name == 'Object' && Object.keys(include).length == 0)
|
||||
!include || (Array.isArray(include) && include.length === 0) ||
|
||||
(isObject(include) && Object.keys(include).length === 0)
|
||||
) {
|
||||
cb(null, objects);
|
||||
return;
|
||||
|
@ -61,7 +71,7 @@ Inclusion.include = function (objects, include, cb) {
|
|||
if (typeof ij === 'string') {
|
||||
ij = [ij];
|
||||
}
|
||||
if (ij.constructor.name === 'Object') {
|
||||
if (isObject(ij)) {
|
||||
var newIj = [];
|
||||
for (var key in ij) {
|
||||
var obj = {};
|
||||
|
@ -76,12 +86,13 @@ Inclusion.include = function (objects, include, cb) {
|
|||
function processIncludeItem(objs, include, keyVals, objsByKeys) {
|
||||
var relations = self.relations;
|
||||
|
||||
if (include.constructor.name === 'Object') {
|
||||
var relationName = Object.keys(include)[0];
|
||||
var subInclude = include[relationName];
|
||||
var relationName, subInclude;
|
||||
if (isObject(include)) {
|
||||
relationName = Object.keys(include)[0];
|
||||
subInclude = include[relationName];
|
||||
} else {
|
||||
var relationName = include;
|
||||
var subInclude = [];
|
||||
relationName = include;
|
||||
subInclude = [];
|
||||
}
|
||||
var relation = relations[relationName];
|
||||
|
||||
|
@ -117,15 +128,16 @@ Inclusion.include = function (objects, include, cb) {
|
|||
}
|
||||
}
|
||||
|
||||
req['where'][relation.keyTo] = {inq: inValues};
|
||||
req['include'] = subInclude;
|
||||
req.where[relation.keyTo] = {inq: inValues};
|
||||
req.include = subInclude;
|
||||
|
||||
return function (cb) {
|
||||
relation.modelTo.find(req, function (err, objsIncluded) {
|
||||
var objectsFrom, j;
|
||||
for (var i = 0; i < objsIncluded.length; i++) {
|
||||
delete keysToBeProcessed[objsIncluded[i][relation.keyTo]];
|
||||
var objectsFrom = objsByKeys[relation.keyFrom][objsIncluded[i][relation.keyTo]];
|
||||
for (var j = 0; j < objectsFrom.length; j++) {
|
||||
objectsFrom = objsByKeys[relation.keyFrom][objsIncluded[i][relation.keyTo]];
|
||||
for (j = 0; j < objectsFrom.length; j++) {
|
||||
if (!objectsFrom[j].__cachedRelations) {
|
||||
objectsFrom[j].__cachedRelations = {};
|
||||
}
|
||||
|
@ -142,8 +154,8 @@ Inclusion.include = function (objects, include, cb) {
|
|||
|
||||
// No relation have been found for these keys
|
||||
for (var key in keysToBeProcessed) {
|
||||
var objectsFrom = objsByKeys[relation.keyFrom][key];
|
||||
for (var j = 0; j < objectsFrom.length; j++) {
|
||||
objectsFrom = objsByKeys[relation.keyFrom][key];
|
||||
for (j = 0; j < objectsFrom.length; j++) {
|
||||
if (!objectsFrom[j].__cachedRelations) {
|
||||
objectsFrom[j].__cachedRelations = {};
|
||||
}
|
||||
|
@ -158,5 +170,5 @@ Inclusion.include = function (objects, include, cb) {
|
|||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue