From 1114a21555c5a69d2d7bc30ff0e5e2e13ddc65e2 Mon Sep 17 00:00:00 2001 From: Robert P Date: Mon, 18 Jan 2016 18:25:00 +1030 Subject: [PATCH 1/3] Added check that skips loading belongsTo relations with falsey local keys --- src/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 62a095b..df96cc6 100644 --- a/src/index.js +++ b/src/index.js @@ -159,10 +159,13 @@ function loadWithRelations (items, resourceConfig, options) { }) } else if (def.type === 'belongsTo' || (def.type === 'hasOne' && def.localKey)) { if (instance) { - task = this.find(resourceConfig.getResource(relationName), DSUtils.get(instance, def.localKey), __options).then(relatedItem => { - instance[def.localField] = relatedItem - return relatedItem - }) + let id = DSUtils.get(instance, def.localKey); + if (id) { + task = this.find(resourceConfig.getResource(relationName), id, __options).then(relatedItem => { + instance[def.localField] = relatedItem + return relatedItem + }) + } } else { task = this.findAll(resourceConfig.getResource(relationName), { where: { From 6a613116c44b331999cb95e7359aaf4f007c796d Mon Sep 17 00:00:00 2001 From: Robert P Date: Tue, 19 Jan 2016 12:35:24 +1030 Subject: [PATCH 2/3] [FIX] Handling case where instance isn't available Without this, js-data-sql will try to query belongsTo relations even without any valid ids. --- src/index.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index df96cc6..d73d404 100644 --- a/src/index.js +++ b/src/index.js @@ -167,22 +167,25 @@ function loadWithRelations (items, resourceConfig, options) { }) } } else { - task = this.findAll(resourceConfig.getResource(relationName), { - where: { - [relationDef.idAttribute]: { - 'in': DSUtils.filter(items.map(function (item) { return DSUtils.get(item, def.localKey) }), x => x) - } - } - }, __options).then(relatedItems => { - DSUtils.forEach(items, item => { - DSUtils.forEach(relatedItems, relatedItem => { - if (relatedItem[relationDef.idAttribute] === item[def.localKey]) { - item[def.localField] = relatedItem + let ids = DSUtils.filter(items.map(function (item) { return DSUtils.get(item, def.localKey) }), x => x) + if (ids.length) { + task = this.findAll(resourceConfig.getResource(relationName), { + where: { + [relationDef.idAttribute]: { + 'in': DSUtils.filter(items.map(function (item) { return DSUtils.get(item, def.localKey) }), x => x) } + } + }, __options).then(relatedItems => { + DSUtils.forEach(items, item => { + DSUtils.forEach(relatedItems, relatedItem => { + if (relatedItem[relationDef.idAttribute] === item[def.localKey]) { + item[def.localField] = relatedItem + } + }) }) + return relatedItems }) - return relatedItems - }) + } } } From ad604a336d3b331306357f0b1c121870264300ea Mon Sep 17 00:00:00 2001 From: Robert P Date: Tue, 19 Jan 2016 12:40:15 +1030 Subject: [PATCH 3/3] And changed the now-redundant function call --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index d73d404..3c57882 100644 --- a/src/index.js +++ b/src/index.js @@ -172,7 +172,7 @@ function loadWithRelations (items, resourceConfig, options) { task = this.findAll(resourceConfig.getResource(relationName), { where: { [relationDef.idAttribute]: { - 'in': DSUtils.filter(items.map(function (item) { return DSUtils.get(item, def.localKey) }), x => x) + 'in': ids } } }, __options).then(relatedItems => {