KyselyK
Kysely3y ago
5 replies
bombillazo

Query building optimization question

Hello, Id like some input from Kysely advanced users about my approach to using the
eb
. If I have a conditional select query, is this efficiently using the
eb
object for my query? Is it ok to use multiple levels of
eb
s ?
// some input
args = {
  param: ['foo', 'bar']
};

await kysely
    .selectFrom('my_table')
    .innerJoin(
      'other',
      'other.id',
      'my_table.other_id',
    )
    .selectAll()
    .$if(!!args.param && args.param.length > 0, (eb) => {
          return eb.where((eb) => {
            if (!args.param) throw Error('param is required');
            return eb(
              eb.ref('data', '->').key('param'),
              'in',
              args.param,
            );
          });
        })
Solution
Creating an expression builder instance is in the order of microseconds. Running the query is in the order of milliseconds or hundreds of milliseconds. You don't need to worry about expression builder instances unless you create a thousand of them per query.
Was this page helpful?