nallwhy

nallwhy

Is it possible to query a custom query with dynamic repo?

I made MyApp.Repo module following Replicas and dynamic repositories — Ecto v3.8.4.

It works for functions in MyApp.Repo, but not works for custom queries.

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :carrier,
    adapter: Ecto.Adapters.Postgres

  def with_dynamic_repo(credentials, callback) do
    default_dynamic_repo = get_dynamic_repo()
    start_opts = [name: nil, pool_size: 1] ++ credentials
    {:ok, repo} = __MODULE__.start_link(start_opts)

    try do
      put_dynamic_repo(repo)
      callback.()
    after
      put_dynamic_repo(default_dynamic_repo)
      Supervisor.stop(repo)
    end
  end
end

credentials = [
  hostname: "localhost",
  username: "customer",
  password: "password",
  port: 48141,
  database: "customer_db"
]

MyApp.Repo.with_dynamic_repo(credentials, fn ->
  # 'orders' table is in 'customer_db' table
  """
  SELECT * FROM orders LIMIT 10;
  """
  
  Ecto.Adapters.SQL.query!(MyApp.Repo, query, [])
end)

# relation "orders" does not exist

It works on below.

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :carrier,
    adapter: Ecto.Adapters.Postgres

  def with_dynamic_repo(credentials, callback) do
    default_dynamic_repo = get_dynamic_repo()
    start_opts = [name: nil, pool_size: 1] ++ credentials
    {:ok, repo} = __MODULE__.start_link(start_opts)

    try do
      put_dynamic_repo(repo)
      callback.(repo)
    after
      put_dynamic_repo(default_dynamic_repo)
      Supervisor.stop(repo)
    end
  end
end

credentials = [
  hostname: "localhost",
  username: "customer",
  password: "password",
  port: 48141,
  database: "customer_db"
]

MyApp.Repo.with_dynamic_repo(credentials, fn repo ->
  # 'orders' table is in 'customer_db' table
  """
  SELECT * FROM orders LIMIT 10;
  """
  
  Ecto.Adapters.SQL.query!(repo, query, [])
end)

But with that, there is no meaning to using dynamic repo.
I don’t know how to use transaction with pid of repo.

Is it possible to query a custom query with dynamic repo?

Marked As Solved

al2o3cr

al2o3cr

From the docs for Ecto.Adapters.SQL:

Generally speaking, you must invoke those functions directly from your repository, for example: MyApp.Repo.query("SELECT true") . You can also invoke them direcltly from Ecto.Adapters.SQL, but keep in mind that in such cases features such as “dynamic repositories” won’t be available.

You want the functions that are added to your repo, for instance MyApp.Repo.query!:

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement