Skip to main content

Use case

We need to access the data from different data sources for different tenants. For example, we are the platform for the online website builder, and each client can only view their data. The same data model is used for all clients.

Configuration

Each client has its own database. In this recipe, the Mango Inc tenant keeps its data in the remote ecom database while the Avocado Inc tenant works with the local database (bootstrapped in the docker-compose.yml file) which has the same data model. To enable multitenancy, use the contextToAppId and contextToOrchestratorId functions to provide distinct identifiers for each tenant. Also, implement the driverFactory function where you can select a data source based on the tenant name. JSON Web Token includes information about the tenant name in the tenant property of the securityContext.
Whenever driverFactory picks a connection based on the securityContext, you must also define contextToOrchestratorId and derive it from the same tenant identifier. driverFactory is called once per data source for every orchestrator id, and the orchestrator id defaults to a single global value for all tenants — so without it, every tenant reuses the database connection resolved for whichever tenant queried first, and one tenant receives another tenant’s data.

Query

To get users for different tenants, we will send two identical requests with different JWTs. Also, we send a query with unknown tenant to show that he cannot access to the data model of other tenants.

Result

We have received different data from different data sources depending on the tenant’s name:

Source code

Please feel free to check out the full source code or run it with the docker-compose up command. You’ll see the result, including queried data, in the console.