Package: 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.FeaturesCreate a new tenant (with domain)Tenant specific configurationsTenant specific migrations and seedersTenant middleware to specify tenant in request on the flyInstallationpipinstallmasonite-multitenancyConfigurationAddMultitenancyProviderto your project inconfig/providers.py:# config/providers.py# ...frommultitenancyimportMultitenancyProvider# ...PROVIDERS=[# ...# Third Party ProvidersMultitenancyProvider,# ...]Then you can publish the package resources (if needed) by doing:pythoncraftpackage:publishmultitenancyCommand UsageYou'll get bunch of commands to manage tenants.Create a new tenantThis will prompt few questions just provider answers and that's it.pythoncrafttenancy:createThis will also automatically generate new database connection based on yourdefaultdatabase connection fromconfig/database.py.List all tenantspythoncrafttenancy:listDelete a tenant# delete a tenant by database namepythoncrafttenancy:delete--tenants=tenant1# orpythoncrafttenancy:delete--tenants=tenant1,tenant2Delete all tenantspythoncrafttenancy:deleteMigrate a tenantpythoncrafttenancy:migrate--tenants=tenant1# orpythoncrafttenancy:migrate--tenants=tenant1,tenant2Migrate all tenantspythoncrafttenancy:migrateSimilary you can usetenancy:migrate:refresh,tenancy:migrate:reset,tenancy:migrate:statusandtenancy:migrate:rollbackcommands.
All commands will take--tenantsoption to specify tenants if you ever need.Seed a tenantpythoncrafttenancy:seed--tenants=tenant1# orpythoncrafttenancy:seed--tenants=tenant1,tenant2Seed all tenantspythoncrafttenancy:seedUsing Tenancy FacadeCreate a new tenantfrommultitenancy.facadesimportTenancy# creates a new tenant and returns instance of new TenantTenancy.create(name='tenant1',domain='tenant1.example.com',database='tenant1',)Get tenantfrommultitenancy.facadesimportTenancy# by idTenancy.get_tenant_by_id(1)# by domainTenancy.get_tenant_by_domain('tenant1.example.com')# by database nameTenancy.get_tenant_by_database('tenant1')Delete tenantfrommultitenancy.facadesimportTenancytenant=Tenant.find(1)Tenancy.delete(tenant)Connectionsfrommultitenancy.facadesimportTenancy# setting tenant specific connectiontenant=Tenant.find(1)Tenancy.set_connection(tenant)# resetting to default connectionTenancy.reset_connection()Event though above approach can be used to set tenant specific connection, and do tenant related tasks, it's recommended to useTenantContextinstead.Using Tenant ContextYou 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 useTenantContextclass to get tenant data.frommultitenancy.contextsimportTenantContextfrommultitenancy.models.TenantimportTenanttenant=Tenant.where('name','=','tenant1').first()withTenantContext(tenant=tenant):# do something with tenant1 data# ...You can also do all other tenant specific tasks like:migrations,seeds.frommultitenancy.contextsimportTenantContextfrommultitenancy.models.TenantimportTenanttenant=Tenant.where('name','=','tenant1').first()withTenantContext(tenant=tenant)asctx:# migrate the databasectx.migrate()ctx.migrate_refresh()ctx.migrate_rollback()ctx.migrate_reset()ctx.migrate_status()# seed the databasectx.seed()Final StepNow 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-routeswill be tenant aware. It means that if you have tenant setup and you are trying to access/tenant-aware-routesthen you will get tenant specific items from the database.TODODifferent database server for each tenantContributingPlease read theContributing Documentationhere.MaintainersYubaraj ShresthaLicensemultitenancy is open-sourced software licensed under theMIT license.