Giter Site home page Giter Site logo

Comments (2)

TriPSs avatar TriPSs commented on September 27, 2024

I have no experience with multi tenancy.

from nestjs-query.

GP4cK avatar GP4cK commented on September 27, 2024

We managed to get it to work by doing this:

Step 1: Create a `TypeOrmConfigService` that extends `TypeOrmOptionsFactory` in which you inject the request to extract the tenant id.
import { Inject, Injectable } from '@nestjs/common';
import { DataSource, DataSourceOptions } from 'typeorm';
import { REQUEST } from '@nestjs/core';
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';
import type { Request } from 'express';

@Injectable()
export class TypeOrmConfigService implements TypeOrmOptionsFactory {

  private databaseID: string;

  constructor(@Inject(REQUEST) readonly req: Request ) {
    this.databaseID = /* Extract the databaseID/tenantID from the request */;
  }

  createTypeOrmOptions(): TypeOrmModuleOptions {
    return {
      ...yourBaseOrmConfig,
      database: this.databaseID,
    };
  }

  static async getOrCreateDataSource(options?: DataSourceOptions): Promise<DataSource> {
    /** Create and initialize the DataSource here. */
  }
}
Step 2: in your `app.module.ts`, use TypeOrm.forRootAsync with the service you created.
import { Module, NestModule } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { TypeOrmConfigService } from "./config/TypeORMConfig.service";

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useClass: TypeOrmConfigService,
      dataSourceFactory: (options) => TypeOrmConfigService.getOrCreateDataSource(options),
    }),
    // Other imports
  ],
})
export class AppModule implements NestModule {}

And you should be good to go. Now when you inject a repository in your services with @InjectRepository(), they automatically become request-scoped and use the right connection.

from nestjs-query.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.