MongoDB (Mongoose)

Warning In this article, you’ll learn how to create a DatabaseModule based on the Mongoose package from scratch using custom components. As a consequence, this solution contains a lot of overhead that you can omit using ready to use and available out-of-the-box dedicated @nestjs/mongoose package. To learn more, see here.

Mongoose is the most popular MongoDB object modeling tool.

Getting started

To start the adventure with this library we have to install all required dependencies:

  1. $ npm install --save mongoose
  2. $ npm install --save-dev @types/mongoose
  1. $ npm install --save mongoose

The first step we need to do is to establish the connection with our database using connect() function. The connect() function returns a Promise, and therefore we have to create an async provider.

database.providers.ts

  1. import * as mongoose from 'mongoose';
  2. export const databaseProviders = [
  3. {
  4. provide: 'DATABASE_CONNECTION',
  5. useFactory: (): Promise<typeof mongoose> =>
  6. mongoose.connect('mongodb://localhost/nest'),
  7. },
  8. ];
  1. import * as mongoose from 'mongoose';
  2. export const databaseProviders = [
  3. {
  4. provide: 'DATABASE_CONNECTION',
  5. useFactory: () => mongoose.connect('mongodb://localhost/nest'),
  6. },
  7. ];

Hint Following best practices, we declared the custom provider in the separated file which has a *.providers.ts suffix.

Then, we need to export these providers to make them accessible for the rest part of the application.

database.module.ts

  1. import { Module } from '@nestjs/common';
  2. import { databaseProviders } from './database.providers';
  3. @Module({
  4. providers: [...databaseProviders],
  5. exports: [...databaseProviders],
  6. })
  7. export class DatabaseModule {}

Now we can inject the Connection object using @Inject() decorator. Each class that would depend on the Connection async provider will wait until a Promise is resolved.

Model injection

With Mongoose, everything is derived from a Schema. Let’s define the CatSchema:

schemas/cat.schema.ts

  1. import * as mongoose from 'mongoose';
  2. export const CatSchema = new mongoose.Schema({
  3. name: String,
  4. age: Number,
  5. breed: String,
  6. });

The CatsSchema belongs to the cats directory. This directory represents the CatsModule.

Now it’s time to create a Model provider:

cats.providers.ts

  1. import { Connection } from 'mongoose';
  2. import { CatSchema } from './schemas/cat.schema';
  3. export const catsProviders = [
  4. {
  5. provide: 'CAT_MODEL',
  6. useFactory: (connection: Connection) => connection.model('Cat', CatSchema),
  7. inject: ['DATABASE_CONNECTION'],
  8. },
  9. ];
  1. import { CatSchema } from './schemas/cat.schema';
  2. export const catsProviders = [
  3. {
  4. provide: 'CAT_MODEL',
  5. useFactory: (connection) => connection.model('Cat', CatSchema),
  6. inject: ['DATABASE_CONNECTION'],
  7. },
  8. ];

Notice In the real-world applications you should avoid magic strings. Both CAT_MODEL and DATABASE_CONNECTION should be kept in the separated constants.ts file.

Now we can inject the CAT_MODEL to the CatsService using the @Inject() decorator:

cats.service.ts

  1. import { Model } from 'mongoose';
  2. import { Injectable, Inject } from '@nestjs/common';
  3. import { Cat } from './interfaces/cat.interface';
  4. import { CreateCatDto } from './dto/create-cat.dto';
  5. @Injectable()
  6. export class CatsService {
  7. constructor(
  8. @Inject('CAT_MODEL')
  9. private catModel: Model<Cat>,
  10. ) {}
  11. async create(createCatDto: CreateCatDto): Promise<Cat> {
  12. const createdCat = new this.catModel(createCatDto);
  13. return createdCat.save();
  14. }
  15. async findAll(): Promise<Cat[]> {
  16. return this.catModel.find().exec();
  17. }
  18. }
  1. import { Injectable, Dependencies } from '@nestjs/common';
  2. @Injectable()
  3. @Dependencies('CAT_MODEL')
  4. export class CatsService {
  5. constructor(catModel) {
  6. this.catModel = catModel;
  7. }
  8. async create(createCatDto) {
  9. const createdCat = new this.catModel(createCatDto);
  10. return createdCat.save();
  11. }
  12. async findAll() {
  13. return this.catModel.find().exec();
  14. }
  15. }

In the above example we have used the Cat interface. This interface extends the Document from the mongoose package:

  1. import { Document } from 'mongoose';
  2. export interface Cat extends Document {
  3. readonly name: string;
  4. readonly age: number;
  5. readonly breed: string;
  6. }

The database connection is asynchronous, but Nest makes this process completely invisible for the end-user. The CatModel class is waiting for the db connection, and the CatsService is delayed until model is ready to use. The entire application can start when each class is instantiated.

Here is a final CatsModule:

cats.module.ts

  1. import { Module } from '@nestjs/common';
  2. import { CatsController } from './cats.controller';
  3. import { CatsService } from './cats.service';
  4. import { catsProviders } from './cats.providers';
  5. import { DatabaseModule } from '../database/database.module';
  6. @Module({
  7. imports: [DatabaseModule],
  8. controllers: [CatsController],
  9. providers: [
  10. CatsService,
  11. ...catsProviders,
  12. ],
  13. })
  14. export class CatsModule {}

Hint Do not forget to import the CatsModule into the root ApplicationModule.