Skip to content

Database Schema

This cookbook shows a complete process that inspects a SQLite database and generates a Markdown table report. The same workflow applies to PostgreSQL, MySQL, and MariaDB; only the returned model shape changes.

ccode/
database/
schema-report.ts
templates/
schema-report.tpl
import type { Context } from "@ccode/context";
import * as Database from "@ccode/database";
export default function main(ctx: Context) {
const inspection = ctx.inspectDatabase("sqlite://./data/application.db");
if (!Database.SQLite.isInspection(inspection)) {
throw new Error("expected a SQLite inspection");
}
const database = inspection.databases[0];
if (!database) {
throw new Error("database has no schema");
}
ctx.generate("templates/schema-report.tpl", "generated/schema.md", {
database,
});
}
# {{ data.database.name }}
{% for table in data.database.tables %}
## {{ table.name }}
| Column | Type | Nullable |
| --- | --- | --- |
{% for column in table.columns %}
| `{{ column.name }}` | `{{ column.type.declaredType }}` | {{ column.nullable }} |
{% endfor %}
{% endfor %}
Terminal window
ccode run database/schema-report

The process owns schema traversal and the template only formats the prepared data. For PostgreSQL, use inspection.database.schemas; for MySQL, MariaDB, and SQLite, use inspection.databases. See Database Inspection for the engine-specific models.