diff --git a/lib/include.js b/lib/include.js index d3428e33..b6cd5d5f 100644 --- a/lib/include.js +++ b/lib/include.js @@ -494,9 +494,9 @@ Inclusion.include = function (objects, include, options, cb) { */ function includeHasManySimple(callback) { //Map for Indexing objects by their id for faster retrieval - var objIdMap = includeUtils.buildOneToOneIdentityMap(objs, relation.keyFrom); + var objIdMap2 = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, relation.keyFrom); // all ids of primary objects to use in query - var sourceIds = Object.keys(objIdMap); + var sourceIds = objIdMap2.originalKeys;//Object.keys(objIdMap); filter.where[relation.keyTo] = { inq: uniq(sourceIds) @@ -510,7 +510,7 @@ Inclusion.include = function (objects, include, options, cb) { return callback(err); } var targetsIdMap = includeUtils.buildOneToManyIdentityMap(targets, relation.keyTo); - includeUtils.join(objIdMap, targetsIdMap, function(obj1, valueToMergeIn){ + includeUtils.join(objIdMap2.simplified, targetsIdMap, function(obj1, valueToMergeIn){ defineCachedRelations(obj1); obj1.__cachedRelations[relationName] = valueToMergeIn; processTargetObj(obj1, function(){}); @@ -845,7 +845,7 @@ Inclusion.include = function (objects, include, options, cb) { * @returns {*} */ function processTargetObj(obj, callback) { - + var isInst = obj instanceof self; // Calling the relation method on the instance diff --git a/lib/include_utils.js b/lib/include_utils.js index 8da849db..52e1be0f 100644 --- a/lib/include_utils.js +++ b/lib/include_utils.js @@ -29,7 +29,7 @@ function buildOneToOneIdentityMap(objs, idName) { * @param origKeyField filed name on value side to pick original key from. * @returns empty object to be filled with key-value pair and additional methods `keys` and `originalKeys` */ -function newIdMap(origKeyField) { +function newIdMap(origKeyField, valueField) { //var idMap = Object.create(null); // not any single properties within our identity map var idMap = {}; Object.defineProperty(idMap, "keys", { // can ask for keys simply by idMap.keys @@ -48,6 +48,17 @@ function newIdMap(origKeyField) { }, enumerable: false // explicitly non-enumerable }); + Object.defineProperty(idMap, "simplified",{ + get: function(){ + var keys = this.keys; + var simplified = {}; + for(var i = 0; i < keys.length; i++) { + var key = keys[i]; + simplified[key] = this[key][valueField]; + } + return simplified; + } + }); return idMap; } /** @@ -59,7 +70,7 @@ function newIdMap(origKeyField) { * @returns {{}} object where keys are ids and values are objects itself */ function buildOneToOneIdentityMapWithOrigKeys(objs, idName) { - var idMap = newIdMap("originalKey"); + var idMap = newIdMap("originalKey", "value"); for(var i = 0; i < objs.length; i++) { var obj = objs[i];