如何在运行时配置Ecto? [英] How to configure Ecto at runtime?

查看:77
本文介绍了如何在运行时配置Ecto?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照设置说明,我在 config / config.exs 文件:

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

如果我的理解是正确的,则在编译时会评估 config.exs

If my understanding is correct, the config.exs is evaluated at compile-time.

是否可以在运行时执行此配置步骤?

Is there a way to do this configuration step at runtime ?

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

This is for an app which will be distributed as a compiled binary (via exrm). The end-user should be able to customize the database url and pool size via flags or environment variables, not by editing sys.config

推荐答案

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

Loading from the system is possible by using {:system, "KEY" } e.g.:

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设置为使用系统属性。当然,用户必须对其进行配置。

In this case you set up Ecto to use the system properties. Of course, a user has to configure it.

这篇关于如何在运行时配置Ecto?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆