Sign in

    daqing/moonway/db does not have a README file

    Column

    pub(all) struct Column {
    name : String
    col_type : ColumnType
    primary : Bool
    }

    ColumnType

    pub(all) enum ColumnType {
    IntT
    RealT
    TextT
    BoolT
    } derive(Eq,
    Debug
    )

    SQLite storage types understood by moonway.

    ColumnType::equal

    fn ColumnType::equal(ColumnType, ColumnType) -> Bool

    ColumnType::not_equal

    fn ColumnType::not_equal(x : ColumnType, y : ColumnType) -> Bool

    Database

    pub struct Database {
    inner :
    Database

    }

    Database::all

    All rows of the table, decoded into models.

    Database::changes

    fn Database::changes(self : Database) -> Int

    Number of rows changed by the most recent statement.

    Database::close

    fn Database::close(self : Database) -> Unit

    Database::count

    fn Database::count(self : Database, t : Table) -> Int

    Number of rows in the table.

    Database::delete_raw

    fn Database::delete_raw(self : Database, t : Table, id : Int) -> Bool

    Delete the row with primary key id. Returns false on failure.

    Database::exec

    fn Database::exec(self : Database, sql : String) -> Bool

    Execute a statement that returns no rows. Returns false on failure.

    Database::find

    fn[T :
    FromJson
    ] Database::find(self : Database, t : Table, id : Int) -> T?

    The first row whose primary key equals id, or None.

    Database::find_raw

    fn Database::find_raw(self : Database, t : Table, id : Int) -> Json?

    The first row whose primary key equals id, as a raw JSON object.

    Database::insert

    fn[T : ToJson] Database::insert(self : Database, t : Table, value : T) -> Bool

    Insert a model value into the table. Fields come from the value's ToJson representation, converted per the column types; fields without a matching column are ignored, columns without a matching field bind NULL.

    Database::insert_raw

    fn Database::insert_raw(self : Database, t : Table, row : Map[String, Json]) -> Bool

    Insert a raw row (column name → JSON value). Missing columns bind NULL.

    Database::last_insert_rowid

    fn Database::last_insert_rowid(self : Database) -> Int64

    Rowid of the most recent successful insert.

    Database::list_tables

    fn Database::list_tables(self : Database) -> Array[String]

    Names of user tables (sqlite_master), excluding internal sqlite_% tables, ordered by name.

    Database::migrate

    fn Database::migrate(self : Database, migrations : Array[Migration]) -> Bool

    Apply pending migrations in ascending version order. Already-applied versions (recorded in the moonway_migrations table) are skipped; the runner stops at the first failing statement and returns false.

    Database::query

    fn[T :
    FromJson
    ] Database::query(self : Database, t : Table, where_clause : String, args : Array[
    SqlValue
    ]) -> Array[T]

    Rows matching a raw WHERE clause with bound arguments, decoded into models. Rows that fail to decode are skipped.

    Database::select_raw

    fn Database::select_raw(self : Database, t : Table, where_clause : String, args : Array[
    SqlValue
    ], limit? : Int, offset? : Int) -> Array[Json]

    Select rows as raw JSON objects with LIMIT/OFFSET pagination. The where clause is appended verbatim after WHERE (pass "1=1" for no filter).

    Database::table_schema

    fn Database::table_schema(self : Database, name : String) -> Table?

    Rebuild a Table descriptor from SQLite's catalog (PRAGMA table_info). Declared types map by keyword: BOOL → BoolT, INT → IntT, REAL/FLOA/DOUB → RealT, everything else → TextT. Returns None for unknown tables.

    Database::update_raw

    fn Database::update_raw(self : Database, t : Table, id : Int, row : Map[String, Json]) -> Bool

    Update the row with primary key id, setting every non-primary column from row (missing columns become NULL). Returns false on failure.

    Migration

    pub(all) struct Migration {
    version : Int
    name : String
    statements : Array[String]
    }

    Table

    pub(all) struct Table {
    name : String
    columns : Array[Column]
    }

    Table::create_sql

    fn Table::create_sql(self : Table) -> String

    CREATE TABLE IF NOT EXISTS statement for this table.

    column

    fn column(name : String, col_type : ColumnType, primary? : Bool) -> Column

    json_to_sql

    fn json_to_sql(j : Json, col_type : ColumnType) ->
    SqlValue
    ?

    Convert a JSON field value (as produced by ToJson-derived models) into a bindable SQLite value for the given column type. Json null maps to SQL NULL. Mismatched shapes degrade to best-effort conversions — the schema and the struct are expected to agree.

    migration

    fn migration(version : Int, name : String, statements : Array[String]) -> Migration

    Declare a forward-only migration: its version, a human-readable name and the SQL statements to apply, in order.

    open

    fn open(path : String) -> Database?

    Open (creating if needed) a SQLite database file. Returns None when the file cannot be opened.

    sql_to_json

    fn sql_to_json(v :
    SqlValue
    , col_type : ColumnType) -> Json

    Convert a SQLite column value back into JSON (for FromJson-derived models), honoring the column type — BoolT columns become JSON booleans.

    table

    fn table(name : String, columns : Array[Column]) -> Table

    Declare a table. This description is the source of truth for migrations and the admin UI. Row values flow through the builtin derive(ToJson,FromJson) — struct field names are the keys, column types drive the storage conversion.