I am not sure I understand the nestjs/mongoose configuration.
I am not getting any results back and am assuming it is querying my admin database.
app.module.ts:
import { ConfigService } from './config/config.service';
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { ConfigModule } from './config/config.module';
import { CaseAssetsModule } from './case-assets/case-assets.module';
@Module({
imports: [
ConfigModule,
CaseAssetsModule,
MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
uri: configService.getString('mongodb.uri'),
useNewUrlParser: true,
useUnifiedTopology: true,
}),
inject: [ConfigService],
})
],
controllers: [],
providers: [],
})
export class AppModule { }
my config setting:
mongodb.uri = mongodb://<<myuser>>:<<mypwd>>#@<<myserver>>:27017/admin
case-assets.module.ts:
import { AssetSchema } from './asset.schema';
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { CaseAssetsController } from './case-assets.controller';
import { CaseAssetsService } from './case-assets.service';
@Module({
imports: [MongooseModule.forFeature([{ name: 'Asset', schema: AssetSchema, collection: 'assets' }])],
controllers: [CaseAssetsController],
providers: [CaseAssetsService]
})
export class CaseAssetsModule {}
Where do I specify the database that I want to connect to? I specify admin because that is the database that I authenticate against.
Gina
You've determined what database you want to connect to via the configuration given to the MongooseModule.forRootAsync()
method. From there, anything used from MongooseModule.forFeature()
uses the same configuration (i.e. the same database)
Okay, now I understand the problem. Looks like with mongo you can supply an authSource
query parameter like so mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?authSource=authDB
. So long as you need auth for every model in your db, this kind of URI should handle it. You can pass this to the MongooseModule.forRoot({})
or MongooseModule.forRootAsync({})
configuration.
If you need this to be on a per model basis, however, I don't think the current MongooseModule
can handle that.
Solution with two connections:
app.module.ts
MongooseModule.forRootAsync({
imports: [ConfigModule],
connectionName: 'firstDB',
useFactory: async (config: ConfigService) => ({
uri: config.get('app.firstDB'),
useNewUrlParser: true,
useUnifiedTopology: true,
}),
inject: [ConfigService],
}),
MongooseModule.forRootAsync({
imports: [ConfigModule],
connectionName: 'secondDB',
useFactory: async (config: ConfigService) => ({
uri: config.get('app.secondDB'),
useNewUrlParser: true,
useUnifiedTopology: true,
}),
inject: [ConfigService],
}),
cats.module.ts
@Module({
imports: [
MongooseModule.forFeature(
[
{
name: 'Cat',
schema: CatSchema,
collection: 'cats',
},
],
'firstDB',
),
],
})
export class CatsModule {}
dogs.module.ts
@Module({
imports: [
MongooseModule.forFeature(
[
{
name: 'Dog',
schema: DogSchema,
collection: 'dogs',
},
],
'secondDB',
),
],
})
export class DogsModule {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With