Ecto.Repo.put_dynamic_repo
put_dynamic_repo
, go back to Ecto.Repo module for more information.
Specs
Sets the dynamic repository to be used in further interactions.
Sometimes you may want a single Ecto repository to talk to
many different database instances. By default, when you call
MyApp.Repo.start_link/1
, it will start a repository with
name MyApp.Repo
. But if you want to start multiple repositories,
you can give each of them a different name:
MyApp.Repo.start_link(name: :tenant_foo, hostname: "foo.example.com")
MyApp.Repo.start_link(name: :tenant_bar, hostname: "bar.example.com")
You can also start repositories without names by explicitly setting the name to nil:
MyApp.Repo.start_link(name: nil, hostname: "temp.example.com")
However, once the repository is started, you can't directly interact with
it, since all operations in MyApp.Repo
are sent by default to the repository
named MyApp.Repo
. You can change the default repo at compile time with:
use Ecto.Repo, default_dynamic_repo: :name_of_repo
Or you can change it anytime at runtime by calling put_dynamic_repo/1
:
MyApp.Repo.put_dynamic_repo(:tenant_foo)
From this moment on, all future queries done by the current process will
run on :tenant_foo
.
Note this feature is experimental and may be changed or removed in future releases.