Open MercatoDocsSearch docsAPI reference

Framework

Query engine

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.

Why

Usage (DI)

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',
  })
}

Model

Typing filters

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.

Custom fields integration

Implementation notes

Query extensions (UMES Phase N)

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.

Encryption-aware finds

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.

Previous: Hybrid Search Module · Next: JSONB indexing layer