Use it in your app
Install Queuert with your database, migrate the schema and run chains against a real state adapter.
The lessons ran on PostgreSQL in PGlite or SQLite in sql.js, inside your browser. In your app the same Queuert code runs against your own database server; the state provider that wraps your client and the transaction you pass in change.
Requirements
- Node.js 24 or later
- TypeScript 5.2+ (the examples use
await using)
Install
Pick one state adapter, optionally a notify adapter and any extras; the command below installs the core queuert package together with them, and the code on this page follows your picks. Your picks are saved in this browser.
Your stack
Supports horizontal scaling and all Queuert features. Requires PostgreSQL 14+. See PostgreSQL state adapter.
Suitable for local development, CLI tools, and embedded applications. Requires SQLite 3.42+. See SQLite state adapter.
For applications whose data lives in MySQL or MariaDB. Requires MySQL 8.4+ or MariaDB 11.4+ (InnoDB). See MySQL / MariaDB state adapter.
Experimental@queuert/mysql is experimental: its API may change in a minor release.
Queuert's state on Redis or Redis Cluster, kept apart from your application database. Requires Redis 6+. See Redis state adapter.
ExperimentalThe Redis state adapter in @queuert/redis is experimental: its API may change in a minor release.
Uses PostgreSQL's built-in LISTEN/NOTIFY. No additional infrastructure beyond your existing PostgreSQL database — already included in @queuert/postgres. See PostgreSQL notify adapter.
Uses Redis pub/sub. Requires Redis 6+. See Redis notify adapter.
Uses NATS pub/sub. Requires NATS 2.2+. See NATS notify adapter.
Experimental@queuert/nats is experimental: its API may change in a minor release.
No extra packages or infrastructure required. The client wakes its own worker runners in memory, and workers in other processes poll the database on a configurable interval. See Without a notify adapter.
For processes on one host, such as workers sharing a SQLite database file through a synchronous provider. No server to run. See IPC notify adapter.
Experimental@queuert/ipc is experimental: its API may change in a minor release.
No extra packages required. The client wakes its own worker runners in memory, so one process needs nothing more; workers in other processes poll the database on a configurable interval. See Without a notify adapter.
@queuert/dashboard: Embeddable web UI for job observation; @queuert/otel: OpenTelemetry metrics and tracing. Covered in Observability.
Terminal
npm install queuert @queuert/postgresThe command covers Queuert and its adapters, not your database client. Install the driver the code below uses the same way, or pick another one on the adapter's page.
Connect and migrate
Create the state adapter from your database client through a state provider, and call migrateToLatest once at startup to create or upgrade Queuert's tables.
db is a pg pool. The provider comes from the PostgreSQL state adapter page, which has one for each driver and ORM.
src/index.ts
const stateProvider = createPgPoolStateProvider({ pool: db });
await using stateAdapter = createPgStateAdapter({
stateProvider,
});
await stateAdapter.migrateToLatest();db is a better-sqlite3 database, whose calls run synchronously. The provider comes from the SQLite state adapter page, which has one for each driver and ORM.
src/index.ts
const stateProvider = createBetterSqlite3StateProvider({ db });
using stateAdapter = createSqliteStateAdapter({ stateProvider });
stateAdapter.migrateToLatest();db is a mysql2 pool. The provider comes from the MySQL / MariaDB state adapter page, which has one for each driver and ORM.
src/index.ts
const stateProvider = createMysql2PoolStateProvider({ pool: db });
await using stateAdapter = createMysqlStateAdapter({ stateProvider, dialect: "mysql" });
await stateAdapter.migrateToLatest();Queuert's state lives in Redis, apart from your application database. The provider comes from the Redis state adapter page.
src/index.ts
await using drivers = new AsyncDisposableStack();
const redis = drivers.adopt(
await createRedisClient({ url: redisContainer.connectionUrl }).connect(),
async (redis) => {
await redis.close();
},
);
await using stateAdapter = createRedisStateAdapter({
stateProvider: createNodeRedisStateProvider({ client: redis }),
keyPrefix: "{queuert}:",
});
await stateAdapter.migrateToLatest();Process
Declare job types with defineJobTypes and write processors as in the lessons. Each handler commits its outcome through applyJobOutcome inside a transaction.
The transaction is one of your own pg client. withTxHooks collects the work Queuert schedules during it, such as notifying workers, and runs it after the commit; see Tx hooks.
src/index.ts
const client = createClient({ stateAdapter, jobTypes });
await using workerRunner = createWorkerRunner({
client,
processors: createProcessors({
client,
jobTypes,
processors: {
"send-welcome-email": {
attemptHandler: async ({ job, applyJobOutcome }) => {
const { rows } = await db.query<{ id: number; name: string; email: string }>(
"SELECT id, name, email FROM users WHERE id = $1",
[job.input.userId],
);
const user = rows[0];
if (!user) throw new Error(`User ${job.input.userId} not found`);
console.log(`Sending welcome email to ${user.email} for ${user.name}`);
await withTxHooks(async (txHooks) => {
const poolClient = await db.connect();
try {
await poolClient.query("BEGIN");
await applyJobOutcome({
poolClient,
txHooks,
output: { sentAt: new Date().toISOString() },
});
await poolClient.query("COMMIT");
} catch (error) {
await poolClient.query("ROLLBACK").catch(() => {});
throw error;
} finally {
poolClient.release();
}
});
},
},
},
}),
});
await workerRunner.start();The transaction is one of your own better-sqlite3 database. withTxHooks collects the work Queuert schedules during it, such as notifying workers, and runs it after the commit; see Tx hooks.
src/index.ts
const client = createClient({ stateAdapter, jobTypes });
await using workerRunner = createWorkerRunner({
client,
processors: createProcessors({
client,
jobTypes,
processors: {
"send-welcome-email": {
attemptHandler: async ({ job, applyJobOutcome }) => {
const user = db
.prepare("SELECT id, name, email FROM users WHERE id = ?")
.get(job.input.userId) as { id: number; name: string; email: string } | undefined;
if (!user) throw new Error(`User ${job.input.userId} not found`);
console.log(`Sending welcome email to ${user.email} for ${user.name}`);
withTxHooks((txHooks) =>
db.transaction(() =>
applyJobOutcome({ db, txHooks, output: { sentAt: new Date().toISOString() } }),
)(),
);
},
},
},
}),
});
await workerRunner.start();The provider's withTransaction opens the transaction on your pool and passes its context to applyJobOutcome.
src/index.ts
const client = createClient({ stateAdapter, jobTypes });
await using workerRunner = createWorkerRunner({
client,
processors: createProcessors({
client,
jobTypes,
processors: {
"send-welcome-email": {
attemptHandler: async ({ job, applyJobOutcome }) => {
const [rows] = await db.query<
({ id: number; name: string; email: string } & RowDataPacket)[]
>("SELECT id, name, email FROM users WHERE id = ?", [job.input.userId]);
const user = rows[0];
if (!user) throw new Error(`User ${job.input.userId} not found`);
console.log(`Sending welcome email to ${user.email} for ${user.name}`);
await stateProvider.withTransaction(async (txCtx) =>
applyJobOutcome({ ...txCtx, output: { sentAt: new Date().toISOString() } }),
);
},
},
},
}),
});
await workerRunner.start();The state adapter's withTransaction opens the transaction in Redis and passes its context to applyJobOutcome.
src/index.ts
const client = createClient({ stateAdapter, jobTypes });
await using workerRunner = createWorkerRunner({
client,
processors: createProcessors({
client,
jobTypes,
processors: {
"send-welcome-email": {
attemptHandler: async ({ job, applyJobOutcome }) => {
console.log(`Sending welcome email to ${job.input.email}`);
await stateAdapter.withTransaction(async (txCtx) =>
applyJobOutcome({ ...txCtx, output: { sentAt: new Date().toISOString() } }),
);
},
},
},
}),
});
await workerRunner.start();Start a chain
createChain takes the transaction of your own write, here an insert into a users table, so the chain exists only if the write commits.
src/index.ts
const chain = await withTxHooks(async (txHooks) => {
const poolClient = await db.connect();
try {
await poolClient.query("BEGIN");
const userResult = await poolClient.query<{ id: number }>(
"INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id",
["Alice", "alice@example.com"],
);
const user = userResult.rows[0];
const result = await client.createChain({
poolClient,
txHooks,
typeName: "send-welcome-email",
input: { userId: user.id },
});
await poolClient.query("COMMIT");
return result;
} catch (error) {
await poolClient.query("ROLLBACK").catch(() => {});
throw error;
} finally {
poolClient.release();
}
});createChain takes the transaction of your own write, here an insert into a users table, so the chain exists only if the write commits.
src/index.ts
const chain = withTxHooks((txHooks) =>
db.transaction(() => {
const user = db
.prepare("INSERT INTO users (name, email) VALUES (?, ?) RETURNING id")
.get("Alice", "alice@example.com") as { id: number };
return client.createChain({
db,
txHooks,
typeName: "send-welcome-email",
input: { userId: user.id },
});
})(),
);createChain takes the transaction of your own write, here an insert into a users table, so the chain exists only if the write commits.
src/index.ts
const chain = await stateProvider.withTransaction(async (txCtx) => {
const [insert] = await txCtx.connection.query<ResultSetHeader>(
"INSERT INTO users (name, email) VALUES (?, ?)",
["Alice", "alice@example.com"],
);
return client.createChain({
...txCtx,
typeName: "send-welcome-email",
input: { userId: insert.insertId },
});
});createChain takes a Redis transaction of the state adapter. It does not commit together with writes to your application database.
src/index.ts
const chain = await stateAdapter.withTransaction(async (txCtx) => {
const created = await client.createChain({
...txCtx,
typeName: "send-welcome-email",
input: { email: "alice@example.com" },
});
stateAdapter.onTransactionCommit(txCtx, () => {
console.log(`Queued chain ${created.id}`);
});
return created;
});Notify workers
Without a notify adapter, the client wakes its own worker runners in memory and workers in other processes poll the database. A notify adapter wakes them as soon as a chain is created; pass it to createClient as notifyAdapter.
See the PostgreSQL notify adapter page for other drivers.
src/index.ts
await using drivers = new AsyncDisposableStack();
const pool = drivers.adopt(
new Pool({ connectionString: pg.connectionString, max: 10 }),
async (pool) => pool.end(),
);
const notifyProvider = createPgPoolNotifyProvider({ pool });
await using notifyAdapter = createPgNotifyAdapter({ notifyProvider });See the Redis notify adapter page for other clients and Redis Cluster.
src/index.ts
await using drivers = new AsyncDisposableStack();
const redis = drivers.adopt(createRedisClient({ url: redisUrl }), async (redis) => {
await redis.quit();
});
await redis.connect();
const redisSubscription = drivers.adopt(
createRedisClient({ url: redisUrl }),
async (redisSubscription) => {
await redisSubscription.quit();
},
);
await redisSubscription.connect();
const notifyProvider = createNodeRedisNotifyProvider({
client: redis,
subscribeClient: redisSubscription,
});
await using notifyAdapter = createRedisNotifyAdapter({ notifyProvider });See the NATS notify adapter page.
src/index.ts
await using nc = await connect(connectionOptions);
await using notifyAdapter = createNatsNotifyAdapter({
nc,
subjectPrefix: "queuert.example",
});For processes on one host that share a SQLite file. See the IPC notify adapter page.
src/index.ts
using notifyAdapter = createIpcNotifyAdapter({
directory: ipcDirectory,
onError: (error) => {
console.error("IPC notify adapter error", error);
},
});No code to add. See Without a notify adapter for the polling interval.
Where to go next
- Integrations: adapter setup, driver options, provider interfaces and observability.
- Guides: task-oriented recipes for chains, failure, timing and lifecycle.
- Glossary: the terms used across the docs.