1

One of the examples for using DataLoader with Knex shows something this:

  user: new DataLoader(ids => db.table('users')
    .whereIn('id', ids).select()
    .then(rows => ids.map(id => rows.find(x => x.id === id)))),

The map there is so that the keys in the array of keys always match up with the objects in the array of results, e.g. if object with id 2 is missing:

array of keys: [1,2,3]
array of results: [object1, undefined, object3]

If you left the map out, you'd get an unbalanced input/output (e.g. when querying for missing ids):

array of keys: [1,2,3]
array of results [object1, object3]

Is there any way to do the map bit with pure SQL?

Jure Triglav
  • 1,862
  • 18
  • 22

1 Answers1

0

It means that database doesn't have row with id 2 so whereIn doesn't return more that 2 rows. There is no way to do that in pure SQL with whereIn. With multiple subqueries it can be done, but it would be bad solution.

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70