MongoDB (Mongoose)
κ²½κ³ μ΄ κΈ°μ¬μμλ 컀μ€ν
μ»΄ν¬λνΈλ₯Ό μ¬μ©νμ¬ μ²μλΆν° Mongoose* ν¨ν€μ§λ₯Ό κΈ°λ°μΌλ‘ DatabaseModule
μ μμ±νλ λ°©λ²μ νμ΅ν©λλ€. κ²°κ³Όμ μΌλ‘ μ΄ μ루μ
μλ μ¦μ μ¬μ© κ°λ₯ν μ μ© @nestjs/mongoose
ν¨ν€μ§λ₯Ό μ¬μ©νμ¬ μλ΅ν μμλ λ§μ μ€λ² ν€λκ° ν¬ν¨λμ΄ μμ΅λλ€. μμΈν λ΄μ©μ μ¬κΈ°λ₯Ό μ°Έμ‘°νμμμ€.
Mongooseμ κ°μ₯ μΈκΈ°μλ MongoDB κ°μ²΄ λͺ¨λΈλ§ λꡬμ
λλ€.
Getting started
μ΄ λΌμ΄λΈλ¬λ¦¬λ‘ λͺ¨νμ μμνλ €λ©΄ νμν λͺ¨λ μ’
μμ±μ μ€μΉν΄μΌν©λλ€.
@@filename()
$ npm install --save mongoose
$ npm install --save-dev @types/mongoose
@@switch
$ npm install --save mongoose
κ°μ₯ λ¨Όμ ν΄μΌ ν μΌμ connect()
ν¨μλ₯Ό μ¬μ©νμ¬ λ°μ΄ν°λ² μ΄μ€μ μ°κ²°νλ κ²μ
λλ€. connect()
ν¨μλ Promise
λ₯Ό λ°ννλ―λ‘ async providerλ₯Ό λ§λ€μ΄μΌ ν©λλ€.
@@filename(database.providers)
import * as mongoose from 'mongoose';
export const databaseProviders = [
{
provide: 'DATABASE_CONNECTION',
useFactory: (): Promise<typeof mongoose> =>
mongoose.connect('mongodb://localhost/nest'),
},
];
@@switch
import * as mongoose from 'mongoose';
export const databaseProviders = [
{
provide: 'DATABASE_CONNECTION',
useFactory: () => mongoose.connect('mongodb://localhost/nest'),
},
];
info ννΈ λͺ¨λ² μ¬λ‘μ λ°λΌ * .providers.ts
μ λ―Έμ¬κ° μλ λ³λμ νμΌλ‘ μ¬μ©μ μ§μ 곡κΈμλ₯Ό μ μΈνμ΅λλ€.
κ·Έλ° λ€μ μμ© νλ‘κ·Έλ¨μ λλ¨Έμ§ λΆλΆμ λν΄ μ‘μΈμ€ν μ μλλ‘ μ΄λ¬ν 곡κΈμλ₯Ό λ΄ λ³΄λ΄μΌν©λλ€.
@@filename(database.module)
import { Module } from '@nestjs/common';
import { databaseProviders } from './database.providers';
@Module({
providers: [...databaseProviders],
exports: [...databaseProviders],
})
export class DatabaseModule {}
μ΄μ @Inject()
λ°μ½λ μ΄ν°λ₯Ό μ¬μ©νμ¬ Connection
κ°μ²΄λ₯Ό μ£Όμ
ν μ μμ΅λλ€. Connection
λΉλκΈ° 곡κΈμμ μμ‘΄νλ κ° ν΄λμ€λ Promise
κ° ν΄κ²° λ λκΉμ§ κΈ°λ€λ¦½λλ€.
Model injection
Mongooseλ₯Ό μ¬μ©νλ©΄ λͺ¨λ κ²μ΄ μ€ν€λ§μμ νμλ©λλ€. CatSchema
λ₯Ό μ μ ν΄ λ΄
μλ€ :
@@filename(schemas/cats.schema)
import * as mongoose from 'mongoose';
export const CatSchema = new mongoose.Schema({
name: String,
age: Number,
breed: String,
});
CatsSchema
λ cats
λλ ν 리μ μν©λλ€. μ΄ λλ ν 리λ CatsModule
μ λνλ
λλ€.
μ΄μ λͺ¨λΈ 곡κΈμλ₯Ό λ§λ€ μ°¨λ‘μ
λλ€.
@@filename(cats.providers)
import { Connection } from 'mongoose';
import { CatSchema } from './schemas/cat.schema';
export const catsProviders = [
{
provide: 'CAT_MODEL',
useFactory: (connection: Connection) => connection.model('Cat', CatSchema),
inject: ['DATABASE_CONNECTION'],
},
];
@@switch
import { CatSchema } from './schemas/cat.schema';
export const catsProviders = [
{
provide: 'CAT_MODEL',
useFactory: (connection) => connection.model('Cat', CatSchema),
inject: ['DATABASE_CONNECTION'],
},
];
μλ¦Ό μ€μ μμ© νλ‘κ·Έλ¨μμλ λ§€μ§ λ¬Έμμ΄μ νΌν΄μΌν©λλ€. CAT_MODEL
κ³Ό DATABASE_CONNECTION
μ λΆλ¦¬ λ constants.ts
νμΌμ 보κ΄ν΄μΌ ν©λλ€.
μ΄μ @Inject()
λ°μ½λ μ΄ν°λ₯Ό μ¬μ©νμ¬ CAT_MODEL
μ CatsService
μ μ½μ
ν μ μμ΅λλ€ :
@@filename(cats.service)
import { Model } from 'mongoose';
import { Injectable, Inject } from '@nestjs/common';
import { Cat } from './interfaces/cat.interface';
import { CreateCatDto } from './dto/create-cat.dto';
@Injectable()
export class CatsService {
constructor(
@Inject('CAT_MODEL')
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();
}
}
@@switch
import { Injectable, Dependencies } from '@nestjs/common';
@Injectable()
@Dependencies('CAT_MODEL')
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();
}
}
μμ μμμ μ°λ¦¬λ Cat
μΈν°νμ΄μ€λ₯Ό μ¬μ©νμ΅λλ€. μ΄ μΈν°νμ΄μ€λ λͺ½κ΅¬μ€ ν¨ν€μ§μμ Document
λ₯Ό νμ₯ν©λλ€:
import { Document } from 'mongoose';
export interface Cat extends Document {
readonly name: string;
readonly age: number;
readonly breed: string;
}
λ°μ΄ν°λ² μ΄μ€ μ°κ²°μ λΉλκΈ°μ΄μ§λ§ Nestλ μ΅μ’
μ¬μ©μκ° μ΄ νλ‘μΈμ€λ₯Ό μμ ν λ³Ό μ μλλ‘ν©λλ€. CatModel
ν΄λμ€λ db μ°κ²°μ κΈ°λ€λ¦¬κ³ μμΌλ©° CatsService
λ λͺ¨λΈμ μ¬μ©ν μ€λΉκ° λ λκΉμ§ μ§μ°λ©λλ€. κ° ν΄λμ€κ° μΈμ€ν΄μ€ν λλ©΄ μ 체 μμ© νλ‘κ·Έλ¨μ μμν μ μμ΅λλ€.
λ§μ§λ§CatsModule
μ λ€μκ³Ό κ°μ΅λλ€.
@@filename(cats.module)
import { Module } from '@nestjs/common';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import { catsProviders } from './cats.providers';
import { DatabaseModule } from '../database/database.module';
@Module({
imports: [DatabaseModule],
controllers: [CatsController],
providers: [
CatsService,
...catsProviders,
],
})
export class CatsModule {}
warning ννΈ CatsModule
μ λ£¨νΈ ApplicationModule
λ‘ κ°μ Έ μ€λ κ²μ μμ§ λ§μμμ€.