Skip to main content
Sonamu provides complete type safety from client to server to database with a single Entity definition. This document explains how types flow through the entire stack.

Type Flow Overview

Compile Time

Type checking with TypeScript - Prevent errors during development

Runtime

Actual data validation with Zod - Block invalid data

Database

PostgreSQL constraints - Guarantee data integrity

API Boundary

Request/Response validation - Safe data exchange

Layer-by-Layer Type Safety

1. Entity β†’ TypeScript

Entity definitions are converted to TypeScript types.
Type safety guaranteed:

2. Entity β†’ Zod Schema

Entity definitions are converted to Zod schemas for runtime validation.

3. Entity β†’ PostgreSQL

Entity definitions are converted to PostgreSQL table schemas.
Database constraints:

API Layer Type Safety

Model β†’ API Client

Model methods are converted to type-safe API clients.

Request Validation

API requests are automatically validated with Zod schemas.
In Fastify router
On validation failure:

Response Types

API responses are also type-safe.

TanStack Query Type Safety

TanStack Query hooks also provide complete type safety.

Subset Type Safety

Subset queries also provide complete type inference.

Full Stack Type Flow Example

Shows how types flow in actual CRUD operations.
1

1. Entity Definition

user.entity.json
Generated: TypeScript types, Zod schemas, PostgreSQL schemas
2

2. Model Implementation

user.model.ts
Generated: API client, TanStack Query hooks
3

3. Frontend Usage

UserForm.tsx
Validation: Compile time (TypeScript) + Runtime (Zod)
4

4. API Request

Validation: Runtime validation with Zod schema
5

5. Database Save

Validation: PostgreSQL constraints
6

6. Response Return

Type inference: API client guarantees return type

Preventing Type Mismatches

Sonamu’s E2E type safety prevents common type mismatch issues.
Problem: API returns different type than expectedSonamu Solution:
Problem: Client sends wrong parametersSonamu Solution:
Problem: Hardcoded string doesn’t match EnumSonamu Solution:
Problem: Using nullable field without null checkSonamu Solution:

Type Safety Verification

How to verify that type safety is working properly.

Compile Time Verification

Runtime Verification

Next Steps

Zod Validation

Detailed Zod validation guide

Entity Types

Understanding Entity type conversion

Generated Types

Using generated types

Testing

Writing type-safe tests