Published on

Masonite Multitenancy


Masonite Multitenancy (WIP)

Multitenancy package for Masonite!

Multitenancy is a feature that allows you to have multiple tenants in your application. This is useful for things like a company having multiple websites, or a company having multiple apps.


  • Create a new tenant (with domain)
  • Tenant specific configurations
  • Tenant specific migrations and seeders
  • Tenant middleware to specify tenant in request on the fly


pip install masonite-multitenancy


Add MultitenancyProvider to your project in config/

# config/
# ...
from multitenancy import MultitenancyProvider

# ...
    # ...
    # Third Party Providers
    # ...

Then you can publish the package resources (if needed) by doing:

python craft package:publish multitenancy

Command Usage

You'll get bunch of commands to manage tenants.

Create a new tenant

This will prompt few questions just provider answers and that's it.

python craft tenancy:create

This will also automatically generate new database connection based on your default database connection from config/

List all tenants

python craft tenancy:list

Delete a tenant

# delete a tenant by database name
python craft tenancy:delete --tenants=tenant1
# or
python craft tenancy:delete --tenants=tenant1,tenant2

Delete all tenants

python craft tenancy:delete

Migrate a tenant

python craft tenancy:migrate --tenants=tenant1
# or
python craft tenancy:migrate --tenants=tenant1,tenant2

Migrate all tenants

python craft tenancy:migrate

Similary you can use tenancy:migrate:refresh, tenancy:migrate:reset, tenancy:migrate:status and tenancy:migrate:rollback commands. All commands will take --tenants option to specify tenants if you ever need.

Seed a tenant

python craft tenancy:seed --tenants=tenant1
# or
python craft tenancy:seed --tenants=tenant1,tenant2

Seed all tenants

python craft tenancy:seed

Using Tenancy Facade

Create a new tenant

from multitenancy.facades import Tenancy

# creates a new tenant and returns instance of new Tenant

Get tenant

from multitenancy.facades import Tenancy

# by id

# by domain

# by database name

Delete tenant

from multitenancy.facades import Tenancy

tenant = Tenant.find(1)


from multitenancy.facades import Tenancy

# setting tenant specific connection
tenant = Tenant.find(1)

# resetting to default connection

Event though above approach can be used to set tenant specific connection, and do tenant related tasks, it's recommended to use TenantContext instead.

Using Tenant Context

You might sometime need to get data from different tenant in your application or you might have to do some logic based on tenant. In this case you can use TenantContext class to get tenant data.

from multitenancy.contexts import TenantContext
from multitenancy.models.Tenant import Tenant

tenant = Tenant.where('name', '=', 'tenant1').first()

with TenantContext(tenant=tenant):
    # do something with tenant1 data
    # ...

You can also do all other tenant specific tasks like: migrations, seeds.

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

    # seed the database

Final Step

Now the multitenancy is almost ready to use. The final step is to make use of tenancy middleware. This middleware will be used to specify tenant in request on the fly. So, basically you have to attach this middleware to all the routes that are tenant aware.

# config/
# ...

Route.get("/", "WelcomeController@show")
Route.get("/tenant-aware-routes", "WelcomeController@show").middleware("multitenancy")

In above example, /tenant-aware-routes will be tenant aware. It means that if you have tenant setup and you are trying to access /tenant-aware-routes then you will get tenant specific items from the database.