0

I am fairly new to the NRWL/NX world. What I am trying to accomplish here is, to use the GraphQL (with the MongoDB) for the API. In past, I've created the GraphQL project with MongoDB in a non-NRWL environment. However since now we have multiple projects, we are moving to NX.

There are couple of MongoDB schema which are used across multiple projects, so I've decided to use them as a library. I generated a library and added following code

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 {}

The MongoDB model is pretty standard.

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);

How can I access the mongodb model via DatabaseModule, Please suggest.

Thanks -N Baua

NBaua
  • 583
  • 4
  • 16
  • 33

1 Answers1

0

Managed to solved the problem by setting up the correct directory structure and importing them in the datahelper.ts file.

We later exported the referenced interfaces from the datahelper.ts into the index.ts and we were done.

Thanks.

enter image description here

NBaua
  • 583
  • 4
  • 16
  • 33