← all modules

Std.Db.Store

Std.Db.Store
  Std.Db.Store — codec-driven persistence (Layer 3 Sky source).

Types
  type Store = Store { name : String , codec : Codec a , pk : String , cols : List ( String, String ) , generated : List String , computed : List ( String, () -> SqlValue ) }
  type alias Table
  type Project = Project (List Table)
  type Cond = CondOp String String SqlValue | CondNull String Bool | CondIn String (List SqlValue) | CondAnd (List Cond) | CondOr (List Cond) | CondNot Cond
  type OrderTerm = OrderTerm String String
  type Query = Query { store : Store a , conds : List Cond , orders : List OrderTerm , limitN : Maybe Int , offsetN : Maybe Int }

Values
  adjust : Db -> Store a -> Cond -> List ( String, Int ) -> Task Error Int
      Atomic relative adjustment of integer columns — `SET col = col + delta` per
  all : Db -> Store a -> Task Error (List a)
      Every row, decoded via the codec.
  and_ : List Cond -> Cond
      All of the conditions (AND). An empty list is always true.
  count : Db -> Query a -> Task Error Int
      Count matching rows via `SELECT COUNT(*)` (filters apply; order/paging ignored).
  create : Db -> Store a -> Task Error ()
      Create the table (dialect-correct DDL derived from the codec).
  defaultBool : String -> Bool -> Store a -> Store a
      Give a column a `DEFAULT` boolean value in the generated DDL (`TRUE`/`FALSE`
  defaultInt : String -> Int -> Store a -> Store a
      Give a column a `DEFAULT` integer value in the generated DDL:
  defaultNow : String -> Store a -> Store a
      Give a column a `DEFAULT` of the current timestamp (`now()` on Postgres,
  defaultText : String -> String -> Store a -> Store a
      Give a column a `DEFAULT` text value in the generated DDL:
  defaultWith : String -> (() -> SqlValue) -> Store a -> Store a
      Give a column an APP-SIDE computed default: a `() -> SqlValue` the runtime
  delete : Db -> Store a -> String -> String -> Task Error Int
      Delete rows where `col = value` (single column).
  deleteWhere : Db -> Store a -> Cond -> Task Error Int
      Delete every row matching a `Cond` — for compound conditions the single-column
  dropProject : Db -> Project -> Task Error (List String)
      DROP every table the project declares PLUS `_sky_migrations` — a fresh
  dropTable : Db -> String -> Task Error (List String)
      DROP a single named table. Does NOT touch `_sky_migrations`.
  dumpSchema : Project -> Task Error ()
      Print the project's schema as JSON (between SKY_SCHEMA_* markers) and exit —
  eq : String -> SqlValue -> Cond
      `col = value`.
  findBy : Db -> Store a -> String -> String -> Task Error (Maybe a)
      The single row where `col = value` (value bound as text), or Nothing.
  fromCodec : String -> Codec a -> Store a
      Define a store from a table name and its codec. The columns are derived
  generated : List String -> Store a -> Store a
      Mark columns the DATABASE fills — a `serial` PK, a `DEFAULT`/`defaultNow`
  gt : String -> SqlValue -> Cond
      `col > value`.
  gte : String -> SqlValue -> Cond
      `col >= value`.
  inList : String -> List SqlValue -> Cond
      `col IN (v1, v2, …)`. An empty list matches nothing.
  insert : Db -> Store a -> a -> Task Error Int
      Insert a record. Columns marked `generated` (a `serial` PK, `defaultNow`) are
  insertMany : Db -> Store a -> List a -> Task Error Int
      Bulk-insert many records in ONE multi-row INSERT — for time-series / batch
  isNull : String -> Cond
      `col IS NULL`.
  like : String -> String -> Cond
      `col LIKE pattern` (use `%` / `_` wildcards in the pattern).
  limit : Int -> Query a -> Query a
      `LIMIT n`.
  lt : String -> SqlValue -> Cond
      `col < value`.
  lte : String -> SqlValue -> Cond
      `col <= value`.
  migrate : Db -> Store a -> Task Error (List String)
      Safe additive migration: create the table if absent, and ADD any columns
  neq : String -> SqlValue -> Cond
      `col <> value`.
  notNull : String -> Cond
      `col IS NOT NULL`.
  not_ : Cond -> Cond
      Negate a condition (NOT).
  offset : Int -> Query a -> Query a
      `OFFSET n` (pair with `limit` for paging).
  or_ : List Cond -> Cond
      Any of the conditions (OR). An empty list is always false.
  orderAsc : String -> Query a -> Query a
      Append `ORDER BY col ASC` (chain for multi-column ordering).
  orderDesc : String -> Query a -> Query a
      Append `ORDER BY col DESC`.
  primaryKey : String -> Store a -> Store a
      Mark the primary-key column (NOT NULL; used by `findBy`/`delete`/`update`).
  project : List Table -> Project
      Build a `Project` from a list of erased tables (via `toTable`). Expose the
  projectTableCount : Project -> Int
      How many tables the project declares — the count `sky db reset` / `sky db
  pushProject : Db -> Project -> Task Error (List String)
      Push the whole project's schema to the live DB — create each missing table
  query : Store a -> Query a
      Start a query over a store:
  resetProject : Db -> Project -> Task Error (List String)
      EMPTY the data from every table the project declares — keeps the schema and
  resetTable : Db -> String -> Task Error (List String)
      EMPTY the data from a single named table (schema + ledger untouched).
  selectRaw : Db -> Codec row -> String -> List SqlValue -> Task Error (List row)
      Run ANY SQL and decode each row into a typed projection record via a codec —
  serial : String -> Store a -> Store a
      Mark an INTEGER primary key as auto-increment (SQLite `INTEGER PRIMARY KEY
  setFields : Db -> Store a -> SqlValue -> List ( String, SqlValue ) -> Task Error Int
      PARTIAL-column update by primary key — the everyday PATCH-by-id:
  sqlOf : Codec a -> a -> SqlValue
      Encode a TYPED value to a `SqlValue` via its codec, so a filter can compare
  toList : Db -> Query a -> Task Error (List a)
      Run the query, decoding every matching row via the store's codec.
  toMaybe : Db -> Query a -> Task Error (Maybe a)
      Run the query with `LIMIT 1`, returning the first matching row or `Nothing`.
  toTable : Store a -> Table
      Erase a store to its `Table` schema.
  touchOnUpdate : String -> Store a -> Store a
      Mark a timestamp column that the DB stamps on INSERT (`defaultNow`) AND that
  transaction : Db -> (Db -> Task Error a) -> Task Error a
      Run a callback inside a DB transaction — Store operations compose by taking
  unique : String -> Store a -> Store a
      Mark a column `UNIQUE` in the generated DDL (e.g. a login email):
  update : Db -> Store a -> a -> Task Error Int
      Update the row identified by the record's PRIMARY KEY, setting every column
  updateFields : Db -> Store a -> Cond -> List ( String, SqlValue ) -> Task Error Int
      PARTIAL-column update by `Cond`: `SET` only the named columns and leave the
  updateWhere : Db -> Store a -> Cond -> a -> Task Error Int
      Update every row matching a `Cond` to the record's column values (PK +
  upsert : Db -> Store a -> a -> Task Error Int
      Insert the record, or UPDATE it in place if its primary key already exists
  where_ : Cond -> Query a -> Query a
      Add a condition. Multiple `where_` calls are AND-combined; use `and_` / `or_`