Skip to main content

Multitenancy package for Masonite!

Project description

Masonite Package GitHub Workflow Status (branch) PyPI Python Version GitHub release (latest by date including pre-releases) License Code style: black

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

masonite-multitenancy-0.0.2.tar.gz (86.5 kB view hashes)

Uploaded Source

Built Distribution

masonite_multitenancy-0.0.2-py3-none-any.whl (15.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page