Skip to content

JavaScript: Support Knex.js query builder - #1516

Open
brandur wants to merge 1 commit into
masterfrom
brandur-js-knex
Open

brandur wants to merge 1 commit into
masterfrom
brandur-js-knex

Conversation

@brandur

@brandur brandur commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

This one comes at the request of a friend of mine: support the Knex.js
query builder [1] for JavaScript.

Here we add a new @riverqueue/driver-knex package very similar to the
existing ones we have for other JavaScript drivers. Knew.js is not
pulled in as a dependency unless the user adds @riverqueue/driver-knex.

Basic usage, which looks the same as other drivers, but with Knex:

import knex from "knex";
import { Client, defineJob } from "riverqueue";
import { KnexDriver } from "@riverqueue/driver-knex";

const connectionString =
  process.env.DATABASE_URL ?? "postgres://localhost/river";
const db = knex({ client: "pg", connection: connectionString });
const client = new Client(new KnexDriver(db));
const syncAccount = defineJob<{ accountId: string }>()({
  kind: "sync_account",
});

await db.transaction(async (tx) => {
  const accountId = "acct_1";
  await tx("account").insert({ id: accountId });
  await client.insert(syncAccount, { accountId }, { tx });
});

We do a little extra and also add support for a Knex transaction to the
special completeTx function. It'd still be usable by extracting a
separate PgDriver transaction and injecting that to Knex, but the
adapter allows its use natively:

import type { Knex } from "knex";
import { Pool } from "pg";
import { Workers } from "riverqueue";
import { knexTransactionAdapter } from "@riverqueue/driver-knex";
import { PgDriver } from "@riverqueue/driver-pg";

const pool = new Pool({ connectionString });
const workers = new Workers<Knex.Transaction>().add(
  syncAccount,
  async ({ completeTx, job }) => {
    await db.transaction(async (tx) => {
      await tx("account")
        .where({ id: job.args.accountId })
        .update({ synced: true });

      await completeTx(tx);
    });
  }
);

const workerClient = new Client(
  new PgDriver(pool, {
    transactionAdapters: [knexTransactionAdapter],
  }),
  { queues: { default: { maxWorkers: 10 } }, workers }
);
const run = await workerClient.start();

[1] https://knexjs.org/

@brandur

brandur commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T04:25:50.194210Z 65128ff Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 65128ff615

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

brandur added a commit that referenced this pull request Oct 17, 2026
This one doesn't something similar to a piece of #1516 that we'd added.

Currently, you can use Prisma on insertions relatively easily, but
workers still run off `PgDriver`, making it somewhat unergonomic to
invoke transactional completion (`completeTx`) from within a worker.
Here, we add an adapter that makes it very easy.

`prismaTransactionAdapter` is configured on `PgDriver`, and from there
you've got easy access to a `completeTx` in any worker that'll take a
Prisma `$transaction` object:

    import { PrismaPg } from "@prisma/adapter-pg";
    import { PgDriver } from "@riverqueue/driver-pg";
    import { prismaTransactionAdapter } from "@riverqueue/driver-prisma";
    import { Pool } from "pg";
    import { Client, defineJob, Workers } from "riverqueue";
    import { PrismaClient, type Prisma } from "./generated/prisma/client.js";

    const connection = { connectionString: process.env.DATABASE_URL };
    const prisma = new PrismaClient({ adapter: new PrismaPg(connection) });

    const syncAccount = defineJob<{ accountId: string }>()({
      kind: "sync_account",
    });

    const workers = new Workers<Prisma.TransactionClient>().add(
      syncAccount,
      async ({ completeTx, job }) => {
        await prisma.$transaction(async (tx) => {
          await tx.account.update({
            where: { id: job.args.accountId },
            data: { synced: true },
          });
          await completeTx(tx);
        });
      }
    );

    const client = new Client(
      new PgDriver(new Pool(connection), {
        transactionAdapters: [prismaTransactionAdapter],
      }),
      { queues: { default: { maxWorkers: 10 } }, workers }
    );

    await client.start();
This one comes at the request of a friend of mine: support the Knex.js
query builder [1] for JavaScript.

Here we add a new `@riverqueue/driver-knex` package very similar to the
existing ones we have for other JavaScript drivers. Knew.js is not
pulled in as a dependency unless the user adds `@riverqueue/driver-knex`.

Basic usage, which looks the same as other drivers, but with Knex:

    import knex from "knex";
    import { Client, defineJob } from "riverqueue";
    import { KnexDriver } from "@riverqueue/driver-knex";

    const connectionString =
      process.env.DATABASE_URL ?? "postgres://localhost/river";
    const db = knex({ client: "pg", connection: connectionString });
    const client = new Client(new KnexDriver(db));
    const syncAccount = defineJob<{ accountId: string }>()({
      kind: "sync_account",
    });

    await db.transaction(async (tx) => {
      const accountId = "acct_1";
      await tx("account").insert({ id: accountId });
      await client.insert(syncAccount, { accountId }, { tx });
    });

We do a little extra and also add support for a Knex transaction to the
special `completeTx` function. It'd still be usable by extracting a
separate `PgDriver` transaction and injecting that to Knex, but the
adapter allows its use natively:

    import type { Knex } from "knex";
    import { Pool } from "pg";
    import { Workers } from "riverqueue";
    import { knexTransactionAdapter } from "@riverqueue/driver-knex";
    import { PgDriver } from "@riverqueue/driver-pg";

    const pool = new Pool({ connectionString });
    const workers = new Workers<Knex.Transaction>().add(
      syncAccount,
      async ({ completeTx, job }) => {
        await db.transaction(async (tx) => {
          await tx("account")
            .where({ id: job.args.accountId })
            .update({ synced: true });

          await completeTx(tx);
        });
      }
    );

    const workerClient = new Client(
      new PgDriver(pool, {
        transactionAdapters: [knexTransactionAdapter],
      }),
      { queues: { default: { maxWorkers: 10 } }, workers }
    );
    const run = await workerClient.start();

[1] https://knexjs.org/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant