/** * 数据源接口定义 * 定义所有数据源的标准接口,便于替换和测试 */ // API 数据源接口 export interface IApiDataSource { get(url: string, params?: any): Promise; post(url: string, data?: any): Promise; put(url: string, data?: any): Promise; delete(url: string, data?: any): Promise; upload(url: string, file: { uri: string; name: string; type: string }, additionalData?: Record): Promise; } // 本地数据源接口 (SQLite) export interface ILocalDataSource { query(sql: string, params?: any[]): Promise; getFirst(sql: string, params?: any[]): Promise; execute(sql: string, params?: any[]): Promise; run(sql: string, params?: any[]): Promise<{ lastInsertRowId: number; changes: number }>; transaction(operations: () => Promise): Promise; batch(operations: (db: ILocalDataSource) => Promise): Promise; enqueueWrite(operation: () => Promise): Promise; initialize(userId?: string): Promise; switchDatabase(userId?: string | null): Promise; closeDatabase(): Promise; } // 缓存数据源接口 export interface ICacheDataSource { get(key: string): Promise; set(key: string, value: T, ttl?: number): Promise; delete(key: string): Promise; clear(): Promise; has(key: string): Promise; getMultiple(keys: string[]): Promise<(T | null)[]>; setMultiple(entries: { key: string; value: T; ttl?: number }[]): Promise; } // 数据源错误类型 export class DataSourceError extends Error { constructor( message: string, public code: string, public source: string, public originalError?: Error ) { super(message); this.name = 'DataSourceError'; } } // 数据源配置 export interface DataSourceConfig { api?: { baseUrl: string; timeout?: number; retryCount?: number; }; local?: { dbName: string; version: number; }; cache?: { maxSize: number; defaultTtl: number; }; }