Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NestJS导出。连接,postgres多租户_Node.js_Typescript_Express_Nestjs_Nestjs Config - Fatal编程技术网

Node.js NestJS导出。连接,postgres多租户

Node.js NestJS导出。连接,postgres多租户,node.js,typescript,express,nestjs,nestjs-config,Node.js,Typescript,Express,Nestjs,Nestjs Config,我试图使用postgres实现多租户体系结构。它在租户服务上按预期工作。我想将此连接导入另一个名为shops的模块。我如何才能做到这一点。您当前看到的是租户模块 import { TenantService } from './tenant.service'; import { TenantController } from './tenant.controller'; import { Global, Module, Scope } from '@nestjs/common'; im

我试图使用postgres实现多租户体系结构。它在租户服务上按预期工作。我想将此连接导入另一个名为shops的模块。我如何才能做到这一点。您当前看到的是租户模块

import { TenantService } from './tenant.service';
    import { TenantController } from './tenant.controller';

import { Global, Module, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Connection, createConnection, getConnectionManager } from 'typeorm';

import * as tenantsOrmconfig from '@config/typeorm/tenant-ormconfig';
import * as ormconfig from '@config/typeorm/ormconfig';
import { ShopModule } from './shop/shop.module';
import { AwsService } from '@config/aws';

const connectionFactory = {
  provide: 'CONNECTION',
  scope: Scope.REQUEST,
  useFactory: async (req) => {
    const teamId = req.headers['x-team-id'];
    console.log('xxxxxxxxxxxxxxxxxxx', teamId);

    if (teamId) {
      const connectionName = `${teamId}`;
      const connectionManager = getConnectionManager();

      if (connectionManager.has(connectionName)) {
        const connection = connectionManager.get(connectionName);
        return Promise.resolve(
          connection.isConnected ? connection : connection.connect(),
        );
      }

      return createConnection({
        ...tenantsOrmconfig,
        entities: [
          ...(tenantsOrmconfig as any).entities,
          ...(ormconfig as any).entities,
        ],
        name: connectionName,
        type: 'postgres',
        schema: connectionName,
      });
    }
  },
  inject: [REQUEST],
};

@Module({
  imports: [ShopModule],
  controllers: [TenantController],
  providers: [connectionFactory, TenantService],
  exports: ['CONNECTION'],
})
export class TenantModule {}
下面给出了它在租户服务中的使用方式

export class TenantService {
  gameRepository;
  constructor(@Inject('CONNECTION') connection) {
    this.gameRepository = connection.getRepository(TenantEntity);
   
  }

我使用@Global()decorator解决了这个问题。它使租户模块成为全局模块,以便可以在整个项目的任何位置导入。 在其他模块上导入时,我使用

imports: [forwardRef(() => TenantModule)],
#具有多租户连接属性的租户模块

@Global()
@Module({
  imports: [ShopModule],
  controllers: [TenantController],
  providers: [connectionFactory, TenantService],
  exports: ['CONNECTION'],
})
export class TenantModule {}
#商店模块,我需要模式选择

@Module({
  imports: [forwardRef(() => TenantModule)],
  controllers: [ShopController],
  providers: [ShopService, AwsService],
})
export class ShopModule {}