ash_postgres/test/support/multitenancy/resources/org.ex

46 lines
910 B
Elixir
Raw Normal View History

2020-10-29 15:26:45 +13:00
defmodule AshPostgres.MultitenancyTest.Org do
@moduledoc false
use Ash.Resource,
data_layer: AshPostgres.DataLayer
resource do
identities do
2021-01-13 14:22:28 +13:00
identity(:unique_by_name, [:name])
2020-10-29 15:26:45 +13:00
end
end
attributes do
2021-01-13 14:27:39 +13:00
uuid_primary_key(:id, writable?: true)
2021-01-13 14:22:28 +13:00
attribute(:name, :string)
2020-10-29 15:26:45 +13:00
end
actions do
2022-04-20 03:08:28 +12:00
defaults([:create, :read, :update, :destroy])
2020-10-29 15:26:45 +13:00
end
postgres do
table "multitenant_orgs"
2022-08-19 06:56:36 +12:00
repo(AshPostgres.TestRepo)
2020-10-29 15:26:45 +13:00
manage_tenant do
2022-08-19 06:56:36 +12:00
template(["org_", :id])
2020-10-29 15:26:45 +13:00
end
end
multitenancy do
2021-01-13 14:22:28 +13:00
strategy(:attribute)
attribute(:id)
global?(true)
parse_attribute({__MODULE__, :tenant, []})
2020-10-29 15:26:45 +13:00
end
relationships do
2022-08-19 06:56:36 +12:00
has_many(:posts, AshPostgres.MultitenancyTest.Post, destination_attribute: :org_id)
has_many(:users, AshPostgres.MultitenancyTest.User, destination_attribute: :org_id)
2020-10-29 15:26:45 +13:00
end
def tenant("org_" <> tenant) do
tenant
end
end