@aradox/multi-orm
Version:
Type-safe ORM with multi-datasource support, row-level security, and Prisma-like API for PostgreSQL, SQL Server, and HTTP APIs
132 lines • 3.37 kB
TypeScript
/**
* Middleware and Hooks System for Row-Level Security
*
* This module defines the types and interfaces for query middleware,
* enabling features like:
* - Row-level security (RLS) / tenant isolation
* - Audit logging
* - Query caching
* - Performance monitoring
* - Data sanitization
*/
/**
* User context for RLS and authorization
*/
export interface QueryContext {
/**
* Authenticated user information
*/
user?: {
id: number | string;
tenantId?: number | string;
role?: string;
permissions?: string[];
[key: string]: any;
};
/**
* Custom context data for middleware
*/
custom?: Record<string, any>;
/**
* Request metadata (for audit logging)
*/
metadata?: {
requestId?: string;
ip?: string;
userAgent?: string;
timestamp?: Date;
[key: string]: any;
};
}
/**
* Query operation types
*/
export type QueryOperation = 'findMany' | 'findUnique' | 'create' | 'createMany' | 'update' | 'updateMany' | 'upsert' | 'delete' | 'deleteMany' | 'count';
/**
* Query arguments (generic structure)
*/
export interface QueryArgs {
where?: any;
select?: any;
include?: any;
orderBy?: any;
skip?: number;
take?: number;
data?: any;
[key: string]: any;
}
/**
* Before query hook - called before query execution
* Can modify args or throw to prevent execution
*/
export type BeforeQueryHook = (params: {
model: string;
operation: QueryOperation;
args: QueryArgs;
context: QueryContext;
}) => QueryArgs | Promise<QueryArgs>;
/**
* After query hook - called after query execution
* Can transform results or perform side effects
*/
export type AfterQueryHook = (params: {
model: string;
operation: QueryOperation;
args: QueryArgs;
result: any;
context: QueryContext;
}) => any | Promise<any>;
/**
* Error hook - called when query fails
*/
export type ErrorQueryHook = (params: {
model: string;
operation: QueryOperation;
args: QueryArgs;
error: Error;
context: QueryContext;
}) => void | Promise<void>;
/**
* Middleware interface
*/
export interface Middleware {
/**
* Name of the middleware (for debugging)
*/
name?: string;
/**
* Called before query execution
*/
beforeQuery?: BeforeQueryHook;
/**
* Called after successful query execution
*/
afterQuery?: AfterQueryHook;
/**
* Called when query throws an error
*/
onError?: ErrorQueryHook;
}
/**
* Middleware chain executor
*/
export declare class MiddlewareChain {
private middlewares;
/**
* Register a middleware
*/
use(middleware: Middleware): void;
/**
* Execute beforeQuery hooks in order
*/
executeBeforeQuery(model: string, operation: QueryOperation, args: QueryArgs, context: QueryContext): Promise<QueryArgs>;
/**
* Execute afterQuery hooks in order
*/
executeAfterQuery(model: string, operation: QueryOperation, args: QueryArgs, result: any, context: QueryContext): Promise<any>;
/**
* Execute error hooks (all run, errors are logged but not thrown)
*/
executeOnError(model: string, operation: QueryOperation, args: QueryArgs, error: Error, context: QueryContext): Promise<void>;
}
//# sourceMappingURL=middleware.d.ts.map