Multi-tenants is an architecture concept that can handle multiple projects in a single project container. If you look at popular sites like Nike.com or Mi.com, you will find out the project redirection based on the continent or country region. This post more about understanding the Angular 8 project package configuration, using this how are we leveraging the project for multi-tenant architecture.
Required Softwares
- NodeJS Version 12+
- Angular Cli 8+
Create an Angular Project
Use the following command to generate a new Angular codebase project.
Video Tutorial
package.json
Angular 8 package files
Project File Struture
Default Angular project structure.
Getting Started
New Tenant Based Struture
Move all of the files under tenants/US
tsconfig.json
Remove baseUrl under the complilerOptions . You will find this in project root level.
tsconfig.app.json
Project configuration file. Include the baseUrl and modify the tsconfig.json path with new location.
tsconfig.spec.json
Unit tests configuratin file. Just apply the same changes like the above one.
E2E - Automation
Move e2e folder under the tenants US folder.
tsconfig.e2e.json
Update the configration paths with new location.
angular.json
Angular compailer configuration file. Replace default project( src ) path settings to new tenants( tenants/US ) stucture.
Shared or Common
Create a shared folder for multiplie tenants.
tsconfig.app.json
Include the paths for shared folder. So that you can easily import the shared resources using @shared/services/* , instead for ../../../shared/services . Note: Exclude the e2e folder from the Angular compiler process.
Generate Shared Components Module
Use Angular CLI command to generate a module for shared components.
CREATE src/shared/shared-components/shared-components.module.ts (200 bytes)
Logo Component
Generate project logo component under shared components. So that all other tenants can reuse.
CREATE src/shared/shared-components/logo/logo.component.scss (0 bytes)
CREATE src/shared/shared-components/logo/logo.component.html (23 bytes)
CREATE src/shared/shared-components/logo/logo.component.spec.ts (614 bytes)
CREATE src/shared/shared-components/logo/logo.component.ts (262 bytes)
UPDATE src/shared/shared-components/shared-components.module.ts (268 bytes)
shared-components.module.ts
Update the explore the newly generated components.
Multiple Tenants
Duplicate the US tenant folder and replace with UK and IN.