Multitenancy package for Masonite!
Project description
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
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
Note: After creating a new tenant, you will need to setup related database configuration in
config/multitenancy.py
.
For example, if your tenant database name is tenant1
, then you need to add the following to config/multitenancy.py
:
# config/multitenancy.py
TENANTS = {
"tenant1": {
"driver": "sqlite",
"database": env("SQLITE_DB_DATABASE", "tenant1.sqlite3"),
"prefix": "",
"log_queries": env("DB_LOG"),
},
}
You can use any database driver that Masonite supports. For example, if you want to use MySQL, then you can use the following:
# config/multitenancy.py
TENANTS = {
"tenant1": {
"driver": "mysql",
"host": env("DB_HOST"),
"user": env("DB_USERNAME"),
"password": env("DB_PASSWORD"),
"database": env("DB_DATABASE"),
"port": env("DB_PORT"),
"prefix": "",
"grammar": "mysql",
"options": {
"charset": "utf8mb4",
},
"log_queries": env("DB_LOG"),
},
}
Note: Make sure you have set the
multitenancy
configuration before running any tenant related commands.
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
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.
Contributing
Please read the Contributing Documentation here.
Maintainers
License
multitenancy is open-sourced software licensed under the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for masonite-multitenancy-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e223ff8490ff84aba7ffd85daf6b5f9f12780c7d4c97483deb8cf51d32108bc1 |
|
MD5 | c07a4253f4a49c0c81c8da1c6f8e7845 |
|
BLAKE2b-256 | 21146ecba4f7cbf93acd88907a367700e7c88e8a52ecc667fc0a0cac15a757f6 |
Hashes for masonite_multitenancy-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 077df041b86d142fe6569c474fffd37aa524c464f92b291fb1e486bda9afcaea |
|
MD5 | 5eca18f401c8c079212778713b894fee |
|
BLAKE2b-256 | 385acaf00781c0b80ad08538bfd22e1741856dac71db136f250f50e9549f7e1a |