Sign in

    moonbitstack/moonctl/model does not have a README file

    DataSourceError

    pub suberror DataSourceError {
    BadDsn(String)
    }

    Raised when a DSN string cannot be parsed into a DataSource.

    DataSource

    pub(all) enum DataSource {
    Sqlite(String)
    Postgres(PgTarget)
    }

    A parsed datasource DSN: which backend to read, and how to reach it.

    DdlColumn

    pub(all) struct DdlColumn {
    name : String
    type_ : String
    primary_key : Bool
    nullable : Bool
    default_ : String?
    }

    One parsed column of a CREATE TABLE statement: the SQL column name, the MoonBit type_ its SQL type maps to (Int/Int64/String/Double/Bool/ Bytes), whether it is a primary key, whether it accepts NULL, and its literal DEFAULT clause if any (kept as source text — it is emitted into DDL comments, never bound).

    DdlTable

    pub(all) struct DdlTable {
    name : String
    columns : Array[DdlColumn]
    }

    A parsed CREATE TABLE: the SQL table name and its ordered columns.

    PgTarget

    pub(all) struct PgTarget {
    host : String
    port : Int
    user : String
    password : String
    database : String
    }

    A PostgreSQL connection target parsed from a postgres:// DSN.

    ReflectedColumn

    pub(all) struct ReflectedColumn {
    table : String
    name : String
    sql_type : String
    primary_key : Bool
    nullable : Bool
    }

    One column of a reflected database schema: the table it belongs to, its name, its raw SQL type text (INTEGER, character varying, VARCHAR(255), …), and whether it is a primary key / accepts NULL. This is the neutral shape a live reader (SQLite PRAGMA table_info, PostgreSQL information_schema.columns) produces and tables_from_reflection folds into DdlTables.

    generate_crud

    fn generate_crud(tables : Array[DdlTable]) -> String

    Generate a moonorm data-access layer from parsed DDL: for every table, the record struct, its @moonorm.Model (columns + from_row + to_columns), a @moonorm.Table descriptor, an up/down migration pair, and typed CRUD (insert/all, plus find_by_id/update/delete_by_id when the table has a single-column primary key). The output compiles against moonbitstack/moonorm + moonbitstack/moondb; a consuming package imports both. This is the .sql-schema counterpart of goctl's model mysql ddl.

    generate_crud_from_ddl

    fn generate_crud_from_ddl(source : String) -> String

    Parse a SQL DDL script and generate its moonorm data-access layer in one step.

    generate_crud_from_reflection

    fn generate_crud_from_reflection(cols : Array[ReflectedColumn]) -> String

    Generate a moonorm data-access layer (models + typed CRUD) straight from a live schema's reflected columns — the in-memory counterpart of generate_crud_from_ddl. The reflect sub-package produces the ReflectedColumns from a real connection.

    generate_model

    fn generate_model(spec :
    Spec
    ) -> String

    Generate a moonorm data layer for every type block in spec. A block whose fields are all storable yields: the model struct; a <table>_model : @moonorm.Model[T] built with @moonorm.Model::new — declared columns (an id column is the primary key), a from_row decoder, and the to_columns projection an INSERT binds; a <table>_table : @moonorm.Table descriptor; and a <table>_up / <table>_down migration pair (create the table idempotently / drop it). This is the explicit MoonBit stand-in for a SQLAlchemy declarative class plus an Alembic revision, which reflection would otherwise synthesise. A block with a non-storable field (a slice, map, or nested message) still gets its struct and a plain @moonorm.Table descriptor.

    The output compiles against moonbitstack/moonorm + moonbitstack/moondb; a consuming package imports both.

    parse_ddl

    fn parse_ddl(source : String) -> Array[DdlTable]

    Parse a SQL DDL script into its CREATE TABLE definitions. Recognises CREATETABLE [IF NOT EXISTS] name ( … ) (quoted or bare name), skipping any other statement. Column types, primary keys (column- or table-level), NOT NULL and DEFAULT clauses are captured; everything needed to emit a moonorm model.

    parse_dsn

    fn parse_dsn(dsn : String) -> DataSource raise DataSourceError

    Parse a datasource DSN into a DataSource. Recognised forms:

    • SQLite: sqlite:PATH, sqlite://PATH, sqlite3:PATH, file:PATH, the literal :memory:, or a bare path ending in .db / .sqlite / .sqlite3.
    • PostgreSQL: postgres://[user[:password]@]host[:port][/database][?…] (and the postgresql:// spelling). The port defaults to 5432, the user to postgres; the database name is required.

    Raises BadDsn on an unrecognised scheme or a PostgreSQL URL missing its host or database.

    tables_from_reflection

    fn tables_from_reflection(cols : Array[ReflectedColumn]) -> Array[DdlTable]

    Fold reflected columns into DdlTables, grouping by table in first-seen order and mapping each raw SQL type onto its MoonBit scalar (the leading type keyword drives the mapping, so character varying and VARCHAR(255) both land on String). The result feeds generate_crud, so a live schema and a .sql file reach the same model generator.