Skip to main content
Models can be auto-generated through Scaffold after Entity definition or written manually. Using Scaffold generates a Model template with basic structure and CRUD methods included.

Model Creation Process

1

Define Entity

First, you need to define an Entity.
user.entity.json
2

Auto Type Generation

Type files are automatically generated when you save the Entity.Generated files:
  • user.types.ts - TypeScript types and Zod schemas
  • sonamu.generated.ts - Base schemas and Enums
  • sonamu.generated.sso.ts - Subset query functions
3

Generate Model Scaffold

Use Sonamu CLI to generate a Model template.
Or generate from the Scaffolding tab in Sonamu UI.
4

Customize Model

Add business logic to the generated Model.

Creating Models with Scaffold

Using CLI

Using Sonamu UI

1

Open Scaffolding Tab

Click the Scaffolding tab in Sonamu UI (http://localhost:34900/sonamu-ui).
2

Select Model Template

Select the “Model” template and specify the Entity.
3

Configure Options

  • Default Search Field: Default search field (e.g., email)
  • Default Order By: Default sorting (e.g., id-desc)
  • Overwrite: Whether to overwrite existing files
4

Execute Generation

Click the “Generate” button to create the Model file.

📸 Needed: Scaffolding tab in Sonamu UI - Model generation screen

Generated Model Structure

Basic structure of a Model generated by Scaffold:
user.model.ts

Generated Methods Explanation

Default methods generated by Scaffold:

1. findById

Retrieves a single record by ID.
Features:
  • Receives Subset as parameter to query only needed fields
  • Throws NotFoundException if record doesn’t exist
  • Creates HTTP endpoint with @api decorator

2. findOne

Retrieves the first record matching conditions.
Features:
  • Returns null if record doesn’t exist (no exception thrown)
  • Internally calls findMany with num: 1, page: 1

3. findMany

Retrieves multiple records based on conditions.
Features:
  • Supports pagination (num, page)
  • Search/filtering (search, keyword)
  • Sorting (orderBy)
  • Returns { rows, total } with ListResult type

4. save

Creates or updates records.
Features:
  • Uses Upsert Builder (Insert or Update)
  • Processes multiple records at once with array
  • Executes wrapped in transaction
  • Returns array of created/updated IDs

5. del

Deletes records.
Features:
  • Receives multiple IDs as array
  • Executes in transaction
  • Only admins can delete with guards: ["admin"]
  • Returns number of deleted records

Creating Models Manually

You can also write manually without using Scaffold.

Minimum Structure

Naming Conventions

Class name must end with ModelClass. Export must end with Model.

Model File Location

Model files are located in the same directory as the Entity:

Writing Types File

Define parameter types to use with Model:
user.types.ts
Types file patterns:
  • {Entity}ListParams: For list queries
  • {Entity}SaveParams: For create/update
  • {Entity}{Action}Params: For specific actions (e.g., LoginParams)

Verifying Model Registration

Verify that the generated Model loads properly:
api/src/application/sonamu.generated.ts
sonamu.generated.ts is automatically updated when you save Entities. When you add a Model, it’s automatically added to this file too.

Next Steps

After creating a Model, learn the following topics:

API Decorator

Creating HTTP APIs with @api decorator

Business Logic

Learning business logic writing patterns

Puri Query Builder

Writing type-safe queries

Testing

Writing Model tests