forked from ash-project/ash_sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathash_sql.ex
More file actions
60 lines (52 loc) · 1.52 KB
/
Copy pathash_sql.ex
File metadata and controls
60 lines (52 loc) · 1.52 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
51
52
53
54
55
56
57
58
59
60
# 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}}}
} = query
) do
repo || sql_behaviour.repo(resource, repo_type(query))
end
def dynamic_repo(
resource,
sql_behaviour,
%_{context: %{data_layer: %{repo: repo}}} = query
) do
repo || sql_behaviour.repo(resource, repo_type(query))
end
def dynamic_repo(resource, sql_behaviour, query) do
sql_behaviour.repo(resource, repo_type(query))
end
defp repo_type(%{lock: lock}) when not is_nil(lock), do: :mutate
defp repo_type(%struct{}), do: struct_to_repo_type(struct)
defp repo_type(_), do: :read
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