home-written map extended with proper .set() method
This commit is contained in:
parent
a5dd9c181a
commit
1cab0164c2
|
@ -495,8 +495,10 @@ Inclusion.include = function (objects, include, options, cb) {
|
||||||
function includeHasManySimple(callback) {
|
function includeHasManySimple(callback) {
|
||||||
//Map for Indexing objects by their id for faster retrieval
|
//Map for Indexing objects by their id for faster retrieval
|
||||||
var objIdMap2 = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, relation.keyFrom);
|
var objIdMap2 = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, relation.keyFrom);
|
||||||
// all ids of primary objects to use in query
|
console.log("### objIdMap2: " + JSON.stringify(objIdMap2));
|
||||||
var sourceIds = objIdMap2.originalKeys;//Object.keys(objIdMap);
|
// 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] = {
|
filter.where[relation.keyTo] = {
|
||||||
inq: uniq(sourceIds)
|
inq: uniq(sourceIds)
|
||||||
|
|
|
@ -32,6 +32,14 @@ function buildOneToOneIdentityMap(objs, idName) {
|
||||||
function newIdMap(origKeyField, valueField) {
|
function newIdMap(origKeyField, valueField) {
|
||||||
//var idMap = Object.create(null); // not any single properties within our identity map
|
//var idMap = Object.create(null); // not any single properties within our identity map
|
||||||
var idMap = {};
|
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
|
Object.defineProperty(idMap, "keys", { // can ask for keys simply by idMap.keys
|
||||||
get: function(){ return Object.keys(this); },
|
get: function(){ return Object.keys(this); },
|
||||||
enumerable: false // explicitly non-enumerable
|
enumerable: false // explicitly non-enumerable
|
||||||
|
@ -75,10 +83,7 @@ function buildOneToOneIdentityMapWithOrigKeys(objs, idName) {
|
||||||
for(var i = 0; i < objs.length; i++) {
|
for(var i = 0; i < objs.length; i++) {
|
||||||
var obj = objs[i];
|
var obj = objs[i];
|
||||||
var id = obj[idName];
|
var id = obj[idName];
|
||||||
idMap[id.toString()] = {
|
idMap.set(id, obj);
|
||||||
originalKey: id,
|
|
||||||
value: obj
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
return idMap;
|
return idMap;
|
||||||
}
|
}
|
||||||
|
@ -103,6 +108,21 @@ function buildOneToManyIdentityMap(objs, idName) {
|
||||||
return idMap;
|
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.
|
* Yeah, it joins. You need three things id -> obj1 map, id -> [obj2] map and merge function.
|
||||||
|
|
Loading…
Reference in New Issue