Skip to main content

Module codegen

Module codegen 

Source
Expand description

Codegen — emit typed bindings for a ClickHouse table in two directions:

  • ClickHouse → Rust rows (ch_type_to_rust / rust_row_struct): the TS→Rust bridge. TypeScript owns a (static) table; this turns its live/spec columns into a Rust #[derive(Row)] struct so the Rust services get faithful, drift-checked rows instead of hand-writing them. Pair with introspect + check_drift. Temporal types map to String (the works-everywhere default over the HTTP/RowBinary boundary; refine to time/chrono behind features).
  • TableSpec → TS + Zod (emit_row_interface / emit_select_schema / emit_insert_schema / emit_ts_module): a createSelectSchema/createInsertSchema style emitter (parity with smooai-postgres-kit). Useful for handing a runtime (dynamic) table’s shape to a TypeScript client as a row interface + Zod schemas.

Functions§

ch_type_to_rust
Map a ClickHouse type string to the Rust type a clickhouse-crate row uses. Wrappers recurse; unknown scalars fall back to String (safe over the wire).
emit_insert_schema
Emit the Zod insert schema — columns with a DEFAULT become .optional().
emit_row_interface
Emit the TS row interface for a table (one field per column).
emit_select_schema
Emit the Zod select schema (z.object(...)) for a table.
emit_ts_module
Emit a full TS module for a table: the zod import, the row interface, and the select + insert schemas, separated by blank lines.
insert_schema_name
The Zod insert-schema const name, e.g. observabilityTracesInsertSchema.
row_type_name
The TS interface name for a table, e.g. observability_tracesObservabilityTracesRow.
rust_row_struct
Emit a Rust row struct for a table’s columns — (column_name, clickhouse_type) pairs. Derives the clickhouse crate’s Row + serde, so it deserializes straight from a query. The emitted source references clickhouse::Row (a dev/consumer dependency); this function only produces the string.
select_schema_name
The Zod select-schema const name, e.g. observabilityTracesSelectSchema.