The Prisma adapter provides PostgreSQL support using Prisma ORM, offering generated types, migrations, and excellent developer tooling.
Installation #
npm install @vorsteh-queue/core @vorsteh-queue/adapter-prisma"pnpm add @vorsteh-queue/core @vorsteh-queue/adapter-prisma"yarn add @vorsteh-queue/core @vorsteh-queue/adapter-prisma"bun add @vorsteh-queue/core @vorsteh-queue/adapter-prisma"Quick Start #
With prisma-client-js #
import { PrismaClient } from "@prisma/client"
import { PostgresPrismaQueueAdapter } from "@vorsteh-queue/adapter-prisma"
import { Queue } from "@vorsteh-queue/core"
const prisma = new PrismaClient()
const adapter = new PostgresPrismaQueueAdapter(prisma)
const queue = new Queue(adapter)
With prisma-client #
import { PrismaPg } from "@prisma/adapter-pg"
import { PrismaClient } from "src/generated/prisma/client"
import { PostgresPrismaQueueAdapter } from "@vorsteh-queue/adapter-prisma"
import { Queue } from "@vorsteh-queue/core"
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
const prisma = new PrismaClient({ adapter })
const adapter = new PostgresPrismaQueueAdapter(prisma)
const queue = new Queue(adapter)
Database Setup #
Schema #
Add the queue job model to your schema.prisma:
prisma/schema.prisma
generator client {
// Required
provider = "prisma-client"
output = "../src/generated/prisma"
// Optional, but required if you're using an older prisma version
// previewFeatures = ["queryCompiler", "driverAdapters"]
// Optional
runtime = "nodejs"
moduleFormat = "esm"
generatedFileExtension = "ts"
importFileExtension = "ts"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model QueueJob {
id String @id @default(uuid())
queueName String @map("queue_name") @db.VarChar(255)
name String @db.VarChar(255)
payload Json @db.JsonB
status String @db.VarChar(50)
priority Int
attempts Int @default(0)
maxAttempts Int @map("max_attempts")
createdAt DateTime @default(dbgenerated("timezone('utc', now())")) @map("created_at") @db.Timestamptz(6)
processAt DateTime @map("process_at") @db.Timestamptz(6)
processedAt DateTime? @map("processed_at") @db.Timestamptz(6)
completedAt DateTime? @map("completed_at") @db.Timestamptz(6)
failedAt DateTime? @map("failed_at") @db.Timestamptz(6)
error Json? @db.JsonB
result Json? @db.JsonB
progress Int? @default(0)
timeout Json? @db.JsonB
cron String? @db.VarChar(255)
repeatEvery Int? @map("repeat_every")
repeatLimit Int? @map("repeat_limit")
repeatCount Int? @default(0) @map("repeat_count")
@@index([queueName, status, priority, createdAt], map: "idx_queue_jobs_status_priority")
@@index([processAt], map: "idx_queue_jobs_process_at")
@@map("queue_jobs")
}Custom Table & Schema Names #
To use the custom table and schema with prisma, you have to customize the prisma schema. Here an example with custom schema and table name ( the relevant parts are highlighted ):
prisma/schema.prisma
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
generator client {
// Required
provider = "prisma-client"
output = "../src/generated/prisma"
// Optional
runtime = "nodejs"
moduleFormat = "esm"
generatedFileExtension = "ts"
importFileExtension = "ts"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
schemas = ["public", "custom_schema"]
}
model CustomQueueJob {
id String @id @default(uuid())
queueName String @map("queue_name") @db.VarChar(255)
name String @db.VarChar(255)
payload Json @db.JsonB
status String @db.VarChar(50)
priority Int
attempts Int @default(0)
maxAttempts Int @map("max_attempts")
createdAt DateTime @default(dbgenerated("timezone('utc', now())")) @map("created_at") @db.Timestamptz(6)
processAt DateTime @map("process_at") @db.Timestamptz(6)
processedAt DateTime? @map("processed_at") @db.Timestamptz(6)
completedAt DateTime? @map("completed_at") @db.Timestamptz(6)
failedAt DateTime? @map("failed_at") @db.Timestamptz(6)
error Json? @db.JsonB
result Json? @db.JsonB
progress Int? @default(0)
timeout Json? @db.JsonB
cron String? @db.VarChar(255)
repeatEvery Int? @map("repeat_every")
repeatLimit Int? @map("repeat_limit")
repeatCount Int? @default(0) @map("repeat_count")
@@index([queueName, status, priority, createdAt], map: "idx_custom_queue_jobs_status_priority")
@@index([processAt], map: "idx_custom_queue_jobs_process_at")
@@map("custom_queue_jobs")
@@schema("custom_schema")
}To use the custom table and schema in the adapter, pass the adapter configuration to the PostgresQueueAdapter:
import { PrismaPg } from "@prisma/adapter-pg"
import { PrismaClient } from "src/generated/prisma/client"
import { PostgresPrismaQueueAdapter } from "@vorsteh-queue/adapter-prisma"
import { Queue } from "@vorsteh-queue/core"
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
const prisma = new PrismaClient({ adapter })
const adapter = new PostgresQueueAdapter(prisma, {
// Your custom model name ( based on the example above )
modelName: "CustomQueueJobs",
// Your custom schema name
schemaName: "custom_schema",
// Your custom table name
tableName: "custom_queue_jobs",
})
const queue = new Queue(adapter)
Want to see it in action?
Checkout our prisma example with custom schema and
table.
Migration #
To run the migrations, we recommend to use the prisma cli.
The Prisma command line interface (CLI) is the primary way to interact with your Prisma project from the command line.