npm i @nestjs/mongoose mongoose
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
@Module({
imports: [MongooseModule.forRoot(process.env.MONGODB_URL)],
})
export class AppModule {}
process.env
는 프로젝트 최상단에 .env
파일에 정의한 환경변수 값을 로딩MONGODB_URL=mongodb://my_user:my_pw@127.0.0.1:27017/my_db
MONGODB_URL=mongodb://my_user:my_pw@remote_host1:27017,remote_host2:27017/my_db
@Schema
데코레이터를 통해 Schema 지정collection
을 지정하지 않을시, 클래스명에 s 붙인 값이 기본으로 지정@Prop
데코레이터로 MongoDB 에 매핑할 필드 지정@Prop
데코레이터가 없을 경우 MongoDB 에 매핑되지 않음@Prop({ required: true })
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';
export type CatDocument = HydratedDocument<Cat>;
@Schema({ collection: 'cats' })
export class Cat {
@Prop({ required: true })
name: string;
@Prop()
age: number;
@Prop()
breed: string;
}
export const CatSchema = SchemaFactory.createForClass(Cat);
@Prop
으로 import * as mongoose from 'mongoose';
import { Owner } from '../owners/schemas/owner.schema';
// inside the class definition
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Owner' })
owner: Owner;
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import { Cat, CatSchema } from './schemas/cat.schema';
@Module({
imports: [MongooseModule.forFeature([{ name: Cat.name, schema: CatSchema }])],
controllers: [CatsController],
providers: [CatsService],
})
export class CatsModule {}
@InjectModel
데코레이터를 이용해 Service 에 Model 을 injectionimport { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Cat, CatDocument } from './schemas/cat.schema';
import { CreateCatDto } from './dto/create-cat.dto';
@Injectable()
export class CatsService {
constructor(@InjectModel(Cat.name) private catModel: Model<CatDocument>) {}
async create(createCatDto: CreateCatDto): Promise<Cat> {
const createdCat = new this.catModel(createCatDto);
return createdCat.save();
}
async findAll(): Promise<Cat[]> {
return this.catModel.find().exec();
}
}
describe('CatsService', () => {
let service: CatsService
let model: Model<Cat>
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ConfigService,
// Model 주입.
{ provide: getModelToken(Cat.name), useClass: Cat },
],
})
.compile()
service = module.get<CatsService>(CatsService)
model = module.get<Model<CatsDto>>(getModelToken(Cat.name))