@aiao/rxdb-plugin-sync 0.0.0-stage → 0.0.26
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/LICENSE +21 -0
- package/README.md +81 -2
- package/dist/SyncManager.d.ts +412 -0
- package/dist/SyncManager.d.ts.map +1 -0
- package/dist/branch-change-id.d.ts +31 -0
- package/dist/branch-change-id.d.ts.map +1 -0
- package/dist/branch-utils.d.ts +8 -0
- package/dist/branch-utils.d.ts.map +1 -0
- package/dist/bulk-sync.d.ts +117 -0
- package/dist/bulk-sync.d.ts.map +1 -0
- package/dist/cascade-blocking.d.ts +38 -0
- package/dist/cascade-blocking.d.ts.map +1 -0
- package/dist/check-repository-updates.d.ts +47 -0
- package/dist/check-repository-updates.d.ts.map +1 -0
- package/dist/cleanup-expired.d.ts +56 -0
- package/dist/cleanup-expired.d.ts.map +1 -0
- package/dist/dependency-graph.d.ts +128 -0
- package/dist/dependency-graph.d.ts.map +1 -0
- package/dist/get-all-repository-sync-status.d.ts +58 -0
- package/dist/get-all-repository-sync-status.d.ts.map +1 -0
- package/dist/get-repository-sync-status.d.ts +68 -0
- package/dist/get-repository-sync-status.d.ts.map +1 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3273 -0
- package/dist/plugin.d.ts +70 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/pull-ancestor-changes.d.ts +41 -0
- package/dist/pull-ancestor-changes.d.ts.map +1 -0
- package/dist/pull-batch.d.ts +16 -0
- package/dist/pull-batch.d.ts.map +1 -0
- package/dist/pull-conflict-utils.d.ts +43 -0
- package/dist/pull-conflict-utils.d.ts.map +1 -0
- package/dist/pull-repository.d.ts +36 -0
- package/dist/pull-repository.d.ts.map +1 -0
- package/dist/pull.d.ts +16 -0
- package/dist/pull.d.ts.map +1 -0
- package/dist/push-branch.d.ts +22 -0
- package/dist/push-branch.d.ts.map +1 -0
- package/dist/push-repository.d.ts +63 -0
- package/dist/push-repository.d.ts.map +1 -0
- package/dist/push.d.ts +14 -0
- package/dist/push.d.ts.map +1 -0
- package/dist/query-cache-outbox.d.ts +120 -0
- package/dist/query-cache-outbox.d.ts.map +1 -0
- package/dist/sync-branches.d.ts +35 -0
- package/dist/sync-branches.d.ts.map +1 -0
- package/dist/sync-listeners.d.ts +20 -0
- package/dist/sync-listeners.d.ts.map +1 -0
- package/dist/sync-manager.utils.d.ts +36 -0
- package/dist/sync-manager.utils.d.ts.map +1 -0
- package/dist/sync-repository.d.ts +63 -0
- package/dist/sync-repository.d.ts.map +1 -0
- package/dist/topological-sort.d.ts +126 -0
- package/dist/topological-sort.d.ts.map +1 -0
- package/dist/version.utils.d.ts +3 -0
- package/dist/version.utils.d.ts.map +1 -0
- package/package.json +58 -3
- package/src/SyncManager.ts +662 -0
- package/src/branch-change-id.ts +70 -0
- package/src/branch-utils.ts +37 -0
- package/src/bulk-sync.ts +338 -0
- package/src/cascade-blocking.ts +61 -0
- package/src/check-repository-updates.ts +143 -0
- package/src/cleanup-expired.ts +271 -0
- package/src/dependency-graph.ts +324 -0
- package/src/get-all-repository-sync-status.ts +115 -0
- package/src/get-repository-sync-status.ts +237 -0
- package/src/index.ts +53 -0
- package/src/plugin.ts +124 -0
- package/src/pull-ancestor-changes.ts +61 -0
- package/src/pull-batch.ts +410 -0
- package/src/pull-conflict-utils.ts +280 -0
- package/src/pull-repository.ts +701 -0
- package/src/pull.ts +204 -0
- package/src/push-branch.ts +61 -0
- package/src/push-repository.ts +1002 -0
- package/src/push.ts +104 -0
- package/src/query-cache-outbox.ts +866 -0
- package/src/sync-branches.ts +156 -0
- package/src/sync-listeners.ts +289 -0
- package/src/sync-manager.utils.ts +48 -0
- package/src/sync-repository.ts +302 -0
- package/src/topological-sort.ts +246 -0
- package/src/version.utils.ts +20 -0
package/src/push.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PushOptions,
|
|
3
|
+
PushResult,
|
|
4
|
+
RxDBError,
|
|
5
|
+
RxDBPartialSyncError,
|
|
6
|
+
SyncBeginEvent,
|
|
7
|
+
SyncCompleteEvent,
|
|
8
|
+
SyncErrorEvent
|
|
9
|
+
} from '@aiao/rxdb';
|
|
10
|
+
import { BulkSyncOptions } from './bulk-sync.js';
|
|
11
|
+
import { pushBranch } from './push-branch.js';
|
|
12
|
+
import type { SyncManager } from './SyncManager.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Push 功能实现
|
|
16
|
+
*
|
|
17
|
+
* 将本地未同步的变更推送到远程
|
|
18
|
+
* 先同步分支数据,再使用 bulkSync 批量推送所有 repositories
|
|
19
|
+
*
|
|
20
|
+
* @param sm - SyncManager 实例
|
|
21
|
+
* @param options - Push 选项
|
|
22
|
+
* @returns Push 结果
|
|
23
|
+
*/
|
|
24
|
+
export async function push(sm: SyncManager, options?: PushOptions): Promise<PushResult> {
|
|
25
|
+
const rxdb = sm.rxdb;
|
|
26
|
+
|
|
27
|
+
// 验证远程适配器配置
|
|
28
|
+
const remoteAdapterName = rxdb.config.sync?.remote?.adapter;
|
|
29
|
+
if (!remoteAdapterName) {
|
|
30
|
+
throw new RxDBError('Remote adapter not configured.');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 触发同步开始事件
|
|
34
|
+
rxdb.dispatchEvent(new SyncBeginEvent('push'));
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
// 先同步分支数据到远程(确保远程有该分支后再推送实体数据)
|
|
38
|
+
const branchPush = await pushBranch(sm);
|
|
39
|
+
|
|
40
|
+
// 使用 bulkSync 批量推送
|
|
41
|
+
const bulkOptions: BulkSyncOptions = {
|
|
42
|
+
operation: 'push',
|
|
43
|
+
repositories: options?.repositoryFilter?.map(item => {
|
|
44
|
+
if (typeof item === 'string') {
|
|
45
|
+
return { namespace: 'public', entity: item };
|
|
46
|
+
}
|
|
47
|
+
return item;
|
|
48
|
+
}),
|
|
49
|
+
...(options?.batchSize === undefined ? {} : { push: { batchSize: options.batchSize } })
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const bulkResult = await sm.bulkSync(bulkOptions);
|
|
53
|
+
|
|
54
|
+
// 汇总结果
|
|
55
|
+
const pushResult: PushResult = {
|
|
56
|
+
pushed: 0,
|
|
57
|
+
failed: 0,
|
|
58
|
+
compacted: 0,
|
|
59
|
+
originalCount: 0,
|
|
60
|
+
failures: []
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
for (const result of bulkResult.results) {
|
|
64
|
+
// 失败仓库自己已提交的进度也要累加 —— 它藏在 `RxDBPartialSyncError.result` 里,
|
|
65
|
+
// 被 bulkSync 原样挂在 `result.result` 上(见 syncSingleRepository)。
|
|
66
|
+
// 与 pull.ts 同口径。
|
|
67
|
+
const repoResult = result.result?.pushResult;
|
|
68
|
+
if (repoResult) {
|
|
69
|
+
pushResult.pushed += repoResult.pushed;
|
|
70
|
+
pushResult.failed += repoResult.failed;
|
|
71
|
+
pushResult.compacted += repoResult.compacted;
|
|
72
|
+
pushResult.originalCount += repoResult.originalCount;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (result.success) continue;
|
|
76
|
+
|
|
77
|
+
// 失败**不折算成 failed += 1**:failed 的单位是变更条数,混进仓库计数会破坏
|
|
78
|
+
// `originalCount = pushed + failed + compacted`。失败结构化保留在 failures 里。
|
|
79
|
+
const failure =
|
|
80
|
+
result.error ??
|
|
81
|
+
new RxDBError(`Repository push failed: ${result.repository.namespace}:${result.repository.entity}`);
|
|
82
|
+
pushResult.failures.push({
|
|
83
|
+
repository: result.repository,
|
|
84
|
+
// 嵌套的 partial error 解包成根因,避免调用方拿到「错误里套错误」(与 pull.ts 同口径)
|
|
85
|
+
error: failure instanceof RxDBPartialSyncError ? failure.cause : failure
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 分叉点在本轮推送前还没有 remoteId 时,上一步只能带 null 上行。
|
|
90
|
+
// 实体变更推完后分叉点已拿到 remoteId,补推一次把远端那一行补全。
|
|
91
|
+
if (branchPush.forkPointPending && pushResult.pushed > 0) {
|
|
92
|
+
await pushBranch(sm);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 触发同步完成事件
|
|
96
|
+
rxdb.dispatchEvent(new SyncCompleteEvent('push', pushResult));
|
|
97
|
+
|
|
98
|
+
return pushResult;
|
|
99
|
+
} catch (error) {
|
|
100
|
+
// 触发同步错误事件
|
|
101
|
+
rxdb.dispatchEvent(new SyncErrorEvent('push', error as Error));
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
}
|