0

我对 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 Baua

4

1 回答 1

0

设法通过设置正确的目录结构并将它们导入 datahelper.ts 文件来解决问题。

我们稍后将引用的接口从datahelper.ts 导出index.ts 中,我们就完成了。

谢谢。

在此处输入图像描述

于 2019-07-25T05:33:39.653 回答