ddlc

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 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/ormcp: 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.

Documentation

Index

Constants

View Source
const (
	LogOpen          = "[..."
	LogClose         = "...]"
	MsgPrefix        = "ddlc handler: "
	MsgExporting     = "Exporting DDL schema..."
	MsgExportFailed  = "Export failed: "
	MsgExportSuccess = "Export successful. Length: "
	MsgBytes         = " bytes"
	ErrNotConfigured = "ddlc handler: export function not configured"
)

Log message prefix and message constants to avoid repeating string literals.

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 ExportFunc added in v0.0.2

type ExportFunc func() (sql string, err error)

ExportFunc produces the DDL SQL for the current project.

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.

type Handler added in v0.0.2

type Handler struct {
	// contains filtered or unexported fields
}

Handler implements the TUI HandlerExecution and Loggable interfaces structurally.

func New

func New() *Handler

New creates a new DDLC TUI handler.

func (*Handler) Execute added in v0.0.2

func (h *Handler) Execute()

Execute triggers the DDL export. Implements devtui.HandlerExecution structurally.

func (*Handler) ExecuteErr added in v0.0.2

func (h *Handler) ExecuteErr() error

ExecuteErr executes the export and returns the error for test assertion.

func (*Handler) Label added in v0.0.2

func (h *Handler) Label() string

Label returns the button label for DevTUI.

func (*Handler) Name added in v0.0.2

func (h *Handler) Name() string

Name returns the TUI handler identifier.

func (*Handler) SetExport added in v0.0.2

func (h *Handler) SetExport(fn ExportFunc)

SetExport injects the DDL export logic.

func (*Handler) SetLog added in v0.0.2

func (h *Handler) SetLog(fn func(messages ...any))

SetLog injects the logging function.

Jump to

Keyboard shortcuts

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