← all modules

Std.Db

Std.Db
  Std.Db — relational database helpers (Layer 3 Sky source).

Types
  type alias Migration
  type SqlValue = SqlString String | SqlInt Int | SqlFloat Float | SqlBool Bool | SqlBytes String | SqlDecimal Decimal | SqlTime Int | SqlMoney Money | SqlNull SqlValue
  type SqlField = SetField SqlValue | OmitField

Values
  close : Db -> Task Error ()
      Close the connection pool (rarely needed — the pool is a memoised handle).
  connect : () -> Task Error Db
      Use the connection configured by `sky.toml`'s `[database]`
  defaultMigration : String -> Migration
      `defaultMigration "0001_users"` — Migration named with empty SQL.
  deleteById : Db -> String -> String -> Task Error Int
      `deleteById db table id` — delete one row by id; returns
  exec : Db -> String -> List a -> Task Error Int
      `exec db sql params` — parameterised SQL with bound args.
  execRaw : Db -> String -> Task Error Int
      `execRaw db sql` — SQL without bound args (DDL,
  findByConditions : Db -> String -> Dict String String -> Task Error (List (Dict String String))
      `findByConditions db table conditions` — AND-joined equality
  findManyByField : Db -> String -> String -> a -> Task Error (List (Dict String String))
      `findManyByField db table fieldName value` — equality
  findOneByField : Db -> String -> String -> a -> Task Error (Maybe (Dict String String))
      `findOneByField db table fieldName value` — equality lookup.
  fromMaybeBool : Maybe Bool -> SqlValue
      `Just b` -> `SqlBool b`; `Nothing` -> SQL NULL. For nullable BOOLEAN columns.
  fromMaybeBytes : Maybe String -> SqlValue
      `Just b` -> `SqlBytes b`; `Nothing` -> SQL NULL. For nullable BLOB columns.
  fromMaybeDecimal : Maybe Decimal -> SqlValue
      `Just d` -> `SqlDecimal d`; `Nothing` -> SQL NULL. For nullable decimal columns.
  fromMaybeFloat : Maybe Float -> SqlValue
      `Just x` -> `SqlFloat x`; `Nothing` -> SQL NULL. For nullable REAL columns.
  fromMaybeInt : Maybe Int -> SqlValue
      `Just n` -> `SqlInt n`; `Nothing` -> SQL NULL. For nullable INTEGER columns.
  fromMaybeMoney : Maybe Money -> SqlValue
      `Just m` -> `SqlMoney m`; `Nothing` -> SQL NULL. For nullable money columns.
  fromMaybeString : Maybe String -> SqlValue
      `Just s` -> `SqlString s`; `Nothing` -> SQL NULL. For nullable TEXT columns.
  fromMaybeTime : Maybe Int -> SqlValue
      `Just t` -> `SqlTime t`; `Nothing` -> SQL NULL. For nullable timestamp columns.
  getBool : String -> row -> Bool
      Read a BOOLEAN/0-1 column from a row by name, defaulting to False when absent.
  getById : Db -> String -> String -> Task Error (Maybe (Dict String String))
      `getById db table id` — fetch a single row by integer id.
  getByIdDecode : Db -> String -> Int -> Decoder a -> Task Error (Maybe a)
      `getByIdDecode db table id decoder` — typed sibling of
  getField : String -> row -> String
      Read a field as a String (the canonical row-element shape).
  getInt : String -> row -> Int
      Read an INTEGER column from a row by name, defaulting to 0 when absent/unparseable (display-only lenient reader).
  getString : String -> row -> String
      Read a TEXT column from a row by name, defaulting to "" when absent.
  insertFields : Db -> String -> List ( String, SqlField ) -> Task Error Int
      `insertFields db table fields` — DEFAULT-omittable INSERT with
  insertFieldsReturning : Db -> String -> List ( String, SqlField ) -> String -> Decoder a -> Task Error (List a)
      `insertFieldsReturning db table fields projection decoder` —
  insertRow : Db -> String -> Dict String String -> Task Error Int
      `insertRow db table fields` — insert a row built from
  migrate : Db -> List Migration -> Task Error (List String)
      Apply pending schema migrations, in list order.
  open : String -> String -> Task Error Db
      `open driver dsn` — opens a database connection.
  query : Db -> String -> List a -> Task Error (List (Dict String String))
      `query db sql params` — parameterised SELECT.  Returns rows
  queryDecode : Db -> String -> List a -> Decoder b -> Task Error (List b)
      `queryDecode db sql params decoder` — typed query with a
  unsafeFindWhere : Db -> String -> String -> List a -> Task Error (List (Dict String String))
      `unsafeFindWhere db table whereClause params` — raw `WHERE`
  updateById : Db -> String -> String -> Dict String String -> Task Error Int
      `updateById db table id fields` — partial update; returns
  updateFields : Db -> String -> List ( String, SqlValue ) -> List ( String, SqlField ) -> Task Error Int
      `updateFields db table whereCols setFields` — PATCH-style update
  withSql : String -> Migration -> Migration
      `withSql sqlBody m` — replace the migration's SQL body.
  withTransaction : Db -> (Db -> Task Error a) -> Task Error a
      Run a block of database work inside a transaction; commits