The Drizzle adapter provides PostgreSQL support using Drizzle ORM, offering excellent TypeScript integration and performance.
Installation #
npm install @vorsteh-queue/core @vorsteh-queue/adapter-drizzlepnpm add @vorsteh-queue/core @vorsteh-queue/adapter-drizzleyarn add @vorsteh-queue/core @vorsteh-queue/adapter-drizzlebun add @vorsteh-queue/core @vorsteh-queue/adapter-drizzleQuick Start #
import { drizzle } from "drizzle-orm/node-postgres"
import { Pool } from "pg"
import { PostgresQueueAdapter } from "@vorsteh-queue/adapter-drizzle"
import { Queue } from "@vorsteh-queue/core"
const pool = new Pool({ connectionString: "postgresql://..." })
const db = drizzle(pool)
const adapter = new PostgresQueueAdapter(db)
const queue = new Queue(adapter)
Supported providers #
- PGlite - https://orm.drizzle.team/docs/connect-pglite
- Postgres.JS - https://orm.drizzle.team/docs/get-started-postgresql#postgresjs
- Node Progress - https://orm.drizzle.team/docs/get-started-postgresql#node-postgres
Database Setup #
Schema #
The adapter includes a pre-defined schema:
import { queueJobsTable } from "@vorsteh-queue/adapter-drizzle"
// Use in your schema file
export { queueJobsTable }
If you don't want to use the pre-defined schema, you can create your own schema definition.
import { sql } from "drizzle-orm"
import { index, integer, jsonb, pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core"
export const queueJobs = pgTable(
"queue_jobs",
{
id: uuid("id")
.primaryKey()
.default(sql`gen_random_uuid()`),
queueName: varchar("queue_name", { length: 255 }).notNull(),
name: varchar("name", { length: 255 }).notNull(),
payload: jsonb("payload").notNull(),
status: varchar("status", { length: 50 }).notNull(),
priority: integer("priority").notNull(),
attempts: integer("attempts").default(0).notNull(),
maxAttempts: integer("max_attempts").notNull(),
createdAt: timestamp("created_at", { withTimezone: true, mode: "date" })
.notNull()
.default(sql`timezone('utc', now())`),
processAt: timestamp("process_at", { withTimezone: true, mode: "date" }).notNull(),
processedAt: timestamp("processed_at", { withTimezone: true, mode: "date" }),
completedAt: timestamp("completed_at", { withTimezone: true, mode: "date" }),
failedAt: timestamp("failed_at", { withTimezone: true, mode: "date" }),
error: jsonb("error"),
result: jsonb("result"),
progress: integer("progress").default(0),
cron: varchar("cron", { length: 255 }),
repeatEvery: integer("repeat_every"),
repeatLimit: integer("repeat_limit"),
repeatCount: integer("repeat_count").default(0),
timeout: jsonb("timeout"),
},
(table) => [
index("idx_queue_jobs_status_priority").on(
table.queueName,
table.status,
table.priority,
table.createdAt,
),
index("idx_queue_jobs_process_at").on(table.processAt),
],
)
export type QueueJob = typeof queueJobs.$inferSelect
export type InsertQueueJob = typeof queueJobs.$inferInsertCustom Table & Schema Names #
To create a table with custom name and schema, you can use the createQueueJobsTable helper, which will create the table without taking care about the field definitions:
import { createQueueJobsTable } from "@vorsteh-queue/adapter-drizzle"
export const { table: customQueueJobs, schema: customSchema } = createQueueJobsTable(
"custom_queue_jobs", // your custom table name
"custom_schema", // your custom schema name
)
To use the custom table and schema in the adapter, pass the adapter configuration to the PostgresQueueAdapter:
import { PostgresQueueAdapter } from "@vorsteh-queue/adapter-drizzle"
const adapter = new PostgresQueueAdapter(db, {
// Your custom model name ( based on the example above - `table: customQueueJobs`)
modelName: "customQueueJobs",
})
Migration #
To run the migrations, we recommend to use drizzle-kit.
Drizzle Kit is a CLI tool for managing SQL database migrations with Drizzle.