0.1.2 โข Published 8 months ago
@resilientmq/mongoose-connector v0.1.2
@resilientmq/mongoose-connector
Mongoose connector for ResilientMQ, enabling seamless integration with MongoDB using Mongoose. Handles event storage, status tracking, and serializer support out-of-the-box.
Table of Contents
- ๐ฆ Installation
- ๐ Purpose
- ๐งฉ Main Concepts
- ๐ง Config: MongooseConnectorConfig
- ๐งฉ Custom Event Storage Format
- ๐ Example: Consumer
- ๐ Example: Publisher
- ๐งช Tests
- Docs
- LICENSE
๐ฆ Installation
npm install @resilientmq/mongoose-connector๐ Purpose
This package acts as a wrapper for the ResilientMQ core logic and provides MongoDB-backed event persistence.
- Automatically injects Mongoose-based
EventStore - Manages a singleton DB connection
- Allows full schema customization via serializer
๐งฉ Main Concepts
| Feature | Description |
|---|---|
setEnvironment(config) | Initializes Mongo + RabbitMQ settings |
consume() | Starts consumer with Mongo-backed storage |
publish(event) | Publishes using resilient pattern |
serializer | Transforms event โ DB formats |
singleton | Keeps one shared MongoDB connection |
๐ง Config: MongooseConnectorConfig
| Property | Type | Required | Description |
|---|---|---|---|
mongo.uri | string | โ | MongoDB URI |
mongo.options | ConnectOptions | โ | Optional connection opts |
rabbit.consumer | Omit<ResilientConsumerConfig, 'store'> | โ | Consumer settings |
rabbit.consumer.model | Model | โ | Custom Mongoose model |
rabbit.consumer.serializer | EventSerializer | โ | Custom serializer |
rabbit.publisher | Omit<ResilientPublisherConfig, 'store'> | โ | Publisher settings |
rabbit.publisher.model | Model | โ | Custom Mongoose model |
rabbit.publisher.serializer | EventSerializer | โ | Custom serializer |
logLevel | 'none' \| 'warn' \| 'info' \| 'error' | โ | Logger verbosity |
๐งฉ Custom Event Storage Format
Supports pluggable serializers to convert the event to your preferred DB structure.
๐ Example: Custom Storage Serializer
const serializer = {
toStorageFormat(event) {
return {
_id: event.id,
body: event.payload,
customStatus: event.status
};
},
fromStorageFormat(doc) {
return {
id: doc._id,
messageId: doc._id,
payload: doc.body,
status: doc.customStatus,
type: 'custom.type'
};
},
getStatusField() {
return 'customStatus';
}
};๐ Example: Consumer
import { setEnvironment, consume } from '@resilientmq/mongoose-connector';
await setEnvironment({
mongo: {
uri: 'mongodb://localhost:27017/events'
},
rabbit: {
consumer: {
connection: 'amqp://localhost',
consumeQueue: {
queue: 'my.queue',
options: { durable: true }
},
eventsToProcess: [
{ type: 'my.event', handler: async (payload) => console.log(payload) }
]
},
publisher: {
connection: 'amqp://localhost'
}
}
});
await consume();๐ Example: Publisher
import { publish } from '@resilientmq/mongoose-connector';
await publish({
id: 'evt-1',
messageId: 'msg-1',
type: 'user.created',
payload: { name: 'Alice' },
status: 'PENDING_PUBLICATION'
});๐งช Tests
- โ Unit tested
- โ Uses Jest + mocks
- โ
Compatible with
jest --coverage