OptionalonCalled before any query execution (get, all, exec).
Use for:
Query context (mutable)
Modified context, original context, or undefined to skip
OptionalonCalled after successful query execution.
Use for:
Query context (read-only)
Query result
Modified result, original result, or undefined
OptionalonCalled before any write operation (run, batch).
Use for:
Write context (mutable)
Modified context, original context, or undefined to skip
OptionalonCalled after successful write operation.
Use for:
Write context (read-only)
Write result
OptionalonCalled before transaction starts.
Use for:
Transaction context
Modified context or undefined to skip
OptionalonCalled after transaction completes (commit or rollback).
Use for:
Transaction context with outcome
OptionalonCalled when any operation fails.
Use for:
The error that occurred
Operation context
Transformed error, original error, or undefined to suppress
onError: async (error, ctx) => {
// Log with full context
logger.error('Database operation failed', {
error: error.message,
operation: ctx.operation,
operationId: ctx.operationId,
duration: Date.now() - ctx.startTime,
});
// Transform to application-specific error
if (error.message.includes('unique constraint')) {
return new DuplicateEntryError('Record already exists');
}
return error;
}
Lifecycle hooks for extending adapter behavior.
All hooks are optional and async. Hooks can:
Remarks
Hook Execution Order:
onBeforeQuery/onBeforeWrite- Can modify contextonAfterQuery/onAfterWrite- Can modify resultonError- Only on failure, can modify/suppress errorThread Safety: Hooks execute sequentially per operation but may run concurrently across different operations.
Example: Complete hook setup for logging and metrics