on the halfway to keeping original keys

This commit is contained in:
Wert_Lex 2015-10-10 19:37:39 +05:00
parent 0864bf7154
commit a5dd9c181a
2 changed files with 17 additions and 6 deletions

View File

@ -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(){});

View File

@ -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];