128 lines
3.7 KiB
TypeScript
128 lines
3.7 KiB
TypeScript
/**
|
|
* SqlJsBackend - Pure JavaScript SQLite for Windows compatibility
|
|
*
|
|
* When better-sqlite3 native compilation fails on Windows,
|
|
* sql.js provides a WASM-based fallback that works everywhere.
|
|
*
|
|
* @module v3/memory/sqljs-backend
|
|
*/
|
|
import { EventEmitter } from 'node:events';
|
|
import { IMemoryBackend, MemoryEntry, MemoryEntryUpdate, MemoryQuery, SearchOptions, SearchResult, BackendStats, HealthCheckResult, EmbeddingGenerator } from './types.js';
|
|
/**
|
|
* Configuration for SqlJs Backend
|
|
*/
|
|
export interface SqlJsBackendConfig {
|
|
/** Path to SQLite database file (:memory: for in-memory) */
|
|
databasePath: string;
|
|
/** Enable query optimization */
|
|
optimize: boolean;
|
|
/** Default namespace */
|
|
defaultNamespace: string;
|
|
/** Embedding generator (for compatibility with hybrid mode) */
|
|
embeddingGenerator?: EmbeddingGenerator;
|
|
/** Maximum entries before auto-cleanup */
|
|
maxEntries: number;
|
|
/** Enable verbose logging */
|
|
verbose: boolean;
|
|
/** Auto-persist interval in milliseconds (0 = manual only) */
|
|
autoPersistInterval: number;
|
|
/** Path to sql.js WASM file (optional, will use CDN default) */
|
|
wasmPath?: string;
|
|
}
|
|
/**
|
|
* SqlJs Backend for Cross-Platform Memory Storage
|
|
*
|
|
* Provides:
|
|
* - Pure JavaScript/WASM implementation (no native compilation)
|
|
* - Windows, macOS, Linux compatibility
|
|
* - Same SQL interface as better-sqlite3
|
|
* - In-memory with periodic disk persistence
|
|
* - Fallback when native SQLite fails
|
|
*/
|
|
export declare class SqlJsBackend extends EventEmitter implements IMemoryBackend {
|
|
private config;
|
|
private db;
|
|
private initialized;
|
|
private persistTimer;
|
|
private SQL;
|
|
private stats;
|
|
constructor(config?: Partial<SqlJsBackendConfig>);
|
|
/**
|
|
* Initialize the SqlJs backend
|
|
*/
|
|
initialize(): Promise<void>;
|
|
/**
|
|
* Shutdown the backend
|
|
*/
|
|
shutdown(): Promise<void>;
|
|
/**
|
|
* Create database schema
|
|
*/
|
|
private createSchema;
|
|
/**
|
|
* Store a memory entry
|
|
*/
|
|
store(entry: MemoryEntry): Promise<void>;
|
|
/**
|
|
* Retrieve a memory entry by ID
|
|
*/
|
|
get(id: string): Promise<MemoryEntry | null>;
|
|
/**
|
|
* Retrieve a memory entry by key within a namespace
|
|
*/
|
|
getByKey(namespace: string, key: string): Promise<MemoryEntry | null>;
|
|
/**
|
|
* Update a memory entry
|
|
*/
|
|
update(id: string, updateData: MemoryEntryUpdate): Promise<MemoryEntry | null>;
|
|
/**
|
|
* Delete a memory entry
|
|
*/
|
|
delete(id: string): Promise<boolean>;
|
|
/**
|
|
* Query memory entries
|
|
*/
|
|
query(query: MemoryQuery): Promise<MemoryEntry[]>;
|
|
/**
|
|
* Semantic vector search (limited without vector index)
|
|
*/
|
|
search(embedding: Float32Array, options: SearchOptions): Promise<SearchResult[]>;
|
|
/**
|
|
* Bulk insert entries
|
|
*/
|
|
bulkInsert(entries: MemoryEntry[]): Promise<void>;
|
|
/**
|
|
* Bulk delete entries
|
|
*/
|
|
bulkDelete(ids: string[]): Promise<number>;
|
|
/**
|
|
* Get entry count
|
|
*/
|
|
count(namespace?: string): Promise<number>;
|
|
/**
|
|
* List all namespaces
|
|
*/
|
|
listNamespaces(): Promise<string[]>;
|
|
/**
|
|
* Clear all entries in a namespace
|
|
*/
|
|
clearNamespace(namespace: string): Promise<number>;
|
|
/**
|
|
* Get backend statistics
|
|
*/
|
|
getStats(): Promise<BackendStats>;
|
|
/**
|
|
* Perform health check
|
|
*/
|
|
healthCheck(): Promise<HealthCheckResult>;
|
|
/**
|
|
* Persist changes to disk (sql.js is in-memory, needs explicit save)
|
|
*/
|
|
persist(): Promise<void>;
|
|
private ensureInitialized;
|
|
private rowToEntry;
|
|
private updateAccessTracking;
|
|
private cosineSimilarity;
|
|
private estimateMemoryUsage;
|
|
}
|
|
//# sourceMappingURL=sqljs-backend.d.ts.map
|