Generating Code
This continues from Adding Relationships. We have four tables in a database. Now we’ll generate typed outputs and a working API from the same schema.
Export types
Section titled “Export types”alab export -f typescriptThis produces TypeScript interfaces for every table. The password column on
auth_user is omitted because col.password_hash() is hidden from exports.
You can export in other formats too:
alab export -f pythonalab export -f goalab export -f rustEvery export reads from the same schema. Change a column once, re-export, and every language stays in sync.
Export the OpenAPI schema
Section titled “Export the OpenAPI schema”alab export -f openapiThis produces an openapi.json file with:
- Standard OpenAPI 3.0 type definitions for every table
x-dbextensions carrying database-specific metadata (constraints, relationships, indexes, semantic type info)
This file is the portable, machine-readable description of your entire data
model. Generators, custom tooling, and third-party tools like QuickType or
openapi-generator can all consume it.
Run a generator
Section titled “Run a generator”Generators transform the schema into arbitrary output files. Let’s use the built-in FastAPI example to produce a working Python API.
Download the generator:
alab gen add https://raw.githubusercontent.com/hlop3z/astroladb/main/examples/generators/generators/fastapi.jsThis saves generators/fastapi.js. Now run it:
alab gen run generators/fastapi -o ./generatedThe output:
generated/├── auth/│ ├── __init__.py│ ├── models.py # Pydantic models with enums and defaults│ └── router.py # CRUD endpoints├── blog/│ ├── __init__.py│ ├── models.py│ └── router.py└── main.py # FastAPI app wiring all routersTo run it:
cd generateduv add fastapi[standard]uv run fastapi dev main.pyOpen http://localhost:8000/docs to see the interactive API documentation.
Two paths, same source
Section titled “Two paths, same source”The exported types and the generated API both came from the same schema files. Neither required you to describe your data model a second time.
This is the core idea:
alab exportgives you typed structures in any supported language.alab gengives you arbitrary file output — APIs, configs, documentation, anything a generator template can produce.openapi.jsonis the portable artifact that any external tool can consume directly.
All three read from the same source of truth. Use whichever combination fits your project.
Where to go from here
Section titled “Where to go from here”- Tables — column modifiers, table modifiers, full syntax
- Semantic Types — what they add over low-level types
- Relationships — all relationship types and referential integrity options
- Migrations — the migration DSL and workflow
- Code Generators — writing your own generators