---
title: Adapters
description: Compare the setup and guarantees for database and jobs adapters.
---

Database adapters supply transactions. Jobs adapters control delivery.

{/* source: packages/logic-sdk/src/adapters/, packages/logic-sdk/src/core/in-process.ts */}

| 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

```ts twoslash
import { init, inProcess } from "logic-sdk";

const { createRegistry } = init<{ scope: { actor: string | null } }>();
const modules = {};
// ---cut---
const registry = createRegistry({ modules, jobs: { adapter: 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

```ts
// 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 take `DrizzleReader<DrizzleTx<typeof db>>` or `DrizzleTx<typeof db>`, which omits `transaction`.
- **Retries.** See [Transaction retries](/docs/concepts/transactions#lost-races-are-retried).
- **Migrations.** Include `jobsTable` for jobs and `cronRunsTable` for crons. Share the jobs table only among instances with the same modules.

## Drizzle SQLite

```ts
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:sqlite` or `bun: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 from `logic-sdk/drizzle/sqlite`.

## Other database adapters

```ts
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.** `SqlDatabase` adds `query(client, text, params)` to `DatabaseAdapter`. Use that client and return rows as objects.
- **SQL.** Migrate with `jobsSchema`. Postgres binds `$1`, `$2`, and so on. With `logic-sdk/sqlite`, bind `?` in order.
- **Types.** Use `db: ReadWrite<Reader, Tx>`. Supply `nest` with savepoints for `createTestRegistry` and `retryable(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

```ts
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 `jobsTable` for `failedJobs()`. Omit the store to report failures without keeping them.
- **Delivery.** Use [`handleQueue`](/docs/guides/cloudflare). Messages can arrive more than once.

## Outbox

```ts
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

<AutoTypeTable path="../../packages/logic-sdk/src/adapters/stored-jobs.ts" name="DatabaseQueueOptions" />

For 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.

## See also

- [Jobs](/docs/concepts/jobs)
- [Node workers](/docs/guides/node)
- [Testing](/docs/guides/testing)
