0.1.2 โ€ข Published 8 months ago

@resilientmq/mongoose-connector v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@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

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

FeatureDescription
setEnvironment(config)Initializes Mongo + RabbitMQ settings
consume()Starts consumer with Mongo-backed storage
publish(event)Publishes using resilient pattern
serializerTransforms event โ†” DB formats
singletonKeeps one shared MongoDB connection

๐Ÿ”ง Config: MongooseConnectorConfig

PropertyTypeRequiredDescription
mongo.uristringโœ…MongoDB URI
mongo.optionsConnectOptionsโŒOptional connection opts
rabbit.consumerOmit<ResilientConsumerConfig, 'store'>โŒConsumer settings
rabbit.consumer.modelModelโŒCustom Mongoose model
rabbit.consumer.serializerEventSerializerโŒCustom serializer
rabbit.publisherOmit<ResilientPublisherConfig, 'store'>โŒPublisher settings
rabbit.publisher.modelModelโŒCustom Mongoose model
rabbit.publisher.serializerEventSerializerโŒ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

๐Ÿ‘ฅ Contributors


๐Ÿ“„ License

MIT

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago