UNPKG

@dqcai/sqlite

Version:

Universal SQLite adapter for Node.js, Browser, Deno, Bun, and React Native with a unified API and query builder.

119 lines 4.39 kB
import { QueryTable, WhereClause, OrderByClause, ImportResult, ColumnMapping, ImportOptions, ServiceStatus, HealthCheckResult } from "../types"; import { UniversalDAO } from "./universal-dao"; export interface FindOptions { where?: WhereClause[]; orderBy?: OrderByClause[]; limit?: number; offset?: number; columns?: string[]; } export type ErrorHandler = (error: Error) => void; export type EventHandler = (data: any) => void; /** * Universal BaseService - An enhanced abstract base class designed to provide * comprehensive CRUD operations and database management features across all * operating systems and frameworks using TypeScript and JavaScript. */ export declare abstract class BaseService<T = any> { protected dao: UniversalDAO | null; protected schemaName: string; protected tableName: string; protected isOpened: boolean; protected isInitialized: boolean; protected errorHandlers: Map<string, ErrorHandler>; protected eventListeners: Map<string, EventHandler[]>; protected primaryKeyFields: string[]; private cache; private reconnectHandler; constructor(schemaName: string, tableName?: string); private bindMethods; /** * Set primary key fields for the service */ setPrimaryKeyFields(fields: string[]): this; /** * Initialize the service and establish database connection */ init(): Promise<this>; /** * Create a new record - Safe version với comprehensive error handling */ create(data: Partial<T>): Promise<T | null>; /** * Update an existing record */ update(id: any, data: Partial<T>): Promise<T | null>; /** * Delete a record by ID */ delete(id: any): Promise<boolean>; /** * Find a record by ID */ findById(id: any): Promise<T | null>; /** * Find the first record matching conditions */ findFirst(conditions?: Record<string, any>): Promise<T | null>; /** * Find all records matching conditions */ findAll(conditions?: Record<string, any>, options?: FindOptions): Promise<T[]>; /** * Count records matching conditions */ count(where?: WhereClause[] | Record<string, any>): Promise<number>; /** * Check if a record exists by ID */ exists(id: any): Promise<boolean>; /** * Truncate table (delete all records and reset auto-increment) */ truncate(): Promise<void>; /** * Bulk insert records */ bulkInsert(items: Partial<T>[]): Promise<ImportResult>; /** * Bulk create records with transaction support */ bulkCreate(dataArray: Record<string, any>[]): Promise<T[]>; /** * Execute operations within a transaction */ executeTransaction(callback: () => Promise<any>): Promise<any>; /** * Import data from CSV */ importFromCSV(csvData: string, options?: { delimiter?: string; hasHeader?: boolean; columnMappings?: ColumnMapping[]; } & Partial<ImportOptions>): Promise<ImportResult>; /** * Import data with column mapping */ importDataWithMapping(data: Record<string, any>[], columnMappings: ColumnMapping[], options?: Partial<ImportOptions>): Promise<ImportResult>; protected buildSelectTable(conditions?: Record<string, any>, options?: FindOptions): QueryTable; protected buildDataTable(data: Record<string, any>): QueryTable; protected buildWhereFromObject(obj: Record<string, any>): WhereClause[]; on(event: string, handler: EventHandler): this; off(event: string, handler: EventHandler): this; protected _emit(event: string, data: any): void; setErrorHandler(errorType: string, handler: ErrorHandler): this; protected _handleError(errorType: string, error: Error): void; protected _validateData(data: any): void; protected _ensureInitialized(): Promise<void>; private ensureValidConnection; getDatabaseInfo(): Promise<any>; getTableInfo(): Promise<any[]>; getStatus(): ServiceStatus; healthCheck(): Promise<HealthCheckResult>; close(): Promise<boolean>; destroy(): void; getAll(conditions?: Record<string, any>, options?: FindOptions): Promise<T[]>; getById(id: string | number): Promise<T | null>; getFirst(conditions?: Record<string, any>): Promise<T | null>; } //# sourceMappingURL=base-service.d.ts.map