Skip to main content
zerotal

Documentation


Documentation / @zerotal/orm / index / OrmContext

Class: OrmContext

Defined in: packages/orm/src/model/OrmContext.ts:22

Execution-scoped container for all mutable ORM state that must NOT leak across requests or tenants — the override/named connections, per-model state-machine transition callbacks, registered global scopes, and lifecycle hooks.

Keeping this state on a swappable context (rather than in module globals) lets each request or tenant boundary run with an isolated ORM view; the app scope integration below swaps a fresh OrmContext in per application scope.

Example

// Scope connection + registrations to a block, then restore:
const prev = useOrmContext(new OrmContext());
try {
  BaseModel.registerConnection("tenant", tenantConn);
  await doTenantWork();
} finally {
  useOrmContext(prev);
}

Constructors

Constructor

new OrmContext(): OrmContext

Returns

OrmContext

Properties

overrideConnection

overrideConnection: SQLInstance | null = null

Defined in: packages/orm/src/model/OrmContext.ts:24

Explicit connection override that wins over the resolved default (used by tests / withDatabase).


namedConnections

namedConnections: Map<string, SQLInstance>

Defined in: packages/orm/src/model/OrmContext.ts:26

Connections registered by name, selectable via static connection.


transitionCallbacks

transitionCallbacks: Map<Function, Map<string, unknown[]>>

Defined in: packages/orm/src/model/OrmContext.ts:28

Per-model onTransition callbacks, keyed by target state (see the State mixin).


globalScopes

globalScopes: Map<Function, Map<string, unknown>>

Defined in: packages/orm/src/model/OrmContext.ts:30

Per-model registered global query scopes.


hooks

hooks: Map<Function, Map<string, unknown[]>>

Defined in: packages/orm/src/model/OrmContext.ts:32

Per-model lifecycle hooks.