|
| 1 | +# SQLite Implementation Compatibility |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +sqlite-diskann uses **duck typing** to support multiple SQLite library implementations. Any library providing the minimal required interface will work automatically, without needing to be explicitly listed as a dependency. |
| 6 | + |
| 7 | +## Required Interface |
| 8 | + |
| 9 | +Your SQLite library must provide these methods: |
| 10 | + |
| 11 | +```typescript |
| 12 | +interface DatabaseLike { |
| 13 | + loadExtension(path: string, entryPoint?: string): void; |
| 14 | + exec(sql: string): void; |
| 15 | + prepare(sql: string): StatementLike; |
| 16 | +} |
| 17 | + |
| 18 | +interface StatementLike { |
| 19 | + run(...params: any[]): { changes: number; lastInsertRowid: number | bigint }; |
| 20 | + all(...params: any[]): any[]; |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +This is the **complete** interface required - just 3 database methods and 2 statement methods. |
| 25 | + |
| 26 | +## Tested Implementations |
| 27 | + |
| 28 | +### node:sqlite (Node 22.5+, experimental) |
| 29 | + |
| 30 | +- **Module**: Built-in to Node.js (since 22.5.0) |
| 31 | +- **Class**: `DatabaseSync` |
| 32 | +- **Compatibility**: ✅ 100% compatible |
| 33 | +- **Performance**: Fastest (no C++ addon loading overhead) |
| 34 | +- **Installation**: None (built-in) |
| 35 | +- **Status**: ⚠️ **Experimental** (stability level 1.1 - Active development) |
| 36 | + |
| 37 | +```typescript |
| 38 | +import { DatabaseSync } from "node:sqlite"; |
| 39 | +import { loadDiskAnnExtension } from "@photostructure/sqlite-diskann"; |
| 40 | + |
| 41 | +const db = new DatabaseSync(":memory:", { allowExtension: true }); |
| 42 | +loadDiskAnnExtension(db); |
| 43 | +``` |
| 44 | + |
| 45 | +**Requirements**: |
| 46 | + |
| 47 | +- Node.js >= 22.5.0 |
| 48 | +- `--experimental-sqlite` flag required |
| 49 | +- Still experimental as of this writing (not recommended for production) |
| 50 | + |
| 51 | +**Run with:** |
| 52 | + |
| 53 | +```bash |
| 54 | +node --experimental-sqlite your-script.js |
| 55 | +``` |
| 56 | + |
| 57 | +### better-sqlite3 |
| 58 | + |
| 59 | +- **Module**: `better-sqlite3` npm package |
| 60 | +- **Class**: `Database` |
| 61 | +- **Compatibility**: ✅ 100% compatible |
| 62 | +- **Performance**: Excellent |
| 63 | +- **Installation**: `npm install better-sqlite3` |
| 64 | + |
| 65 | +```typescript |
| 66 | +import Database from "better-sqlite3"; |
| 67 | +import { loadDiskAnnExtension } from "@photostructure/sqlite-diskann"; |
| 68 | + |
| 69 | +const db = new Database(":memory:"); |
| 70 | +loadDiskAnnExtension(db); |
| 71 | +``` |
| 72 | + |
| 73 | +### @photostructure/sqlite |
| 74 | + |
| 75 | +- **Module**: `@photostructure/sqlite` npm package |
| 76 | +- **Class**: `DatabaseSync` |
| 77 | +- **Compatibility**: ✅ 100% compatible |
| 78 | +- **Performance**: Identical to node:sqlite |
| 79 | +- **Installation**: `npm install @photostructure/sqlite` |
| 80 | + |
| 81 | +```typescript |
| 82 | +import { DatabaseSync } from "@photostructure/sqlite"; |
| 83 | +import { loadDiskAnnExtension } from "@photostructure/sqlite-diskann"; |
| 84 | + |
| 85 | +const db = new DatabaseSync(":memory:", { allowExtension: true }); |
| 86 | +loadDiskAnnExtension(db); |
| 87 | +``` |
| 88 | + |
| 89 | +**Note**: This is an independent implementation, written from scratch, that is strictly API-compatible with the latest version of Node.js's `node:sqlite` module. It supports older Node.js versions that lack the built-in `node:sqlite` module. |
| 90 | + |
| 91 | +## Known Differences Between Implementations |
| 92 | + |
| 93 | +All three implementations are compatible, but there are minor differences to be aware of: |
| 94 | + |
| 95 | +### Return Type: `lastInsertRowid` |
| 96 | + |
| 97 | +All three implementations return `{ changes: number, lastInsertRowid: number | bigint }` from `stmt.run()`. The `lastInsertRowid` field can be either `number` or `bigint` depending on the row ID value. |
| 98 | + |
| 99 | +**Recommended handling:** |
| 100 | + |
| 101 | +```typescript |
| 102 | +const { lastInsertRowid } = stmt.run(params); |
| 103 | +const id = |
| 104 | + typeof lastInsertRowid === "bigint" ? Number(lastInsertRowid) : lastInsertRowid; |
| 105 | +``` |
| 106 | + |
| 107 | +### Error Messages |
| 108 | + |
| 109 | +Each implementation throws different error types and messages. Your error handling should check message content rather than error type: |
| 110 | + |
| 111 | +```typescript |
| 112 | +try { |
| 113 | + loadDiskAnnExtension(db); |
| 114 | +} catch (error) { |
| 115 | + console.error("Failed to load extension:", error.message); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Extension Loading |
| 120 | + |
| 121 | +- **node:sqlite**: Requires `--experimental-sqlite` flag (all versions, still experimental) |
| 122 | +- **better-sqlite3**: No flags needed, stable |
| 123 | +- **@photostructure/sqlite**: No flags needed, stable |
| 124 | + |
| 125 | +## Using Your Own SQLite Wrapper |
| 126 | + |
| 127 | +If you have a custom SQLite wrapper, it will work with sqlite-diskann if it provides the required interface: |
| 128 | + |
| 129 | +```typescript |
| 130 | +class MyCustomDatabase { |
| 131 | + loadExtension(path: string, entryPoint?: string): void { |
| 132 | + // Your implementation |
| 133 | + } |
| 134 | + |
| 135 | + exec(sql: string): void { |
| 136 | + // Your implementation |
| 137 | + } |
| 138 | + |
| 139 | + prepare(sql: string): MyCustomStatement { |
| 140 | + // Your implementation |
| 141 | + return new MyCustomStatement(sql); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +class MyCustomStatement { |
| 146 | + run(...params: any[]): { changes: number; lastInsertRowid: number | bigint } { |
| 147 | + // Your implementation |
| 148 | + return { changes: 1, lastInsertRowid: 123 }; |
| 149 | + } |
| 150 | + |
| 151 | + all(...params: any[]): any[] { |
| 152 | + // Your implementation |
| 153 | + return []; |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +Then use it with sqlite-diskann: |
| 159 | + |
| 160 | +```typescript |
| 161 | +import { loadDiskAnnExtension } from "@photostructure/sqlite-diskann"; |
| 162 | + |
| 163 | +const db = new MyCustomDatabase(); |
| 164 | +loadDiskAnnExtension(db); // Works! |
| 165 | +``` |
| 166 | + |
| 167 | +## Type Safety |
| 168 | + |
| 169 | +TypeScript will accept any object with the correct shape: |
| 170 | + |
| 171 | +```typescript |
| 172 | +// This type-checks but will fail at runtime |
| 173 | +const fakeDb = { |
| 174 | + exec: () => {}, |
| 175 | + prepare: () => ({ run: () => ({}), all: () => [] }), |
| 176 | + loadExtension: () => { |
| 177 | + throw new Error("Not implemented"); |
| 178 | + }, |
| 179 | +}; |
| 180 | + |
| 181 | +loadDiskAnnExtension(fakeDb); // Compiles, throws at runtime |
| 182 | +``` |
| 183 | + |
| 184 | +This is intentional - duck typing trades compile-time safety for runtime flexibility. The benefit is that **any** SQLite library works without modification. |
| 185 | + |
| 186 | +## Migration Guide |
| 187 | + |
| 188 | +### Switching Between Implementations |
| 189 | + |
| 190 | +All three implementations have identical APIs for the methods we use. To switch: |
| 191 | + |
| 192 | +#### From @photostructure/sqlite to node:sqlite (Node 22+) |
| 193 | + |
| 194 | +```bash |
| 195 | +npm uninstall @photostructure/sqlite |
| 196 | +``` |
| 197 | + |
| 198 | +```diff |
| 199 | +-import { DatabaseSync } from "@photostructure/sqlite"; |
| 200 | ++import { DatabaseSync } from "node:sqlite"; |
| 201 | + |
| 202 | + const db = new DatabaseSync(":memory:", { allowExtension: true }); |
| 203 | + // Rest of code unchanged |
| 204 | +``` |
| 205 | + |
| 206 | +#### From @photostructure/sqlite to better-sqlite3 |
| 207 | + |
| 208 | +```bash |
| 209 | +npm uninstall @photostructure/sqlite |
| 210 | +npm install better-sqlite3 |
| 211 | +``` |
| 212 | + |
| 213 | +```diff |
| 214 | +-import { DatabaseSync } from "@photostructure/sqlite"; |
| 215 | ++import Database from "better-sqlite3"; |
| 216 | + |
| 217 | +-const db = new DatabaseSync(":memory:", { allowExtension: true }); |
| 218 | ++const db = new Database(":memory:"); |
| 219 | + // Rest of code unchanged |
| 220 | +``` |
| 221 | + |
| 222 | +## Advantages of Duck Typing |
| 223 | + |
| 224 | +### No Peer Dependency Warnings |
| 225 | + |
| 226 | +Without peer dependencies, users never see warnings like: |
| 227 | + |
| 228 | +``` |
| 229 | +npm WARN sqlite-diskann@1.0.0 requires a peer of better-sqlite3@>=11.0.0 |
| 230 | +``` |
| 231 | + |
| 232 | +### Zero Dependencies |
| 233 | + |
| 234 | +Users with Node 22+ don't need to install any additional packages. The package has **zero runtime dependencies**. |
| 235 | + |
| 236 | +### Future-Proof |
| 237 | + |
| 238 | +If a new SQLite library is released tomorrow with the same interface, it will work with sqlite-diskann without any changes. |
| 239 | + |
| 240 | +### Flexibility |
| 241 | + |
| 242 | +Users can: |
| 243 | + |
| 244 | +- Mock the database for testing |
| 245 | +- Use wrapped/proxied database instances |
| 246 | +- Switch implementations without changing application code |
| 247 | +- Use internal/enterprise SQLite libraries |
| 248 | + |
| 249 | +## Testing |
| 250 | + |
| 251 | +The test suite runs against all available implementations automatically: |
| 252 | + |
| 253 | +```bash |
| 254 | +npm run test:ts |
| 255 | +``` |
| 256 | + |
| 257 | +Each available implementation gets its own test suite. If an implementation is not installed or not available on the current Node version (e.g., `node:sqlite` on Node 20), those tests are automatically skipped. |
| 258 | + |
| 259 | +## Summary |
| 260 | + |
| 261 | +- **Interface**: Only 3 database methods + 2 statement methods required |
| 262 | +- **No dependencies**: Users choose their own SQLite library |
| 263 | +- **100% compatible**: node:sqlite, better-sqlite3, @photostructure/sqlite all work |
| 264 | +- **Future-proof**: Any library matching the interface will work |
| 265 | +- **Type-safe**: TypeScript ensures compile-time compatibility |
0 commit comments