ddlc

package module
v0.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 2 Imported by: 0

README

ddlc

ddlc is the DDL contract leaf of the tinywasm SQL ecosystem. It provides the core interfaces and utilities for database schema exporting and topological sorting of foreign keys.

To keep the runtime environment lightweight and free of unnecessary dependencies (especially for WASM compilation), all build-time DDL-generation capabilities and CLI schemas are decoupled from the core runtime (tinywasm/orm) and reside here.

Core Features

  • Exporter Interface (exporter.go): An interface implemented by dialect-specific SQL adapters (such as tinywasm/sqlt and tinywasm/postgres) to convert models to their corresponding SQL CREATE TABLE and index definitions in foreign key dependency order.
  • FieldExt Struct (field_ext.go): Holds foreign key database metadata (Ref, RefColumn, OnDelete). By locating it in ddlc, dialect compilers and generated models do not need to pull in the heavier runtime ORM module.
  • TopologicalSort (sort.go): Performs Kahn's topological sort (BFS) over models implementing schema extensions (SchemaExt() []FieldExt) to ensure parent tables are created before child tables.

Implementations & Consumers

Implementers of Exporter
  • tinywasm/sqlt: The SQLite database adapter compiler.
  • tinywasm/postgres: The PostgreSQL database adapter compiler.
Callers of Exporter
  • tinywasm/ormc: The ORM code generator (via ExportSQL).
  • tinywasm/sqlmcp: The MCP db tool (via db_export_schema).

Leaf-Dependency Guarantee

To guarantee portability and allow compilation in frontend or WASM environments, the root package of github.com/tinywasm/ddlc depends only on:

  • github.com/tinywasm/model
  • github.com/tinywasm/fmt

It does not import tinywasm/orm or any database adapters.

tui Subpackage (dev console only)

tui/handler.go provides a devtui.HandlerExecution-compatible Handler (Name, Label, SetLog, Execute) that runs an injected ExportFunc and writes the resulting DDL to a file (config/schema.sql by default, override with SetOutputPath). It lives in its own package, separate from the root contract, so that sqlt/postgres/ormc/sqlmcp — which only need the leaf contract — never compile file-I/O or dev-console code. Only tinywasm/app should import github.com/tinywasm/ddlc/tui.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TopologicalSort added in v0.0.2

func TopologicalSort(models []model.Model) ([]model.Model, error)

TopologicalSort returns models sorted so parents come before children (Kahn's BFS). Models not implementing SchemaExt() are treated as having no FK deps. Returns error on circular FK dependency.

Types

type Exporter added in v0.0.2

type Exporter interface {
	ExportDDL(models []model.Model) (string, error)
}

Exporter is implemented by SQL adapter compilers (sqlt, postgres). ExportDDL returns CREATE TABLE + index statements for all models, in FK dependency order.

type FieldExt added in v0.0.2

type FieldExt struct {
	model.Field
	Ref       string // FK: target table name. Empty = no FK.
	RefColumn string // FK: target column. Empty = auto-detect PK of Ref table.
	OnDelete  string // Override ON DELETE action. Empty = CASCADE (default for all FKs).
}

FieldExt extends model.Field with database-specific metadata (foreign keys). Used internally by adapters that support FK constraints.

Directories

Path Synopsis
Package tui provides the DevTUI execution handler for DDL export.
Package tui provides the DevTUI execution handler for DDL export.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL