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.
Features
- Create a new tenant (with domain)
- Tenant specific configurations
- Tenant specific migrations and seeders
- Tenant middleware to specify tenant in request on the fly
Installation
pip install masonite-multitenancy
Configuration
Add MultitenancyProvider to your project in config/providers.py:
# config/providers.py
# ...
from multitenancy import MultitenancyProvider
# ...
PROVIDERS = [
# ...
# Third Party Providers
MultitenancyProvider,
# ...
]
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/database.py.
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
Tenancy.create(
name='tenant1',
domain='tenant1.example.com',
database='tenant1',
)
Get tenant
from multitenancy.facades import Tenancy
# by id
Tenancy.get_tenant_by_id(1)
# by domain
Tenancy.get_tenant_by_domain('tenant1.example.com')
# by database name
Tenancy.get_tenant_by_database('tenant1')
Delete tenant
from multitenancy.facades import Tenancy
tenant = Tenant.find(1)
Tenancy.delete(tenant)
Connections
from multitenancy.facades import Tenancy
# setting tenant specific connection
tenant = Tenant.find(1)
Tenancy.set_connection(tenant)
# resetting to default connection
Tenancy.reset_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
ctx.migrate()
ctx.migrate_refresh()
ctx.migrate_rollback()
ctx.migrate_reset()
ctx.migrate_status()
# seed the database
ctx.seed()
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/routes.py
# ...
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.
TODO
- Different database server for each tenant
Contributing
Please read the Contributing Documentation here.
Maintainers
License
multitenancy is open-sourced software licensed under the MIT license.
Metadata
Release files for masonite-multitenancy 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| masonite-multitenancy-0.0.5.tar.gz | 12.1 kB | Without provenance | Details for masonite-multitenancy-0.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| masonite_multitenancy-0.0.5-py3-none-any.whl | Python 3 | none | any | Details for masonite_multitenancy-0.0.5-py3-none-any.whl |
Total release size: 28.6 kB
Release files / masonite-multitenancy-0.0.5.tar.gz
| Download URL | masonite-multitenancy-0.0.5.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1056f7e81345fcc1fbb057b17eff54da6e6a8772c64c8a9906580e2a7cf80766
|
|
BLAKE2b-256 checksum How to use checksums |
4ac2ad822ba5b9f8b3b1c3bdb136001d8f8cb155ee77f7bd5fc7edd171ce35dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.5
|
Release files / masonite_multitenancy-0.0.5-py3-none-any.whl
| Download URL | masonite_multitenancy-0.0.5-py3-none-any.whl |
|---|---|
| Size | 16.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1a62802b9f70ce64436cfa3f29f431cbbf0d233f5d7b4c9ef8125a72a4cf50e1
|
|
BLAKE2b-256 checksum How to use checksums |
851889e1b0415e66ca19742b89d1434a4b366a882d639f6fcd54e0a08df840ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.5
|
Security for masonite-multitenancy 0.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.