Framework
A module is the boundary around a business feature. Start with the entity it owns, then locate the page, API and permissions that expose it. Generated registries tell the runtime which of those contributions your application enabled.
This app supports modular features delivered as either:
@open-mercato/core (monorepo package)apps/mercato/src/modules/* (take precedence)In a standalone app, local modules live under src/modules/ and are registered in src/modules.ts. To scaffold one, publish it as an npm package and dedicated GitHub repository, or link repository development back into the same app, see Publishing Custom Modules.
id (special cases: auth).apps/mercato/src/modules.ts.Create @open-mercato/core/modules/<module>/index.ts exporting metadata (or override via apps/mercato/src/modules/<module>/index.ts):
import type { ModuleInfo } from '@/modules/registry'
export const metadata: ModuleInfo = {
name: '<module-id>',
title: 'Human readable title',
version: '0.1.0',
description: 'Short description',
author: 'You',
license: 'MIT',
// Optional: declare hard dependencies (module ids)
// The generator will fail with a clear message if missing
requires: ['some_other_module']
}
Generators expose modulesInfo for listing.
Create @open-mercato/core/modules/<module>/di.ts exporting register(container) to add/override services and components. To override/extend, add apps/mercato/src/modules/<module>/di.ts.
import { asClass, asValue } from 'awilix'
import type { AppContainer } from '@/lib/di/container'
export function register(container: AppContainer) {
// container.register({ myService: asClass(MyService).scoped() })
// container.register({ myComponent: asValue(MyComponent) })
}
@open-mercato/core/modules/<module>.apps/mercato/src/modules/<module>.
apps/mercato/src/modules/<module>/frontend/<path>.tsx -> overrides /<path>apps/mercato/src/modules/<module>/backend/<path>.tsx -> overrides /backend/<path>.../backend/page.tsx -> serves /backend/<module>apps/mercato/src/app/(frontend)/[[...slug]]/page.tsxapps/mercato/src/app/(backend)/backend/[[...slug]]/page.tsxCrudForm or DataTable).apps/mercato/src/modules/<module>/widgets/injection/**/widget.ts(x) plus a mapper widgets/injection-table.ts that ties spot ids to widget ids.crud-form:<entityId>, data-table:<tableId>[:header|:footer], backend:record:current, backend:layout:top|footer, backend:sidebar:top|footer, legacy backend-mutation:global, admin.page:<path>:before|after).page.meta.ts (for Next-style page.tsx)<name>.meta.ts (for direct files)meta.ts (folder-level, applies to the page in the same folder)import type { PageMetadata } from '@open-mercato/shared/modules/registry'
export const metadata: PageMetadata = { /* ... */ }
requireAuth: booleanrequireRoles: readonly string[]requireFeatures: readonly string[] (fine-grained permissions)title or pageTitle: stringgroup or pageGroup: stringorder or pageOrder: numbericon: string (backend Next-style pages)navHidden: boolean (hide from admin nav)visible?: (ctx) => boolean|Promise<boolean>enabled?: (ctx) => boolean|Promise<boolean>Precedence: if a *.meta.ts file is present it is used; otherwise, the generator will look for export const metadata in the page module (server-only).
@open-mercato/core/modules/auth/frontend/login.tsxapps/mercato/src/modules/auth/frontend/login.tsx
@open-mercato/core/modules/<module>/api/....apps/mercato/src/modules/<module>/api/....apps/mercato/src/app/api/[...slug]/route.ts and dispatches by method + path.
requireAuth, requireRoles, and requireFeatures.@open-mercato/core/modules/<module>/data/entities.ts (fallbacks: db/entities.ts or schema.ts) and review the entities reference for conventions and helpers.data/extensions.ts (see Data Extensibility doc). Avoid mutating core entities to stay upgrade-safe.apps/mercato/src/modules/<module>/data/entities.override.ts.yarn generate.yarn db:generate -> writes into each module's package: packages/<pkg>/src/modules/<module>/migrations (falls back to apps/mercato/src/modules/<module>/migrations only for app-local modules).yarn db:migrate.yarn db:greenfield -> removes all existing migration files and snapshots from all modules.See also:
eventBus.emitEvent as documented in the events & subscribers guide.apps/mercato/src/modules/<module>/subscribers/*.ts; each file exports a default handler and metadata described in the file structure section.ce.ts exporting entities. Each item can include optional fields.entities[].fields into customFieldSets so yarn mercato entities install seeds them.packages/my-module/src/modules/my_module/ce.ts):export const entities = [
{
id: 'my_module:calendar_entity',
label: 'Calendar Entity',
showInSidebar: true,
fields: [
{ key: 'title', kind: 'text', label: 'Title', required: true, indexed: true, filterable: true, formEditable: true },
{ key: 'when', kind: 'text', label: 'When', filterable: true, formEditable: true },
{ key: 'location', kind: 'text', label: 'Location', filterable: true, formEditable: true },
{ key: 'notes', kind: 'multiline', label: 'Notes', editor: 'markdown', formEditable: true },
],
},
]
Notes:
data/fields.ts is no longer supported. Always declare fields under ce.ts as shown above.showInSidebar), which is set during entities install.yarn mercato entities install) processes every tenant by default and skips system entities automatically. Use --tenant <id>, --global, or --no-global to control the scope, and --dry-run / --force when testing.apps/mercato/src/modules/<module>/data/validators.ts or packages/<pkg>/src/modules/<module>/data/validators.ts.userLoginSchema, tenantCreateSchema).type Input = z.infer<typeof userLoginSchema>.packages/<pkg>/src/modules/<module>/cli.ts default export (argv) => void|Promise<void>.mercato dispatches to module CLIs: yarn mercato <module> ....node_modules.apps/mercato/src/modules/<module> via a postinstall script or a wrapper package; or copy its files into apps/mercato/src/modules/<module>./db/entities.ts so migrations generate.yarn generate to refresh the registry, entities, and DI.apps/mercato/src/i18n/<locale>.json (e.g., en, pl).packages/<pkg>/src/modules/<module>/i18n/<locale>.json.Module.translations.useT() hook for client components (@/lib/i18n/context).loadDictionary(locale) for server components (@open-mercato/shared/lib/i18n/server).resolveTranslations() to fetch locale/dictionary plus ready-to-use translators.createTranslator(dict) to build a reusable translator from a dictionary.createFallbackTranslator(dict) to get a single-call translate(key, fallback, params?).translateWithFallback(t, key, fallback, params?) for graceful defaults and string interpolation.Client usage:
"use client"
import { useT } from '@/lib/i18n/context'
import { translateWithFallback } from '@open-mercato/shared/lib/i18n/translate'
export default function MyComponent() {
const t = useT()
const heading = translateWithFallback(t, 'my_module.moduleTitle', 'My Module')
return <h1>{heading}</h1>
}
Server usage:
import { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'
export default async function Page() {
const { translate } = await resolveTranslations()
const title = translate('backend.title', 'Dashboard')
const subtitle = translate('backend.subtitle', 'Manage your workspace', { org: 'Acme' })
return (
<>
<h1>{title}</h1>
<p>{subtitle}</p>
</>
)
}
Modules must be isomorphic (self-contained) to ensure proper isolation and migration generation:
@ManyToOne/@OneToMany relationships across module boundaries.@Property fields for foreign keys (e.g., tenantId, organizationId).This ensures that:
directory defines tenants and organizations.tenant_id and organization_id FKs.tenant_id and organization_id.modulesInfo exported from @/modules/registry or @/modules/generated.di.ts. The container loads core defaults first, then applies registrars from each module in order, allowing overrides.apps/mercato/src/modules.ts and list modules to load, e.g.:
{ id: 'auth', from: '@open-mercato/core' }, { id: 'directory', from: '@open-mercato/core' }, { id: 'customers', from: '@open-mercato/core' }.@open-mercato/core, search in @open-mercato/search, events in @open-mercato/events, etc.apps/mercato/src/modules/<module>/... and take precedence over package files with the same relative path.apps/mercato/src/modules.ts.yarn generate) and migrations (yarn db:*) only include enabled modules.packages/<pkg>/src/modules/<module>/migrations for package modules, or apps/mercato/src/modules/<module>/migrations for app-local modules.Beyond core and app-level modules, the core team, partners, and the community publish reusable modules as npm packages. Open Mercato Modules lists them with what each one adds, its license, and the Open Mercato versions it installs on. They install in one command and can be ejected for full local ownership. Search it before you build a feature, and list your own module there to share it without modifying the core platform.
See Modules catalog for installation, ejection, and listing details.
Before building from scratch, check the Reference Example Module showcase – a capability-by-capability index into apps/mercato/src/modules/example/, the canonical, source-present-but-runtime-disabled module that demonstrates nearly every extension point this framework exposes (entities, APIs, forms, events, UMES, AI tools, and more), each with an exact link to the file that proves it.
Open src/modules/example/backend/todos/ in a classic standalone app. Follow the page into api/todos/route.ts, then inspect data/entities.ts, commands/ and acl.ts under the same module. The directory boundary shows which code is responsible for the record.