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:34900/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.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
where field to define a PostgreSQL partial index. A partial index covers only rows matching the given condition, reducing index size and improving performance.
where value is a raw SQL predicate without the WHERE keyword. It is used as-is in the generated migration, so never concatenate user input into this field.
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
Complete process of creating Entity in Sonamu UI
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)Cautions
Next Steps
After completing Entity definition, learn the following topics:Using Sonamu UI
Learn all Sonamu UI features and shortcuts
Field Types
Learn all available field types and options
Relations
Learn how to define Entity relationships
Enums
Learn how to define and use Enum types