請先安裝本次需要的模組。
cmd指令
yarn add mongoose yarn add @types/mongoose --dev
我们在 src/app/database.provider.ts实例化一个Mongoose
'use strict';import * as mongoose from 'mongoose';export const databaseProviders = [
{ provide: 'MongoDBConnection', useFactory: async (): Promise<mongoose.Connection> => {
(mongoose as any).Promise = global.Promise; return await mongoose.connect('mongodb://localhost:27017/IronManNest', { useMongoClient: true
})
}
}
]先使用mongoose.connect()建立一下與MongoDB的連線,這會回傳MongooseThenable,再透過global.Promise 覆寫它,避免程式發出警告,實際上這也是一個非同步的Component。
使用Mongoose的人都知道我们要实例化一个schema对象
所以接下来我们必须定义一个schema对象src/app/User/schemas/user.schema.ts
import * as mongoose from 'mongoose';export const UsersSchema = new mongoose.Schema(
{ Name: String, Age: Number
},
{ collection: 'Users', versionKey: false },
)接下来是usersProviderssrc/app/Users/users.providers.ts
'use strict';import { Connection, connection } from 'mongoose';import { UsersSchema } from './schemas/users.schema';export const UsersProvider = [
{ provide: 'UsersRepository', useFactory: (connection: Connection) => connection.model('Users', UsersSchema), inject: ['MongoDBConnection']
}
]然后定义一下 Users的数据类型接口src/app/Users/interfaces/IUsers.ts
'use strict';import { Document } from 'mongoose';export interface IUsers extends Document {
readonly _id: number;
readonly Name: string;
readonly Age: number;
}再来是定义@Body()的数据接口,让dot接口和interfaces中的字段进行映射src/app/Users/DTO/createUses.dto.ts
import { ApiModelProperty } from '@nestjs/swagger';
export class CreateUsersDTO { @ApiModelProperty()
readonly _id: number; @ApiModelProperty()
readonly Name: string; @ApiModelProperty()
readonly Age: number;
}接下来是要在UsersServices中依赖注入一下
'use strict';import { Component, Inject } from '@nestjs/common';import { IUsers, IUsersService } from './interfaces/index';import { Model } from 'mongoose';import { InjectRepository } from '@nestjs/typeorm';import { CreateUsersDTO } from './DTO/createUsers.dto';
@Component()export class UsersServices implements IUsersService{ constructor( @Inject('UsersRepository') private readonly usersRepository: Model<IUsers>) { }
}@Inject('UsersRepository') 对应是是export const UsersProvider = [
{
provide: 'UsersRepository',
useFactory: (connection: Connection) => connection.model('Users', UsersSchema),
inject: ['MongoDBConnection']
}
]中的 UsersRepository
最后别忘了
在模块中已用一下 UsersServices ...UsersProvider
'use strict';import { Module } from '@nestjs/common';import { UsersServices } from '../users.service';import { UsersProvider } from '../users.providers';import { DatabaseModule } from '../../database.module';import { UsersController } from './users.controller';
@Module({ modules: [DatabaseModule], controllers: [UsersController], provides: [
UsersServices,
...UsersProvider
]
})export class UsersModule { }如果你觉得上面的方法比较复杂的话,还有第二中配置,这种配置是全局的。
在 app.moudule.ts 使用 @nestjs/mongoose
@Module({
imports: [
MongooseModule.forRoot('127.0.0.1:27017/nest'),
MongooseModule.forFeature([
{ name: "User", schema: UserSchema },
])
UploadModule,
CmsModule
],
controllers: [AppController],
providers: [AppService],
})对应的Server层
export class UsersServices implements IUsersService{
constructor( @Inject('UsersRepository') private readonly usersRepository: Model<IUsers>) { }
}@Inject('User') 便可
作者:傻梦兽
链接:https://www.jianshu.com/p/7045542da01a
随时随地看视频