A ready-made database module that reads config :moebius, connection: [...]. Add it to
your supervision tree, or define your own with use Moebius.Database.
Summary
Functions
Bulk loads rows into a table with Postgres's COPY protocol, the fastest way to write
many rows. Returns {:ok, count} or {:error, message}; if any row fails, none are
written.
Asks Postgres how it will run a query, and returns the plan as text.
The pool's size and how much of it is in use.
Rolls back the current transaction, which then returns {:error, value}.
Starts this database's connection pool. See Moebius.Database for the options.
Streams the rows of a query or document query through a server-side cursor, so large results never sit in memory at once. Enumerate and resume it in the same process. Callback queries on this database share the cursor's connection and transaction. An owned transaction commits on completion or early halt and rolls back on an error, throw or exception. Inside an existing transaction, that caller controls the outcome.
Runs fun in a transaction and returns what it returns.
Functions
See Moebius.Db.run/1.
Bulk loads rows into a table with Postgres's COPY protocol, the fastest way to write
many rows. Returns {:ok, count} or {:error, message}; if any row fails, none are
written.
rows is any Enumerable of keyword lists or maps, including a lazy Stream, which is
sent in chunks so memory stays flat however many rows there are. The columns are the
keys of the first row, unless you pass :columns.
:columns- the columns to fill, in order.:chunk- rows sent per message to the server (default 5,000).
"events.csv"
|> File.stream!()
|> CSV.decode!(headers: true)
|> Stream.map(&%{name: &1["name"], at: &1["at"]})
|> MyApp.Db.copy(:events)
Asks Postgres how it will run a query, and returns the plan as text.
:analyze- also run the query and report real timings and row counts. The query runs inside a transaction that is rolled back, so writes are not kept.
See Moebius.Db.first/1.
The pool's size and how much of it is in use.
Rolls back the current transaction, which then returns {:error, value}.
Starts this database's connection pool. See Moebius.Database for the options.
Streams the rows of a query or document query through a server-side cursor, so large results never sit in memory at once. Enumerate and resume it in the same process. Callback queries on this database share the cursor's connection and transaction. An owned transaction commits on completion or early halt and rolls back on an error, throw or exception. Inside an existing transaction, that caller controls the outcome.
:chunk- rows fetched per round-trip (default 500).
db(:events) |> sort(:id) |> MyApp.Db.stream() |> Stream.each(&handle/1) |> Stream.run()
Runs fun in a transaction and returns what it returns.
Returning {:error, reason} is still a normal return and commits. Call rollback/1
to abort an application-level failure. Connection handles are valid only in the
owning process while its checkout is active.
If a statement inside fails, or fun calls rollback/1, the transaction is rolled back
and this returns {:error, reason}. If fun raises anything else, the transaction is
rolled back and the exception is re-raised. A transaction inside a transaction becomes a
savepoint, so it can fail without taking the outer one down.