Framework
An order list may combine ordinary columns, custom fields and values contributed by another module. Describe that projection to the query engine and supply the caller's scope so filtering and paging apply to the assembled records.
The query layer provides a consistent API to fetch data across base entities, module extensions, and custom fields. It is available via DI as queryEngine.
The implementation registered behind that key is HybridQueryEngine, which reads through the shared entity_indexes JSONB index and delegates to the join-based BasicQueryEngine under four specific conditions. See Hybrid query engine for the routing and fallback rules, and JSONB indexing layer for the index design, backfill CLI, and performance tips.
import type { AppContainer } from '@open-mercato/shared/lib/di/container'
import { E } from '@open-mercato/core/datamodel/entities'
import { id, email, name } from '@open-mercato/core/datamodel/entities/user'
import type { QueryEngine } from '@open-mercato/shared/lib/query/types'
import { SortDir } from '@open-mercato/shared/lib/query/types'
export async function listUsers(container: AppContainer) {
const query = container.resolve<QueryEngine>('queryEngine')
return await query.query(E.auth.user, {
fields: [id, email, name, 'cf:vip'],
includeExtensions: true, // joins registered extensions
includeCustomFields: true, // auto-discovers keys via custom_field_defs (tenant-scoped only)
// Filters: array syntax (legacy)
filters: [
{ field: 'cf:vip', op: 'eq', value: true },
{ field: email, op: 'ilike', value: '[email protected]' },
],
sort: [{ field: email, dir: SortDir.Asc }],
page: { page: 1, pageSize: 25 },
// REQUIRED: tenant scope
tenantId: 'uuid-string-here',
// OPTIONAL: for base entities and virtual entities
organizationId: 'uuid-string-here',
})
}
entity: an EntityId string of the form <module>:<entity>; used to resolve the base table and registered extensions.fields: base columns and/or custom fields prefixed as cf:<key>. Prefer importing base columns from @open-mercato/<pkg>/datamodel/entities/<entity> (e.g., import { id, email } from '@open-mercato/core/datamodel/entities/user').includeExtensions: true to include all linked extension entities; or string[] of extension entity ids.includeCustomFields: true to include all CFs; or string[] of keys.filters: support base fields and cf:<key>.
{ field, op, value }.filters: {
title: { $ne: null },
email: { $ilike: '[email protected]' },
'cf:vip': true, // shorthand for $eq
}
cf_<key> (e.g., cf_priority), which is treated the same as cf:<key>.sort: base fields and cf:<key>. Use generated field constants and SortDir (e.g., { field: email, dir: SortDir.Asc }).page: paging options.tenantId: required. All queries must include tenant scope.organizationId: optional. Applied in addition to tenantId for base entities and virtual entities.withDeleted: include soft-deleted rows when true. By default, when a base table has a deleted_at column, queries exclude rows where deleted_at is not null.You can get compile-time help for filters by using the generic Where type from @open-mercato/shared/lib/query/types:
import type { Where } from '@open-mercato/shared/lib/query/types'
// Define a field→type map for your query
type UserFields = {
id: string
email: string
name: string | null
created_at: Date
'cf:vip': boolean
}
const filters: Where<UserFields> = {
email: { $ilike: '[email protected]' },
name: { $ne: null },
'cf:vip': true,
}
await query.query(E.auth.user, { filters })
If you don’t provide the generic, filters falls back to a permissive shape.
fields array using the cf: prefix (for example, cf:priority). They will be projected directly into the result objects.{ field: 'cf:priority', op: 'eq', value: 'High' }.tenantId. Organization filters are optional but recommended for back-office pages.defineFields DSL in your module’s ce.ts so that generators surface field metadata to UIs. Custom fields created through the admin UI appear automatically once they are published.BasicQueryEngine and HybridQueryEngine require tenantId.tenant_id; organization_id is not applied for CFs.tenant_id and, if provided, additionally by organization_id.tenant_id (required) and optional organization_id when the column exists. When the base table exposes deleted_at, rows with a non-null value are excluded unless withDeleted: true is passed.modules.generated.ts to discover entityExtensions and join them.custom_field_values to surface cf:* fields and filter/sort them efficiently; aggregate when multiple values exist.Both engines support an optional extensions context that activates sync lifecycle events and query-level enrichers around the core SQL query. When provided, the pipeline runs before-query subscribers, re-applies scope guards, executes enrichers, and runs after-query subscribers.
const result = await queryEngine.query('customers:person', {
tenantId: auth.tenantId,
organizationId: auth.orgId,
extensions: {
userId: auth.userId,
container: diContainer,
userFeatures: auth.features,
resolve: (name) => diContainer.resolve(name),
},
})
See the full guide: Query engine extensibility.
When you fetch data via MikroORM outside of the query engine, use the decryption helpers so encrypted columns are decoded with the right tenant/org scope:
import { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'
import { User } from './data/entities'
const users = await findWithDecryption(em, User, { tenant_id: tenantId }, undefined, {
tenantId,
organizationId,
})
const user = await findOneWithDecryption(em, User, { id: userId }, undefined, {
tenantId,
organizationId,
})
These helpers no-op when encryption is disabled but ensure encrypted relations that lack explicit scopes are decrypted safely. Prefer them over raw em.find / em.findOne for direct reads.