Giter Club home page Giter Club logo

multitenancy-adonisjs-5's Introduction

Multitenancy Adonis JS 5

Overview

This boiler demonstrate implementation of multi-tenant approach. Each tenant will have its own database.

Database connections

We will register two database connections in our database config file, namely landlord and tenant. keeping landlord as default database which will be used by super-admin.

Tenants

Tenants table resides in landlord database. All tenants entries will be stored here. The simpill_public_key column in this table serves a vital role in switching database on every request.

Switching database LifeCycle

All HTTP requests that are tenant based needs to go through a global middleware called "SwitchTenant". The middleware extracts the simpill_public_key from header of the http request and use it to get tenant details from default database i.e landlord. Details includes its database name. Now, we programmatically switch the database of tenant connection using database library.

Note: We keep a track record if the upcoming http requests are coming from the same simpill_public_key. If yes, we donot change connection database. We only change connection if the upcoming request is different from the previous database. Ignoring this will result in severe performance issues or even failure of every http request.

Migrations for landlord

The migration files for landlord are pre-configured to be created in /migrations/landlord folder. This is done to maintain migrations for landlord and tenants separately. Use the following command to create a migration for landlord:

node ace make:migration tenants --connection=landlord

Use command to migrate landlord:

node ace migration:run --connection=landlord

Migrations for tenants

This boiler ships with a custom command to migrate database to every tenant stored in tenants table in landlord database. The command uses tenant migrations directory i.e /migrations/tenant for all tenants.

node ace make:migration users --connection=tenant

Note: Make sure there are tenants stored in tenants table in landlord database before running tenants migration.

Use command to migrate all tenants.

node ace tenant:migrate

or, you can also migrate a single tenant using command:

node ace tenant:migrate <dbName>

where dbName is tenant's database

Rollback landlord and tenants

Rollback command for landlord are pretty standard. We just need to specify connection before running landlord migrations. Use command to rollback landlord:

node ace migration:rollback --connection=landlord

Use command to rollback all tenant's databases:

node ace tenant:rollback

or, you can also rollback a single tenant using command:

node ace tenant:rollback <dbName>

where dbName is tenant's database Note: It's recommended not to run rollbacks in production enviroment.

Conclusion

The following solution is tested with benchmark testing and can handle thousands of concurrent requests from multple tenants at the same time.

multitenancy-adonisjs-5's People

Contributors

faaizulhasan avatar salman-kashfy avatar waqartekrevol avatar

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.