'I am trying to connect two database based on connection Name in mongodb by using needle-innovision / nestjs-tenancy module

I am trying to connect two databases in mongodb by using this module 1.master database & 2.TenantId database which we are passing in request header.

But It is connecting only one database but not both. Please help me on this

requirement: 1.master database & need to insert tenant record in tenant collection 2.tenant database & need to insert user record in user collection

issue: It is connection only one database but not both how to use providers in this tenancy module?

import { Module } from '@nestjs/common';
import { TenancyModule  } from '@needle-innovision/nestjs-tenancy';
import { UserController } from './user.controller';
import { UserService } from './user.service';
import { User, UserSchema } from './user.schema';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Tenant, TenantSchema } from '../tenant/tenant.schema';


@Module({
    imports: [
        TenancyModule.forRootAsync({
            useFactory: async (cfs: ConfigService) => {
              return {
                tenantIdentifier: 'organisation',
                options: () => {
                  console.log('mongose...opp',cfs.get('config.mongooseChildOptions'));
                  return  cfs.get('config.mongooseChildOptions')
                },
                uri: (tenantId: string) => `${cfs.get('config.dbUrl')}${tenantId}`,
              }
            },
            inject: [ConfigService],
          }),
        TenancyModule.forFeature([{ name: User.name, schema: UserSchema }]),
TenancyModule.forRootAsync({
            useFactory: async (cfs: ConfigService) => {
              return {
                tenantIdentifier: 'organisation',
                options: () => {
                  console.log('mongose...opp',cfs.get('config.mongooseOptions'));
                  return  cfs.get('config.mongooseOptions')
                },
                uri: (tenantId: string) => `${cfs.get('config.dbUrl')}master`,
              }
            },
            inject: [ConfigService],
          }),
        TenancyModule.forFeature([{ name: Tenant.name, schema: TenantSchema }]),
      ],
    controllers: [UserController],
    providers: [UserService],
})
export class UserModule { }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source