gpt4 book ai didi

elixir - 如何在运行时配置 Ecto?

转载 作者:行者123 更新时间:2023-12-01 08:52:51 25 4
gpt4 key购买 nike

setup instructions 之后,我的 config/config.exs 文件中有以下 Ecto 配置:

config :my_app, MyApp.Repo,
adapter: Ecto.Adapters.Postgres,
url: "postgresql://postgres@localhost/myrepo",
size: 20

如果我的理解是正确的,那么 config.exs 将在编译时进行评估。

有没有办法在运行时执行此配置步骤?

这是用于将作为编译的二进制文件分发的应用程序(通过 exrm )。最终用户应该能够通过标志或环境变量来自定义数据库 url 和池大小,而不是通过编辑 sys.config

最佳答案

可以使用 {:system, "KEY" } 从系统加载,例如:

config :my_app Repo
url: {:system, "DATABASE_URL" },
size: {:system, "DATABASE_POOL_SIZE" }

反而
config :my_app, Repo,
url: "ecto://postgres:postgres@localhost/ecto_simple",
size: 20

在这种情况下,您将 Ecto 设置为使用系统属性。当然,用户必须对其进行配置。

关于elixir - 如何在运行时配置 Ecto?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37283387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com