PGMQ- TS 导弹
PGMQ-TS是一个TypeScript库,以PostgreSQL为后端提供消息队列执行. 它相当于pgmq-rs的TypeScript,为在你的Node.js应用程序中执行消息队列提供了一个强而安全的方法.
特征
- 🔒 类型安全: 完整类型Script支持,并附有信件类型的通用软件
- 🎯 简单 API: 队列操作的易用接口
- 📦 PostgreSQL 备份: 收发量 PostgreSQL 的可靠性和ACID 属性
- 🔄 信件可见度: 信件处理的可配置可见度超时
- 📚 信件归档: 已处理信件存档的内置支持
- 🔌 连接集合: 有效的数据库连接管理
安装
npm install @baz-scm/pgmq-ts
# or
yarn add @baz-scm/pgmq-ts
# or
pnpm add @baz-scm/pgmq-ts
快速启动
import { PGMQ } from '@baz-scm/pgmq-ts';
// Initialize PGMQ with your PostgreSQL connection string
const pgmq = new PGMQ('postgresql://user:password@localhost:5432/dbname');
// Create the PGMQ schema and a queue
await pgmq.createSchema();
await pgmq.createQueue('my_queue');
// Define your message type
interface MyMessage {
id: string;
data: {
value: string;
};
}
// Send a message
await pgmq.sendMessage<MyMessage>('my_queue', {
id: '123',
data: {
value: 'Hello PGMQ!'
}
}, 0);
// Read a message (with 60 second visibility timeout)
const message = await pgmq.readMessage<MyMessage>('my_queue', 60);
if (message) {
// Process the message
console.log(message.message); // Access the typed message content
// After processing, either delete or archive the message
await pgmq.deleteMessage('my_queue', message.msgId);
// or
await pgmq.archiveMessage('my_queue', message.msgId);
}
队列操作
创建队列
await pgmq.createQueue('my_queue');
使用队列对象
您也可以为更集中的操作获得一个队列对象:
const queue = pgmq.getQueue('my_queue');
// Read messages from the queue
const message = await queue.readMessage<MyMessage>();
// Delete a message
await queue.deleteMessage(messageId);
// Archive a message
await queue.archiveMessage(messageId);
信件可见度
可见度超时决定了消息在被读取后隐藏的时间. 这使得其他消费者无法在此期间处理同一信息:
// Message will be hidden for 60 seconds after being read
const message = await pgmq.readMessage<MyMessage>('my_queue', 60);
最佳做法
- 总是定义您信件类型的类型脚本接口
- 通过调用处理连接清理
pgmq.end()关闭时 - 根据您的处理需要使用适当的可见度超时
- 考虑归档重要信件而不是删除
- 校验队列名称( 仅定义和下划线字符)
捐款
欢迎捐款! 请随意提交拉力请求.
许可证
这个项目根据麻省理工学院许可证获得许可----详情见LICENSE文件.