Open MercatoDocsSearch docsAPI reference

Framework

Entities and migrations

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.

Authoring entities

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:

Extensions

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.

Migrations

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