Sonamu UI’s Entity management feature allows you to easily create and edit Entities through a visual interface. It’s much faster and more intuitive than directly modifying files via CLI.
Entity Tab Structure
The Entity tab consists of two main areas:
- Left Sidebar: Entity list and new Entity add button
- Right Content: Selected Entity details (Properties, Indexes, Relations, Subsets, Enums)
Creating an Entity
Click the [+ New Entity] button at the bottom of the sidebar.
When the modal opens, enter the following information:
| Field | Description | Example |
|---|
| Entity ID | Entity identifier (PascalCase) | Product, OrderItem |
| Title | Display title | Product, Order Item |
| Parent Entity | Parent Entity (optional) | Order |
If you specify a Parent Entity, it becomes a child of that Entity. Example: Order → OrderItem
3. Add Basic Properties
Created Entities include these basic fields by default:
{
properties: [
{ name: "id", type: "int", primaryKey: true, autoIncrement: true },
{ name: "title", type: "string", length: 255 },
{ name: "created_at", type: "datetime", default: "CURRENT_TIMESTAMP" },
]
}
Editing Properties
Adding a Property
- Click [+ Add Property] button
- Enter Property information:
| Field | Required | Description | Example |
|---|
| Name | ✅ | Field name (snake_case) | email, phone_number |
| Type | ✅ | Data type | string, int, decimal |
| Length | Conditional | String length | 255 |
| Nullable | | Allow NULL | true |
| Default | | Default value | 0, CURRENT_TIMESTAMP |
| Comment | | Description | User email |
Supported data types:
- String:
string, text, mediumtext, longtext
- Number:
int, bigint, float, double, decimal
- Date:
date, datetime, timestamp
- Other:
boolean, json, enum
Editing a Property
- Click on a Property row to enter edit mode
- Modify values
- Click elsewhere or press Enter to save
Deleting a Property
Click the [×] button on the Property row.
Data loss warning: Deleting a Property will delete all data in that column.
Make sure to backup data before running the migration.
Managing Indexes
Adding an Index
- Scroll to the Indexes section
- Click [+ Add Index] button
- Enter Index information:
| Field | Description | Example |
|---|
| Fields | Index target fields (multiple selectable) | email, created_at |
| Type | Index type | index, unique, fulltext |
Index types:
- index: Regular index (improves search performance)
- unique: Unique index (prevents duplicates)
- fulltext: Full-text search index (text search)
Composite Index
Select multiple fields to create a composite index:
indexes: [
{ fields: ["user_id", "created_at"], type: "index" },
{ fields: ["email"], type: "unique" },
]
Managing Relations
belongsTo (N:1 Relationship)
Meaning: “This Entity belongs to another Entity”
Example: Post belongs to User
- Click [+ Add] button in the belongsTo section
- Enter information:
| Field | Description | Example |
|---|
| Entity | Entity to reference | User |
| As | Relation alias | author |
| Foreign Key | Foreign key field name (optional) | author_id |
Generated code:
belongsTo: [
{ entityId: "User", as: "author" }
]
hasMany (1:N Relationship)
Meaning: “This Entity has multiple of another Entity”
Example: User has multiple Posts
- Click [+ Add] button in the hasMany section
- Enter information:
| Field | Description | Example |
|---|
| Entity | Target Entity | Post |
| As | Relation alias | posts |
Generated code:
hasMany: [
{ entityId: "Post", as: "posts" }
]
Managing Enums
Use Enums when a field can only have limited values.
Adding an Enum
- Click [+ Add Enum] button in the Enums section
- Enter Enum information:
| Field | Description | Example |
|---|
| Name | Enum name | UserRole |
| Values | Value list (comma-separated) | admin, user, guest |
Generated code:
enums: [
{
name: "UserRole",
values: ["admin", "user", "guest"]
}
]
Using in Property:
{
name: "role",
type: "enum",
enum: "UserRole",
default: "user"
}
AI-Assisted Entity Creation
Click the [💬 AI] button at the bottom of the sidebar to open the AI chat interface.
Create Entity with AI
Example prompt:
Create a Post Entity for blog posts.
I need title, content, author, creation date, and view count.
AI generates the Entity definition and adds it automatically.
Modify Entity with AI
Example prompt:
Add a profile image field to the User Entity.
AI analyzes the existing Entity and applies the modifications.
Be specific: The more clearly you describe field types, constraints, and relationships, the more accurate results you’ll get.
Practical Example
E-commerce Product Entity
export const ProductEntity = {
properties: [
{ name: "id", type: "int", primaryKey: true, autoIncrement: true },
{ name: "title", type: "string", length: 255 },
{ name: "description", type: "text", nullable: true },
{ name: "price", type: "decimal", precision: 10, scale: 2 },
{ name: "stock", type: "int", default: 0 },
{ name: "category_id", type: "int" },
{ name: "status", type: "enum", enum: "ProductStatus" },
{ name: "created_at", type: "datetime", default: "CURRENT_TIMESTAMP" },
{ name: "updated_at", type: "datetime", onUpdate: "CURRENT_TIMESTAMP" },
],
indexes: [
{ fields: ["category_id"] },
{ fields: ["title"], type: "fulltext" },
{ fields: ["status", "created_at"] },
],
belongsTo: [
{ entityId: "Category", as: "category" }
],
hasMany: [
{ entityId: "Review", as: "reviews" }
],
enums: [
{
name: "ProductStatus",
values: ["active", "inactive", "soldout"]
}
],
} satisfies EntityType;
Applying Changes
After modifying an Entity, you need to create and run a migration to reflect changes in the database.
Workflow
- Edit Entity: Modify Entity in the UI
- Create Migration: Generate migration in the Migration tab
- Run Migration: Run migration to apply to DB
- Scaffolding: Generate Model and test code
Entity files (.entity.ts) are automatically saved when modified in the UI.
Next Steps