fourdotsix / tenancy-mapping
Tenant data handling for stancl/tenancy
Fund package maintenance!
secrethash
www.buymeacoffee.com/shashwatmishra
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/fourdotsix/tenancy-mapping
Requires
- php: ^8.3
- dallgoot/yaml: ^1.0
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
- stancl/tenancy: dev-master
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
README
Tenant data handling (mapping)
Tenant specific data handling for stancl/tenancy.
What?
Mappings are the key-value pairs that can be used to get non-sensitive data while maintaining the tenant context. These are compiled and stored in an in-memory database, ex: Redis, giving faster data access. On a base level, there are two types of mappings:
-
Descriptors: These are useful when different tenant types refers to some part of the application differently.
For example: An LMS SaaS application, can serve both businesses and educational institutions. An educational institution might refer to it's learners as Students while businesses might refer them as Employees. In such cases descriptor comes handy for dynamic descriptors. The descriptor mappings would be as such:
--- # file: university.yml name: Institution ORG mappings description: Mappings for tenant university.example.co mappings: learner: student institute: university
This can be accessed as such:
descriptor('learner')->title(); // Student descriptor(key: 'institute', default: 'institute'); // university descriptor(key: 'institute', default: 'institute', i18n: true); // महाविद्यालय
-
Settings: As the name suggests, these can be used for non-sensitive tenant specific settings. It provides faster access to settings data using key-value pairs and keeping these in YAML files can provide an audit trail for changes in GIT. Settings are merged by default, i.e., there can be a separate tenant's YAML file created with overrides and compilation will merge the settings in generic settings YAML file during compilation. This can be disabled in config file, if needed.
Why?
When creating a SaaS application, the biggest challenge is customization according to the tenant. Not all organizations are the same, therefore, tailoring specific software features can become impossible while onboarding new tenants and scalability can really take a hit.
Installation
-
Install the package via composer:
composer require fourdotsix/tenancy-mapping
-
Publish the config file with:
php artisan vendor:publish --tag="tenancy-mapping-config" -
Publish the migrations with:
php artisan vendor:publish --tag="tenancy-mapping-migrations" -
Publish the mappings folder with:
php artisan vendor:publish --tag="tenancy-mapping-mappings" -
Add the Tenancy Bootstrapper in your
config/tenancy.php:'bootstrappers' => [ ... // Integration bootstrappers ... Fourdotsix\TenancyMapping\Bootstrappers\MappingTenancyBootstrapper::class, ],
-
(optional|recommended) Set
TenantTypeenum: This helps in creating a consistent and safe way of defining tenant types. Although it's optional, it's still recommended.-
Create an Enum and implement
Fourdotsix\TenancyMapping\Contracts\TenantType, Ex:<?php namespace App\Enums; use Fourdotsix\TenancyMapping\Concerns\EnumToArray; use Fourdotsix\TenancyMapping\Contracts\TenantType as Contract; enum TenantType: string implements Contract { use EnumToArray; case Institute = 'institute'; case University = 'university'; case Business = 'business'; }
-
Add casts in your Tenant Model:
protected $casts = [ ... 'type' => TenantType::class, ... ];
-
-
(optional) Overriding
MappingType: Although this might not be needed in normal scenarios, but by overridingMappingTypeEnum you can add new mapping types as well as override Enum methods. After creating a newMappingTypeEnum you can directly use that in functions & Facades while fetching mappings.-
Create an Enum that implements
Fourdotsix\TenancyMapping\Contracts\MappingTypeand uses traitFourdotsix\TenancyMapping\Concerns\FunctionalMappingTypes, Ex:<?php namespace App\Enums; use Fourdotsix\TenancyMapping\Concerns\FunctionalMappingTypes; use Fourdotsix\TenancyMapping\Contracts\MappingType as Contract; enum MappingType: string implements Contract { use FunctionalMappingTypes; case Descriptor = 'descriptor'; case Settings = 'settings'; }
-
Usage
Mappings are key-value pairs, i.e. even nested mappings are compiled into dot-notation keys. Although, still a nested mapping can have an array as a value if it's a YAML array, i.e. YAML arrays are preserved and not converted into dot-notation.
YAML Mapping Files
Mappings are saved in the mapping directory defined in the config. By default, it doesn't needs to be changed. But feel free to change it if needed. All the mapping files follow a basic structure, i.e. they all have name, description & mappings, where mappings is the object that contains all the mappings:
--- # Descriptor name: Generic descriptor mappings description: Descriptor Mappings for all tenant types (generic) mappings: example: one exampleTwo: two ...
--- name: Settings for all tenant types (Generic) description: These are generic settings for all the tenant types mappings: fallback: - logo: https://example.com/images/logo.svg - currency: USD tracking: - active: yes - domain: null ...
Compilation
Compilation can be made using artisan command:
php artisan mappings:compile
You can force clear old compilation values using --force. These comes handy during deployments to make sure the new mappings are added and the removed mappings are cleared.
Compilations can be cleared too using artisan command:
php artisan mappings:clear
Fetching Mapping
Mappings can be fetched either by using mapping helpers or Mapping Facade.
Helper
Mappings can be fetched using helper methods. descriptor() helper returns Illuminate\Support\Stringable making it easier to manipulate strings.
use Fourdotsix\TenancyMapping\Enums\MappingType; ... // Getting a mapping value $mapping = mapping(MappingType::Descriptor, 'institute'); // university // Getting a Descriptor's Value $descriptor = descriptor(key: 'learner', default: 'learner', i18n: false); // student $descriptor->plural()->title(); // Students // Getting a settings map value $setting = settings_map(key: 'fallback.logo', default: 'https://example.com/example.png'); // (string) https://example.com/images/logo.svg $track = settings_map(key: 'tracking.active', default: false, return_type: 'bool'); // (bool) true
Facade
Facade let's you fetch mapping values as-is, without any manipulations as oppose to manipulations done by helper functions. Facade resolves \Fourdotsix\TenancyMapping\Mapping making available all the methods too.
use Fourdotsix\TenancyMapping\Facades\Mapping; use Fourdotsix\TenancyMapping\Enums\MappingType; ... // Fetch a specific mapping by key $mapping = Mapping::get(type: MappingType::Descriptor, key: 'learner'); // student // Get all the mappings for a Mapping Type $mappings = Mapping::all(MappingType::Descriptor); // array of all the mappings // Compile Mappings for a mapping type & tenant Mapping::compile($tenant, MappingType::Settings); // Clear compiled mappings for a specific mapping type Mapping::clear(MappingType::Descriptor);
Testing 🚧
🚧 WIP
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
From the folks at Four Dot Six (4.6)