@aiao/rxdb-adapter-sqliteai 0.0.23 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @aiao/rxdb-adapter-sqliteai
2
+
3
+ `@aiao/rxdb` 的 [sqliteai](https://github.com/sqliteai) 存储适配器,为核心引擎提供基于 sqliteai 运行时的 SQLite 后端。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ pnpm add @aiao/rxdb @aiao/rxdb-adapter-sqliteai rxjs
9
+ ```
10
+
11
+ ## 用法
12
+
13
+ ```typescript
14
+ import { createSqliteClient, RxDBAdapterSqliteai, sqliteaiLoad } from '@aiao/rxdb-adapter-sqliteai';
15
+ ```
16
+
17
+ - `RxDBAdapterSqliteai`:接入 `@aiao/rxdb` 的适配器实现
18
+ - `createSqliteClient` / `SqliteaiClient`:sqliteai 客户端
19
+ - `sqliteaiLoad`:运行时加载
20
+ - 类型:`SqliteaiOptions`
21
+
22
+ 适配器共享内核(表名解析、规则编译、基础 Repository)来自 [`@aiao/rxdb-adapter-sqlite-core`](../rxdb-adapter-sqlite-core)。
23
+
24
+ ## 文档
25
+
26
+ - 仓库主页:[https://github.com/aiao-io/aiao](https://github.com/aiao-io/aiao)
27
+ - 适配器与存储后端指南见项目文档站
28
+
29
+ ## License
30
+
31
+ [MIT](https://github.com/aiao-io/aiao/blob/main/LICENSE)
@@ -1,14 +1,19 @@
1
+ import { SqliteClientLike } from '@aiao/rxdb-adapter-sqlite-core';
1
2
  import { SqliteaiOptions } from './sqliteai.interface.js';
2
- import { SqliteaiClient } from './SqliteaiClient.js';
3
3
  /**
4
4
  * 创建 SqliteAI 客户端实例并完成初始化。
5
5
  *
6
6
  * 如果 `options` 中指定了 worker / sharedWorker,会通过 `wrapWithComlink` 把客户端代理到 worker;
7
7
  * 否则在主线程直接 new。
8
8
  *
9
+ * 返回类型是 {@link SqliteClientLike} 而非具体的 `SqliteaiClient`:worker 模式下拿到的是
10
+ * Comlink 远端代理,它把每个方法都 Promise 化,断言成实现类会让 `beginTransactionSql(): string`
11
+ * 这类同步签名在跨线程模式下变成谎报(SQLC-040)。
12
+ *
9
13
  * @param dbName - 数据库名(用于持久化文件名归一化)
10
14
  * @param options - SqliteAI 适配器选项(含 OPFS / worker / 缓存大小等)
11
- * @returns 已 `init` 完成、可直接执行 SQL 的客户端
15
+ * @returns 已 `init` 完成、可直接执行 SQL 的客户端(主线程实例或其远端代理)
16
+ * @throws worker / sharedWorker 模式下传入 `locateFile` / `print` / `printErr` 等函数型选项时抛错
12
17
  */
13
- export declare function createSqliteClient(dbName: string, options: SqliteaiOptions): Promise<SqliteaiClient>;
18
+ export declare function createSqliteClient(dbName: string, options: SqliteaiOptions): Promise<SqliteClientLike>;
14
19
  //# sourceMappingURL=create_sqlite_client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create_sqlite_client.d.ts","sourceRoot":"","sources":["../src/create_sqlite_client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAA4B,MAAM,yBAAyB,CAAC;AACpF,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC,CAgB1G"}
1
+ {"version":3,"file":"create_sqlite_client.d.ts","sourceRoot":"","sources":["../src/create_sqlite_client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkD,KAAK,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AACvH,OAAO,EAAE,eAAe,EAA4B,MAAM,yBAAyB,CAAC;AAGpF;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAkB5G"}
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- export { ROWID, RxDBAdapterSqliteError, SqliteRepository, buildRuleGroup, get_table_name as sqliteGetTableName, get_table_name_by_metadata as sqliteGetTableNameByMetadata } from '@aiao/rxdb-adapter-sqlite-core';
1
+ export { BATCH_TIMEOUT, ROWID, RxDBAdapterSqliteError, SqliteRepository, buildRuleGroup, get_table_name as sqliteGetTableName, get_table_name_by_metadata as sqliteGetTableNameByMetadata } from '@aiao/rxdb-adapter-sqlite-core';
2
2
  export type { GenerateSqlResult } from '@aiao/rxdb-adapter-sqlite-core';
3
3
  export { createSqliteClient } from './create_sqlite_client.js';
4
4
  export { RxDBAdapterSqliteai } from './RxDBAdapterSqliteai.js';
5
5
  export { sqliteaiLoad } from './sqliteai-load.utils.js';
6
- export type { SqliteaiOptions } from './sqliteai.interface.js';
6
+ export type { SqliteaiLoadOptions, SqliteaiOptions, SqliteaiRepositoryConstructor } from './sqliteai.interface.js';
7
7
  export { SqliteaiClient } from './SqliteaiClient.js';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,EACL,sBAAsB,EACtB,gBAAgB,EAChB,cAAc,EACd,cAAc,IAAI,kBAAkB,EACpC,0BAA0B,IAAI,4BAA4B,EAC3D,MAAM,gCAAgC,CAAC;AACxC,YAAY,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAGxE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,YAAY,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,EACb,KAAK,EACL,sBAAsB,EACtB,gBAAgB,EAChB,cAAc,EACd,cAAc,IAAI,kBAAkB,EACpC,0BAA0B,IAAI,4BAA4B,EAC3D,MAAM,gCAAgC,CAAC;AACxC,YAAY,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAGxE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,YAAY,EAAE,mBAAmB,EAAE,eAAe,EAAE,6BAA6B,EAAE,MAAM,yBAAyB,CAAC;AACnH,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -1,29 +1,32 @@
1
- import { Oo1ClientBase as e, ROWID as t, RxDBAdapterSqliteBase as n, RxDBAdapterSqliteError as r, SqliteRepository as i, buildOo1InitOptions as a, buildRuleGroup as o, defaultWarn as s, get_table_name as c, get_table_name_by_metadata as l, withGlobalOo1LoadLock as u, withPatchedOpfsProxyWorker as d, withSqliteApiConfig as f, wrapWithComlink as p } from "@aiao/rxdb-adapter-sqlite-core";
2
- import m from "@sqliteai/sqlite-wasm";
1
+ import { BATCH_TIMEOUT as e, Oo1ClientBase as t, ROWID as n, RxDBAdapterSqliteBase as r, RxDBAdapterSqliteError as i, SqliteRepository as a, assertLoadOptionsTransferable as o, assertOo1Static as s, buildOo1InitOptions as c, buildRuleGroup as l, defaultWarn as u, get_table_name as d, get_table_name_by_metadata as f, isSameOo1LoadConfig as p, toOo1LoadFingerprint as m, withGlobalOo1LoadLock as h, withPatchedOpfsProxyWorker as g, withSqliteApiConfig as _, wrapWithComlink as v } from "@aiao/rxdb-adapter-sqlite-core";
2
+ import y from "@sqliteai/sqlite-wasm";
3
3
  //#region src/sqliteai-load.utils.ts
4
- var h, g = !1;
5
- async function _(e) {
6
- let t = !!e?.opfs && !!e.opfsProxyPath, n = () => h && (!t || g) ? h : void 0;
7
- return n() || u(async () => {
4
+ var b, x;
5
+ async function S(e) {
6
+ let t = m(e), n = () => b && x && p(x, t) ? b : void 0;
7
+ return n() || h(async () => {
8
8
  let r = n();
9
9
  if (r) return r;
10
- let i = a(e), o = await d(e, async () => await f({ warn: s }, async () => await m(i)));
11
- return o.config && (o.config.warn = s), h = o, g = t, o;
10
+ let i = c(e), a = await g(e, async () => await _({ warn: u }, async () => {
11
+ let e = await y(i);
12
+ return s(e), e;
13
+ }));
14
+ return a.config && (a.config.warn = u), b = a, x = t, a;
12
15
  });
13
16
  }
14
17
  //#endregion
15
18
  //#region src/SqliteaiClient.ts
16
- var v = class extends e {
19
+ var C = class extends t {
17
20
  get clientName() {
18
21
  return "sqliteai";
19
22
  }
20
23
  loadModule(e) {
21
- return _(e);
24
+ return S(e);
22
25
  }
23
26
  };
24
27
  //#endregion
25
28
  //#region src/create_sqlite_client.ts
26
- async function y(e, t) {
29
+ async function w(e, t) {
27
30
  let n = {
28
31
  opfs: t.opfs,
29
32
  wasmPath: t.wasmPath,
@@ -34,21 +37,23 @@ async function y(e, t) {
34
37
  cacheSizeKb: t.cacheSizeKb,
35
38
  batchTimeout: t.batchTimeout,
36
39
  opfsFallback: t.opfsFallback
37
- }, r = p(new v(), t);
40
+ };
41
+ o(n, t);
42
+ let r = v(new C(), t);
38
43
  return await r.init(e, n), r;
39
44
  }
40
45
  //#endregion
41
46
  //#region src/sqliteai.interface.ts
42
- var b = "sqliteai", x = class extends n {
47
+ var T = "sqliteai", E = class extends r {
43
48
  options;
44
49
  #e;
45
- name = b;
50
+ name = T;
46
51
  constructor(e, t) {
47
52
  super(e, t), this.options = t, this.#e = e.config.dbName;
48
53
  }
49
54
  async createClient() {
50
- return y(this.#e, this.options);
55
+ return w(this.#e, this.options);
51
56
  }
52
57
  };
53
58
  //#endregion
54
- export { t as ROWID, r as RxDBAdapterSqliteError, x as RxDBAdapterSqliteai, i as SqliteRepository, v as SqliteaiClient, o as buildRuleGroup, y as createSqliteClient, c as sqliteGetTableName, l as sqliteGetTableNameByMetadata, _ as sqliteaiLoad };
59
+ export { e as BATCH_TIMEOUT, n as ROWID, i as RxDBAdapterSqliteError, E as RxDBAdapterSqliteai, a as SqliteRepository, C as SqliteaiClient, l as buildRuleGroup, w as createSqliteClient, d as sqliteGetTableName, f as sqliteGetTableNameByMetadata, S as sqliteaiLoad };
@@ -1 +1 @@
1
- {"version":3,"file":"sqliteai-load.utils.d.ts","sourceRoot":"","sources":["../src/sqliteai-load.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,SAAS,EAIf,MAAM,gCAAgC,CAAC;AAExC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAKnE;;;;;GAKG;AACH,wBAAgB,sBAAsB,IAAI,IAAI,CAG7C;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,SAAS,CAAC,CA6BpF"}
1
+ {"version":3,"file":"sqliteai-load.utils.d.ts","sourceRoot":"","sources":["../src/sqliteai-load.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAUL,KAAK,SAAS,EACf,MAAM,gCAAgC,CAAC;AAExC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAYnE;;;;;GAKG;AACH,wBAAgB,sBAAsB,IAAI,IAAI,CAG7C;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,SAAS,CAAC,CAkCpF"}
@@ -17,9 +17,9 @@ interface SqliteaiOptionsBase extends IRxDBAdapterOptions {
17
17
  printErr?: (msg: string) => void;
18
18
  /** @see https://www.sqlite.org/pragma.html#pragma_cache_size @default 51200 */
19
19
  cacheSizeKb?: number;
20
- /** @default 16 (BALANCED) @see BATCH_TIMEOUT */
20
+ /** @default 16(BALANCED)@see BATCH_TIMEOUT */
21
21
  batchTimeout?: number;
22
- /** Behaviour when OPFS database opening fails. @default 'memory' */
22
+ /** OPFS 数据库打开失败时的行为。@default 'throw' @see Oo1ClientBase */
23
23
  opfsFallback?: OpfsFallback;
24
24
  worker?: boolean;
25
25
  workerInstance?: Worker;
@@ -1 +1 @@
1
- {"version":3,"file":"sqliteai.interface.d.ts","sourceRoot":"","sources":["../src/sqliteai.interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC3F,OAAO,KAAK,EAAE,YAAY,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAE1F;;;;;;;GAOG;AACH,UAAU,mBAAoB,SAAQ,mBAAmB;IACvD,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;IACtC,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9B,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,+EAA+E;IAC/E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gDAAgD;IAChD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,oEAAoE;IACpE,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,oBAAoB,CAAC,EAAE,YAAY,CAAC;CACrC;AAED,eAAe;AACf,KAAK,aAAa,GAAG,UAAU,GAAG,CAAC,KAAK,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,OAAO,CAAC,CAAC;AAEtE,mEAAmE;AACnE,MAAM,MAAM,6BAA6B,CAAC,CAAC,SAAS,cAAc,CAAC,aAAa,CAAC,IAAI,KACnF,OAAO,EAAE,qBAAqB,EAC9B,UAAU,EAAE,UAAU,KACnB,CAAC,CAAC;AAEP,gDAAgD;AAChD,MAAM,WAAW,eAAgB,SAAQ,mBAAmB;IAC1D,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,6BAA6B,CAAC,cAAc,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;CAC7F;AAED,4DAA4D;AAC5D,MAAM,MAAM,mBAAmB,GAAG,IAAI,CACpC,mBAAmB,EACjB,MAAM,GACN,UAAU,GACV,eAAe,GACf,YAAY,GACZ,OAAO,GACP,UAAU,GACV,aAAa,GACb,cAAc,GACd,cAAc,CACjB,CAAC;AAEF,2BAA2B;AAC3B,eAAO,MAAM,YAAY,EAAG,UAAmB,CAAC"}
1
+ {"version":3,"file":"sqliteai.interface.d.ts","sourceRoot":"","sources":["../src/sqliteai.interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC3F,OAAO,KAAK,EAAE,YAAY,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAE1F;;;;;;;GAOG;AACH,UAAU,mBAAoB,SAAQ,mBAAmB;IACvD,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;IACtC,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9B,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,+EAA+E;IAC/E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,8CAA8C;IAC9C,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,2DAA2D;IAC3D,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,oBAAoB,CAAC,EAAE,YAAY,CAAC;CACrC;AAED,eAAe;AACf,KAAK,aAAa,GAAG,UAAU,GAAG,CAAC,KAAK,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,OAAO,CAAC,CAAC;AAEtE,mEAAmE;AACnE,MAAM,MAAM,6BAA6B,CAAC,CAAC,SAAS,cAAc,CAAC,aAAa,CAAC,IAAI,KACnF,OAAO,EAAE,qBAAqB,EAC9B,UAAU,EAAE,UAAU,KACnB,CAAC,CAAC;AAEP,gDAAgD;AAChD,MAAM,WAAW,eAAgB,SAAQ,mBAAmB;IAC1D,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,6BAA6B,CAAC,cAAc,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;CAC7F;AAED,4DAA4D;AAC5D,MAAM,MAAM,mBAAmB,GAAG,IAAI,CACpC,mBAAmB,EACjB,MAAM,GACN,UAAU,GACV,eAAe,GACf,YAAY,GACZ,OAAO,GACP,UAAU,GACV,aAAa,GACb,cAAc,GACd,cAAc,CACjB,CAAC;AAEF,2BAA2B;AAC3B,eAAO,MAAM,YAAY,EAAG,UAAmB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@aiao/rxdb-adapter-sqliteai",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
+ "license": "MIT",
4
5
  "type": "module",
5
6
  "main": "./dist/index.js",
6
7
  "module": "./dist/index.js",
@@ -8,7 +9,6 @@
8
9
  "exports": {
9
10
  "./package.json": "./package.json",
10
11
  ".": {
11
- "@aiao/source": "./src/index.ts",
12
12
  "types": "./dist/index.d.ts",
13
13
  "import": "./dist/index.js",
14
14
  "default": "./dist/index.js"
@@ -16,6 +16,10 @@
16
16
  },
17
17
  "files": [
18
18
  "dist",
19
+ "src",
20
+ "!src/**/*.spec.*",
21
+ "!src/**/*.test.*",
22
+ "!src/**/__tests__/**",
19
23
  "!**/*.tsbuildinfo"
20
24
  ],
21
25
  "publishConfig": {
@@ -29,11 +33,12 @@
29
33
  },
30
34
  "dependencies": {
31
35
  "@sqliteai/sqlite-wasm": "3.50.4-wasm.1.0.0-sync.1.1.2-vector.1.0.0-memory.1.3.5",
32
- "@aiao/rxdb-adapter-sqlite-core": "0.0.23",
33
- "@aiao/rxdb": "0.0.23"
36
+ "@aiao/rxdb": "0.0.24",
37
+ "@aiao/rxdb-adapter-sqlite-core": "0.0.24"
34
38
  },
35
39
  "devDependencies": {
40
+ "comlink": "^4.4.2",
36
41
  "ms": "^2.1.3",
37
- "@aiao/rxdb-test": "0.0.23"
42
+ "@aiao/rxdb-test": "0.0.24"
38
43
  }
39
44
  }
@@ -0,0 +1,34 @@
1
+ import type { RxDB } from '@aiao/rxdb';
2
+ import { RxDBAdapterSqliteBase, type SqliteClientLike } from '@aiao/rxdb-adapter-sqlite-core';
3
+ import { createSqliteClient } from './create_sqlite_client.js';
4
+ import { ADAPTER_NAME, type SqliteaiOptions } from './sqliteai.interface.js';
5
+
6
+ /**
7
+ * 基于 `@sqliteai/sqlite-wasm` 的 RxDB 本地存储适配器。
8
+ *
9
+ * 与 `rxdb-adapter-sqlite`(官方 sqlite-wasm 绑定)API 对齐,差异:
10
+ * - WASM 二进制来自 SqliteAI 的发行版(OPFS / Worker 配置一致)
11
+ * - 客户端实现继承 `Oo1ClientBase`,复用 sqlite-core 的队列与事件批处理
12
+ */
13
+ export class RxDBAdapterSqliteai extends RxDBAdapterSqliteBase {
14
+ readonly #dbName: string;
15
+ override readonly name: string = ADAPTER_NAME;
16
+
17
+ constructor(
18
+ rxdb: RxDB,
19
+ readonly options: SqliteaiOptions
20
+ ) {
21
+ super(rxdb, options);
22
+ this.#dbName = rxdb.config.dbName;
23
+ }
24
+
25
+ protected override async createClient(): Promise<SqliteClientLike> {
26
+ return createSqliteClient(this.#dbName, this.options);
27
+ }
28
+ }
29
+
30
+ declare module '@aiao/rxdb' {
31
+ interface RxDBAdapters {
32
+ [ADAPTER_NAME]: RxDBAdapterSqliteai;
33
+ }
34
+ }
@@ -0,0 +1,21 @@
1
+ import { BATCH_TIMEOUT, Oo1ClientBase, type Oo1Static } from '@aiao/rxdb-adapter-sqlite-core';
2
+ import { sqliteaiLoad } from './sqliteai-load.utils.js';
3
+ import type { SqliteaiLoadOptions } from './sqliteai.interface.js';
4
+
5
+ export { BATCH_TIMEOUT };
6
+
7
+ /**
8
+ * SqliteAI 的 oo1 客户端实现。
9
+ *
10
+ * 仅声明 `clientName` 与 `loadModule`,其余生命周期(init / disconnect / 事件批处理 /
11
+ * 队列 / 自定义函数注册 / OPFS fallback)全部由 `Oo1ClientBase` 提供。
12
+ */
13
+ export class SqliteaiClient extends Oo1ClientBase<SqliteaiLoadOptions> {
14
+ protected get clientName(): string {
15
+ return 'sqliteai';
16
+ }
17
+
18
+ protected loadModule(options?: SqliteaiLoadOptions): Promise<Oo1Static> {
19
+ return sqliteaiLoad(options);
20
+ }
21
+ }
@@ -0,0 +1,38 @@
1
+ import { assertLoadOptionsTransferable, wrapWithComlink, type SqliteClientLike } from '@aiao/rxdb-adapter-sqlite-core';
2
+ import { SqliteaiOptions, type SqliteaiLoadOptions } from './sqliteai.interface.js';
3
+ import { SqliteaiClient } from './SqliteaiClient.js';
4
+
5
+ /**
6
+ * 创建 SqliteAI 客户端实例并完成初始化。
7
+ *
8
+ * 如果 `options` 中指定了 worker / sharedWorker,会通过 `wrapWithComlink` 把客户端代理到 worker;
9
+ * 否则在主线程直接 new。
10
+ *
11
+ * 返回类型是 {@link SqliteClientLike} 而非具体的 `SqliteaiClient`:worker 模式下拿到的是
12
+ * Comlink 远端代理,它把每个方法都 Promise 化,断言成实现类会让 `beginTransactionSql(): string`
13
+ * 这类同步签名在跨线程模式下变成谎报(SQLC-040)。
14
+ *
15
+ * @param dbName - 数据库名(用于持久化文件名归一化)
16
+ * @param options - SqliteAI 适配器选项(含 OPFS / worker / 缓存大小等)
17
+ * @returns 已 `init` 完成、可直接执行 SQL 的客户端(主线程实例或其远端代理)
18
+ * @throws worker / sharedWorker 模式下传入 `locateFile` / `print` / `printErr` 等函数型选项时抛错
19
+ */
20
+ export async function createSqliteClient(dbName: string, options: SqliteaiOptions): Promise<SqliteClientLike> {
21
+ const loadOptions: SqliteaiLoadOptions = {
22
+ opfs: options.opfs,
23
+ wasmPath: options.wasmPath,
24
+ opfsProxyPath: options.opfsProxyPath,
25
+ locateFile: options.locateFile,
26
+ print: options.print,
27
+ printErr: options.printErr,
28
+ cacheSizeKb: options.cacheSizeKb,
29
+ batchTimeout: options.batchTimeout,
30
+ opfsFallback: options.opfsFallback
31
+ };
32
+
33
+ assertLoadOptionsTransferable(loadOptions, options);
34
+
35
+ const client = wrapWithComlink(new SqliteaiClient(), options);
36
+ await client.init(dbName, loadOptions);
37
+ return client;
38
+ }
package/src/index.ts ADDED
@@ -0,0 +1,18 @@
1
+ // 从 core 重新导出共享类型和函数。
2
+ export {
3
+ BATCH_TIMEOUT,
4
+ ROWID,
5
+ RxDBAdapterSqliteError,
6
+ SqliteRepository,
7
+ buildRuleGroup,
8
+ get_table_name as sqliteGetTableName,
9
+ get_table_name_by_metadata as sqliteGetTableNameByMetadata
10
+ } from '@aiao/rxdb-adapter-sqlite-core';
11
+ export type { GenerateSqlResult } from '@aiao/rxdb-adapter-sqlite-core';
12
+
13
+ // sqliteai 专用导出。
14
+ export { createSqliteClient } from './create_sqlite_client.js';
15
+ export { RxDBAdapterSqliteai } from './RxDBAdapterSqliteai.js';
16
+ export { sqliteaiLoad } from './sqliteai-load.utils.js';
17
+ export type { SqliteaiLoadOptions, SqliteaiOptions, SqliteaiRepositoryConstructor } from './sqliteai.interface.js';
18
+ export { SqliteaiClient } from './SqliteaiClient.js';
@@ -0,0 +1,80 @@
1
+ import {
2
+ assertOo1Static,
3
+ buildOo1InitOptions,
4
+ defaultWarn,
5
+ isSameOo1LoadConfig,
6
+ toOo1LoadFingerprint,
7
+ withGlobalOo1LoadLock,
8
+ withPatchedOpfsProxyWorker,
9
+ withSqliteApiConfig,
10
+ type Oo1LoadFingerprint,
11
+ type Oo1Static
12
+ } from '@aiao/rxdb-adapter-sqlite-core';
13
+ import sqlite3InitModule from '@sqliteai/sqlite-wasm';
14
+ import type { SqliteaiLoadOptions } from './sqliteai.interface.js';
15
+
16
+ let cachedModule: Oo1Static | undefined;
17
+ /**
18
+ * 缓存模块对应的加载指纹。
19
+ *
20
+ * @remarks
21
+ * SQLAI-001:早先只记一个布尔「是否用过 OPFS proxy patch」,
22
+ * 于是不同 `wasmPath` / `opfsProxyPath` / `locateFile` 的后续调用会静默拿到首个模块。
23
+ */
24
+ let cachedFingerprint: Oo1LoadFingerprint | undefined;
25
+
26
+ /**
27
+ * 清除模块级 SqliteAI WASM 缓存。
28
+ *
29
+ * 仅供测试使用:每个测试用例需要重新加载干净模块时调用,
30
+ * 生产代码不应依赖这个钩子。
31
+ */
32
+ export function resetSqliteaiLoadCache(): void {
33
+ cachedModule = undefined;
34
+ cachedFingerprint = undefined;
35
+ }
36
+
37
+ /**
38
+ * 加载 SqliteAI WASM 模块(带模块级缓存)。
39
+ *
40
+ * 第一次调用会下载并初始化 WASM;之后调用复用缓存的 `Oo1Static`。
41
+ * 若新调用需要 OPFS proxy patch 而缓存模块未启用过,会重新加载一次以应用 patch。
42
+ *
43
+ * @param options - 加载选项(含 OPFS / wasmPath / printErr 等)
44
+ * @returns 共享的 `Oo1Static` 实例
45
+ */
46
+ export async function sqliteaiLoad(options?: SqliteaiLoadOptions): Promise<Oo1Static> {
47
+ const fingerprint = toOo1LoadFingerprint(options);
48
+ // 只有「决定加载什么」的配置完全一致才复用缓存;否则重新加载。
49
+ // 早先的缓存键只有一个布尔(是否用过 OPFS proxy patch),
50
+ // 不同 wasmPath / opfsProxyPath / locateFile 会静默拿到首个模块。
51
+ const cacheHit = (): Oo1Static | undefined =>
52
+ cachedModule && cachedFingerprint && isSameOo1LoadConfig(cachedFingerprint, fingerprint) ? cachedModule : undefined;
53
+
54
+ const hit = cacheHit();
55
+ if (hit) {
56
+ return hit;
57
+ }
58
+
59
+ return withGlobalOo1LoadLock(async () => {
60
+ const lockedHit = cacheHit();
61
+ if (lockedHit) {
62
+ return lockedHit;
63
+ }
64
+
65
+ const initOptions = buildOo1InitOptions(options);
66
+ const module = await withPatchedOpfsProxyWorker(options, async () => {
67
+ return await withSqliteApiConfig({ warn: defaultWarn }, async () => {
68
+ const loadedModule: unknown = await sqlite3InitModule(initOptions);
69
+ assertOo1Static(loadedModule);
70
+ return loadedModule;
71
+ });
72
+ });
73
+ if (module.config) {
74
+ module.config.warn = defaultWarn;
75
+ }
76
+ cachedModule = module;
77
+ cachedFingerprint = fingerprint;
78
+ return module;
79
+ });
80
+ }
@@ -0,0 +1,60 @@
1
+ import type { EntityType, IEntity, IRxDBAdapterOptions, RepositoryBase } from '@aiao/rxdb';
2
+ import type { OpfsFallback, RxDBAdapterSqliteBase } from '@aiao/rxdb-adapter-sqlite-core';
3
+
4
+ /**
5
+ * SqliteAI 适配器的可配置项(不含自定义 repository 注册)。
6
+ *
7
+ * - `opfs` / `opfsProxyPath` / `opfsFallback`:OPFS 持久化相关
8
+ * - `wasmPath` / `locateFile`:WASM 资源定位
9
+ * - `worker` / `sharedWorker` + `*Instance`:跨线程运行
10
+ * - `cacheSizeKb` / `batchTimeout`:性能调优
11
+ */
12
+ interface SqliteaiOptionsBase extends IRxDBAdapterOptions {
13
+ opfs?: boolean;
14
+ wasmPath?: string;
15
+ opfsProxyPath?: string;
16
+ locateFile?: (name: string) => string;
17
+ print?: (msg: string) => void;
18
+ printErr?: (msg: string) => void;
19
+ /** @see https://www.sqlite.org/pragma.html#pragma_cache_size @default 51200 */
20
+ cacheSizeKb?: number;
21
+ /** @default 16(BALANCED)@see BATCH_TIMEOUT */
22
+ batchTimeout?: number;
23
+ /** OPFS 数据库打开失败时的行为。@default 'throw' @see Oo1ClientBase */
24
+ opfsFallback?: OpfsFallback;
25
+ worker?: boolean;
26
+ workerInstance?: Worker;
27
+ sharedWorker?: boolean;
28
+ sharedWorkerInstance?: SharedWorker;
29
+ }
30
+
31
+ /** 任意实体构造器。 */
32
+ type AnyEntityType = EntityType & (new (...args: never[]) => IEntity);
33
+
34
+ /** 自定义 Repository 类的构造器签名(用户可覆盖默认 Repository / TreeRepository)。 */
35
+ export type SqliteaiRepositoryConstructor<T extends RepositoryBase<AnyEntityType>> = new (
36
+ adapter: RxDBAdapterSqliteBase,
37
+ EntityType: EntityType
38
+ ) => T;
39
+
40
+ /** SqliteAI 适配器完整选项(含可选的自定义 Repository 注册表)。 */
41
+ export interface SqliteaiOptions extends SqliteaiOptionsBase {
42
+ repositories?: Record<string, SqliteaiRepositoryConstructor<RepositoryBase<AnyEntityType>>>;
43
+ }
44
+
45
+ /** `sqliteaiLoad` 接受的运行期选项子集(剥离 worker / repository 配置)。 */
46
+ export type SqliteaiLoadOptions = Pick<
47
+ SqliteaiOptionsBase,
48
+ | 'opfs'
49
+ | 'wasmPath'
50
+ | 'opfsProxyPath'
51
+ | 'locateFile'
52
+ | 'print'
53
+ | 'printErr'
54
+ | 'cacheSizeKb'
55
+ | 'batchTimeout'
56
+ | 'opfsFallback'
57
+ >;
58
+
59
+ /** 在 RxDB 中注册此适配器使用的名称。 */
60
+ export const ADAPTER_NAME = 'sqliteai' as const;