on the halfway to keeping original keys
This commit is contained in:
parent
0864bf7154
commit
a5dd9c181a
|
@ -494,9 +494,9 @@ 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 objIdMap = includeUtils.buildOneToOneIdentityMap(objs, relation.keyFrom);
|
var objIdMap2 = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, relation.keyFrom);
|
||||||
// all ids of primary objects to use in query
|
// 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] = {
|
filter.where[relation.keyTo] = {
|
||||||
inq: uniq(sourceIds)
|
inq: uniq(sourceIds)
|
||||||
|
@ -510,7 +510,7 @@ Inclusion.include = function (objects, include, options, cb) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
var targetsIdMap = includeUtils.buildOneToManyIdentityMap(targets, relation.keyTo);
|
var targetsIdMap = includeUtils.buildOneToManyIdentityMap(targets, relation.keyTo);
|
||||||
includeUtils.join(objIdMap, targetsIdMap, function(obj1, valueToMergeIn){
|
includeUtils.join(objIdMap2.simplified, targetsIdMap, function(obj1, valueToMergeIn){
|
||||||
defineCachedRelations(obj1);
|
defineCachedRelations(obj1);
|
||||||
obj1.__cachedRelations[relationName] = valueToMergeIn;
|
obj1.__cachedRelations[relationName] = valueToMergeIn;
|
||||||
processTargetObj(obj1, function(){});
|
processTargetObj(obj1, function(){});
|
||||||
|
@ -845,7 +845,7 @@ Inclusion.include = function (objects, include, options, cb) {
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
function processTargetObj(obj, callback) {
|
function processTargetObj(obj, callback) {
|
||||||
|
|
||||||
var isInst = obj instanceof self;
|
var isInst = obj instanceof self;
|
||||||
|
|
||||||
// Calling the relation method on the instance
|
// Calling the relation method on the instance
|
||||||
|
|
|
@ -29,7 +29,7 @@ function buildOneToOneIdentityMap(objs, idName) {
|
||||||
* @param origKeyField filed name on value side to pick original key from.
|
* @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`
|
* @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.create(null); // not any single properties within our identity map
|
||||||
var idMap = {};
|
var idMap = {};
|
||||||
Object.defineProperty(idMap, "keys", { // can ask for keys simply by idMap.keys
|
Object.defineProperty(idMap, "keys", { // can ask for keys simply by idMap.keys
|
||||||
|
@ -48,6 +48,17 @@ function newIdMap(origKeyField) {
|
||||||
},
|
},
|
||||||
enumerable: false // explicitly non-enumerable
|
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;
|
return idMap;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -59,7 +70,7 @@ function newIdMap(origKeyField) {
|
||||||
* @returns {{}} object where keys are ids and values are objects itself
|
* @returns {{}} object where keys are ids and values are objects itself
|
||||||
*/
|
*/
|
||||||
function buildOneToOneIdentityMapWithOrigKeys(objs, idName) {
|
function buildOneToOneIdentityMapWithOrigKeys(objs, idName) {
|
||||||
var idMap = newIdMap("originalKey");
|
var idMap = newIdMap("originalKey", "value");
|
||||||
|
|
||||||
for(var i = 0; i < objs.length; i++) {
|
for(var i = 0; i < objs.length; i++) {
|
||||||
var obj = objs[i];
|
var obj = objs[i];
|
||||||
|
|
Loading…
Reference in New Issue