Ash FrameworkAF
Ash Framework7mo ago
7 replies
Joan Gavelán

Multiple Tenant Resources

In my app, I have two tenant resources: Organization and Establishment. I'm using the new scope feature and the value for current_tenant is a map containing both tenants. I need to provide an Ash.ToTenant implementation that extracts the correct tenant depending on the resource being queried.

I've tried this:

# organization.ex
defimpl Ash.ToTenant do
  def to_tenant(%{organization: %{id: id}}, _resource), do: id
  def to_tenant(%{id: id}, _resource), do: id
  def to_tenant(id, _resource) when is_binary(id), do: id
end


# establishment.ex
defimpl Ash.ToTenant do
  def to_tenant(%{establishment: %{id: id}}, _resource), do: id
  def to_tenant(%{id: id}, _resource), do: id
  def to_tenant(id, _resource) when is_binary(id), do: id
end


But I'm getting the following error:

[error] ** (Protocol.UndefinedError) protocol Ash.ToTenant not implemented for type Map


And when I provide implementations for the
Map
type:
# organization.ex
defimpl Ash.ToTenant, for: Map do
  def to_tenant(%{organization: %{id: id}}, _resource), do: id
end

# establishment.ex
defimpl Ash.ToTenant, for: Map do
  def to_tenant(%{establishment: %{id: id}}, _resource), do: id
end


It works but, I get the following warning:
    warning: redefining module Ash.ToTenant.Map (current version loaded from _build/dev/lib/lamashka/ebin/Elixir.Ash.ToTenant.Map.beam)
    │
 94 │   defimpl Ash.ToTenant, for: Map do
    │   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/lamashka/establishments/establishment.ex:94: Ash.ToTenant.Map (module)


Should I just ignore it, or is there a better approach to this?
Was this page helpful?