https://github.com/py-package/masonite-multitenancy
Multitenancy package for Masonite!
https://github.com/py-package/masonite-multitenancy
masonite-framework python
Last synced: 2 months ago
JSON representation
Multitenancy package for Masonite!
- Host: GitHub
- URL: https://github.com/py-package/masonite-multitenancy
- Owner: py-package
- License: mit
- Created: 2022-07-08T11:28:39.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-02-05T18:36:06.000Z (over 1 year ago)
- Last Synced: 2025-04-23T14:14:17.592Z (2 months ago)
- Topics: masonite-framework, python
- Language: Python
- Homepage:
- Size: 148 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
# 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
- [x] Create a new tenant (with domain)
- [x] Tenant specific configurations
- [x] Tenant specific migrations and seeders
- [x] Tenant middleware to specify tenant in request on the fly### Installation
```bash
pip install masonite-multitenancy
```### Configuration
Add _`MultitenancyProvider`_ to your project in `config/providers.py`:
```python
# config/providers.py
# ...
from multitenancy import MultitenancyProvider# ...
PROVIDERS = [
# ...
# Third Party Providers
MultitenancyProvider,
# ...
]
```Then you can publish the package resources (if needed) by doing:
```bash
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.
```bash
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**
```bash
python craft tenancy:list
```**Delete a tenant**
```bash
# delete a tenant by database name
python craft tenancy:delete --tenants=tenant1
# or
python craft tenancy:delete --tenants=tenant1,tenant2
```**Delete all tenants**
```bash
python craft tenancy:delete
```**Migrate a tenant**
```bash
python craft tenancy:migrate --tenants=tenant1
# or
python craft tenancy:migrate --tenants=tenant1,tenant2
```**Migrate all tenants**
```bash
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**
```bash
python craft tenancy:seed --tenants=tenant1
# or
python craft tenancy:seed --tenants=tenant1,tenant2
```**Seed all tenants**
```bash
python craft tenancy:seed
```### Using Tenancy Facade
**Create a new tenant**
```python
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**
```python
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**
```python
from multitenancy.facades import Tenancytenant = Tenant.find(1)
Tenancy.delete(tenant)
```**Connections**
```python
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.
```python
from multitenancy.contexts import TenantContext
from multitenancy.models.Tenant import Tenanttenant = 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`.
```python
from multitenancy.contexts import TenantContext
from multitenancy.models.Tenant import Tenanttenant = 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.
```python
# 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
- [x] Different database server for each tenant
### Contributing
Please read the [Contributing Documentation](CONTRIBUTING.md) here.
### Maintainers
- [x] [Yubaraj Shrestha](https://www.github.com/yubarajshrestha)
### License
multitenancy is open-sourced software licensed under the [MIT license](LICENSE).