Class: InMemoryContext
Defined in: src/testing/InMemoryContext.ts:20
Lightweight, zero-setup in-memory DbContext for fast unit and integration testing.
Powered by MockDbAdapter with full in-memory SQL execution, query inspection,
and state reset capabilities.
Extends
Accessors
idempotency
Get Signature
get idempotency():
IdempotencyManager
Defined in: src/context/DbContext.ts:148
Request idempotency manager. Guarantees that duplicate requests or network retries never execute twice.
Returns
Inherited from
outbox
Get Signature
get outbox():
OutboxDispatcher
Defined in: src/context/DbContext.ts:158
Transactional Outbox dispatcher for guaranteed at-least-once domain event publishing.
Returns
Inherited from
pool
Get Signature
get pool():
IConnectionPool|undefined
Defined in: src/context/DbContext.ts:168
The connection pool managing active connections, heartbeat, and diagnostics, if configured.
Returns
IConnectionPool | undefined
Inherited from
provider
Get Signature
get provider():
DbProvider
Defined in: src/context/DbContext.ts:297
Returns the database provider type currently active (e.g. 'sqlite', 'postgres', 'mysql', 'mssql').
Usecase
Use this to write provider-conditional logic or display diagnostics in status dashboards.
Example
if (context.provider === 'postgres') {
// perform postgres-specific JSONB query
}
Returns
The active DbProvider identifier.
Inherited from
adapter
Get Signature
get adapter():
IDbAdapter
Defined in: src/context/DbContext.ts:307
Provides direct access to the underlying low-level database adapter.
Usecase
Use this when low-level adapter operations or driver-specific utilities are needed.
Returns
The active IDbAdapter instance.
Inherited from
cache
Get Signature
get cache():
IQueryCache|undefined
Defined in: src/context/DbContext.ts:778
Returns the configured query cache provider if one was registered in DbContextOptions.
Usecase
Use this to manually inspect or clear cached query entries across the application.
Returns
IQueryCache | undefined
Inherited from
executedQueries
Get Signature
get executedQueries():
object[]
Defined in: src/testing/InMemoryContext.ts:66
Returns all queries executed against this test context.
Returns
object[]
Constructors
Constructor
new InMemoryContext(
options?):InMemoryContext
Defined in: src/testing/InMemoryContext.ts:24
Parameters
| Parameter | Type |
|---|---|
options? | InMemoryContextOptions |
Returns
InMemoryContext
Overrides
Methods
forTenant()
forTenant(
tenantId):this
Defined in: src/context/DbContext.ts:85
Sets the active tenant identifier on this context instance and returns this for chaining.
Parameters
| Parameter | Type | Description |
|---|---|---|
tenantId | string | number | The tenant identifier (string or number). |
Returns
this
this DbContext instance.
Usecase
Scope a DbContext instance to a specific tenant in Express/Fastify request middleware.
Example
const ctx = new AppDbContext().forTenant(req.headers['x-tenant-id']);
const customers = await ctx.customers.toList(); // auto-filtered by tenant_id
Inherited from
on()
on<
T>(event,handler):this
Defined in: src/context/DbContext.ts:113
Registers a domain or lifecycle event handler (e.g. 'Account:created', '*:deleted', 'EntityCreated').
Type Parameters
| Type Parameter | Default type |
|---|---|
T | any |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | The event name or pattern to listen to. |
handler | EventHandler<T> | Callback function invoked with the event payload or entity. |
Returns
this
this DbContext instance for chaining.
Example
db.on('Account:created', async (entity) => {
await eventBus.publish(new AccountCreatedEvent(entity));
});
Inherited from
once()
once<
T>(event,handler):this
Defined in: src/context/DbContext.ts:121
Registers a one-time domain or lifecycle event handler.
Type Parameters
| Type Parameter | Default type |
|---|---|
T | any |
Parameters
| Parameter | Type |
|---|---|
event | string |
handler | EventHandler<T> |
Returns
this
Inherited from
off()
off(
event,handler?):this
Defined in: src/context/DbContext.ts:129
Unregisters a domain or lifecycle event handler.
Parameters
| Parameter | Type |
|---|---|
event | string |
handler? | EventHandler |
Returns
this
Inherited from
emit()
emit(
event,payload,alias?):Promise<void>
Defined in: src/context/DbContext.ts:137
Emits a domain or lifecycle event to all matching registered listeners.
Parameters
| Parameter | Type |
|---|---|
event | string |
payload | any |
alias? | string |
Returns
Promise<void>
Inherited from
withIdempotencyKey()
withIdempotencyKey<
T>(key,fn,options?):Promise<T>
Defined in: src/context/DbContext.ts:180
Executes an operation with automatic idempotency deduplication. If the key has already been completed, returns the cached result without repeating the operation.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | Unique client idempotency key (e.g. UUID, orderId, request ref). |
fn | () => Promise<T> | Business transaction function. |
options? | IdempotencyOptions | Idempotency lock and TTL configurations. |
Returns
Promise<T>
Inherited from
createUnitOfWork()
createUnitOfWork():
UnitOfWork<InMemoryContext>
Defined in: src/context/DbContext.ts:193
Creates a new Unit of Work instance bound to this DbContext session. Enables batching multiple DbSet operations with topological dependency ordering and single-transaction commit.
Returns
UnitOfWork<InMemoryContext>
Inherited from
onConfiguring()
protectedonConfiguring(options):void
Defined in: src/context/DbContext.ts:264
Override this method to configure database providers, connection strings, replica routing, and logging.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | DbContextOptionsBuilder | Fluent builder for database configuration. |
Returns
void