UNPKG

@onurege3467/zerohelper

Version:

ZeroHelper is a versatile high-performance utility library and database framework for Node.js, fully written in TypeScript.

72 lines (71 loc) 2.76 kB
export type HookType = 'beforeInsert' | 'afterInsert' | 'beforeUpdate' | 'afterUpdate' | 'beforeDelete' | 'afterDelete'; export type HookFunction = (table: string, data: any) => Promise<void> | void; /** * Defines the common interface that all database adapters must implement. */ export declare abstract class IDatabase { protected hooks: Record<HookType, HookFunction[]>; /** * Registers a lifecycle hook. */ on(hook: HookType, fn: HookFunction): void; protected runHooks(hook: HookType, table: string, data: any): Promise<void>; /** * Returns performance metrics for the database and cache. */ getMetrics(): { database: { totalOperations: number; averageDuration: string; slowestOperations: import("./telemetry").DatabaseMetrics[]; recentLogs: import("./telemetry").DatabaseMetrics[]; }; cache: { ratio: string; hits: number; misses: number; }; }; protected recordMetric(operation: string, table: string, duration: number): void; /** * Selects multiple records based on the specified conditions. */ abstract select<T = any>(table: string, where?: Record<string, any> | null): Promise<T[]>; /** * Selects a single record based on the specified conditions. */ abstract selectOne<T = any>(table: string, where?: Record<string, any> | null): Promise<T | null>; /** * Inserts a new record. */ abstract insert(table: string, data: Record<string, any>): Promise<number | string | any>; /** * Updates records matching the specified conditions. */ abstract update(table: string, data: Record<string, any>, where: Record<string, any>): Promise<number>; /** * Updates a record or inserts it as a new record if it doesn't exist (Upsert). */ abstract set(table: string, data: Record<string, any>, where: Record<string, any>): Promise<any>; /** * Deletes records matching the specified conditions. */ abstract delete(table: string, where: Record<string, any>): Promise<number>; /** * Inserts multiple records in bulk. */ abstract bulkInsert(table: string, dataArray: Record<string, any>[]): Promise<number>; /** * Increments numeric fields. */ abstract increment(table: string, increments: Record<string, number>, where: Record<string, any>): Promise<number>; /** * Decrements numeric fields. */ abstract decrement(table: string, decrements: Record<string, number>, where: Record<string, any>): Promise<number>; /** * Safely closes the database connection. */ abstract close(): Promise<void>; } export default IDatabase;