DEV Community

Cover image for Create a multi-tenant application in Nest.js Part 3 (multi-database setup)
ismaeil-shajar
ismaeil-shajar

Posted on • Edited on

Create a multi-tenant application in Nest.js Part 3 (multi-database setup)

In part 1, we set up the nestjs framework and configured and tested microservices architecture application using nest.js. In part 2, we used Sequelize and mongoose to access the database and tested for both MySQL database and MongoDB.

Async Connection

In this part; we will see how to let the application connect to multiple databases depending on the request. Since it is a multi-tenancy application, each tenant has their own database containing their data accessing the same application, thus the application needs to connect to different databases.
We will change the pass repository option method and use forRootAsync() instead of forRoot(), we need to use a custom class for configuration.

For both sequelize and mongoose, add this:

MongooseModule.forRootAsync({
    useClass:MongooseConfigService
  }),
SequelizeModule.forRootAsync({
      useClass:SequelizeConfigService
})
Enter fullscreen mode Exit fullscreen mode

We will create a config file and two classes: MongooseConfigService and SequelizeConfigService

The plan here is to add injection for each incoming request and use a domain to switch between connections.

So we need to use @Injectable({scope:Scope.REQUEST}), and on the class constructor we use @Inject(REQUEST) private read-only request so we can get host information from request data.

For example let say the domain is example.com, and we have a tenant called company1, then the domain will be company1.example.com. Same thing for company2 tenant, the domain will be company2.example.com and so on.

config/MongooseConfigService.ts

import { Inject, Injectable, Scope } from "@nestjs/common";
import { REQUEST } from "@nestjs/core";
import { MongooseModuleOptions, MongooseOptionsFactory } from "@nestjs/mongoose";

@Injectable({scope:Scope.REQUEST})
export class MongooseConfigService implements MongooseOptionsFactory {
    constructor(@Inject(REQUEST) private readonly request,){}

  createMongooseOptions(): MongooseModuleOptions {
    let domain:string[]
    let database='database_development'
    if(this.request.data ){
      domain=this.request.data['host'].split('.')
      console.log(this.request)
    }
    else{
      domain=this.request['headers']['host'].split('.')
    }

    console.log(domain)
    if(domain[0]!='127' && domain[0]!='www' && domain.length >2){
      database='tenant_'+domain[0]
      console.log('current DB',database)
    }
    return {
      uri: 'mongodb://localhost:27017/'+database,
    };
  }
}
Enter fullscreen mode Exit fullscreen mode

config/SequelizeConfigService.ts

import { Inject, Injectable, Scope } from "@nestjs/common";
import { REQUEST } from "@nestjs/core";
import { CONTEXT, RedisContext, RequestContext } from "@nestjs/microservices";
import { SequelizeModuleOptions, SequelizeOptionsFactory} from "@nestjs/sequelize";

@Injectable({scope:Scope.REQUEST})
export class SequelizeConfigService implements SequelizeOptionsFactory {
    constructor(@Inject(REQUEST) private readonly request:RequestContext){}

    createSequelizeOptions(): SequelizeModuleOptions {

      let domain:string[]
      let database='database_development'
      if(this.request.data ){
        domain=this.request.data['host'].split('.')
        console.log(this.request)
      }
      else{
        domain=this.request['headers']['host'].split('.')
      }

      console.log(domain)
      if(domain[0]!='127' && domain[0]!='www' && domain.length >2){
        database='tenant_'+domain[0]
        console.log('current DB',database)
      }

    return {
      dialect: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'ismaeil',
      password: 'root',
      database: database,
      autoLoadModels: true,
      synchronize: true,
    };
  }
}
Enter fullscreen mode Exit fullscreen mode

Performance -- Production

in production we need to avoid create connection in every request so we will do some edits in user module and services.

Solution:

Our problem here is connection was create every request and this connection not close and also I don't reuse.
So we can use even close connection or use existing connection in new request or use both and manage when create and when close.

Closing connection

To close connection first wen need to access it.by naming connections we can access connection using @InjectConnection then in service we can close conection every time after finish .
So we need this edits

in user-service.module.ts


     SequelizeModule.forRootAsync({
      name: 'development',
       useClass:SequelizeConfigService
     }),
     SequelizeModule.forFeature([Users], 'development')], // use connection name in forFeature
Enter fullscreen mode Exit fullscreen mode

and in user-service.service.ts

export class UserServiceService {
  constructor(
    @InjectConnection('development') private readonly sequelize: Sequelize, // access connection by name 'development'
    @InjectModel(Users, 'development')
  private readonly userModel: typeof Users){}
  async findAll() {
    let result =await this.userModel.findAll()
    this.sequelize.close() // after  every use will close connection
    return result;
  }
  /// the rest 
}
Enter fullscreen mode Exit fullscreen mode

Use existing connection

In order to prevent the creation of SequelizeConfigService inside SequelizeModule and use a provider imported from a different module, you can use the useExisting syntax.

and we need to create external module that provide sequelize configuration .

create new file named user-config.module.ts


@Module({
  providers: [SequelizeConfigService],
  exports:[SequelizeConfigService]
})

export class UserConfigModule {}
Enter fullscreen mode Exit fullscreen mode

then edit user-service.module.ts

  SequelizeModule.forRootAsync({
    imports:[UserConfigModule],
    useExisting: SequelizeConfigService,
  }),
Enter fullscreen mode Exit fullscreen mode

Use both

if we add ability to use both ways code will be like this

user-service.module.ts

@Module({
   imports: [
  SequelizeModule.forRootAsync({
    imports:[UserConfigModule],
    name: 'development',
    useExisting: SequelizeConfigService,
  }),
    SequelizeModule.forFeature([Users], 'development')],
  controllers: [UserServiceController],
  providers: [UserServiceService],
})

export class UserServiceModule {}
Enter fullscreen mode Exit fullscreen mode

user-service.service.ts

@Injectable()
export class UserServiceService {
  constructor(@InjectConnection('development') private readonly sequelize: Sequelize,
    @InjectModel(Users, 'development')
  private readonly userModel: typeof Users){}
  async findAll() {
    let result =await this.userModel.findAll()
    //console.log(this.sequelize.close())  // optional or you can manage it 
    return result;
  }

  async create( createUserDto:CreateUserDto):Promise<Users> {
    return this.userModel.create(<Users>createUserDto)

  }
}
Enter fullscreen mode Exit fullscreen mode

and we have new module

user-config.module.ts

import { Module } from '@nestjs/common';
import { SequelizeConfigService } from './sequelize-config-service';

@Module({
  providers: [SequelizeConfigService],
  exports:[SequelizeConfigService]
})

export class UserConfigModule {}
Enter fullscreen mode Exit fullscreen mode

Testing

After finishing the configuration, we need to do some work to test it because we need to map our localhost and IP to a domain.
I will try to use two ways to test the application locally but for the production, it will be a configuration in your domain provider.

1- Edit the hosts file in your local machine and edit this file every time you add a tenant

Go to the following file in Linux: /etc/hosts and in windows: c:\windows\system32\drivers\etc\hosts and add

## lines
127.0.0.1   example.com
127.0.0.1   company1.example.com
127.0.0.1   company2.example.com
Enter fullscreen mode Exit fullscreen mode

2- Use local dns

In linux you can install dnsmasq and follow these steps

1- Install dnsmasq in NetworkManager.

2- Add the configuration file sudo nano /etc/NetworkManager/dnsmasq.d/dnsmasq-localhost.conf.

add this line in the file:

address=/.example.com/127.0.0.1
Enter fullscreen mode Exit fullscreen mode

3- Restart the service sudo systemctl reload NetworkManager.

Source code available in git branch multi-database

Next in part 4 we will add a security level and user roles.

Top comments (14)

Collapse
 
rikinotnull profile image
rikicmd

Are you going to continue to part 4?

Collapse
 
ismaeil_shajar profile image
ismaeil-shajar • Edited

Yes will be post next days

Collapse
 
verygreenboi profile image
Thompson Edolo

Would be interesting to see ABAC or PBAC implementation as opposed to just RBAC. Great tutorial.

Collapse
 
rikinotnull profile image
rikicmd

Thank you for the tutorial. I'm really waiting for part 4.

Thread Thread
 
ismaeil_shajar profile image
ismaeil-shajar
Collapse
 
faizershaikh profile image
Faizer Shaikh

it's great but what if I have used schema based separation and I have used public schema to store and manage information about all tenants and if I have some apis for public schema then it merges connections and gives wrong result to user and also if there are some paths or APIs on which we dont need sequelize connection then also it creates connection for that API,for eg; if we have public folder and we want to access a file from that folder at that time also it creates new sequelize connection

Collapse
 
alfianriv profile image
Alfian Rivaldi

I already create nestjs application Multi Tenancy with 1 connection database and multi schema
github.com/alfianriv/nestjs-multi-...

Collapse
 
ismaeil_shajar profile image
ismaeil-shajar

That nice you use poostgres right ?

Collapse
 
alfianriv profile image
Alfian Rivaldi

yes, i'm using postgres with multi schema

Collapse
 
andycharles6 profile image
andycharles6

Is it going to create a new connection for each request and destroy it after the request?

Collapse
 
ismaeil_shajar profile image
Info Comment hidden by post author - thread only accessible via permalink
ismaeil-shajar

yes because the injection scope is set to SINGLETON you can read more about in docs.nestjs.com/fundamentals/injec...

Collapse
 
ismaeil_shajar profile image
ismaeil-shajar

you are right I fix this and update post

Collapse
 
nileshmoradiya profile image
Nilesh

@ismaeil_shajar I really liked and enjoy the series. Any hints on when you're going to post the next article?

Collapse
 
crazyoptimist profile image
crazyoptimist

Thanks for the nice content!
Just a side question, how did you manage to auotmate sub domain creation when every tenant onboards?

Some comments have been hidden by the post's author - find out more