Fix the target id resolution

This commit is contained in:
Raymond Feng 2015-05-13 16:10:07 -07:00
parent f9bd1544f9
commit b5b7bab096
1 changed files with 32 additions and 27 deletions

View File

@ -283,7 +283,6 @@ Inclusion.include = function (objects, include, cb) {
* @param callback
*/
function includeHasManyThrough(callback) {
var debug = require('debug')('loopback:include:includeHasManyThrough');
var sourceIds = [];
//Map for Indexing objects by their id for faster retrieval
var objIdMap = {};
@ -317,7 +316,7 @@ Inclusion.include = function (objects, include, cb) {
/**
* 1st DB Call of 2 step process. Get through model objects first
*/
relation.modelThrough.all(throughFilter, throughFetchHandler);
relation.modelThrough.find(throughFilter, throughFetchHandler);
/**
* Handle the results of Through model objects and fetch the modelTo items
* @param err
@ -362,8 +361,11 @@ Inclusion.include = function (objects, include, cb) {
/**
* 2nd DB Call of 2 step process. Get modelTo (target) objects
*/
relation.modelTo.all(filter, targetsFetchHandler);
relation.modelTo.find(filter, targetsFetchHandler);
function targetsFetchHandler(err, targets) {
if (err) {
return callback(err);
}
var tasks = [];
//simultaneously process subIncludes. Call it first as it is an async
//process.
@ -396,7 +398,6 @@ Inclusion.include = function (objects, include, cb) {
* @param callback
*/
function includeReferencesMany(callback) {
var debug = require('debug')('loopback:include:includeReferencesMany');
var allTargetIds = [];
//Map for Indexing objects by their id for faster retrieval
var targetObjsMap = {};
@ -406,6 +407,11 @@ Inclusion.include = function (objects, include, cb) {
// use modelFrom.keyFrom in where filter later
var targetIds = obj[relation.keyFrom];
if (targetIds) {
if (typeof targetIds === 'string') {
// For relational DBs, the array is stored as stringified json
// Please note obj is a plain object at this point
targetIds = JSON.parse(targetIds);
}
//referencesMany has multiple targetIds per obj. We need to concat
// them into allTargetIds before DB Call
allTargetIds = allTargetIds.concat(targetIds);
@ -424,10 +430,11 @@ Inclusion.include = function (objects, include, cb) {
filter.where[relation.keyTo] = {
inq: allTargetIds
};
/**
* Make the DB Call, fetch all target objects
*/
relation.modelTo.all(filter, targetFetchHandler);
relation.modelTo.find(filter, targetFetchHandler);
/**
* Handle the fetched target objects
* @param err
@ -468,7 +475,6 @@ Inclusion.include = function (objects, include, cb) {
* @param callback
*/
function includeHasMany(callback) {
var debug = require('debug')('loopback:include:includeHasMany');
var sourceIds = [];
//Map for Indexing objects by their id for faster retrieval
var objIdMap = {};
@ -488,7 +494,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[relation.keyTo] = {
inq: sourceIds
};
relation.modelTo.all(filter, targetFetchHandler);
relation.modelTo.find(filter, targetFetchHandler);
/**
* Process fetched related objects
* @param err
@ -526,7 +532,6 @@ Inclusion.include = function (objects, include, cb) {
* @param callback
*/
function includePolymorphic(callback) {
var debug = require('debug')('loopback:include:includePolymorphic');
var targetIdsByType = {};
//Map for Indexing objects by their type and targetId for faster retrieval
var targetObjMapByType = {};
@ -562,6 +567,7 @@ Inclusion.include = function (objects, include, cb) {
function processPolymorphicType(modelType, callback) {
var typeFilter = {where: {}};
utils.mergeQuery(typeFilter, filter);
var targetIds = targetIdsByType[modelType];
typeFilter.where[relation.keyTo] = {
inq: targetIds
};
@ -573,7 +579,7 @@ Inclusion.include = function (objects, include, cb) {
' specified but no model exists with such name'));
return;
}
Model.all(typeFilter, targetFetchHandler);
Model.find(typeFilter, targetFetchHandler);
/**
* Process fetched related objects
* @param err
@ -616,7 +622,6 @@ Inclusion.include = function (objects, include, cb) {
* @param callback
*/
function includeOneToOne(callback) {
var debug = require('debug')('loopback:include:includeOneToOne');
var targetIds = [];
var objTargetIdMap = {};
for (var i = 0; i < objs.length; i++) {
@ -642,7 +647,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[relation.keyTo] = {
inq: targetIds
};
relation.modelTo.all(filter, targetFetchHandler);
relation.modelTo.find(filter, targetFetchHandler);
/**
* Process fetched related objects
* @param err