From c26194da4d5974cbdafd8ccedc67d31d0584831c Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Tue, 11 Feb 2025 15:56:43 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Allow=20optional=20TTL=20on=20`dele?= =?UTF-8?q?ted`=20field?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now that `deleted` is a `Date`, we can add a TTL index to it. This change adds an optional `expireAfterSeconds` option, which is passed through to the `deleted` index options if set to a `number`. --- mongodb-queue.ts | 12 ++++++++++-- package.json | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/mongodb-queue.ts b/mongodb-queue.ts index be44b59..ed38b4a 100644 --- a/mongodb-queue.ts +++ b/mongodb-queue.ts @@ -10,7 +10,7 @@ * **/ -import {Collection, Db, Filter, FindOneAndUpdateOptions, ObjectId, Sort, UpdateFilter, WithId} from 'mongodb'; +import {Collection, CreateIndexesOptions, Db, Filter, FindOneAndUpdateOptions, ObjectId, Sort, UpdateFilter, WithId} from 'mongodb'; function now(): string { return (new Date()).toISOString(); @@ -25,6 +25,7 @@ export type QueueOptions = { delay?: number; deadQueue?: MongoDBQueue; maxRetries?: number; + expireAfterSeconds?: number; }; export type AddOptions = { @@ -64,6 +65,7 @@ export class MongoDBQueue { private readonly delay: number; private readonly maxRetries: number; private readonly deadQueue: MongoDBQueue; + private readonly expireAfterSeconds: number; public constructor(db: Db, name: string, opts: QueueOptions = {}) { if (!db) { @@ -76,6 +78,7 @@ export class MongoDBQueue { this.col = db.collection(name); this.visibility = opts.visibility || 30; this.delay = opts.delay || 0; + this.expireAfterSeconds = opts.expireAfterSeconds; if (opts.deadQueue) { this.deadQueue = opts.deadQueue; @@ -84,10 +87,15 @@ export class MongoDBQueue { } public async createIndexes(): Promise { + const deletedOptions: CreateIndexesOptions = {sparse: true}; + if (typeof this.expireAfterSeconds === 'number') { + deletedOptions.expireAfterSeconds = this.expireAfterSeconds; + } + await Promise.all([ this.col.createIndex({visible: 1}, {sparse: true}), this.col.createIndex({ack: 1}, {unique: true, sparse: true}), - this.col.createIndex({deleted: 1}, {sparse: true}), + this.col.createIndex({deleted: 1}, deletedOptions), // Index for efficient counts on in-flight this.col.createIndex({visible: 1, ack: 1}, { diff --git a/package.json b/package.json index e561532..59e6cce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@reedsy/mongodb-queue", - "version": "8.0.0", + "version": "8.1.0", "description": "Message queues which uses MongoDB.", "main": "mongodb-queue.js", "scripts": {