diff --git a/lib/include.js b/lib/include.js index b6cd5d5f..c27da09f 100644 --- a/lib/include.js +++ b/lib/include.js @@ -495,8 +495,10 @@ Inclusion.include = function (objects, include, options, cb) { function includeHasManySimple(callback) { //Map for Indexing objects by their id for faster retrieval var objIdMap2 = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, relation.keyFrom); - // all ids of primary objects to use in query - var sourceIds = objIdMap2.originalKeys;//Object.keys(objIdMap); + console.log("### objIdMap2: " + JSON.stringify(objIdMap2)); + // all ids of primary objects to use in query are original. So, ObjectIds stay ObjectIds and Ints stay Ints. + // No .toString() conversion. + var sourceIds = objIdMap2.originalKeys; filter.where[relation.keyTo] = { inq: uniq(sourceIds) diff --git a/lib/include_utils.js b/lib/include_utils.js index 52e1be0f..99919cd2 100644 --- a/lib/include_utils.js +++ b/lib/include_utils.js @@ -32,6 +32,14 @@ function buildOneToOneIdentityMap(objs, idName) { function newIdMap(origKeyField, valueField) { //var idMap = Object.create(null); // not any single properties within our identity map var idMap = {}; + Object.defineProperty(idMap, "set", { + value: function(origKey, value){ + var key = origKey.toString(); + this[key] = {}; + this[key][origKeyField] = origKey; + this[key][valueField] = value; + } + }); Object.defineProperty(idMap, "keys", { // can ask for keys simply by idMap.keys get: function(){ return Object.keys(this); }, enumerable: false // explicitly non-enumerable @@ -75,10 +83,7 @@ function buildOneToOneIdentityMapWithOrigKeys(objs, idName) { for(var i = 0; i < objs.length; i++) { var obj = objs[i]; var id = obj[idName]; - idMap[id.toString()] = { - originalKey: id, - value: obj - }; + idMap.set(id, obj); } return idMap; } @@ -103,6 +108,21 @@ function buildOneToManyIdentityMap(objs, idName) { return idMap; } +function buildOneToManyIdentityMapWithOrigKeys(objs, idName) { + var idMap = newIdMap("originalKey", "value"); + for(var i = 0; i < objs.length; i++) { + var obj = objs[i]; + var id = obj[idName]; + var idString = id.toString(); + if(idString in idMap) { + idMap[idString]["value"].push(obj); + } else { + idMap.set(id, [obj]); + } + } + return idMap; +} + /** * Yeah, it joins. You need three things id -> obj1 map, id -> [obj2] map and merge function.