, Module.create/3
. : , , , , .
, . , .
defmodule A do
def go(module, table, fields) do
Module.create(module, quote do
use MyApp.Web, :model
schema unquote(table) do
unquote(for {name, type} <- fields do
quote do
field unquote(name), unquote(type)
end
end)
end
end, Macro.Env.location(__ENV__))
end
end
A.go MyPost, "posts", [
{:title, :string},
{:content, :string},
]
IO.inspect MyApp.Repo.all(MyPost)
:
[debug] QUERY OK source="posts" db=2.8ms queue=0.1ms
SELECT p0."id", p0."title", p0."content" FROM "posts" AS p0 []
[%MyPost{__meta__:
content: "Hello from Joe", id: 1, title: "Hello"},
%MyPost{__meta__:
content: "Hello from Mike", id: 2, title: "Hello"},
%MyPost{__meta__:
content: "Hello from Robert", id: 3, title: "Hello"}]