What is an Entity?
An Entity is a data model definition that includes:- Database Schema - Table structure, columns, indexes
- TypeScript Types - Type definitions for type safety
- Relations - Connections with other Entities
- Subsets - Field combinations for API responses
- Enums - Enumeration types and labels
How to Define an Entity
It’s recommended to visually define Entities in Sonamu UI (
http://localhost:1028/sonamu-ui). Sonamu UI provides auto-completion, validation, and real-time preview features.entity.json File Structure
Entities are saved asentity.json files with the following structure:
api/src/application/{entity}/{entity}.entity.json
Required Fields
id
The unique identifier for the Entity. Written in PascalCase.- Used for Entity class names and type names
- Example:
UserModel,User,UserBaseSchema
table
The database table name. Written in snake_case.title
The display name for the Entity.props
An array of Entity properties (columns). Each property defines its type and options.| Option | Type | Description | Default |
|---|---|---|---|
name | string | Property name (snake_case) | required |
type | string | Data type | required |
desc | string | Description | - |
nullable | boolean | Allow NULL | false |
dbDefault | string | DB default value | - |
Learn More
- Field Types - All available data types
- Relations - Defining Entity relationships
Optional Fields
parentId
Used when inheriting from another Entity.indexes
Defines database indexes.index- Regular index (improves search performance)unique- Unique index (prevents duplicates)hnsw- HNSW index for Vector searchivfflat- IVFFlat index for Vector search
subsets
Defines field combinations for API responses.Using dot notation in Subsets, you can include fields from related Entities. Sonamu automatically generates JOINs.
enums
Defines enumeration types and labels.Practical Examples
Basic Entity Example
user.entity.json
Example with Relations
employee.entity.json
Defining Entities in Sonamu UI
-
Access Sonamu UI
-
Create Entity
- Click “Entities” tab
- Click “Create Entity” button
- Enter Entity ID, table name, Title
-
Add Properties (Props)
- Click “Add Property” button to add new properties
- Select type and configure options
- Change order with drag and drop
-
Define Subsets
- Add subset keys in “Subsets” tab
- Select fields to include with checkboxes
- Relation fields expand as a tree structure
-
Save and Generate
- Click “Save” button
- Entity file auto-generated
- Migration auto-generated
Learn More
- Using Sonamu UI - Detailed UI guide
What Gets Auto-Generated After Entity Definition
When you define and save an Entity, Sonamu automatically generates the following:TypeScript Types
Types and Zod schemas generated in
{entity}.types.ts fileDatabase Migration
Migration file generated with table creation SQL
Base Schemas
Base schemas and Enums added to
sonamu.generated.tsModel Scaffold
{entity}.model.ts template generated (when selected)