SQLiteAI 适配器
@aiao/rxdb-adapter-sqliteai 基于 @sqliteai/sqlite-wasm 提供带 AI 扩展的 SQLite 支持,在标准 SQLite 能力之上内置向量存储与相似度搜索,适合需要本地 AI 推理或语义检索的场景。
安装
- npm
- Yarn
- pnpm
- Bun
npm install @aiao/rxdb @aiao/rxdb-adapter-sqliteai
yarn add @aiao/rxdb @aiao/rxdb-adapter-sqliteai
pnpm add @aiao/rxdb @aiao/rxdb-adapter-sqliteai
bun add @aiao/rxdb @aiao/rxdb-adapter-sqliteai
基础使用
import { RxDB, SyncType } from '@aiao/rxdb';
import { RxDBAdapterSqliteai } from '@aiao/rxdb-adapter-sqliteai';
const rxdb = new RxDB({
dbName: 'demo',
entities: [Article],
sync: { local: { adapter: 'sqliteai' }, type: SyncType.None }
});
rxdb.adapter(
'sqliteai',
async db =>
new RxDBAdapterSqliteai(db, {
wasmPath: '/sqliteai/sqliteai.wasm',
opfs: true // 使用 OPFS 持久化
})
);
await rxdb.connect('sqliteai');
配置选项
interface SqliteaiOptions {
// WASM 文件路径
wasmPath?: string;
locateFile?: (name: string) => string;
// OPFS 持久化
opfs?: boolean;
opfsProxyPath?: string;
opfsFallback?: 'memory' | 'idb'; // OPFS 失败时的回退策略,默认 'memory'
// Worker 配置
worker?: boolean;
workerInstance?: Worker;
sharedWorker?: boolean;
sharedWorkerInstance?: SharedWorker;
// 性能调优
cacheSizeKb?: number; // 默认 51200(50MB)
batchTimeout?: number; // 默认 16ms(BALANCED)
// 调试
print?: (msg: string) => void;
printErr?: (msg: string) => void;
}
OPFS 持久化
rxdb.adapter(
'sqliteai',
async db =>
new RxDBAdapterSqliteai(db, {
opfs: true,
opfsFallback: 'idb', // OPFS 不可用时回退到 IndexedDB
wasmPath: '/sqliteai/sqliteai.wasm'
})
);
Worker 模式
// sqliteai.worker.ts
import { SqliteaiWorker } from '@aiao/rxdb-adapter-sqliteai';
new SqliteaiWorker().listen();
rxdb.adapter(
'sqliteai',
async db =>
new RxDBAdapterSqliteai(db, {
worker: true,
workerInstance: new Worker(new URL('./sqliteai.worker', import.meta.url), { type: 'module' }),
wasmPath: '/sqliteai/sqliteai.wasm'
})
);
与加密适配器配合
import { RxDBAdapterSqliteai } from '@aiao/rxdb-adapter-sqliteai';
const adapter = await RxDBAdapterSqliteai.create({ name: 'app.db', opfs: true });
const db = await RxDB.create({ adapter, entities: [Document] });
await adapter.encryption.unlock({ passphrase: 'my-passphrase' });
与其他 SQLite 适配器对比
| 维度 | rxdb-adapter-sqliteai | rxdb-adapter-sqlite-wasm | rxdb-adapter-wa-sqlite |
|---|---|---|---|
| 底层 WASM | @sqliteai/sqlite-wasm | @subframe7536/sqlite-wasm | wa-sqlite |
| AI / 向量扩展 | ✅ | ❌ | ❌ |
| FTS5 | ✅ | ✅ | ❌ |
| 全文搜索插件 | ❌(暂不兼容) | ✅ | ❌ |
| OPFS 支持 | ✅ | ✅ | ✅ |
| 推荐场景 | 本地 AI / 语义检索 | 全文搜索 / 通用场景 | 简单浏览器本地存储 |