elixir - Assigning Repo to module attribute? -
how can use module attributes set repo
on fly? example, have staging
, production
envs:
defmodule test @repo "staging" def repo case @repo "staging" -> alias myapp.schools.staging.repo, as: srepo "production" -> alias myapp.schools.prod.repo, as: srepo end end end
if do: test.repo
get: elixir.myapp.schools.staging.repo
so, why did not got expecting myapp.schools.staging.repo
?
edit
i tried return module without alias:
def repo case @repo "staging" -> myapp.schools.staging.repo "production" -> myapp.schools.prod.repo end end
but, got elixir.myapp.schools.staging.repo
if want switch repo based on value in params received controller functions (instead of more common way of running separate instance of app, 1 myapp.repo
pointing staging , 1 pointing production), can create function returns appropriate repo module based on params, , call functions on returned value.
defmodule myapp def repo("staging"), do: myapp.staging.repo def repo("production"), do: myapp.production.repo end defmodule myapp.postcontroller ... # env can "staging" or "production" def index(conn, %{"env" => env}) repo = myapp.repo(env) render conn, "index.html", posts: repo.all(post) end end
Comments
Post a Comment