async_postgres/pg_types/row_mapping

Procs

proc fieldContext(owner, field: string): string {....raises: [], tags: [],
    forbids: [].}
Error prefix naming the field, or only its owner for a positional one.
proc findColumn(data: RowData; owner, field, key: string; exact: bool;
                optional = false; fromQuery = true): int {.
    ...raises: [PgTypeError], tags: [], forbids: [].}
Index of the column key names among data's fields. Unless exact, case and _ are ignored as in Nim identifiers, and among several such matches only a byte-exact one is taken. When optional, no match is -1 instead of an error. fromQuery is whether the columns come from the caller's query, which could alias them.
proc getDomain[T: distinct](row: Row; col: int): T
Read a domain column as the distinct type T, the way to reads a field of that type: with a get overload for T visible where this is called, else as the type T is declared from, by the same rule. A get for T must not call this for T: it would call itself. PostgreSQL sends a domain column as its base type, so a DateTime base follows the column's type (timestamp, timestamptz or date). An array over a domain cannot be read in binary (see rfBinary).
proc getDomainOpt[T: distinct](row: Row; col: int): Option[T]
NULL-safe version of getDomain.
proc mapRow[T: object | tuple | ref object](row: Row; _: typedesc[T];
    cols: var seq[int]): T
to(row, T) with the field-to-column indices cached in cols: resolved on the first row, or before a loop by resolveRowColumns. Sibling-only.
proc resolveRowColumns[T](data: RowData; _: typedesc[T]; cols: var seq[int])
mapRow's field-to-column resolution without a row, so a result with no rows still reports a field that has no column or matches several, or an anonymous tuple of the wrong length. cols stays empty for an anonymous tuple, which maps by position. Sibling-only.
proc to[T: object | tuple | ref object](row: Row; _: typedesc[T]): T

Read row into a T: an object, a ref object or a named tuple field by field, each from the column its name matches, or an anonymous tuple from all the columns in order.

A field name matches a column ignoring case and _, as Nim identifiers do, so createdAt reads created_at; among several matches only a byte-exact one is taken. {.pgColumn: "name".} names the column instead (matched byte for byte) and {.pgSkip.} leaves the field at its default. Columns no field matches are ignored.

A field of type F is read with row.get(col, F), so a get overload declared for your own type is used, as is the one pgComposite declares; either must be visible where to is called. Without one: Option[F] is none for NULL; int reads any integer column; DateTime, seq[DateTime] and their ranges and multiranges, with bounds of a distinct DateTime too, follow the column's type (timestamp, timestamptz or date); an enum, seq of one or seq[Option] of one goes through getEnum / getEnumArray / getEnumArrayElemOpt; PgArray[E] through getArrayND; a distinct type (a domain) as its base type. Any other type, an object included, is a compile error.

PgArray[E] needs the column in binary format: rfAuto sends text on a statement's first execution, and simpleQuery / rfText never send binary, so query with rfBinary.

Raises PgTypeError, prefixed with T.field, when a field has no column, matches more than one, or cannot be read (a NULL in a field that is not an Option, for one). Name matching needs the row's field metadata, which query results carry.

type User = object
  id: int64
  name: string
  createdAt: DateTime
  email: Option[string]

let row = await conn.queryRow(
  sql"SELECT id, name, created_at, email FROM users WHERE id = {id}")
let user = row.to(User)

Macros

macro isCaseObject(T: typedesc): bool
Whether T or one of its bases has a case section.

Templates

template pgColumn(name: string) {.pragma.}
Field pragma for to: read the column name, matched byte for byte, instead of the one the field name matches.
template pgSkip() {.pragma.}
Field pragma for to: read no column and keep the field's default.