Adapters
Compare the setup and guarantees for database and jobs adapters.
Database adapters supply transactions. Jobs adapters control delivery.
| Adapter | Storage | Retries | Delay | key |
unique and replace |
Deduplicates ticks |
|---|---|---|---|---|---|---|
inProcess() |
Memory | No | No | Local | Same-commit unique |
No |
databaseQueue() |
Postgres or SQLite | Yes | Yes | Yes | Yes | Yes |
cloudflareQueue() |
Queue after handoff | Redelivery | 24 hours | No | No | No |
outbox(queue) |
Postgres or SQLite | Yes | Yes | Yes | Yes | Yes |
In-process
const const registry: Registry<{
actor: string | null;
}, {}, Record<string, never>, {}, AppError<never, undefined> | AppError<never, {
issues: {
message: string;
path?: PropertyKey[];
}[];
}>>
registry = const createRegistry: <{}, {
actor: string | null;
}>(options: TypedOptions<{
scope: {
actor: string | null;
};
}, {}, {
actor: string | null;
}>) => Registry<{
actor: string | null;
}, {}, Record<string, never>, {}, AppError<never, undefined> | AppError<never, {
issues: {
message: string;
path?: PropertyKey[];
}[];
}>>
Create the registry once at startup: `createRegistry({ modules, database, services })`.createRegistry({ modules: OwnModules<{
scope: {
actor: string | null;
};
}, {}>
One entry per module: `{ bookings, availability }` with `import * as bookings from "./bookings"`.modules, jobs?: JobsOptions<{
actor: string | null;
}, {
actor: string | null;
}, NoDb, {
readonly [x: string]: never;
}> | undefined
Background work: `jobs: { adapter: databaseQueue(), scope: { store, load } }`. Default: in-process, scope as-is (nothing waits).jobs: { adapter?: JobsAdapter | undefined`inProcess()` (default), or a durable one like `databaseQueue()`. Its own options go in its factory.adapter: function inProcess(): JobsAdapterDefault adapter: runs each job right after commit, in this process, one try (like Laravel's sync queue). It can't
hold a job for later, so a `delay` or a waiting `ctx.schedule` is refused. Lost on crash: use `databaseQueue` when that matters.inProcess() } });
- Default. The request runs jobs after commit or hands them to
waitUntil. - Crash. In-memory jobs can disappear after commit. Terminal failures reach
onError.
Drizzle Postgres
// db/schema.ts
export { jobsTable, cronRunsTable } from "logic-sdk/drizzle/pg";
// registry.ts
import { databaseQueue, drizzleAdapter } from "logic-sdk/drizzle/pg";
const registry = createRegistry({
modules,
database: { adapter: drizzleAdapter(db) },
jobs: { adapter: databaseQueue(), scope: { store, load } },
});
- Drivers. Use Drizzle 1.0 with interactive transactions. Examples include node-postgres, postgres.js, PGlite, Neon over WebSocket, and Hyperdrive.
- Types. Use
DrizzleDb<typeof db>. Helpers takeDrizzleReader<DrizzleTx<typeof db>>orDrizzleTx<typeof db>, which omitstransaction. - Retries. See Transaction retries.
- Migrations. Include
jobsTablefor jobs andcronRunsTablefor crons. Share the jobs table only among instances with the same modules.
Drizzle SQLite
import { drizzle } from "drizzle-orm/node-sqlite";
import { databaseQueue, drizzleAdapter } from "logic-sdk/drizzle/sqlite";
const open = () => drizzle("app.db");
const registry = createRegistry({
modules,
database: { adapter: drizzleAdapter(open) },
jobs: { adapter: databaseQueue(), scope: { store, load } },
});
- Drivers. Use a local file with
node:sqliteorbun:sqlite. The jobs table needs SQLite 3.42 or later. - Files. Use a temporary file per test suite. The adapter rejects
":memory:"and does not support D1, libsql, or Turso. - Connections. Create one adapter per process. The adapter keeps a reader and opens each transaction separately. Configure foreign keys in
open. - Writes. The adapter uses WAL,
BEGIN IMMEDIATE, and a five-second lock wait. Use a mutation or a separate connection for script writes. - Rollback. If
RAISE(ROLLBACK)ends the transaction, further writes can commit outside it. Let the error escape. - Types and tables. Use
DrizzleDb<ReturnType<typeof open>>. Import both tables fromlogic-sdk/drizzle/sqlite.
Other database adapters
import type { SqlDatabase } from "logic-sdk";
import { databaseQueue, jobsSchema } from "logic-sdk/postgres";
const database = {
db: prisma,
transaction: (fn) => prisma.$transaction(fn),
query: (client, text, params) => client.$queryRawUnsafe(text, ...params),
} satisfies SqlDatabase<PrismaClient, Prisma.TransactionClient>;
const jobs = { adapter: databaseQueue(), scope: { store, load } };
const registry = createRegistry({ modules, database: { adapter: database }, jobs });
- Contract.
SqlDatabaseaddsquery(client, text, params)toDatabaseAdapter. Use that client and return rows as objects. - SQL. Migrate with
jobsSchema. Postgres binds$1,$2, and so on. Withlogic-sdk/sqlite, bind?in order. - Types. Use
db: ReadWrite<Reader, Tx>. Supplynestwith savepoints forcreateTestRegistryandretryable(error)for transaction retries. - Timeouts. Set the transaction timeout in your ORM adapter. The SDK tests Postgres and SQLite. The Prisma example is untested.
Cloudflare Queues
import { cloudflareQueue } from "logic-sdk/cloudflare";
import { failedJobs } from "logic-sdk/drizzle/pg";
const jobs = {
adapter: cloudflareQueue(env.JOBS),
failed: failedJobs(),
scope: { store, load },
};
- Failures. Migrate
jobsTableforfailedJobs(). Omit the store to report failures without keeping them. - Delivery. Use
handleQueue. Messages can arrive more than once.
Outbox
import { outbox } from "logic-sdk/drizzle/pg";
import { cloudflareQueue } from "logic-sdk/cloudflare";
const jobs = { adapter: outbox(cloudflareQueue(env.JOBS)), scope: { store, load } };
- Tables. Use the database queue migrations. The outbox keeps failed rows and rejects an additional failed store.
- Signals. The outbox signals each job or each key per commit. The consumer signals continuation after a minute and reschedules retries within the queue’s delay limit.
- Recovery. Sweep regularly. The outbox and inline database queue leave a 30-second window for initial delivery before sweeps take over.
Stored adapter options
runIn?"request" | "worker"
Who runs a job due now. `"request"` (default): the request that committed it, right after commit (after the response, with `waitUntil`), unless `work(registry)` runs in this process: then that loop takes it, and the request answers at once. `"worker"`: workers in other processes run every job, and requests only write rows.
"request" | "worker"lease?number
Seconds a runner owns a job before a sweep may take it over: how soon a crash is noticed. Default 300. A running job extends it every third of it, for 15 minutes, so it needn't cover your slowest listener, unless one takes longer than that.
numberbackoff?(attempt: number) => number
Seconds to wait after a failed try. Default `defaultBackoff`, the curve Graphile Worker uses.
(attempt: number) => numberFor jobs beyond the renewal window, set a lease that covers their runtime. With one connection, keep mutations under two thirds of the lease.
Custom jobs adapters
Implement JobsAdapter. Declare capabilities such as holds, retries, replaces, and orders. Use enqueue(tx, jobs) inside the transaction and deliver(jobs, runner) after commit. Pass plain JSON jobs. The runner decides which errors retry. The adapter records the outcome. The contract can change before 1.0.