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