Use graphile-worker with orchid-orm, including support for transactions.
pnpm add graphile-worker-orchid
Create worker utils:
import { makeWorkerUtils } from "graphile-worker-orchid"
import { orchidORM } from "orchid-orm"
const db = orchidORM({ databaseURL: "..." }, {
// table definitions
})
const {
addJob,
removeJob,
waitJob,
} = makeWorkerUtils(db)
Add job with job name, params, and possibly options:
await addJob("test", { foo: 123 }, {
jobKey: "testing",
})
This also works in transaction:
await db.$transaction(async () => {
const user = await db.user.select("id", "name", "email").create(userInput)
// The job will only by added when and if the transaction commits.
await addJob("sendRegistrationEmail", { user })
})
Remove job by job key:
await removeJob("testing")
Wait for a job to complete by its job ID:
const job = await addJob("test", { foo: 123 })
// Will throw if the job fails (reaches max retry attempts).
await waitJob(job.id)
The function waits by simple polling. You can override the polling interval with:
await waitJob(job.id, {
pollInterval: 200, // Default is 1000 ms
})