1.1.0 • Published 5 months ago
@quazex/nestjs-mongodb v1.1.0
NestJS MongoDB Module
Core features:
- Based on official MongoDB client for NodeJS;
 - Covered with unit and e2e tests;
 - Basic module without unnecessary boilerplate.
 
Installation
To install the package, run:
npm install @quazex/nestjs-mongodb mongodbUsage
Importing the Module
To use the MongoDB module in your NestJS application, import it into your root module (e.g., AppModule).
import { Module } from '@nestjs/common';
import { MongoModule } from '@quazex/nestjs-mongodb';
@Module({
    imports: [
        MongoModule.forRoot({
            url: 'mongodb://localhost:27017',
        }),
        MongoModule.forCollection('some_col'),
    ],
})
export class AppModule {}Using Mongo connection
Once the module is registered, you can inject instances of the MongoClient, Db or Collection into your providers:
import { Injectable } from '@nestjs/common';
import { InjectMongoClient, InjectMongoDatabase, InjectMongoCollection } from '@quazex/nestjs-mongodb';
import { Collection, Db, Document, MongoClient, ObjectId, WithId } from 'mongodb';
@Injectable()
export class CollectionService {
    constructor(
        @InjectMongoClient() private readonly client: MongoClient,
        @InjectMongoDatabase() private readonly database: Db,
        @InjectMongoCollection('some_col') private readonly collection: Collection,
    ) {}
    async insert(document: WithId<Document>) {
        await this.collection.insertOne(document);
    }
    async findOne(_id: ObjectId) {
        return this.collection.findOne({
            _id,
        });
    }
}Async Configuration
If you need dynamic configuration, use forRootAsync:
import { Module } from '@nestjs/common';
import { MongoModule } from '@quazex/nestjs-mongodb';
@Module({
    imports: [
        MongoModule.forRootAsync({
            useFactory: async (config: SomeConfigProvider) => ({
                url: config.MONGODB_URL,
            }),
            inject: [
                SomeConfigProvider,
            ],
        }),
    ],
})
export class AppModule {}Connection and graceful shutdown
By default, this module doesn't manage client connection on application bootstrap or shutdown. You can read more about lifecycle hooks on the NestJS documentation page.
// main.ts
const app = await NestFactory.create(AppModule);
// Starts listening for shutdown hooks
app.enableShutdownHooks(); // <<<
await app.listen(process.env.PORT ?? 3000);// app.bootstrap.ts
import { Injectable, OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
import { InjectMongoClient } from '@quazex/nestjs-mongodb';
import { MongoClient } from 'mongodb';
@Injectable()
export class AppBootstrap implements OnApplicationBootstrap, OnApplicationShutdown {
    constructor(@InjectMongoClient() private readonly client: MongoClient) {}
    public async onApplicationBootstrap(): Promise<void> {
        await this.client.connect();
    }
    public async onApplicationShutdown(): Promise<void> {
        await this.client.close();
    }
}License
MIT