Framework
A module's entity definition is the long-lived contract with PostgreSQL. Give the record an identity, decide how its tenant and organization are carried, then review the migration against the rows an existing installation already has.
Each module owns its MikroORM entities under data/entities.ts. Entities must follow the naming conventions documented in the repository root: plural snake_case tables, UUID primary keys, timestamp columns, and optional organization_id / tenant_id foreign keys.
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
@Entity({ tableName: 'inventory_items' })
export class InventoryItem {
@PrimaryKey({ type: 'uuid' })
id!: string;
@Property()
sku!: string;
@Property()
name!: string;
@Property({ nullable: true })
organization_id?: string | null;
@Property({ defaultRaw: 'now()' })
created_at!: Date;
@Property({ defaultRaw: 'now()', onUpdate: () => new Date() })
updated_at!: Date;
@Property({ nullable: true })
deleted_at?: Date | null;
}
Guidelines:
deleted_at timestamptz null; the query engine excludes deleted records automatically.When one module needs to extend another’s entity, create a dedicated extension entity inside the extending module and declare the link in data/extensions.ts. See Extending data entities for details.
yarn db:generate to emit per-module migrations inside apps/mercato/src/modules/<module>/migrations (or packages/<pkg>/src/modules/...).yarn db:migrate – the runner executes modules in dependency order.By keeping schema logic within modules and using extension entities, teams can evolve their domain models independently without breaking other packages.
Previous: Extending data entities · Next: Hybrid query engine