Moebius.Database (Moebius v5.0.0)

Copy Markdown View Source

Turns a module into a database you can run commands against.

defmodule MyApp.Db do
  use Moebius.Database
end

# in your application's supervision tree
children = [
  {MyApp.Db, Moebius.get_connection()}
]

Each database module owns a pool of connections, started as one child of your tree. If Postgres goes away, the pool keeps running and calls return {:error, message} until it comes back. Nothing else in your tree restarts.

Connection options

  • :url - postgres://user:pass@host:port/database, or the separate :hostname, :port, :database, :username, :password. A missing username or password falls back to PGUSER / PGPASSWORD.
  • :socket_dir - connect over a Unix socket in this directory instead of TCP.
  • :ssl - true (use TLS if the server offers it) or :required, with :ssl_opts.
  • :pool_size - the most connections the pool opens (default 10).
  • :pool_min - connections opened at start and kept when idle (default: pool_size, so the pool is full from the start). Set it lower to let an idle pool shrink.
  • :checkout_timeout - how long a call waits for a free connection, in ms (default 5000).
  • :queue_max - how many calls may wait for a connection at once (default 50).
  • :max_lifetime - recycle each connection after this many ms, for proxies and firewalls that drop long-lived connections.
  • :statement_timeout, :lock_timeout, :idle_in_transaction_session_timeout - Postgres settings applied to every connection, e.g. statement_timeout: "30s". Setting statement_timeout is a good idea: the driver itself waits for a query forever.
  • :settings - any other Postgres settings, as a keyword list.
  • :application_name - shown in pg_stat_activity (default "moebius").

Summary

Functions

Runs a command on the connection it names (cmd.conn is the database module), or on the connection of a transaction.

Functions

execute(cmd)

Runs a command on the connection it names (cmd.conn is the database module), or on the connection of a transaction.

execute(cmd, conn)