Giter Club home page Giter Club logo

datasource-per-tenant's Introduction

Datasource Per Tenant

Proof-of-concept: can we can dynamically provide datasources for different customers?

How does this work?

The provided code has the minimum possible implementation for it to work.

Using Spring Data, we have 3 entities: Company, User and Addresss.

Company and User belongs to the main (central) database. We need a central database with the minimum amount of info to authenticate the user and bind him to a tenant (in this case, Company).

Address is just a dummy entity with a single id attribute. It belongs to a tenant specific database.

Which database? The magic starts here.

After being authenticated through OAuth2 with Spring Security, when using any repository created under io.github.marcelothebuilder.datasourcepertenant.repository.customer, the DataSource used during the request will be decided by:

  • Querying the user tenant info;
  • Obtaining the tenant database credentials (url, username, password);
  • Providing the DataSource bean based on those credentials.

Benefits

  • No need to keep a set of pre-defined database connection info;
  • No need to be tied to a single database vendor (if an orm is used, the dialect can be dynamic);
  • No need to re-deploy when a new customer comes in, just add it's credentials to the main database either by hand or automatically.

Usage

Create your own DataSource selection strategy

Implement io.github.marcelothebuilder.datasourcepertenant.persistence.tenancy.MultitenantDataSource as a primary bean.

In this example, the implementation is done at io.github.marcelothebuilder.datasourcepertenant.persistence.tenancy.impl.MultitenantDataSourceImpl which delegates the datasource creation strategy to a io.github.marcelothebuilder.datasourcepertenant.persistence.tenancy.impl.DataSourceTenantFactory bean. The DataSourceTenantFactory bean is not required, but it makes the code design easier. Our main logic is defined in io.github.marcelothebuilder.datasourcepertenant.persistence.tenancy.impl.CentralDatabaseDataSourceTenantFactory.

License

Distributed under the GNU GENERAL PUBLIC LICENSE Version 3 license. See LICENSE for more information.

datasource-per-tenant's People

Contributors

marcelothebuilder avatar stacksharebot avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

sallakimane

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.