Ash Multi-Tenancy
Ash 는 Multitenancy 를 기본적으로 지원한다.
defmodule Hoing.Clubs.Member do
# ...
multitenancy do
strategy :attribute
attribute :club_id
end
relationships do
belongs_to :club, Hoing.Clubs.Club do
allow_nil? false
end
belongs_to :user, Hoing.Accounts.User do
allow_nil? false
end
end
identities do
identity :club_scoped_user, [:user_id], message: "already exists in this club"
end
end
위와같이 설정하기만 하면 바로 multi-tenancy 를 적용할 수 있다.
Multi-Tenancy 를 적용하면?
모든 action 은 항상 tenant 값이 있어야 동작한다.
Read 시 설정된 Tenant 값으로 항상 filtering 된다.
identity 는 기본적으로 tenant scope 내에서 적용된다.
위club_scoped_useridentity 에 :user_id 만 unique 설정을 했지만 실제로는 club_id, user_id 가 unique 조건으로 걸린다.
단순하게 Multi-Tenancy 를 적용할 수 있어서 좋았다.
다만 가끔 Read 시 tenancy 를 확인하지 않고 싶은 경우가 있을 수 있다.
그럴땐 아래와 같이 read action 에서 multitenancy 설정을 bypass 로 하면 tenant setting 없이도 조회 가능하다.
defmodule Hoing.Clubs.Member do
# ...
actions do
read :read do
primary? true
multitenancy :bypass
end
end
multitenancy do
strategy :attribute
attribute :club_id
end
end