Registry / database / ts-mongo-queue

ts-mongo-queue

JSON →
library1.5.1jsnpmunverified

A TypeScript queue implementation backed by MongoDB, built from mongodb-queue. Current version 1.5.1 wraps MongoDB Node driver v6 and supports MongoDB 7. Offers delayed messages, visibility timeout (default 30s), max retries, dead-letter queue, and configurable recursion limit via environment variable. Designed for Node.js and TypeScript projects needing a simple, persistent message queue without additional infrastructure. Release cadence: as needed; last update February 2024.

npm install ts-mongo-queue
INSTALL
IMPORT
SIG · TS-MONGO-QUEUE
T
ts-mongo-queue
databasejavascriptv1.5.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MongoQueue
import { MongoQueue } from 'ts-mongo-queue'
import MongoQueue from 'ts-mongo-queue'
Named export, not default. CommonJS: const { MongoQueue } = require('ts-mongo-queue').
QueueOptions
import { QueueOptions } from 'ts-mongo-queue'
TypeScript type for the optional 'opts' parameter. Not needed at runtime.
MongoQueue (as constructor-like function)
const queue = MongoQueue(client, 'queueName', options)
const queue = new MongoQueue(client, 'queueName')
MongoQueue is a function, not a class. Do NOT use the 'new' keyword.

Shows connecting to MongoDB, creating a queue, adding a message, getting it, and acknowledging removal.

import { MongoClient } from 'mongodb'; import { MongoQueue } from 'ts-mongo-queue'; async function main() { const uri = process.env.MONGO_URI ?? 'mongodb://localhost:27017'; const client = new MongoClient(uri); await client.connect(); const queue = MongoQueue(client, 'my-queue', { visibility: 60, delay: 0 }); // Add a message const addResult = await queue.add({ task: 'do_something' }); console.log('Added message ID:', addResult.messageId); // Fetch a message const msg = await queue.get(); if (msg) { console.log('Received payload:', msg.payload); // Acknowledge and delete await queue.ack(msg.ack); } await client.close(); } main().catch(console.error);
Debug
Known issues
breakingDo not use 'new MongoQueue()'. MongoQueue is a function, not a class.
fix
Use MongoQueue(client, name, opts) without 'new'.
affects: all
gotchaThe 'visibility' option controls how long a message is hidden after being fetched. If you don't call ack or requeue within that window, the message becomes visible again and may be processed by another consumer.
fix
Ensure your processing completes within visibility timeout or call requeue to defer.
affects: all
deprecatedPreviously, the queue name was passed as the second argument; this is still supported but not documented in the README.
fix
Always pass name as second argument: MongoQueue(client, 'myqueue').
affects: >=1.0.0
gotchaThe library does not create a TTL index on the added date. Over time, old messages will accumulate unless you manually clean up or use a capped collection.
fix
Consider setting a TTL index on the 'createdAt' field in your MongoDB collection, or use a capped collection.
affects: all
Errors
Common errors & fixes
TypeError: MongoQueue is not a constructor
Using 'new MongoQueue()' instead of calling MongoQueue as a function.
fix
Change to: const queue = MongoQueue(client, 'queueName');
Error: Cannot find module 'mongodb'
Missing peer dependency 'mongodb'.
fix
Run: npm install mongodb
MongoServerError: E11000 duplicate key error collection: test.queues index: _id_ dup key
Two messages with the same _id inserted. May happen if you manually set _id and there's a race condition.
fix
Let the library generate _id automatically by omitting it from payload.
Upgrade
Version history
1.5.1latest on npm
Audit
Dependencies
mongodbrequiredMongoDB driver is required to connect and interact with MongoDB.
Agent activity
19 hits · last 30 days
node
16
Meta
2
OpenAI (training)
1
Resources