Skip to main content
Deno 2 is finally here 🎉️
Learn more

deno-pgmq

Postgres Message Queue (PGMQ) Deno Client Library

https://deno.land/x/pgmq@v0.1.4

Installation

To use deno-pgmq in your Deno project, simply import it directly from the module URL.

For example:

import { Pgmq } from "https://deno.land/x/pgmq@v0.1.4/mod.ts";

Environment Variables (Examples)

DATABASE_URL=postgres://postgres:password@localhost:5432/postgres
MAX_POOL_SIZE=20
LAZY=true

Usage

First, Start a Postgres instance with the PGMQ extension installed:

docker run -d --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 quay.io/tembo/pgmq-pg:latest

Then:

import { Pgmq } from "https://deno.land/x/pgmq@v0.1.4/mod.ts";

console.log("Connecting to Postgres...");
// Specify the connection parameters manually
let pgmq;
try {
  pgmq = await Pgmq.new({
    dsn: "postgresql://postgres:postgres@localhost:54322/postgres", // Supabase local
    lazy: false, // Set lazy loading based on your preference
    maxPoolSize: 20,
  });
} catch (err) {
  console.error("Failed to connect to Postgres", err);
  Deno.exit(1);
}

// You can also use environment variables to set the connection parameters
// $ export DATABASE_URL='postgresql://postgres:postgres@localhost:54322/postgres'
// $ export LAZY=true
// $ export MAX_POOL_SIZE=20
// const pgmq = await Pgmq.new()

const qName = "my_queue";
console.log(`Creating queue ${qName}...`);

await pgmq.queue.create(qName).catch((err) => {
  console.error("Failed to create queue", err);
  Deno.exit(1);
});

interface Msg {
  id: number;
  name: string;
}

const msg: Msg = { id: 1, name: "testMsg" };
console.log("Sending message...");
const msgId = await pgmq.msg.send(qName, msg).catch((err) => {
  console.error("Failed to send message", err);
  Deno.exit(1);
});

const vt = 30;
const receivedMsg = await pgmq.msg.read<Msg>(qName, vt).catch((err) => {
  console.error("No messages in the queue", err);
  Deno.exit(1);
});

console.log("Received message...");
if (receivedMsg) {
  console.dir(receivedMsg.message, { depth: null });
} else {
  console.log("No message received.");
}

console.log("Archiving message...");
await pgmq.msg.archive(qName, msgId).catch((err) => {
  console.error("Failed to archive message", err);
  Deno.exit(1);
});

API

Supported Functionalities

Tasks

The following tasks are available:

$ deno task test
$ deno task lint
$ deno task fmt
$ deno task coverage