Skip to content

Commit c2a683b

Browse files
authored
Merge pull request #1007 from horiaradu/master
Optimize related model queries
2 parents d027202 + c3f6299 commit c2a683b

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/include.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ Inclusion.include = function(objects, include, options, cb) {
212212
}
213213
}
214214
}
215+
if (!foreignKeys.length) {
216+
return cb(null, []);
217+
}
215218
if (filter.limit || filter.skip || filter.offset) {
216219
// Force the find to be performed per FK to honor the pagination
217220
pageSize = 1;
@@ -235,6 +238,10 @@ Inclusion.include = function(objects, include, options, cb) {
235238
}
236239

237240
var items = [];
241+
// Optimization: no need to resolve keys that are an empty array
242+
listOfFKs = listOfFKs.filter(function(keys) {
243+
return keys.length > 0;
244+
});
238245
async.each(listOfFKs, function(foreignKeys, done) {
239246
var newFilter = {};
240247
for (var f in filter) {

0 commit comments

Comments
 (0)