Moebius.Db (Moebius v5.0.1)

Copy Markdown View Source

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

all(table)

See Moebius.Db.run/1.

child_spec(opts)

copy(table, rows, opts \\ [])

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)

create_document_table(name)

create_document_table(cmd, doc)

explain(cmd, opts \\ [])

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.

find(cmd, id)

first(cmd)

one(table)

See Moebius.Db.first/1.

pool_status()

The pool's size and how much of it is in use.

rollback(value)

Rolls back the current transaction, which then returns {:error, value}.

run(sql)

run(sql, params)

run(sql, conn, params)

run_batch(command_batch)

save(cmd, doc)

save(cmd, doc, conn)

start_link(opts)

Starts this database's connection pool. See Moebius.Database for the options.

stream(cmd, opts \\ [])

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()

transact_batch(command_batch)

transaction(fun)

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.