Type Safety & Typegen
Type Safety & Typegen
NodeStack bridges the gap between your SQLite schema and frontend client code with automatic TypeScript type generation.
Generating Types via CLI
Run the typegen command in your terminal:
npx @nodestackapp/server typegen --out src/nodestack-types.ts
This inspects your live database schema and outputs TypeScript interfaces for every collection, field constraints, and relation links:
// Generated by nodestack typegen
export interface PostsRecord {
id: string;
title: string;
content: string;
views: number;
published: boolean;
author: string;
created: string;
updated: string;
}
export interface UsersRecord {
id: string;
email: string;
name?: string;
avatar?: string;
created: string;
updated: string;
}
export type Schema = {
posts: PostsRecord;
users: UsersRecord;
};
Live Type Export Endpoint
You can also fetch type definitions directly from the running server over HTTP:
GET /_/types.d.ts
For example, download directly in your package scripts:
curl http://localhost:8090/_/types.d.ts > types/nodestack.d.ts
Applying Types to nodestack-client
Pass your generated Schema type to NodeStackClient:
import NodeStackClient from 'nodestack-client';
import type { Schema, PostsRecord } from './nodestack-types';
const client = new NodeStackClient<Schema>('http://localhost:8090');
// Autocomplete on collection name!
// Autocomplete on record properties!
const post = await client.collection('posts').getOne('rec_123');
console.log(post.title); // TypeScript knows post is PostsRecord!