## Building Multitenancy in Masonite: A Deep Dive into the Masonite Multitenancy Package
Are you looking to build a robust application that can serve multiple tenants with ease? Look no further! In this article, we will explore the power of multitenancy in Masonite using the Masonite Multitenancy package. Whether you are building a platform for multiple websites or a suite of apps for a company, multitenancy can help you achieve your goals efficiently.
### Features
Let’s start by exploring the features offered by the Masonite Multitenancy package.
– [x] Create a new tenant with a domain.
– [x] Configure tenant-specific settings.
– [x] Manage tenant-specific migrations and seeders.
– [x] Utilize tenant middleware to handle tenant-specific requests on the fly.
### Installation
To get started with the Masonite Multitenancy package, you need to install it using pip. Simply run the following command in your terminal:
“`bash
pip install masonite-multitenancy
“`
Next, you need to add the MultitenancyProvider to your project’s providers configuration. Open the `config/providers.py` file and add the following line:
“`python
from multitenancy import MultitenancyProvider
PROVIDERS = [
# …
MultitenancyProvider,
# …
]
“`
If you need to publish the package resources, you can run the following command:
“`bash
python craft package:publish multitenancy
“`
### Command Usage
The Masonite Multitenancy package provides a set of commands for managing tenants. Let’s explore some of the most commonly used commands:
**Create a new tenant**
To create a new tenant, you can run the `tenancy:create` command. This command will prompt you with a few questions to gather the necessary information. Simply provide the answers, and the new tenant will be created.
“`bash
python craft tenancy:create
“`
This command will automatically generate a new database connection based on your default database connection specified in the `config/database.py` file.
**List all tenants**
To list all the tenants in your application, you can use the `tenancy:list` command.
“`bash
python craft tenancy:list
“`
**Delete a tenant**
If you need to delete a specific tenant, you can use the `tenancy:delete` command. Simply provide the tenant’s database name as an argument.
“`bash
python craft tenancy:delete –tenants=tenant1
“`
You can also delete multiple tenants at once by providing a comma-separated list of database names.
**Migrate a tenant**
To perform database migrations for a specific tenant, you can use the `tenancy:migrate` command. Simply provide the tenant’s database name as an argument.
“`bash
python craft tenancy:migrate –tenants=tenant1
“`
You can also migrate multiple tenants at once by providing a comma-separated list of database names.
**Seed a tenant**
If you need to seed data for a specific tenant, you can use the `tenancy:seed` command. Similar to the previous commands, you need to provide the tenant’s database name as an argument.
“`bash
python craft tenancy:seed –tenants=tenant1
“`
### Using Tenancy Facade
The Masonite Multitenancy package provides a convenient way to interact with tenants using the Tenancy Facade. Let’s explore some of the available methods:
**Create a new tenant**
To create a new tenant programmatically, you can use the `Tenancy.create()` method. This method returns an instance of the newly created tenant.
“`python
from multitenancy.facades import Tenancy
Tenancy.create(
name=’tenant1′,
domain=’tenant1.example.com’,
database=’tenant1′,
)
“`
**Get tenant**
To retrieve a specific tenant, you can use the `Tenancy.get_tenant_by_` methods. You can retrieve a tenant by their ID, domain, or database name.
“`python
from multitenancy.facades import Tenancy
Tenancy.get_tenant_by_id(1)
Tenancy.get_tenant_by_domain(‘tenant1.example.com’)
Tenancy.get_tenant_by_database(‘tenant1’)
“`
**Delete tenant**
If you need to delete a tenant, you can use the `Tenancy.delete()` method. Simply provide the tenant object as an argument.
“`python
from multitenancy.facades import Tenancy
tenant = Tenant.find(1)
Tenancy.delete(tenant)
“`
**Connections**
The Tenancy Facade also provides methods for setting and resetting tenant-specific database connections. You can use the `Tenancy.set_connection()` method to set a specific tenant’s connection, and the `Tenancy.reset_connection()` method to reset it to the default connection.
“`python
from multitenancy.facades import Tenancy
tenant = Tenant.find(1)
Tenancy.set_connection(tenant)
Tenancy.reset_connection()
“`
While the above approach can be used to set tenant-specific connections and perform tenant-related tasks, it is recommended to use the Tenant Context for a more streamlined experience.
### Using Tenant Context
The Tenant Context provides a powerful way to work with tenant data within a specific context. It allows you to perform migrations, seeds, and other tenant-specific tasks with ease.
“`python
from multitenancy.contexts import TenantContext
from multitenancy.models.Tenant import Tenant
tenant = Tenant.where(‘name’, ‘=’, ‘tenant1’).first()
with TenantContext(tenant=tenant):
# Perform actions within the tenant context
# …
“`
You can perform various tasks within the context, such as migrating the database, refreshing migrations, rolling back migrations, resetting migrations, checking migration status, and seeding the database.
“`python
from multitenancy.contexts import TenantContext
from multitenancy.models.Tenant import Tenant
tenant = Tenant.where(‘name’, ‘=’, ‘tenant1’).first()
with TenantContext(tenant=tenant) as ctx:
# Migrate the database
ctx.migrate()
ctx.migrate_refresh()
ctx.migrate_rollback()
ctx.migrate_reset()
ctx.migrate_status()
# Seed the database
ctx.seed()
“`
### Final Step
To make your application fully tenant-aware, you need to use the tenancy middleware. This middleware allows you to specify the tenant in the request on the fly. Simply attach the middleware to the routes that are tenant-aware.
“`python
# config/routes.py
Route.get(“/”, “WelcomeController@show”)
Route.get(“/tenant-aware-routes”, “WelcomeController@show”).middleware(“multitenancy”)
“`
In the example above, the `/tenant-aware-routes` route will be tenant-aware. If you have set up a tenant and try to access this route, you will receive tenant-specific items from the database.
### Conclusion
In this article, we have explored the power of multitenancy in Masonite using the Masonite Multitenancy package. We have learned about its features, installation steps, and command usage. Additionally, we have delved into using the Tenancy Facade and Tenant Context for managing tenants, connections, and performing database tasks. Lastly, we have discussed the implementation details of tenancy middleware for creating tenant-aware routes. With this knowledge, you can now confidently build multitenant applications using Masonite!
Happy coding!
### References
– [Masonite Multitenancy Package](https://github.com/py-package/masonite-multitenancy)
– [Official Masonite Documentation](https://docs.masoniteproject.com)
– [Masonite Official GitHub Repository](https://github.com/py-package/masonite-multitenancy)
Leave a Reply