-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathash_sql.ex
More file actions
50 lines (42 loc) · 1.31 KB
/
ash_sql.ex
File metadata and controls
50 lines (42 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# SPDX-FileCopyrightText: 2024 ash_sql contributors <https://github.com/ash-project/ash_sql/graphs/contributors>
#
# SPDX-License-Identifier: MIT
defmodule AshSql do
@moduledoc false
def dynamic_repo(resource, sql_behaviour, %{
__ash_bindings__: %{context: %{data_layer: %{repo: repo}}}
}) do
repo || sql_behaviour.repo(resource, :read)
end
def dynamic_repo(resource, sql_behaviour, %struct{context: %{data_layer: %{repo: repo}}}) do
type = struct_to_repo_type(struct)
repo || sql_behaviour.repo(resource, type)
end
def dynamic_repo(resource, sql_behaviour, %struct{}) do
sql_behaviour.repo(resource, struct_to_repo_type(struct))
end
def repo_opts(_repo, sql_behaviour, timeout, tenant, resource) do
if Ash.Resource.Info.multitenancy_strategy(resource) == :context do
[prefix: tenant]
else
if schema = sql_behaviour.schema(resource) do
[prefix: schema]
else
[]
end
end
|> add_timeout(timeout)
end
defp add_timeout(opts, timeout) when not is_nil(timeout) do
Keyword.put(opts, :timeout, timeout)
end
defp add_timeout(opts, _), do: opts
defp struct_to_repo_type(struct) do
case struct do
Ash.Changeset -> :mutate
Ash.Query -> :read
Ecto.Query -> :read
Ecto.Changeset -> :mutate
end
end
end