2021-07-25 03:28:58 +12:00
|
|
|
defmodule AshPostgres.MultitenancyTest.User do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:52:28 +13:00
|
|
|
domain: AshPostgres.MultitenancyTest.Domain,
|
2021-07-25 03:28:58 +12:00
|
|
|
data_layer: AshPostgres.DataLayer
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id, writable?: true)
|
2024-03-28 09:52:28 +13:00
|
|
|
attribute(:name, :string, public?: true)
|
|
|
|
attribute(:org_id, :uuid, public?: true)
|
2021-07-25 03:28:58 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
postgres do
|
|
|
|
table "users"
|
|
|
|
repo AshPostgres.TestRepo
|
|
|
|
end
|
|
|
|
|
2022-04-20 03:08:28 +12:00
|
|
|
actions do
|
2024-03-28 09:52:28 +13:00
|
|
|
default_accept(:*)
|
|
|
|
|
2022-04-20 03:08:28 +12:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
2021-07-25 03:28:58 +12:00
|
|
|
multitenancy do
|
|
|
|
# Tells the resource to use the data layer
|
|
|
|
# multitenancy, in this case separate postgres schemas
|
|
|
|
strategy(:attribute)
|
|
|
|
attribute(:org_id)
|
|
|
|
parse_attribute({__MODULE__, :parse_tenant, []})
|
|
|
|
global?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:52:28 +13:00
|
|
|
belongs_to(:org, AshPostgres.MultitenancyTest.Org) do
|
|
|
|
public?(true)
|
|
|
|
end
|
2024-04-23 12:42:11 +12:00
|
|
|
|
|
|
|
has_many :posts, AshPostgres.MultitenancyTest.Post do
|
|
|
|
public?(true)
|
|
|
|
end
|
2021-07-25 03:28:58 +12:00
|
|
|
end
|
|
|
|
|
2024-04-23 11:06:06 +12:00
|
|
|
aggregates do
|
|
|
|
list(:years_visited, :posts, :last_word)
|
|
|
|
end
|
|
|
|
|
2021-07-25 03:28:58 +12:00
|
|
|
def parse_tenant("org_" <> id), do: id
|
|
|
|
end
|