埃克托 |如何获取应用程序中的所有模式模块
Ecto | How to get all schema modules in the application
我正在尝试为一些脚手架的自动化创建一个混合任务,我想获取应用程序中所有具有架构和嵌入架构实现的模块名称的列表。最终我想要一个包含字段定义的所有模式的列表,而不仅仅是模块名称。
我们能得到它吗?如果是,那么 ecto 如何提供这样的 api?
根据 Ecto.Schema
的文档,
Any schema module will generate the __schema__
function that can be used for runtime introspection of the schema.
您可以将它与 Erlang 和 Elixir 自己的内省功能一起使用,以过滤所有相关模块并巧妙地检索任何已定义模式的详细信息,例如:
{:ok, modules} = :application.get_key(:your_app, :modules)
modules
|> Enum.filter(&({:__schema__, 1} in &1.__info__(:functions)))
|> Enum.each(&(IO.inspect(&1.__schema__(:fields))))
我正在尝试为一些脚手架的自动化创建一个混合任务,我想获取应用程序中所有具有架构和嵌入架构实现的模块名称的列表。最终我想要一个包含字段定义的所有模式的列表,而不仅仅是模块名称。 我们能得到它吗?如果是,那么 ecto 如何提供这样的 api?
根据 Ecto.Schema
的文档,
Any schema module will generate the
__schema__
function that can be used for runtime introspection of the schema.
您可以将它与 Erlang 和 Elixir 自己的内省功能一起使用,以过滤所有相关模块并巧妙地检索任何已定义模式的详细信息,例如:
{:ok, modules} = :application.get_key(:your_app, :modules)
modules
|> Enum.filter(&({:__schema__, 1} in &1.__info__(:functions)))
|> Enum.each(&(IO.inspect(&1.__schema__(:fields))))