Multitenant Applications
Introduction
Using view entities in your multitenant applications ensures that users will only see data from the correct tenant they are associated with.
Use Case
For this purpose of this use case, the following domain model is used:
You are developing a project management tool that is used by multiple organizations. You do not want users from one organization to see projects from other organizations. To do this, you want to use a view entity that filters tenant data.
Creating a View Entity
Create a view entity that gets the account and tenant information from the current user. To do this, follow these steps.
-
Open your domain model and add a view entity named CurrentUserVE.
-
Add the following query to the OQL editor:
SELECT u.UserID as UserID, u.FullName as FullName, t.TenantID as TenantID FROM MultiTenantApp.AppUser u JOIN u/MultiTenantApp.AppUser_Account/Administration.Account a JOIN u/MultiTenantApp.AppUser_Tenant/MultiTenantApp.Tenant t WHERE a.ID = '[%CurrentUser%]'
This view entity will always contain one row that contains the information of the current user.
-
Create another view entity named ProjectVE.
-
Add the following query to the OQL editor:
SELECT p.ProjectID as ProjectID, t.TenantID as TenantID, p.Name as Name, p.Description as Description, p.StartDate as StartDate, p.EndDate as EndDate, p.Status as Status, p.CreatedAt as CreatedAt, p.UpdatedAt as UpdatedAt FROM MultiTenantApp.Project as p
-
Generate an overview page by right-clicking this view entity > Generate overview pages.
-
Run your app locally. The view entity shows all projects shown in the database.
Filter the View Entity
With these new view entities, you want to only see the projects of a specific tenant that is associated with the current user. This is done by using the CurrentUserVE as a filter. To do this, follow the steps.
-
Double-click CurrentUserVE.
-
In the OQL editor, add three additional lines to the end of the query:
SELECT p.ProjectID as ProjectID, t.TenantID as TenantID, p.Name as Name, p.Description as Description, p.StartDate as StartDate, p.EndDate as EndDate, p.Status as Status, p.CreatedAt as CreatedAt, p.UpdatedAt as UpdatedAt FROM MultiTenantApp.Project as p JOIN p/MultiTenantApp.Project_Tenant/MultiTenantApp.Tenant t JOIN MultiTenantApp.CurrentUserVE cu on (1=1) WHERE t.TenantID = cu.TenantID
-
Generate an overview page by right-clicking this view entity > Generate overview pages.
-
Run your app locally. ProjectVE only shows projects of the current user’s tenant.