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

State adapter

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.

Notify adapter (optional, for reduced latency)

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.

Extras (optional)

@queuert/dashboard: Embeddable web UI for job observation; @queuert/otel: OpenTelemetry metrics and tracing. Covered in Observability.

Package manager

Terminal

npm install queuert @queuert/postgres

The 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();

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();

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();
  }
});

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 });

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

Search guides, integrations and every exported symbol.

↑ ↓ to navigate↵ to open