Overview
Lightweight reactive extension to official Node.js MongoDB driver.
Features
- ObjectId mapping. Automatically converts the
_id
field from theObjectId
to astring
. - ️️Reactive. Fires events as a document created, updated, or deleted from the database;
- CUD operations timestamps. Automatically sets
createdOn
,updatedOn
, anddeletedOn
timestamps for CUD operations; - Schema validation. Validates your data before saving;
- Paging. Implements high-level paging API;
- Soft delete. By default, documents don’t remove from the collection, but are marked with the
deletedOn
field; - Extendable. API is easily extendable, you can add new methods or override existing ones;
- Outbox support. node-mongo can create collections with
_outbox
postfix that stores all CUD events for implementing the transactional outbox pattern;
The following example shows some of these features:
Installation
Connect to Database
Usually, you need to define a file called db
that does two things:
- Creates database instance and connects to the database;
- Exposes factory method
createService
to create different Services to work with MongoDB;
Services
Service is a collection wrapper that adds all node-mongo features. Under the hood it uses Node.js MongoDB native methods.
createService
method returns the service instance. It accepts two parameters: collection name and ServiceOptions.
Schema validation
Node-mongo supports any schema library, but we recommend Zod, due to this ability to generate TypeScript types from the schemas.
Zod
Joi
Node-mongo validates documents before save.
Reactivity
The key feature of the node-mongo
is that each create, update or delete operation publishes a CUD event.
${collectionName}.created
${collectionName}.updated
${collectionName}.deleted
Events are used to easily update denormalized data and also to implement complex business logic without tight coupling of different entities.
SDK support two type of events:
In-memory events
- Enabled by default;
- Events can be lost on service failure;
- Events are stored in
eventBus
(Node.js EventEmitter instance); - For handling these events type you will use Events API;
- Designed for transferring events inside a single Node.js process. Events handlers listens node-mongo
eventBus
.
Transactional events
- Can be enabled by setting
{ outbox: true }
when creating a service; - Guarantee that every database write will produce an event;
- Events are stored in special collections with
_outbox
postfix; - For handling these events type you will use
watch
(method for working with Change Streams) on the outbox table; - Designed for transferring events to messages broker like Kafka. Events handlers should listen to message broker events (You need to implement this layer yourself).
On the project start, we recommend using in-memory
events. When your application becomes tougher you should migrate to transactional
events.
Options and Types
ServiceOptions
Option | Description | Default value |
---|---|---|
skipDeletedOnDocs | Skip documents with the deletedOn field | true |
schemaValidator | Validation function that will be called on data save | - |
publishEvents | Publish CUD events on save. | true |
addCreatedOnField | Set the createdOn field to the current timestamp on document creation. | true |
addUpdatedOnField | Set updateOne field to the current timestamp on the document update. | true |
outbox | Use transactional events instead of in-memory events | false |
escapeRegExp | Escape $regex values to prevent special characters from being interpreted as patterns. | false |
collectionOptions | MongoDB CollectionOptions | {} |
collectionCreateOptions | MongoDB CreateCollectionOptions | {} |
CreateConfig
Overrides ServiceOptions
parameters for create operations.
ReadConfig
Overrides ServiceOptions
parameters for read operations.
UpdateConfig
Overrides ServiceOptions
parameters for update operations.
DeleteConfig
Overrides ServiceOptions
parameters for delete operations.
InMemoryEvent
InMemoryEventHandler
OnUpdatedProperties
Extending API
Extending API for a single service.
Extending API for all services.