我需要在config.exs中定义多个ecto Repo,但我不想一个一个定义:
config CC, CC.Repo.S0,
adapter: Ecto.Adapters.Postgres,
hostname: "192.168.0.100",
database: "postgres",
username: "postgres",
password: "12345678"
config CC, CC.Repo.S1,
adapter: Ecto.Adapters.Postgres,
hostname: "192.168.0.101",
database: "postgres",
username: "postgres",
password: "12345678"
...
所以我定义了一个 repo 列表并尝试在循环中定义它们:
__repo_all__ = [
[ hostname: "192.168.0.100",
database: "postgres",
username: "postgres",
password: "12345678" ],
[ hostname: "192.168.0.101",
database: "postgres",
username: "postgres",
password: "12345678" ]]
__repo_count__ = Enum.count(__repo_all__)
config CC, :repo_all, __repo_all__
config CC, :repo_count, __repo_count__
Enum.reduce(__repo_all__, 0, fn(opts, n) ->
config CC, String.to_atom(Enum.join([CC.Repo, ".S", n])),
[{:adapter, Ecto.Adapters.Postgres} | opts]
n + 1
end)
调用 Application.get_all_env(CC) 时我看不到任何 repo 配置,但 :repo_all 和 :repo_count 的配置值都是可见的。
我该怎么做才能让它发挥作用?
提前致谢!