Skip to content

JavaScript: Easy Prisma adapter for completeTx - #1517

Open
brandur wants to merge 1 commit into
masterfrom
brandur-js-prisma-transactions
Open

brandur wants to merge 1 commit into
masterfrom
brandur-js-prisma-transactions

Conversation

@brandur

@brandur brandur commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

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 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();
@brandur
brandur force-pushed the brandur-js-prisma-transactions branch from c7c6f42 to dce7ff9 Compare October 17, 2026 04:45
@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:50:42.551145Z dce7ff9 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: dce7ff9d0f

ℹ️ 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
brandur requested a review from bgentry October 17, 2026 04:54
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