needle-innovision / nestjs-tenancy

Multi-tenancy approach for nestjs - currently supported only for mongodb with mongoose
MIT License
187 stars 58 forks source link

I am trying to connect two database based on connection Name in mongodb by using this module #18

Closed simha-sc closed 2 years ago

simha-sc commented 2 years ago

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 { }
sandeepsuvit commented 2 years ago

Hi @simha-sc, thanks for writing in, Regarding your issue i am not sure why you have added the TenancyModule configuration twice in the UserModule. Can you provide me with a minimal code configuration in a git repo so that i can check what is it thats missing.

sandeepsuvit commented 2 years ago

Closing due to inactivity