Ecto.Repo是数据库的包装器
我们定义存储库的方式是
defmodule Repo do
use Ecto.Repo, otp_app : :my_app
end
Repo的配置必须在你应用程序环境中
通常是在config/config.exs:
config :my_app, Repo,
adapter: Ecto.Adapters.Postgres,
database: "ecto_simple",
username: "postgres",
password: "postgres",
hostname: "localhost",
# OR use a URL to connect instead
url: "postgres://postgres:postgres@localhost/ecto_simple"
Repo中每一个存储库都定义了start_link/0
通常这个函数是不会调用的,二十应用程序的监督树的一部分
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(Repo, [])
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end