@betsys-nestjs/mongo
v5.0.0
Published
Mongo library for NestJS.
Downloads
8
Maintainers
Keywords
Readme
Mongo library
This library is responsible for handling MongoDB connections and queries. It contains connection, get collection logic and open connection monitoring.
Environment variables
This library consumes the following environment variables.
| Variable name | Description |
|----------------------------------|-------------------|
| CONFIG_MONGO_CURSOR_BATCH_SIZE
| Cursor batch size |
Dependencies
| Package | Version | | ---------------- | ------- | | bson | ^4.0.0 | | mongodb | ^4.0.0 | | @nestjs/core | ^10.0.0 | | @nestjs/common | ^10.0.0 | | @nestjs/config | ^3.0.0 | | reflect-metadata | <1.0.0 | | rxjs | ^7.0.0 |
Usage
To register new MongoDB connection just add this to your module imports and provide config as a first parameter:
import { Module } from '@nestjs/common';
import { MongoModule } from '@betsys-nestjs/mongo';
@Module({
imports: [
MongoModule.forFeature({
connectionConfig: {
URI: 'mongodb://localhost:27017/',
poolSize: 10,
},
batchSize: 10,
}),
],
})
class InfrastructureModule {
}
And to use the connection just import MongoConnection
provider:
import { Injectable } from '@nestjs/common';
import { InjectConnectionProvider, MongoConnection } from '@betsys-nestjs/mongo';
@Injectable()
export class DoSomethingWithConnectionOperation {
constructor(
@InjectConnectionProvider()
private readonly mongoConnection: MongoConnection,
) {
}
async doSomethingWithCollection(): Promise<void> {
const connection = this.mongoConnection.getConnection();
...
await this.mongoConnection.closeConnection();
}
}
Or use directly MongoCollection
provider:
import { Injectable } from '@nestjs/common';
import {
InjectCollectionProvider,
MongoCollectionProviderInterface,
} from '@betsys-nestjs/mongo';
interface DummyDocument {
id: number;
usefulInformation: string;
}
@Injectable()
export class DoSomethingWithCollection {
constructor(
@InjectCollectionProvider()
private readonly collectionProvider: MongoCollectionProviderInterface
) {
}
async getDocument(id: number): Promise<DummyDocument | null> {
const collection = await this.collectionProvider
.getCollection<DummyDocument>();
return collection.findOne({ id });
}
}
Or to load batches of documents, use MongoCursorUtils
:
import { Injectable } from '@nestjs/common';
import {
InjectCollectionProvider,
InjectCursorUtils,
MongoCollectionProviderInterface,
} from '@betsys-nestjs/mongo';
interface DummyDocument {
id: number;
usefulInformation: string;
}
@Injectable()
export class LoadMultipleDocuments {
constructor(
@InjectCursorUtils()
private readonly cursorUtils: MongoCursorUtils,
@InjectCollectionProvider()
private readonly collectionProvider: MongoCollectionProviderInterface,
) {
}
async getAllDocuments(): Promise<DummyDocument> {
const cursor = this.cursorUtils.queryCursor(
async () => this.collectionProvider.getCollection<DummyDocument>,
{}
);
while (cursor.hasNext()) {
const doc = await cursor.next();
...
}
}
}
Multiple connection support
If you want to connect to multiple instances to MongoDB, use different handles:
import { Module } from '@nestjs/common';
import { MongoModule } from '@betsys-nestjs/mongo';
@Module({
imports: [
MongoModule.forFeature(mongoConfig1(), 'MONGO1'),
MongoModule.forFeature(mongoConfig2(), 'MONGO2'),
],
})
class InfrastructureModule {
}