@aiao/rxdb-adapter-electron 0.0.0-stage → 0.0.28

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.
Files changed (70) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +394 -2
  3. package/dist/RxDBAdapterElectron.d.ts +61 -0
  4. package/dist/RxDBAdapterElectron.d.ts.map +1 -0
  5. package/dist/electron-adapter.interface.d.ts +23 -0
  6. package/dist/electron-adapter.interface.d.ts.map +1 -0
  7. package/dist/electron-file-host.d.ts +52 -0
  8. package/dist/electron-file-host.d.ts.map +1 -0
  9. package/dist/electron-sqlite-host.d.ts +65 -0
  10. package/dist/electron-sqlite-host.d.ts.map +1 -0
  11. package/dist/host.d.ts +19 -0
  12. package/dist/host.d.ts.map +1 -0
  13. package/dist/host.js +768 -0
  14. package/dist/index.d.ts +22 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +36 -0
  17. package/dist/node-sqlite-engine.d.ts +97 -0
  18. package/dist/node-sqlite-engine.d.ts.map +1 -0
  19. package/dist/pglite/RxDBAdapterElectronPGlite.d.ts +96 -0
  20. package/dist/pglite/RxDBAdapterElectronPGlite.d.ts.map +1 -0
  21. package/dist/pglite/desktop-pglite-client.d.ts +144 -0
  22. package/dist/pglite/desktop-pglite-client.d.ts.map +1 -0
  23. package/dist/pglite/desktop-pglite-results.d.ts +14 -0
  24. package/dist/pglite/desktop-pglite-results.d.ts.map +1 -0
  25. package/dist/pglite/electron-pglite-backup-error.d.ts +32 -0
  26. package/dist/pglite/electron-pglite-backup-error.d.ts.map +1 -0
  27. package/dist/pglite/pglite-adapter.interface.d.ts +68 -0
  28. package/dist/pglite/pglite-adapter.interface.d.ts.map +1 -0
  29. package/dist/pglite/restore-electron-pglite-database.d.ts +68 -0
  30. package/dist/pglite/restore-electron-pglite-database.d.ts.map +1 -0
  31. package/dist/pglite-host/electron-pglite-host.d.ts +129 -0
  32. package/dist/pglite-host/electron-pglite-host.d.ts.map +1 -0
  33. package/dist/pglite-host/pglite-host-data-dir.d.ts +75 -0
  34. package/dist/pglite-host/pglite-host-data-dir.d.ts.map +1 -0
  35. package/dist/pglite-host/pglite-host-lock.d.ts +48 -0
  36. package/dist/pglite-host/pglite-host-lock.d.ts.map +1 -0
  37. package/dist/pglite-host/pglite-host-restore.d.ts +38 -0
  38. package/dist/pglite-host/pglite-host-restore.d.ts.map +1 -0
  39. package/dist/pglite-host/pglite-host-runtime.d.ts +90 -0
  40. package/dist/pglite-host/pglite-host-runtime.d.ts.map +1 -0
  41. package/dist/pglite-host.d.ts +17 -0
  42. package/dist/pglite-host.d.ts.map +1 -0
  43. package/dist/pglite-host.js +738 -0
  44. package/dist/pglite.d.ts +22 -0
  45. package/dist/pglite.d.ts.map +1 -0
  46. package/dist/pglite.js +493 -0
  47. package/dist/sqlite-script.d.ts +26 -0
  48. package/dist/sqlite-script.d.ts.map +1 -0
  49. package/package.json +94 -3
  50. package/src/RxDBAdapterElectron.ts +105 -0
  51. package/src/electron-adapter.interface.ts +25 -0
  52. package/src/electron-file-host.ts +758 -0
  53. package/src/electron-sqlite-host.ts +301 -0
  54. package/src/host.ts +64 -0
  55. package/src/index.ts +71 -0
  56. package/src/node-sqlite-engine.ts +631 -0
  57. package/src/pglite/RxDBAdapterElectronPGlite.ts +152 -0
  58. package/src/pglite/desktop-pglite-client.ts +614 -0
  59. package/src/pglite/desktop-pglite-results.ts +24 -0
  60. package/src/pglite/electron-pglite-backup-error.ts +128 -0
  61. package/src/pglite/pglite-adapter.interface.ts +78 -0
  62. package/src/pglite/restore-electron-pglite-database.ts +429 -0
  63. package/src/pglite-host/electron-pglite-host.ts +829 -0
  64. package/src/pglite-host/pglite-host-data-dir.ts +224 -0
  65. package/src/pglite-host/pglite-host-lock.ts +154 -0
  66. package/src/pglite-host/pglite-host-restore.ts +460 -0
  67. package/src/pglite-host/pglite-host-runtime.ts +166 -0
  68. package/src/pglite-host.ts +50 -0
  69. package/src/pglite.ts +51 -0
  70. package/src/sqlite-script.ts +134 -0
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Electron 本地 SQLite 适配器。
3
+ *
4
+ * @module RxDBAdapterElectron
5
+ */
6
+
7
+ import type { RxDB } from '@aiao/rxdb';
8
+ import { RxDBAdapterSqliteBase, type SqliteBackupStorage, type SqliteClientLike } from '@aiao/rxdb-adapter-sqlite-core';
9
+ import {
10
+ DEFAULT_DATABASE_SUFFIX,
11
+ DesktopSqliteClient,
12
+ assertValidDesktopDatabaseName,
13
+ connectDesktopRestoreTarget,
14
+ resolveDesktopHostTransport,
15
+ type DesktopOptions
16
+ } from '@aiao/rxdb-adapter-sqlite-core/desktop-host';
17
+ import { ADAPTER_NAME } from './electron-adapter.interface.js';
18
+
19
+ /**
20
+ * 把 RxDB 接到 Electron 应用私有目录里的真实 SQLite 文件上。
21
+ *
22
+ * @remarks
23
+ * 数据落在主进程的 `node:sqlite` 连接里,renderer 只通过一条窄传输层发请求,
24
+ * 因此它既拿不到文件系统句柄,也拿不到物理路径(AC#3)。
25
+ *
26
+ * 适配器本身没有自己的查询实现:它复用 {@link RxDBAdapterSqliteBase} 的全套 SQL、事务
27
+ * 与分支切换逻辑,只把「客户端从哪来」换成了桌面 host。Tauri 侧是同构的另一个类
28
+ * (`@aiao/rxdb-adapter-tauri` 的 `RxDBAdapterTauri`),两者共用同一份线协议与 renderer client。
29
+ */
30
+ export class RxDBAdapterElectron extends RxDBAdapterSqliteBase {
31
+ readonly #databaseName: string;
32
+ /** 备份与恢复争用的锁名,按库文件而不是 `dbName` 区分(见 {@link backupStorage})。 */
33
+ readonly #storageKey: string;
34
+ override readonly name: string = ADAPTER_NAME;
35
+
36
+ /** 解析出的逻辑数据库名,与 host 侧的物理文件一一对应。 */
37
+ get databaseName(): string {
38
+ return this.#databaseName;
39
+ }
40
+
41
+ /**
42
+ * @param rxdb - 绑定的 RxDB 实例
43
+ * @param options - 传输层与逻辑数据库名
44
+ * @throws {@link RxDBAdapterDesktopError} 逻辑数据库名越出应用作用域时抛 `invalid_database_name`
45
+ */
46
+ constructor(
47
+ rxdb: RxDB,
48
+ readonly options: DesktopOptions = {}
49
+ ) {
50
+ super(rxdb, options);
51
+ this.#databaseName = options.databaseName ?? `${rxdb.config.dbName}${DEFAULT_DATABASE_SUFFIX}`;
52
+ // 在构造期就校验:名字非法时根本没有能打开的库,等发出 IPC 才报错只会让原因离现场更远。
53
+ assertValidDesktopDatabaseName(this.#databaseName);
54
+ this.#storageKey = `${this.name}:file:${this.#databaseName}`;
55
+ }
56
+
57
+ protected override async createClient(): Promise<DesktopSqliteClient> {
58
+ return DesktopSqliteClient.connect(
59
+ this.options.transport ?? resolveDesktopHostTransport(),
60
+ { engine: 'sqlite', databaseName: this.#databaseName },
61
+ { batchTimeout: this.options.batchTimeout }
62
+ );
63
+ }
64
+
65
+ /**
66
+ * 桌面库是应用数据目录里的一个库文件,备份与恢复都按它支持。
67
+ *
68
+ * @remarks
69
+ * `storageKey` 用库文件名而不是 `dbName`:配了同一个 `databaseName` 的两个实例打开的是同一个文件,
70
+ * 必须争同一把锁;同名实例恢复到另一个 `databaseName`(新的数据位置)则与原库互不相干。
71
+ *
72
+ * @returns 持久化存储后端,`label` 为 `file`
73
+ */
74
+ protected override backupStorage(): SqliteBackupStorage {
75
+ return { kind: 'persistent', label: 'file', storageKey: this.#storageKey };
76
+ }
77
+
78
+ /**
79
+ * Electron host 以 defensive 模式运行 SQLite,改不了影子表。
80
+ *
81
+ * @remarks
82
+ * host 在特权主进程里执行 renderer 透传的 SQL;defensive 挡住直接改写影子表、`writable_schema` 这类
83
+ * 能把库文件改坏的操作,不能为了恢复而关掉。含 FTS5 等虚表的库因此不在 Electron 的备份矩阵里。
84
+ *
85
+ * @returns 恒为 `false`
86
+ */
87
+ protected override shadowTablesWritable(): boolean {
88
+ return false;
89
+ }
90
+
91
+ /**
92
+ * 别的窗口、别的进程可能开着同一个库文件,Web Lock 挡不住它们:恢复与清理前先让这条连接独占库文件。
93
+ *
94
+ * @returns 独占库文件的连接
95
+ */
96
+ protected override createRestoreTargetClient(): Promise<SqliteClientLike> {
97
+ return connectDesktopRestoreTarget(() => this.createClient(), this.#storageKey);
98
+ }
99
+ }
100
+
101
+ declare module '@aiao/rxdb' {
102
+ interface RxDBAdapters {
103
+ [ADAPTER_NAME]: RxDBAdapterElectron;
104
+ }
105
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Electron 适配器在本包内的身份。
3
+ *
4
+ * @remarks
5
+ * 选项形状与逻辑文件名后缀已下沉到 `@aiao/rxdb-adapter-sqlite-core/desktop-host`
6
+ * (两个桌面运行时共用同一份);留在这里的只有**注册名**——它是每个运行时包各自的身份,
7
+ * 共享层不替它们定名。
8
+ *
9
+ * 名字按 `<引擎>-<运行时>` 构成(与 `@aiao/rxdb-adapter-miniprogram` 的
10
+ * `wa-sqlite-miniprogram` 同构)。它不能只叫 `electron`:US-208 会在同一个运行时上
11
+ * 再立一个 PGlite 适配器(`pglite-electron`),到时两者都要能同时注册。
12
+ *
13
+ * @module electron-adapter.interface
14
+ */
15
+
16
+ import type { DesktopHostAdapterName } from '@aiao/rxdb-adapter-sqlite-core/desktop-host';
17
+
18
+ /**
19
+ * 适配器在 `RxDBAdapters` 注册表中的名字。
20
+ *
21
+ * @remarks
22
+ * `satisfies` 把它钉在共享层的登记表上:这个名字改了而 `DESKTOP_HOST_ADAPTER_NAMES` 没跟上,
23
+ * 是编译错误而不是某个下游在运行期才发现的 `adapter_mismatch`。
24
+ */
25
+ export const ADAPTER_NAME = 'sqlite-electron' as const satisfies DesktopHostAdapterName;