@universal-packages/mongodb-queue
v1.1.0
Published
MongoDB queue system
Downloads
11
Readme
MongoDB Queue
MongoDB queue system for mongodb.
Install
npm install @universal-packages/mongodb-queue
npm install mongodbMongoQueue
An instance of MongoQueue allows you to start enqueuing items to be retrieved later from anywhere and at the right time.
import { MongoQueue } from '@universal-packages/mongodb-queue'
const mongoQueue = new MongoQueue({ identifier: 'my-queue', url: 'mongodb://localhost:27017/my-app' })
await mongoQueue.connect()
await mongoQueue.enqueue({ job: 'update-users', which: 'all' }, 'normal')
/// Later
const item = await mongoQueue.dequeue('normal)
console.log(item.payload)
// > { job: 'update-users', which: 'all' }
Options
MongoQueue takes the same options as the mongodb client.
Additionally takes the following ones:
clientMongoClientIf you already have a client working in your app you can pass the instance here to not connect another client inside theMongoQueueinstance.identifierStringThis will be used as the collection name to store the queue items.
Instance methods
connect()
If you are not passing your own client in options you will need to tell the MongoQueue to connect its internal client.
await mongoQueue.connect()disconnect()
If you are not passing your own client in options you will need to tell the MongoQueue to disconnect its internal client when you no logger need it.
await mongoQueue.disconnect()enqueue(payload: Object, queue: string, [options])
Enqueues a payload with the given queue. Return the metadata related to the enqueued item.
await mongoQueue.enqueue({ data: '❨╯°□°❩╯︵┻━┻' }, 'low')
await mongoQueue.enqueue({ data: '❨╯°□°❩╯︵┻━┻' }, 'normal')
await mongoQueue.enqueue({ data: '❨╯°□°❩╯︵┻━┻' }, 'high')
await mongoQueue.enqueue({ data: '❨╯°□°❩╯︵┻━┻' }, 'whatever')
const item = await mongoQueue.enqueue({ data: '❨╯°□°❩╯︵┻━┻' }, 'normal')
console.log(item)
// > { dequeueAfter: 56462165
// > enqueuedAt: 56462165
// > id: 'complex-id'
// > payload: { data: '❨╯°□°❩╯︵┻━┻' },
// > queue: 'normal' }options
You can also pass options related to where an item should be available to dequeue. At takes priority over wait.
atDateThe item will not be available before this date.waitStringexpressed with human language like2 hoursor1 dayand the enqueue item will not be available before current time plus wait time.
await mongoQueue.enqueue({ data: '❨╯°□°❩╯︵┻━┻' }, 'normal', {})dequeue(queue: string)
Dequeues an item from the given queue if it is ready to be dequeued.
await mongoQueue.dequeue('low')
await mongoQueue.dequeue('normal')
await mongoQueue.dequeue('high')
await mongoQueue.dequeue('whatever')
const item = await mongoQueue.dequeue('normal')
console.log(item)
// > { dequeueAfter: 56462165
// > enqueuedAt: 56462165
// > id: 'complex-id'
// > payload: { data: '❨╯°□°❩╯︵┻━┻' },
// > queue: 'normal' }Typescript
This library is developed in TypeScript and shipped fully typed.
Contributing
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
