Quick start
Install Golem, declare a model, extend model<T>(), add a method, and run it.
Install
npm install @exprtlabs/golem
npm install --save-dev @exprtlabs/golem-model typescript@~7.1.0-betaCheck the requirements. Add "type": "module" to package.json and this to tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"rootDir": ".",
"outDir": ".build"
},
"include": [
"src/**/*.ts"
]
}Declare your fields
priority is a number in your code and a word in JSON. A TSerde tag names the two functions that convert a field to and from its JSON form. First write them in src/priority-word.ts. The generated file imports them, so they cannot live in the model file:
const PRIORITY_WORDS = ['low', 'normal', 'high'] as const;
export type PriorityWord = (typeof PRIORITY_WORDS)[number];
export function toPriorityWord(value: number): PriorityWord {
const word = PRIORITY_WORDS[value - 1];
if (!word) throw new RangeError('Priority must be 1, 2 or 3.');
return word;
}
export function fromPriorityWord(word: PriorityWord): number {
return PRIORITY_WORDS.indexOf(word) + 1;
}Then mark a type with IModel in src/task.model.ts. The TSerde tag ties priority to those functions. Integer, Minimum<1> and Maximum<3> keep the number between 1 and 3, so a task with another priority fails validation:
import { model, type IModel } from '@exprtlabs/golem/model';
import type { Integer, Maximum, MinLength, Minimum, TSerde } from '@exprtlabs/golem/tags';
import type { fromPriorityWord, toPriorityWord } from './priority-word.js';
export type TTask = IModel<{
title: string & MinLength<1>;
done: boolean;
// Your code sorts by a number from 1 to 3; the JSON carries a word.
priority: number & Integer & Minimum<1> & Maximum<3> & TSerde<typeof toPriorityWord, typeof fromPriorityWord>;
}>;The generator reads src/**/*.model.ts and src/**/*.models.ts. model.suffixes changes the endings and model.sources names the files.
Declare an IObject instead to keep plain objects (IObject or IModel).
Add methods
Extend model<TTask>() in src/task.model.ts, below the declaration, with no imports from generated files:
export class Task extends model<TTask>() {
public toggle(): void {
this.done = !this.done;
}
}Your code type-checks immediately without running generation first. To add a field later, change TTask: toggle() stays untouched.
Configure Golem
Add golem.yaml at the project root:
version: 1
model: {}model: {} turns on the models job with its defaults. Run npx golem generate whenever you want to inspect or pre-generate files into .golem/models/ (the default model.output); golem build generates automatically before compiling. No *.generated.ts files are written beside src/ (ignore or commit generated files).
Use the model
Create src/main.ts:
import { toWireFormat } from '@exprtlabs/golem/serde';
import { validateInstance } from '@exprtlabs/golem/validation';
import { Task } from './task.model.js';
const task = new Task({ title: 'Review the draft', done: false, priority: 2 });
task.toggle();
console.log(toWireFormat<Task>(task)); // { title: 'Review the draft', done: true, priority: 'normal' }
console.log(await validateInstance(task)); // []toWireFormat<Task>(task) returns the JSON form of the task as an object. TypeScript erases the <Task> argument at run time, so a build tool rewrites model<T>() and the serde calls to generated code (Build tools). Every class extending model<T>() also exposes static convenience methods: Task.parse, Task.fromWireFormat, Task.stringify, Task.toWireFormat, Task.validate and Task.assert. Standalone generic calls (parse<Task>(), stringify<Task>(), fromWireFormat<Task>(), toWireFormat<Task>(), validate<Task>(), assertValue<Task>()) continue to work too.
Run it
npx golem build && node .build/src/main.jsBuilt tsconfig.json.
{ title: 'Review the draft', done: true, priority: 'normal' }
[]golem build is a build tool. It generates first, checks your sources, and writes JavaScript into .build/ with the toWireFormat call replaced by generated code. A bundler plugin or the Node loader can be the build tool instead (Build tools). Run golem watch to regenerate while you edit (daily loop).
Your main.ts holds only the code above, so this is all it prints. The full example's main.ts continues with one line per feature. npm start in examples/models prints them all.
Requirements
| Tool | Version |
|---|---|
| Node.js | 22.15 or newer |
typescript | >=7.1.0-0 <8 |
The Node loader needs --experimental-strip-types before Node.js 22.18 (Node versions).
Golem uses the typescript 7.1+ package installed in your project.
Packages for each generator
Each generator is a development dependency. Install it with typescript, which none of them brings:
| To generate | Development dependency |
|---|---|
| TypeScript models | npm install --save-dev @exprtlabs/golem-model typescript@~7.1.0-beta |
| An OpenAPI to TypeScript SDK | npm install --save-dev @exprtlabs/golem-openapi-ts typescript@~7.1.0-beta |
| OpenAPI from NestJS controllers | npm install --save-dev @exprtlabs/golem-ts-openapi typescript@~7.1.0-beta |
Generated models and clients import the runtime:
npm install @exprtlabs/golemGenerating OpenAPI from controllers needs no runtime. A NestJS app that registers NestModelInterceptor does need it, so it lists @exprtlabs/golem-ts-openapi in dependencies.
An app with the MobX extension imports @exprtlabs/golem-openapi-ts at run time, so it lists that package in dependencies, with mobx.
The golem command comes from @exprtlabs/golem, which every generator depends on:
- Install it in the project before you run
npx golem. An unrelated npm package is also namedgolem. - Without a local install,
npxoffers to download and run that package. In CI,npxruns it without asking. - In scripts and CI, run
npx --no-install golem. It fails instead of running the other package.
The @exprtlabs/* package subpaths are ESM-only. Import them from ESM application code, or invoke the CLI directly.
Peer dependencies
@exprtlabs/golem-model declares the compiler as a peer dependency. The two OpenAPI packages declare it as an optional peer, and declare @exprtlabs/golem as a peer, so your project holds one copy of each:
| Package | Peer dependencies |
|---|---|
@exprtlabs/golem-model | typescript >=7.1.0-0 <8 |
@exprtlabs/golem-ts-openapi | @exprtlabs/golem, and @exprtlabs/golem-model, typescript >=7.1.0-0 <8, @nestjs/common ^11.0.0 and rxjs ^7.8.0 (all four optional; the last two for NestModelInterceptor) |
@exprtlabs/golem-openapi-ts | @exprtlabs/golem, and @exprtlabs/golem-model, typescript >=7.1.0-0 <8 and mobx ^6.13.0 (all three optional; mobx for the MobX extension) |
@exprtlabs/golem has only optional peer dependencies:
typescript>=7.1.0-0 <8, forgolem build, the plugins and the loadervite>=5, for the Vite pluginesbuild^0.25.12, for the esbuild plugineslint^9.0.0 || ^10.0.0, for the ESLint rule
npm install --save-dev typescript@~7.1.0-betaProject settings
"type": "module"inpackage.json"module": "NodeNext"and"moduleResolution": "NodeNext", or"module": "ESNext"and"moduleResolution": "Bundler"- target
ES2022or newer for top-levelawait - With NodeNext,
.jsextensions on relative imports, including generated files. With Bundler resolution, imports with no extension:golem buildadds the full file name to the JavaScript it writes (import paths) "outDir": ".build""rootDir": "."
golem.yaml names the project with tsconfig, the tsconfig.json beside it by default. A NestJS app needs more compiler settings (set up TypeScript). Choose the build tool in Build tools.
Runtime dependencies
Validation is on and MobX is off by default. validation also takes sanity, which checks the shape only.
Setting in golem.yaml | Extra application dependencies |
|---|---|
Validation on (validation: true) | none |
Shape checks only (validation: sanity) | none |
MobX on (extensions: { mobx: {} } in the openapi-ts block) | npm install mobx @exprtlabs/golem-openapi-ts |
Validation off (validation: false) | none |
validation goes at the top level of golem.yaml (all settings). The MobX extension goes in the openapi-ts block (MobX extension).
