Skip to main content
Entity is a core component of Sonamu projects, defining both database tables and TypeScript types together.

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 as entity.json files with the following structure:
File Location: 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.
If the table name is omitted, it’s auto-generated based on the Entity ID: - Userusers - BlogPostblog_posts

title

The display name for the Entity.

props

An array of Entity properties (columns). Each property defines its type and options.
Basic Property Options:
Learn More - Field Types - All available data types - Relations - Defining Entity relationships

Optional Fields

parentId

Used when inheriting from another Entity.
Using parentId inherits all props from the parent Entity. Use carefully.

indexes

Defines database indexes.
Index Types:
  • index - Regular index (improves search performance)
  • unique - Unique index (prevents duplicates)
  • hnsw - HNSW index for Vector search
  • ivfflat - IVFFlat index for Vector search
Partial Index: Use the where field to define a PostgreSQL partial index. A partial index covers only rows matching the given condition, reducing index size and improving performance.
The 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.
Subset Usage:
Including Relation Fields:
Using dot notation in Subsets, you can include fields from related Entities. Sonamu automatically generates JOINs.

enums

Defines enumeration types and labels.
Enum Usage:

Practical Examples

Basic Entity Example

user.entity.json

Example with Relations

employee.entity.json

Defining Entities in Sonamu UI

  1. Access Sonamu UI
  2. Create Entity
    • Click “Entities” tab
    • Click “Create Entity” button
    • Enter Entity ID, table name, Title
  3. Add Properties (Props)
    • Click “Add Property” button to add new properties
    • Select type and configure options
    • Change order with drag and drop
  4. Define Subsets
    • Add subset keys in “Subsets” tab
    • Select fields to include with checkboxes
    • Relation fields expand as a tree structure
  5. 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 file

Database Migration

Migration file generated with table creation SQL

Base Schemas

Base schemas and Enums added to sonamu.generated.ts

Model Scaffold

{entity}.model.ts template generated (when selected)

Cautions

Be Careful When Modifying Entity Definitions - Be cautious when deleting columns or changing types on deployed tables - Recommend making changes gradually through migrations - Consider referential integrity when changing Relations
Entity Design Tips - Always start IDs with integer type - Include created_at, updated_at fields - Explicitly specify indexes for fields requiring unique constraints - Add indexes to frequently searched fields

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