Search code examples
javascriptnode.jstypescriptknex.js

Dataloader with Knex.js


This code was working fine before I updated to the dataloader: 2.0.0

const productLoader = new DataLoader(async keys => {
  const products: Product[] = await knex('product')
    .whereIn('id', keys)
    .select()

  const productMap: any = {}

  products.forEach((p: any) => {
    productMap[p.id] = p
  })

  return keys.map((k: any) => productMap[k])
})

export default productLoader

Now it gives error:

loader.ts:7:14 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type '"id"' is not assignable to parameter of type 'string[]'.

7     .whereIn('id', keys)
               ~~~~

  node_modules/knex/types/index.d.ts:1137:5
    1137     <TRecordInner, TResultInner>(
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1138       columnNames: string[],
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1139       values: QueryBuilder<TRecordInner, TResultInner>
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1140     ): QueryBuilder<TRecord, TResult>;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The last overload is declared here.


Found 1 error.

What did I do wrong?

// package.json

"dataloader": "^2.0.0",
"knex": "^0.20.2",

Stack: Apollo-server-express, TypeScript, Postgres, Knex.js


Solution

  • It seems that knex (TypeScript) is expecting you to set an array of strings (string[]) as first parameter of whereIn, like the following :

    const products: Product[] = await knex('product')
      .whereIn(['id'], keys)
      .select();
    

    This is the same as when you're searching in multiple columns (the following example comes from Knex.js documentation) :

    knex.select('name').from('users')   
      .whereIn(['account_id', 'email'], [[3, '[email protected]'], [4, '[email protected]']])
    

    Hope it helps,
    Best regards