Framework
An import and an admin form may both edit a customer. Route those writes through the data engine when they need the same lifecycle events, so subscribers and indexes see a consistent record change whichever caller initiated it.
Purpose: centralize write operations that should emit standard events, so indexing and other subscribers remain consistent regardless of where writes originate.
API
setCustomFields({ entityId, recordId, organizationId?, tenantId?, values, notify?, pinOrganizationId? })
<module>.<entity>.updated with { id, organizationId, tenantId } when notify !== false (default true).pinOrganizationId (default false) additionally scopes the reconciling delete/lookup by organizationId – see Scope reconciliation on write for when a caller must set it.A custom field value's logical key is (entity_id, record_id, field_key) – the organization and tenant columns record which scope the value was written under, they are not part of the identity. The Query Engine follows that rule: its value join matches the three key columns, tolerates a NULL tenant, and does not filter organization_id at all. (The query-index reader in query_index/lib/indexer.ts does scope organization_id, but only ever for a record it is already indexing under that organization, so it never returns another organization's row.)
recordId must therefore be unique across organizations and tenants for a given entity_id. Every ORM-backed caller passes the record's own primary key, which satisfies this. A caller that passed a per-organization natural key – a code, a slug, a settings name – would have one organization's write delete another organization's live values.
This does not hold for custom_entities_storage records written through DataEngine.createCustomEntityRecord/updateCustomEntityRecord under ENTITIES_BACKCOMPAT_EAV_FOR_CUSTOM: that table upserts on (entity_type, entity_id, organization_id), so the same recordId can be a live record in two organizations of the same tenant. Those two call sites pass pinOrganizationId: true to setCustomFields, which additionally scopes the reconciling delete/lookup by organizationId – restoring the pre-reconciliation organization pin for exactly the callers whose recordId is not globally unique.
Each setCustomFields write is authoritative over every row a reader in the caller's own tenant scope could return for that record: same logical key, any organization, and either the caller's tenant or the instance-global NULL tenant. When a record's organization has changed since the field was last written, the write removes the row left behind by the previous organization instead of adding a second live row next to it; the same pass collapses duplicates from two writes that raced. Reconciliation happens per field key, only for keys present in values, so a field a record never writes again keeps whatever rows it already has.
Another tenant's own rows are never touched. A cross-tenant DELETE would be a worse failure than the duplicate it prevents, so a record moved between tenants can still leave an unreachable row behind under its old tenant; that row answers no query, and cleaning it up is an out-of-band audit concern. Rows with a NULL tenant are a deliberate exception: they are instance-global and answer every tenant's reads, so leaving one behind would reproduce exactly the ambiguity this reconciles – a tenant-scoped write removes them.
The table intentionally carries no unique constraint on that key: a multi-value custom field stores one row per selected value, all sharing (entity_id, record_id, field_key, organization_id, tenant_id) and differing only in the value column. The cf_values_entity_record_key_idx index backs the reconciliation lookup and the reader join, but the "one live row per single-value field" invariant is enforced by the write path in packages/core/src/modules/entities/lib/helpers.ts. Any code that writes custom_field_values directly rather than through setCustomFields bypasses that guarantee.
DI registration
dataEngine from the request container:
packages/shared/src/lib/di/container.tsDefaultDataEngine in packages/shared/src/lib/data/engine.ts.Usage
packages/shared/src/lib/crud/factory.ts calls dataEngine.setCustomFields(...) on create/update when custom fields are enabled.dataEngine.setCustomFields(...) instead of writing CFs directly.Example
const { resolve } = await createRequestContainer()
const de = resolve('dataEngine') as DataEngine
await de.setCustomFields({
entityId: 'example:todo',
recordId: todoId,
organizationId: orgId,
tenantId,
values: { priority: 3, severity: 'high' },
})
Why this matters
onChanged hooks for CF writes; events are emitted at the data layer.Extending
create, update, and delete helpers to DataEngine to funnel all entity mutations through the same layer (for audit, validation, or derived writes) while keeping module boundaries clean.Related files
packages/shared/src/lib/data/engine.tspackages/shared/src/lib/di/container.ts: queryEngine, dataEnginepackages/shared/src/lib/crud/factory.tspackages/example/src/modules/example/cli.tsPrevious: Role-Based Access Control · Next: Dev Runtime Diagnostics