Search code examples
javascriptnode.jsknex.js

How to prevent Knex.js from running a query object when returning it from an async function?


I have a node.js backend that constructs DB queries dynamically from various inputs using Knex.js. Some of the inputs need to be processed asynchronously. My problem is, that I can't return a knex query object from an async function (or of course in a Promise resolve function) because this triggers the execution of the query. Currently I need to process all my async inputs before handing them to the query building functions but that really limits their composability. Is there a way to prevent Knex from executing a query object in an async context?


Solution

  • Thanks to Mikael Lepistö's answer I got an idea how to work around this. As he pointed out Knex queries are thenables by virtue of having a then function. The JavaScript await keyword actually calls the then function of any object you present to it, regardless if promise or not. So in order to prevent query execution on await (or .then()) you can remove/rename the queries then function. E.g.

    const getQuery = async () => {
      const qb = knex("users")
        .select("id")
        .limit(100);
      qb.promise = qb.then;
      qb.then = undefined;
      return qb;
    };
    
    const query = await getQuery();
    console.log(query.toString());
    console.log(await query.promise());
    

    UPDATE, WARNING: Don't try this at home kids :)

    I feel obliged to point to Mikael's valid criticism in the comments. This a hacky and potential dangerous shortcut to writing your own wrapper class and might make your code harder to understand. But I also stand by my assessment that with proper TypeScript typing in my specific use case it's a valid and efficient solution.

    UPDATE2: Now without messing up the prototype :). Setting .then to undefined on the instance works just fine.