|
| 1 | +# @vorsteh-queue/adapter-drizzle |
| 2 | + |
| 3 | +Drizzle ORM adapter for Vorsteh Queue supporting PostgreSQL databases. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **PostgreSQL Support**: Full PostgreSQL compatibility with PGlite, node-postgres, and postgres.js |
| 8 | +- **Type Safety**: Full TypeScript support with Drizzle ORM |
| 9 | +- **SKIP LOCKED**: Concurrent job processing without lock contention |
| 10 | +- **JSON Payloads**: Complex data structures with proper serialization |
| 11 | +- **UTC-First**: All timestamps stored as UTC for reliable timezone handling |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +- **Node.js 20+** |
| 16 | +- **PostgreSQL 12+** (for SKIP LOCKED support) |
| 17 | +- **ESM only** - This package is ESM-only and cannot be imported with `require()` |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install @vorsteh-queue/adapter-drizzle drizzle-orm |
| 23 | +# or |
| 24 | +pnpm add @vorsteh-queue/adapter-drizzle drizzle-orm |
| 25 | +``` |
| 26 | + |
| 27 | +> **Note**: Make sure your project has `"type": "module"` in package.json or use `.mjs` file extensions. |
| 28 | +
|
| 29 | +## Usage |
| 30 | + |
| 31 | +```typescript |
| 32 | +import { drizzle } from "drizzle-orm/node-postgres" |
| 33 | +import { Pool } from "pg" |
| 34 | + |
| 35 | +import { PostgresQueueAdapter } from "@vorsteh-queue/adapter-drizzle" |
| 36 | +import { Queue } from "@vorsteh-queue/core" |
| 37 | + |
| 38 | +// Setup PostgreSQL connection |
| 39 | +const pool = new Pool({ |
| 40 | + connectionString: "postgresql://user:password@localhost:5432/database", |
| 41 | +}) |
| 42 | +const db = drizzle(pool) |
| 43 | + |
| 44 | +interface EmailPayload { |
| 45 | + to: string |
| 46 | + subject: string |
| 47 | + body: string |
| 48 | +} |
| 49 | + |
| 50 | +interface EmailResult { |
| 51 | + messageId: string |
| 52 | + sent: boolean |
| 53 | +} |
| 54 | + |
| 55 | +// Create adapter and queue |
| 56 | +const adapter = new PostgresQueueAdapter(db) |
| 57 | +const queue = new Queue(adapter, { name: "my-queue" }) |
| 58 | + |
| 59 | +// Register job handlers |
| 60 | +queue.register<EmailPayload, EmailResult>("send-email", async (job) => { |
| 61 | + console.log(`Sending email to ${job.payload.to}`) |
| 62 | + |
| 63 | + // Send email logic here |
| 64 | + await sendEmail(job.payload) |
| 65 | + |
| 66 | + return { |
| 67 | + messageId: "msg_123", |
| 68 | + sent: true, |
| 69 | + } |
| 70 | +}) |
| 71 | + |
| 72 | +// Add jobs |
| 73 | +await queue.add("send-email", { |
| 74 | + |
| 75 | + subject: "Welcome!", |
| 76 | + body: "Welcome to our service!", |
| 77 | +}) |
| 78 | + |
| 79 | +// Start processing |
| 80 | +queue.start() |
| 81 | +``` |
| 82 | + |
| 83 | +## Schema Setup |
| 84 | + |
| 85 | +### Using Drizzle Schemas (Recommended) |
| 86 | + |
| 87 | +```typescript |
| 88 | +import { drizzle } from "drizzle-orm/node-postgres" |
| 89 | + |
| 90 | +import { postgresSchema } from "@vorsteh-queue/adapter-drizzle" |
| 91 | + |
| 92 | +const db = drizzle(pool, { schema: postgresSchema }) |
| 93 | +``` |
| 94 | + |
| 95 | +### Using Drizzle Kit Migrations |
| 96 | + |
| 97 | +```typescript |
| 98 | +// src/schema/index.ts - Your application schema |
| 99 | +import { pgTable, serial, varchar } from "drizzle-orm/pg-core" |
| 100 | + |
| 101 | +import { postgresSchema } from "@vorsteh-queue/adapter-drizzle" |
| 102 | + |
| 103 | +// Your existing tables |
| 104 | +export const users = pgTable("users", { |
| 105 | + id: serial("id").primaryKey(), |
| 106 | + name: varchar("name", { length: 255 }), |
| 107 | +}) |
| 108 | + |
| 109 | +// Export queue schema alongside your schema |
| 110 | +export const queueJobs = postgresSchema.queueJobs |
| 111 | +``` |
| 112 | + |
| 113 | +```bash |
| 114 | +# Generate and run migrations |
| 115 | +npx drizzle-kit generate |
| 116 | +npx drizzle-kit migrate |
| 117 | +``` |
| 118 | + |
| 119 | +## Supported PostgreSQL Drivers |
| 120 | + |
| 121 | +- **node-postgres** (`pg`) |
| 122 | +- **postgres.js** (`postgres`) |
| 123 | +- **PGlite** (for embedded/testing) |
| 124 | + |
| 125 | +## Testing |
| 126 | + |
| 127 | +```bash |
| 128 | +pnpm test |
| 129 | +``` |
| 130 | + |
| 131 | +## License |
| 132 | + |
| 133 | +MIT License - see LICENSE file for details. |
0 commit comments