rxdb-adapter-wa-sqlite
RxDB 适配器,使用 wa-sqlite 在浏览器中运行 SQLite。
功能特性
- 本地优先: 在浏览器中通过 WebAssembly 运行完整 SQLite
- 零服务器: 无需后端服务器,数据存储在本地
- SQLite 兼容: 支持标准 SQLite 语法和功能
- 响应式: 数据变化自动触发更新
- 高性能: 使用 Web Worker 避免阻塞主线程
何时使用
- 需要轻量级本地数据库(WASM 体积小)
- 需要 SQLite 生态兼容性
- 需要 FTS5 全文搜索
- 对启动速度和内存占用敏感的应用
与其他适配器对比
| 特性 | wa-sqlite | sqlite-wasm | PGlite |
|---|---|---|---|
| 数据库引擎 | SQLite | SQLite | PostgreSQL |
| WASM 大小 | ~500KB | ~800KB | ~3MB |
| 全文搜索 | FTS5 | FTS5 | tsvector |
| VFS 选项 | 多种(IDB/OPFS) | 标准 | 固定 |
| 成熟度 | 高 | 官方支持 | 较新 |
安装
npm install @aiao/rxdb @aiao/rxdb-adapter-wa-sqlite
# 或
pnpm add @aiao/rxdb @aiao/rxdb-adapter-wa-sqlite
使用
import { RxDB, SyncType } from '@aiao/rxdb';
import { RxDBAdapterWaSqlite } from '@aiao/rxdb-adapter-wa-sqlite';
const rxdb = new RxDB({
dbName: 'app',
context: { userId: 'current-user' },
entities: [],
sync: {
local: { adapter: 'wa-sqlite' },
type: SyncType.None
}
});
rxdb.adapter(
'wa-sqlite',
db =>
new RxDBAdapterWaSqlite(db, {
vfs: 'IDBBatchAtomicVFS',
async: true,
wasmPath: '/wa-sqlite/wa-sqlite-async.wasm'
})
);
await rxdb.connect('wa-sqlite');
VFS 选项
wa-sqlite 支持多种虚拟文件系统(VFS):
- IDBBatchAtomicVFS: IndexedDB 后端,批量原子写入,推荐用于生产
- OPFSCoopSyncVFS: Origin Private File System,需要 SharedArrayBuffer 支持
- MemoryVFS: 纯内存存储,适合测试
完整示例
参考 dev-rxdb-angular 中的集成示例。
Classes
| Class | Description |
|---|---|
| RxDBAdapterSqlite | Backend-agnostic base class for SQLite adapters. |
| RxDBAdapterSqliteError | RxDB SQLite 适配器错误类 |
| SqliteClient | 类型安全的同步事件调度器。 |
| SqliteRepository | 操作 entity 仓库 |
Interfaces
| Interface | Description |
|---|---|
| GenerateSqlResult | - |
| RxDBAdapterSqliteBase | Backend-agnostic base class for SQLite adapters. |
| SqliteOptions | wa-sqlite 适配器配置。 |
Type Aliases
| Type Alias | Description |
|---|---|
| SqliteRepositoryConstructor | SQLite 仓库构造函数类型。 |
Variables
| Variable | Description |
|---|---|
| buildRuleGroup | 生成 ruleGroup sql 查询条件 |
| ROWID | sqlite 行 id 列名 |
| sqliteGetTableName | 获取表名 |
| sqliteGetTableNameByMetadata | 通过元数据获取表名 |
| WA_SQLITE_VFS_LIST | wa-sqlite 支持的 VFS 及运行环境能力。 |
Functions
| Function | Description |
|---|---|
| sqliteLoad | 加载 wa-sqlite 模块并注册选定 VFS。 |
References
RxDBAdapterWaSqlite
Renames and re-exports RxDBAdapterSqlite
WaSqliteClient
Renames and re-exports SqliteClient
waSqliteLoad
Renames and re-exports sqliteLoad
WaSqliteOptions
Renames and re-exports SqliteOptions
WaSqliteRepositoryConstructor
Renames and re-exports SqliteRepositoryConstructor