简体   繁体   中英

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

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, 'test3@example.com'], [4, 'test4@example.com']])

Hope it helps,
Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM