Postgres Message Queue (PGMQ) Deno Client Library
https://deno.land/x/[email protected]
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/[email protected]/mod.ts";
Due to a Deno bug, please make sure that you are using Deno 1.44.1 or later. If you are using this library with Supabase edge functions, make sure that the reported runtime compatibility matches this (example below):
Using supabase-edge-runtime-1.58.3 (compatible with Deno v1.45.2)
DATABASE_URL=postgres://postgres:password@localhost:5432/postgres
MAX_POOL_SIZE=20
LAZY=true
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/[email protected]/mod.ts";
console.log("Connecting to Postgres...");
// Specify the connection parameters manually
let pgmq: Pgmq;
try {
pgmq = await Pgmq.new({
dsn:
"postgresql://postgres:[email protected]:54322/postgres?sslmode=require",
// Optional parameters
caFilePaths: ["./certs/prod-ca-2021.crt"],
});
} 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);
});
The following tasks are available:
$ deno task test
$ deno task lint
$ deno task fmt
$ deno task coverage