Fly.Repo (Fly Postgres v0.3.4) View Source

This wraps the built-in Ecto.Repo functions to proxy writable functions like insert, update and delete to be performed on the an Elixir node in the primary region.

To use it, rename your existing repo module and add a new module with the same name as your original repo like this.

Original code:

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

Changes to:

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

  # Dynamically configure the database url based for runtime environment.
  def init(_type, config) do
    {:ok, Keyword.put(config, :url, Fly.Postgres.database_url())}
  end
end

defmodule Core.Repo do
  use Fly.Repo, local_repo: MyApp.Repo.Local
end

Using the same name allows your existing code to seamlessly work with the new repo.

When explicitly managing database transactions like using Multi or start_transaction, when used to modify data, those functions should be called by an RPC so they run in the primary region.

Fly.RPC.rpc_region(:primary, {MyModule, :my_function_that_uses_multi, [my,
args]}, opts)