Skip to content
Contents

PostgreSQL

Background supports PostgreSQL. PostgreSQL queue and storage relies on the pg package.

Please install it as below.

bash
npm install pg

Example Usage

typescript
import { Pool } from "pg";
import {
  PostgreSQLJobQueue,
  PostgreSQLJobStorage
} from "@backgroundjs/core";

const pool = new Pool({
  connectionString: process.env.POSTGRESQL_URL!,
});

const storage = new PostgreSQLJobStorage(pool, {
  tableName: "jobs",
});
const queue = new PostgreSQLJobQueue(storage, {
  concurrency: 2,
  maxRetries: 3,
  name: "test-queue",
  processingInterval: 1000,
});

queue.register("test-job", async (data) => {
  await new Promise((resolve) => setTimeout(resolve, 3000));
  return data;
});

await queue.add("test-job",{message:"hello from postgresql job"})

await queue.addRepeatable("test-job",{message:"hello from the postgresql repeatable"},{
    every: 1,
    unit: "seconds",
})

const scheduledTime = new Date(Date.now() + 1000 * 60);

await queue.schedule("test-job",{message:"scheduled job"},scheduledTime)