Skip to main content
In this guide, you’ll learn how to create a Post entity using Sonamu UI, create a database table, and auto-generate Model files through scaffolding.

Prerequisites

When the API server (pnpm dev) is running, Sonamu UI is automatically served at http://localhost:1028/sonamu-ui.

Step 1: Access Sonamu UI

Open http://localhost:1028/sonamu-ui in your browser.
Sonamu UI main screen
Sonamu UI is a web interface for visually managing entities. Here you can:
  • Create and edit entities
  • Add/modify/delete props
  • Define relations
  • Manage Enum types
  • Define Subsets
  • Create and run migrations
  • Scaffolding (auto-generate Model files)

Step 2: Create a New Entity

1

Click Entity Tab

Click the Entity tab in the Sonamu UI top menu to see existing entities.
Entity tab
2

Click New Entity Button

Click the New Entity button at the bottom of the left sidebar.
Entity creation modal
3

Enter Basic Information and Create

Enter the basic information for the new entity and click the Create button:
  • Entity ID: Post (starts with uppercase, singular)
  • Table Name: posts (lowercase, plural)
  • Title: Post
Created Post entity
Entity ID is used as the TypeScript class name, and Table Name is the actual database table name.

Step 3: Define Props

Now add props to the Post entity:
1

Add Basic Props

Click the Add a prop button to add the following props:
Prop NameTypeDescriptionOptions
idintegerIDPrimary Key
created_atdateCreated AtDB Default: CURRENT_TIMESTAMP
titlestringTitleLength: 255
contentstringContent-
authorstringAuthorLength: 100
view_countintegerView CountDB Default: 0
id prop
  • Name: id
  • Type: integer
  • Description: ID
created_at prop
  • Name: created_at
  • Type: date
  • Description: Created At
  • DB Default: CURRENT_TIMESTAMP
title prop
  • Name: title
  • Type: string
  • Description: Title
  • Length: 255
content prop
  • Name: content
  • Type: string
  • Description: Content
  • Length: empty (creates TEXT type)
author prop
  • Name: author
  • Type: string
  • Description: Author
  • Length: 100
view_count prop
  • Name: view_count
  • Type: integer
  • Description: View Count
  • DB Default: 0
Prop addition UI
2

Define Subsets

In the Subsets section, define data query forms:
  • Subset A (Admin): id, created_at, title, content, author, view_count - Full information
  • Subset P (Public): id, title, author, created_at - Public information
  • Subset SS (Secure Short): id, title, author - Summary information
Subset editing screen
Subsets pre-define which props to include when querying data through APIs. For details, see the Subset Guide.
3

Define Enums

In the Enums section, define sorting and search options:PostOrderBy
  • id-desc: ID newest first
  • created_at-desc: Created at newest first
  • view_count-desc: Highest view count first
PostSearchField
  • id: ID
  • title: Title
  • author: Author
Enum editing screen

Step 4: Database Migration

Now you need to apply the entity definition to the database.
1

Go to Migration Tab

Click the Migration tab in Sonamu UI.
Migration tab screen
2

Generate Migration

Click the Generate button to auto-generate a new migration Code File.Click the <> button in the generated file list to open the migration file page in Cursor. The file content looks like this:
Migration file opened in Cursor
3

Run Migration

You can see pending migrations in the Migration tab.Click the all button, then click Apply to Latest to see migration targets in the modal.Select Shadow DB Testing and click the commit button to create the database table.
4

Verify

Connect to the database using a PostgreSQL client or pgAdmin to verify the posts table was created.

Step 5: Scaffolding (Auto-Generate Model Files)

After migration is complete, you can auto-generate Model files using Sonamu’s scaffolding feature.
1

Go to Scaffolding Tab

Click the Scaffolding tab in Sonamu UI.
Scaffolding tab screen
2

Select Post Entity

Select Post as the entity to scaffold.
3

Select Templates

Select templates to generate:
  • Model - post.model.ts (business logic and API endpoints)
  • Model Test - post.model.test.ts (test file)
Template selection UI
Scaffolding auto-generates template files, so you don’t need to write code from scratch. Generated files can be modified to fit your project.
4

Preview (Optional)

Click the Preview button to preview the code that will be generated.
Code preview modal
5

Generate

Click the Generate button to create the files.
Generated file list
The following files are generated:
  • api/src/application/post/post.model.ts
  • api/src/application/post/post.model.test.ts
You can also run this from the terminal:
cd api
pnpm sonamu scaffold model Post
pnpm sonamu scaffold model_test Post

Step 6: Review Generated Model File

Let’s check the post.model.ts file generated by scaffolding:
post.model.ts file opened in VS Code
The generated Model file includes basic CRUD APIs. You can add or modify business logic as needed.
The API server automatically restarts via HMR (Hot Module Replacement):
HMR restart logs in terminal

Step 7: Auto-Generate Frontend Service

When you write APIs, frontend Service files are automatically generated.
web/src/services/PostService.ts (auto-generated)
export class PostService {
  static async findById(subset: PostSubsetKey, id: number) {
    const res = await axios.get(`/api/posts/${id}`, {
      params: { subset },
    });
    return res.data;
  }

  static async findMany(subset: PostSubsetKey, params?: PostListParams) {
    const res = await axios.get("/api/posts", {
      params: { subset, ...params },
    });
    return res.data;
  }

  static async save(params: PostSaveParams) {
    const res = await axios.post("/api/posts", params);
    return res.data;
  }

  static async del(id: number) {
    await axios.delete(`/api/posts/${id}`);
  }
}

Step 8: Test the API

Now let’s test if the API is working correctly.

Test with REST Client

curl -X POST http://localhost:1028/api/posts \
  -H "Content-Type: application/json" \
  -d '{
    "title": "First Post",
    "content": "This is my first post created with Sonamu.",
    "author": "John Doe"
  }'

Test in Browser

Navigate to http://localhost:1028/api/posts?subset=C to view the JSON response.

Complete! 🎉

Congratulations! You’ve successfully created your first entity. You’ve completed the following:
  • ✅ Defined entity with Sonamu UI
  • ✅ Created database table
  • ✅ Auto-generated types and schemas
  • ✅ Auto-generated Model and Test files with scaffolding
  • ✅ Auto-generated REST API
  • ✅ Auto-generated frontend Service
  • ✅ Tested the API
Benefits of ScaffoldingUsing scaffolding:
  • Basic CRUD APIs are auto-generated without writing Model files from scratch
  • Test file templates are generated together, making test writing easier
  • Maintains consistent code style across the project
  • Generated code can be modified anytime to add business logic

Next Steps