Skip to content

Add ability to either wrap queries or extract db or executor #1689

@GiacoCorsiglia

Description

@GiacoCorsiglia

I have a helper function that takes a baseQuery: SelectQueryBuilder and paginates it, returning two queries: one with limit/offset, and another that selects a count.

To make this resilient to GROUP BY clauses in the baseQuery, I want to do

countQuery = db.selectFrom(baseQuery.as("__base")).select(eb => eb.fn.countAll().as("count"))

But—unless I'm mistaken, this will "remove" the count query from whatever transaction the baseQuery might be part of. That is, the following does not work as desired (every query in the same transaction):

db.transaction().execute((tx) => {
  const baseQuery = tx.selectFrom(...)

  const { pageQuery, countQuery } = paginate(baseQuery);

  // Not actually in the transaction (I think).
  countQuery.execute()
})

To address this, I either have to thread tx into the paginate function, or I can do a trick with baseQuery.withPlugin() to wrap:

const countQuery = baseQuery.withPlugin({
  transformQuery(args) {
    const baseQuery = new ExpressionWrapper(args.node);
    const eb = expressionBuilder();
    const countQuery = eb.selectFrom(baseQuery.as('__base')).select(eb => eb.fn.countAll().as('count'));

    return countQuery.toOperationNode();
  },

  async transformResult(args) {
    return args.result;
  }
});

which I think works.

It would be nice to have some canonical solution to this. A few initial ideas:

  1. Add SelectQueryBuilder.wrap(): baseQuery.wrap((eb, query) => eb.selectFrom(query)...)
  2. Expose SelectQueryBuilder.db or something like it
  3. Accept an optional argument in execute() that lets you pin the execution to a given transaction.

Thanks for reading! Kysely is so awesome 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiRelated to library's APIenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions