MongoDB
There are 2 ways of dealing with the MongoDB. You can use a TypeORM that provides a MongoDB support or Mongoose which is the most popular MongoDB object modelling tool.
If you wanna stay with the TypeORM you can follow these steps.
Otherwise, we'll use a dedicated @nestjs/mongoose package.
Firstly, we need to install all of the required dependencies:
$ npm install --save @nestjs/mongoose mongoose
Once the installation process is completed, we can import the MongooseModule into the root ApplicationModule.
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
@Module({
imports: [MongooseModule.forRoot('mongodb://localhost/nest')],
})
export class ApplicationModule {}
The forRoot() method accepts the same configuration object as mongoose.connect() from the Mongoose package.
Model injection
With Mongoose, everything is derived from a Schema. Let's define the CatSchema:
import * as mongoose from 'mongoose';
export const CatSchema = new mongoose.Schema({
name: String,
age: Number,
breed: String,
});
The CatsSchema belongs to the cats directory.
This directory represents the CatsModule. It's your decision where you gonna keep your schema files. From my point of view, the best way's to hold them nearly their domain, in the appropriate module directory.
Let's have a look at the CatsModule:
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import { CatSchema } from './schemas/cat.schema';
@Module({
imports: [MongooseModule.forFeature([{ name: 'Cat', schema: CatSchema }])],
controllers: [CatsController],
components: [CatsService],
})
export class CatsModule {}
This module uses forFeature() method to define which models shall be registered in the current scope.
Now we can inject the CatModel to the CatsService using the @InjectModel() decorator:
import { Model } from 'mongoose';
import { Component } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Cat } from './interfaces/cat.interface';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatSchema } from './schemas/cat.schema';
@Component()
export class CatsService {
constructor(@InjectModel(CatSchema) private readonly catModel: Model<Cat>) {}
async create(createCatDto: CreateCatDto): Promise<Cat> {
const createdCat = new this.catModel(createCatDto);
return await createdCat.save();
}
async findAll(): Promise<Cat[]> {
return await this.catModel.find().exec();
}
}
import { Model } from 'mongoose';
import { Component, Dependencies } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { CatSchema } from './schemas/cat.schema';
@Component()
@Dependencies(InjectModel(CatSchema))
export class CatsService {
constructor(catModel) {
this.catModel = catModel;
}
async create(createCatDto) {
const createdCat = new this.catModel(createCatDto);
return await createdCat.save();
}
async findAll() {
return await this.catModel.find().exec();
}
}
That's all. The full source code's available here.
Hint Don't forget to import theCatsModuleinto the rootApplicationModule.