我对NRWL / NX世界还很陌生。 我要在这里完成的工作是将GraphQL(与MongoDB一起使用)用于API。 过去,我在非NRWL环境中使用MongoDB创建了GraphQL项目。 但是,由于现在我们有多个项目,所以我们正在迁移到NX。
有多个跨多个项目使用的MongoDB模式,所以我决定将它们用作库。我生成了一个库并添加了以下代码
import { MongooseModule } from '@nestjs/mongoose';
import { ConfigService, ConfigModule } from '@another-lib/config-helper';
import { Module } from '@nestjs/common';
import { Location } from './model/location'; //This wouldn't be accessible from elsewhere
export const databaseProviders = [
MongooseModule.forRootAsync({
imports: [ ConfigModule ],
inject: [ ConfigService ],
useFactory: async (config: ConfigService) => ({
uri: config.get('MONGODB_URI'),
useNewUrlParser: true,
useFindAndModify: false,
}),
}),
];
@Module({
imports: [ ...databaseProviders, Location ],
exports: [ ...databaseProviders, Location ],
})
export class DatabaseModule {}
MongoDB模型是非常标准的。
import * as mongoose from 'mongoose';
const LocationSchema = new mongoose.Schema(
{
LocationName: {
type: String,
},
LocationCode: {
type: String,
},
isPickable: {
type: Boolean,
},
TemplateID: {
type: String,
},
},
{ collection: 'locations', timestamps: true },
);
export interface ILocation extends mongoose.Document {
_id: string;
LocationName: string;
LocationCode: string;
isPickable: boolean;
TemplateID: string;
}
//used for the server
export interface ILocationModel extends mongoose.Model<ILocation> {}
// export const LocationSchema = mongoose.model('location', _LocationSchema);
export const Location: ILocationModel = <ILocationModel>mongoose.model<ILocation>('Location', LocationSchema);
如何通过 DatabaseModule 访问mongodb模型,请提出建议。
谢谢 -N鲍阿