Skip to content

Code Generators

alab gen lets you write JavaScript functions that transform your schema into any output — Python models, REST routers, GraphQL resolvers, Terraform configs, or anything else you can express as text files.

  1. Alab loads your schema and builds a schema object with models and tables.
  2. Your generator receives that object inside gen().
  3. You return a render({...}) call mapping file paths to string contents.
  4. Alab writes the files to disk.

Every generator is a single .js file:

export default gen((schema) =>
render({
"models.py": buildModels(schema),
"router.py": buildRouter(schema),
}),
);
FunctionDescription
gen(fn)Entry point. Receives a callback with the schema.
render({path: string})Output. Maps relative file paths to string contents.
perTable(schema, fn)Helper. Iterates schema.tables, merges results.
perNamespace(schema, fn)Helper. Iterates schema.models by namespace, merges results.
json(value, indent?)Helper. Safe JSON.stringify wrapper.
dedent(str)Helper. Strips common leading whitespace.

Inside gen(), schema is a deeply frozen (immutable) object:

FieldTypeDescription
schema.modelsobjectTables grouped by namespace: { ns: table[] }.
schema.tablesarrayFlat array of all tables across namespaces.
// schema.models is { namespace: [table, ...] }
const namespaces = Object.keys(schema.models);
const authTables = schema.models["auth"];
// or iterate all tables directly
for (const table of schema.tables) {
console.log(table.name);
}

Each table object has:

{
name: "user",
table: "auth_user",
primary_key: "id",
timestamps: true,
columns: [
{ name: "id", type: "uuid" },
{ name: "username", type: "string", unique: true },
{ name: "role", type: "enum", enum: ["admin", "editor", "viewer"], default: "viewer" },
// ...
]
}
Terminal window
alab gen run <file> -o <output_dir>

The .js extension is optional:

Terminal window
alab gen run generators/fastapi -o ./generated
FlagDescription
-o, --outputRequired. Output directory.
Terminal window
alab gen add <url>

Downloads the file to generators/ in your project root:

Terminal window
alab gen add https://raw.githubusercontent.com/hlop3z/astroladb/main/examples/generators/generators/fastapi.js

This saves generators/fastapi.js. If the file already exists, use --force:

Terminal window
alab gen add --force <url>

A full working example lives at examples/generators on GitHub.

From a single schema file, the FastAPI generator produces:

FileContents
auth/models.pyPydantic models with enums, defaults, Optional
auth/router.pyCRUD endpoints (list, get, create, update, delete)
auth/__init__.pyRe-export
main.pyFastAPI app wiring all routers
Terminal window
# 1. Generate the code
alab gen run generators/fastapi -o ./generated
# 2. Install dependencies and run
cd generated
uv add fastapi[standard]
uv run fastapi dev main.py
# 3. Open http://localhost:8000/docs

Start from the pattern:

export default gen((schema) => {
const files = {};
for (const [ns, tables] of Object.entries(schema.models)) {
for (const table of tables) {
files[`${ns}/${table.name}.txt`] = `Table: ${table.table}`;
}
}
return render(files);
});

Generators run inside a sandboxed JavaScript runtime:

What’s blockedWhy
require / importNo Node modules.
fetch / XMLHttpRequestNo network access.
eval / Function()No code injection.
async / PromiseNo async — keeps output deterministic.
process / envNo environment leaks.
Prototype mutationAll prototypes frozen.

Execution timeout: 5 minutes. Max output: 500 MB, 10,000 files.

Official examples are available in the examples/generators directory:

GeneratorDescriptionExample Link
Python FastAPIREST API with Pydantic modelsView Code
Go ChiREST API with Chi routerView Code
Rust AxumREST API with Axum frameworkView Code
TypeScript tRPCType-safe RPC with tRPCView Code

Each example includes a complete working implementation you can run immediately.