From c3f62991c39cfb63a0897f5d7272e79f422013b8 Mon Sep 17 00:00:00 2001 From: Horia Radu Date: Thu, 21 Jul 2016 14:26:24 +0300 Subject: [PATCH] Optimize related model queries No longer query for the related/included model when no results are obtained in the first query. fix: https://github.com/strongloop/loopback-datasource-juggler/issues/1006 --- lib/include.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/include.js b/lib/include.js index c9882813..a04bf7e0 100644 --- a/lib/include.js +++ b/lib/include.js @@ -212,6 +212,9 @@ Inclusion.include = function(objects, include, options, cb) { } } } + if (!foreignKeys.length) { + return cb(null, []); + } if (filter.limit || filter.skip || filter.offset) { // Force the find to be performed per FK to honor the pagination pageSize = 1; @@ -235,6 +238,10 @@ Inclusion.include = function(objects, include, options, cb) { } var items = []; + // Optimization: no need to resolve keys that are an empty array + listOfFKs = listOfFKs.filter(function(keys) { + return keys.length > 0; + }); async.each(listOfFKs, function(foreignKeys, done) { var newFilter = {}; for (var f in filter) {