AshScenario.Multitenancy (ash_scenario v0.6.0)
View SourceHelper functions for detecting and handling multitenancy in Ash resources.
This module provides automatic detection of attribute-based multitenancy and extracts tenant values from attributes without requiring any changes to prototype or scenario definitions.
Summary
Functions
Builds create options with tenant if needed.
Extracts tenant information from attributes for a given resource.
Checks if a resource uses attribute-based multitenancy.
Checks if a resource has multitenancy configured.
Gets the multitenancy strategy for a resource.
Gets the tenant attribute name for a resource with attribute-based multitenancy.
Functions
Builds create options with tenant if needed.
Takes existing options and adds the tenant option if a tenant value is provided.
Extracts tenant information from attributes for a given resource.
For resources with attribute-based multitenancy, this function:
- Detects the tenant attribute name
- Extracts the tenant value from attributes
- Returns clean attributes without the tenant attribute
For resources without multitenancy or with context-based multitenancy, returns the original attributes unchanged.
Examples
# Resource with attribute multitenancy on :organization_id
iex> extract_tenant_info(MyApp.Post, %{title: "Test", organization_id: "org-123"})
{:ok, "org-123", %{title: "Test"}}
# Resource without multitenancy
iex> extract_tenant_info(MyApp.Blog, %{name: "Test Blog"})
{:ok, nil, %{name: "Test Blog"}}
Checks if a resource uses attribute-based multitenancy.
Examples
iex> has_attribute_multitenancy?(MyApp.Post)
true
iex> has_attribute_multitenancy?(MyApp.Blog)
false
Checks if a resource has multitenancy configured.
Examples
iex> has_multitenancy?(MyApp.Post)
true
iex> has_multitenancy?(MyApp.Blog)
false
@spec multitenancy_strategy(module()) :: :attribute | :context | nil
Gets the multitenancy strategy for a resource.
Returns :attribute, :context, or nil.
Gets the tenant attribute name for a resource with attribute-based multitenancy.
Returns nil if the resource doesn't use attribute-based multitenancy.