Open MercatoDocsSearch docsAPI reference

Architecture

How a request moves through Open Mercato

Follow a request from an admin page to its module, request-scoped services and database. These boundaries show where an application can change behavior.

Each enabled module declares the pieces it contributes. Applications can replace or override those pieces locally.

Architecture flow at a glance

From module discovery to service calls and events
Generators register module contributions. At runtime, services and subscribers work with the database.
Diagram source
graph TD
  A[Modules] -- pages/apis/entities --> B[Generators]
  B --> C[Runtime Registry]
  C --> D[Next.js App]
  C --> E[DI Container]
  E --> F[Services & Query Engine]
  F --> G[(PostgreSQL)]
  E --> H[Event Bus]
  H --> I[Subscribers]
  I --> F

    classDef focus fill:#e0f29c,stroke:#34402d,color:#172011,stroke-width:2px
    class C focus

At build time, module generators collect every page, API handler, entity, subscriber, and feature definition into registries. During runtime, the Next.js App Router loads these registries to render pages, wire API routes, and boot the Awilix dependency-injection container. The container exposes services such as the query engine, data engine, RBAC checks, and event bus to both API handlers and subscribers.

Key principles:

Modular architecture at a glance

Module building blocks

File / folderPurpose
index.tsModule metadata (id, title, version, optional query-index configuration).
acl.tsFeature flags exported as features (used by RBAC).
di.tsAwilix registrar (register(container)) that installs services, repositories, and factories.
backend/* & frontend/*Next.js App Router pages. Optional *.meta.ts files supply navigation + RBAC metadata.
api/<method>/<path>.tsREST handlers, usually via the CRUD factory.
data/entities.ts & data/extensions.tsMikroORM entities and links to extend other modules.
data/validators.tsZod schemas reused by APIs, CLIs, and forms.
subscribers/*.tsEvent subscribers with metadata (event, persistent, optional sync for in-pipeline lifecycle).
data/guards.tsMutation guards with priority ordering, payload modification, and afterSuccess callbacks (UMES M).
commands/interceptors.tsCommand interceptors with beforeExecute/afterExecute hooks for execute and undo flows (UMES M).
widgets/dashboard/*Dashboard widgets auto-discovered by the admin UI.
ce.tsCustom entities & fields definitions for the Entities module.
i18n/<locale>.jsonTranslation bundles for module-specific copy.

Runtime stack

Frontend, admin, API and storage layers
Frontend and admin pages use the application API, entity definitions, cache and database.
Diagram source
flowchart TD
    subgraph Presentation
        FE["Frontend app<br/>/app/(frontend)"]
        BE["Backend app<br/>/app/(backend)"]
    end
    API["API routes<br/>src/app/api/[...slug]"]
    CE["Custom entities & fields<br/>(EAV store + cache)"]
    CACHE["Redis / in-memory cache"]
    DB[(PostgreSQL)]

    FE -->|auto-discovered pages| API
    BE -->|server actions| API
    FE -->|module metadata| CE
    BE -->|query engine| CE
    API -->|CRUD factory| CACHE
    API -->|Data Engine| DB
    CACHE --> DB
    CE --> DB

    classDef focus fill:#e0f29c,stroke:#34402d,color:#172011,stroke-width:2px
    class API focus

Multi-tenant directory

Custom application architecture

Application source and installed packages feed the runtime
Enabled modules and local overrides supply generated registries that are used by the application.
Diagram source
flowchart LR
    subgraph App
        SRC["apps/mercato/src/app/*<br/>pages/layouts"]
        SM["apps/mercato/src/modules/*<br/>overrides"]
        MT["apps/mercato/src/modules.ts<br/>module toggle"]
    end
    subgraph Packages
        CORE["@open-mercato/core<br/>modules"]
        EXTRA["Custom packages<br/>@open-mercato/*"]
    end
    SRC --> Router["Frontend & backend routers"]
    SM --> Generator["yarn generate"]
    CORE --> Generator
    EXTRA --> Generator
    MT --> Generator
    Generator --> Registry["modules.generated.ts<br/>entities.generated.ts"]
    Registry --> Runtime["Runtime (routes, APIs, DI)"]

    classDef focus fill:#e0f29c,stroke:#34402d,color:#172011,stroke-width:2px
    class Generator focus

Extension points

Request lifecycle

Every request (page load, API call, server action) runs through the same pipeline:

  1. Routing – Catch-all routers locate the target module file based on metadata from modules.generated.ts.
  2. DI scope – createRequestContainer builds a request-scoped Awilix container, registers core services, then applies module di.ts registrars.
  3. Auth & RBAC – requireAuth, requireRoles, and requireFeatures metadata are enforced before handler logic executes.
  4. Business logic – Handlers typically call module services, the query engine, or the data engine.
  5. Events & subscribers – Writes first run sync subscribers and mutation guards inline in the request pipeline, then emit events that persistent subscribers process asynchronously (for example, index rebuilds, notifications).

See the dedicated request lifecycle reference for a deeper breakdown.

Translations


This architecture lets you assemble enterprise features by combining modules, configure them per tenant or organisation, and override behaviour safely as your product evolves. Continue with the Framework reference to dive into the module APIs in detail.

Previous: Standalone AI development harness