Typed model
likec4 gen model turns your sources into a TypeScript module.
The module holds the model data and types for every id in your model.
You import it like any other module and query it with the model API.
Generate the module
Section titled “Generate the module”npx likec4 gen model --outfile ./src/likec4-model.tspnpx likec4 gen model --outfile ./src/likec4-model.tsyarn dlx likec4 gen model --outfile ./src/likec4-model.tsbunx likec4 gen model --outfile ./src/likec4-model.tsgen ts is an alias of gen model.
codegen and generate are aliases of gen.
| Option | Description |
|---|---|
[path] |
Directory with sources. Default: the current directory, or the LIKEC4_WORKSPACE environment variable |
-o, --outfile |
Output file. Must end with .ts, .mts or .cts. Default: likec4-model.ts in the workspace (in the project folder, if the workspace has several projects) |
--project |
Project to generate, if the workspace has several projects |
--skip-layout |
Skip the layout. Faster, but views have no positions or sizes |
--use-dot |
Use the local Graphviz binaries (dot must be on PATH) instead of the bundled WASM |
--use-core-package |
Import from @likec4/core instead of likec4 |
The file is a snapshot of your sources.
Run the command again after you change them, for example in a prebuild script.
What the module exports
Section titled “What the module exports”likec4model: aLikeC4Modelinstance with the model data. By default, views are laid out.- Types for ids:
$ElementId,$DeploymentId,$ViewId. - Types for the specification:
$ElementKind,$RelationKind,$DeploymentKind,$Tag,$Tags,$MetadataKey. $Aux: the type parameter ofLikeC4Modelthat carries all of the above.
The module imports LikeC4Model from likec4/model.
Add likec4 to the dependencies of the project that uses the file:
npm i likec4pnpm add likec4yarn add likec4bun add likec4Use it
Section titled “Use it”import { likec4model, type $ViewId } from './likec4-model'
// Iterate viewsfor (const view of likec4model.views()) { console.log(view.id, view.titleOrId)}
// Look up an element. TypeScript checks the id.const api = likec4model.element('cloud.backend.api')for (const rel of api.incoming()) { console.log(`${rel.source.title} -> ${api.title}`)}
// Error: the id is not in the model// likec4model.element('cloud.backend.apii')
// Use the id types in your own codefunction linkToView(id: $ViewId) { return `/view/${id}`}element() and view() accept only known ids and throw if the id is not found.
findElement() and findView() accept any string, suggest known ids and return null if the id is not found.
See the API reference for all methods.
Typed model or LikeC4.fromWorkspace
Section titled “Typed model or LikeC4.fromWorkspace”Both give you a LikeC4Model.
They differ in when the sources are parsed.
| Typed model | LikeC4.fromWorkspace |
|
|---|---|---|
| Parses sources | Once, at build time | Every time, at runtime |
| Runs in | Node.js and the browser | Node.js only |
| Ids | Typed | Plain strings |
| Model updates | Run gen model again |
Read on each load, or with watch |
Use the typed model in browser code, in React components, and in tests.
Use LikeC4.fromWorkspace in Node.js scripts that need the current sources, or that must change them.