db.d.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * db.ts - Cross-runtime SQLite compatibility layer
  3. *
  4. * Provides a unified Database export that works under both Bun (bun:sqlite)
  5. * and Node.js (better-sqlite3). The APIs are nearly identical — the main
  6. * difference is the import path.
  7. *
  8. * On macOS, Apple's system SQLite is compiled with SQLITE_OMIT_LOAD_EXTENSION,
  9. * which prevents loading native extensions like sqlite-vec. When running under
  10. * Bun we call Database.setCustomSQLite() to swap in Homebrew's full-featured
  11. * SQLite build before creating any database instances.
  12. */
  13. export declare const isBun: boolean;
  14. /**
  15. * Open a SQLite database. Works with both bun:sqlite and better-sqlite3.
  16. */
  17. export declare function openDatabase(path: string): Database;
  18. /**
  19. * Common subset of the Database interface used throughout QMD.
  20. */
  21. export interface Database {
  22. exec(sql: string): void;
  23. prepare(sql: string): Statement;
  24. loadExtension(path: string): void;
  25. close(): void;
  26. /**
  27. * Wrap a synchronous function in a SQLite transaction. better-sqlite3 opens
  28. * `BEGIN IMMEDIATE` on entry and `COMMIT` on return; on throw it rolls back
  29. * AND re-throws. bun:sqlite has the same shape. Used by `generateEmbeddings`
  30. * to batch per-row INSERTs into a single WAL fsync (i-fkpnar9i).
  31. */
  32. transaction<T extends unknown[], R>(fn: (...args: T) => R): (...args: T) => R;
  33. }
  34. export interface Statement {
  35. run(...params: any[]): {
  36. changes: number;
  37. lastInsertRowid: number | bigint;
  38. };
  39. get(...params: any[]): any;
  40. all(...params: any[]): any[];
  41. }
  42. /**
  43. * Load the sqlite-vec extension into a database.
  44. *
  45. * Throws with platform-specific fix instructions when the extension is
  46. * unavailable.
  47. */
  48. export declare function loadSqliteVec(db: Database): void;