Last active
November 15, 2016 07:46
-
-
Save BoLaMN/e2fb1473cae8081ac255fdd0cbe927ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/include.js b/lib/include.js | |
index d36d497..052da5f 100644 | |
--- a/lib/include.js | |
+++ b/lib/include.js | |
@@ -334,12 +334,13 @@ Inclusion.include = function(objects, include, options, cb) { | |
} | |
//Let's add a placeholder where query | |
filter.where = filter.where || {}; | |
+ var filterKey = (relation.type === 'hasOne') ? relation.keyFrom : relation.keyTo; | |
//if fields are specified, make sure target foreign key is present | |
var fields = filter.fields; | |
- if (Array.isArray(fields) && fields.indexOf(relation.keyTo) === -1) { | |
- fields.push(relation.keyTo); | |
- } else if (isPlainObject(fields) && !fields[relation.keyTo]) { | |
- fields[relation.keyTo] = true; | |
+ if (Array.isArray(fields) && fields.indexOf(filterKey) === -1) { | |
+ fields.push(filterKey); | |
+ } else if (isPlainObject(fields) && !fields[filterKey]) { | |
+ fields[filterKey] = true; | |
} | |
// | |
@@ -865,17 +866,21 @@ Inclusion.include = function(objects, include, options, cb) { | |
function includeOneToOne(callback) { | |
var targetIds = []; | |
var objTargetIdMap = {}; | |
+ | |
+ var lookupKey = (relation.type === 'belongsTo') ? relation.keyFrom : relation.keyTo; | |
+ | |
for (var i = 0; i < objs.length; i++) { | |
var obj = objs[i]; | |
if (relation.type === 'belongsTo') { | |
- if (obj[relation.keyFrom] === null || | |
- obj[relation.keyFrom] === undefined) { | |
+ if (obj[lookupKey] === null || | |
+ obj[lookupKey] === undefined) { | |
defineCachedRelations(obj); | |
obj.__cachedRelations[relationName] = null; | |
continue; | |
} | |
} | |
- var targetId = obj[relation.keyFrom]; | |
+ | |
+ var targetId = obj[lookupKey]; | |
if (targetId) { | |
targetIds.push(targetId); | |
var targetIdStr = targetId.toString(); | |
@@ -885,13 +890,14 @@ Inclusion.include = function(objects, include, options, cb) { | |
defineCachedRelations(obj); | |
obj.__cachedRelations[relationName] = null; | |
} | |
- filter.where[relation.keyTo] = { | |
+ var filterKey = (relation.type === 'belongsTo') ? relation.keyTo : relation.keyFrom; | |
+ filter.where[filterKey] = { | |
inq: uniq(targetIds), | |
}; | |
relation.applyScope(null, filter); | |
findWithForeignKeysByPage(relation.modelTo, filter, | |
- relation.keyTo, 0, options, targetFetchHandler); | |
+ filterKey, 0, options, targetFetchHandler); | |
/** | |
* Process fetched related objects | |
@@ -915,7 +921,7 @@ Inclusion.include = function(objects, include, options, cb) { | |
function targetLinkingTask(next) { | |
async.each(targets, linkOneToMany, next); | |
function linkOneToMany(target, next) { | |
- var targetId = target[relation.keyTo]; | |
+ var targetId = target[filterKey]; | |
var objList = objTargetIdMap[targetId.toString()]; | |
async.each(objList, function(obj, next) { | |
if (!obj) return next(); | |
@@ -974,6 +980,9 @@ Inclusion.include = function(objects, include, options, cb) { | |
* @param cb | |
*/ | |
function setIncludeData(result, cb) { | |
+ if (relation.embed) { | |
+ relationName = relation.keyFrom; | |
+ } | |
if (isInst) { | |
if (Array.isArray(result) && !(result instanceof List)) { | |
result = new List(result, relation.modelTo); | |
@@ -1026,6 +1035,9 @@ Inclusion.include = function(objects, include, options, cb) { | |
return callback(err); | |
} else { | |
defineCachedRelations(obj); | |
+ if (relation.embed) { | |
+ relationName = relation.keyFrom; | |
+ } | |
obj.__cachedRelations[relationName] = result; | |
return setIncludeData(result, callback); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 633970b2563facc7bc66b317516f8945621701b5 Mon Sep 17 00:00:00 2001 | |
From: Nathan Bolam <ellirion@gmail.com> | |
Date: Tue, 15 Nov 2016 18:07:07 +1030 | |
Subject: [PATCH] no message | |
--- | |
lib/relation-definition.js | 4 ++-- | |
1 file changed, 2 insertions(+), 2 deletions(-) | |
diff --git a/lib/relation-definition.js b/lib/relation-definition.js | |
index 84225df..d119d6b 100644 | |
--- a/lib/relation-definition.js | |
+++ b/lib/relation-definition.js | |
@@ -1893,8 +1893,8 @@ HasMany.prototype.build = HasOne.prototype.build = function(targetModelData) { | |
HasOne.prototype.related = function(condOrRefresh, options, cb) { | |
var self = this; | |
var modelTo = this.definition.modelTo; | |
- var fk = this.definition.keyTo; | |
- var pk = this.definition.keyFrom; | |
+ var pk = this.definition.keyTo; | |
+ var fk = this.definition.keyFrom; | |
var definition = this.definition; | |
var modelInstance = this.modelInstance; | |
var newValue; | |
-- | |
2.2.1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment