@univerjs-pro/collaboration-history-database-sqlite 1.3.1 → 1.4.0
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 +201 -0
- package/README.md +48 -16
- package/README.zh-CN.md +46 -19
- package/dist/index.cjs +52 -1
- package/dist/index.d.ts +39 -1
- package/dist/index.mjs +5 -1
- package/package.json +11 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
English | [简体中文](./README.zh-CN.md)
|
|
4
4
|
|
|
5
|
-
A persistent SQLite `IHistoryDatabaseAdapter`
|
|
5
|
+
A persistent SQLite `IHistoryDatabaseAdapter` using the stable synchronous `libsql` API. Requires Node.js 22 or later.
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Create the Adapter
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
pnpm add \
|
|
@@ -12,35 +12,67 @@ pnpm add \
|
|
|
12
12
|
@univerjs-pro/collaboration-history-database-sqlite
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
The caller must create the database parent directory first:
|
|
16
|
+
|
|
15
17
|
```ts
|
|
16
|
-
import {
|
|
18
|
+
import { mkdir } from 'node:fs/promises';
|
|
17
19
|
import { SQLiteHistoryDatabaseAdapter } from '@univerjs-pro/collaboration-history-database-sqlite';
|
|
20
|
+
import { UniverHistoryService } from '@univerjs-pro/collaboration-history-service';
|
|
18
21
|
|
|
19
|
-
|
|
22
|
+
await mkdir('./data', { recursive: true });
|
|
23
|
+
|
|
24
|
+
const database = new SQLiteHistoryDatabaseAdapter({
|
|
20
25
|
filename: './data/collaboration.sqlite',
|
|
21
26
|
busyTimeoutMs: 5_000,
|
|
22
27
|
});
|
|
23
|
-
const historyService = new UniverHistoryService({
|
|
24
|
-
collabService,
|
|
25
|
-
dbAdapter: historyDatabase,
|
|
26
|
-
});
|
|
28
|
+
const historyService = new UniverHistoryService({ collabService, dbAdapter: database });
|
|
27
29
|
|
|
28
30
|
await historyService.dispose();
|
|
29
|
-
await
|
|
31
|
+
await database.dispose();
|
|
30
32
|
```
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
Pass the Adapter to `UniverHistoryService` as `dbAdapter`. The same file can hold the core, Comment and Worktree SQLite adapters.
|
|
35
|
+
|
|
36
|
+
## SQLite behavior
|
|
33
37
|
|
|
34
|
-
|
|
38
|
+
- It does not change `journal_mode`; configure WAL, checkpointing, or backup policy before opening the Adapter when needed.
|
|
39
|
+
- Segment boundaries are stored in `collaboration_history_records`; History reads the changesets themselves from the core adapter.
|
|
40
|
+
- Schema version `('history', 2)` is stored in `collaboration_schema_versions`, which other Univer collaboration components share; `PRAGMA user_version` is not used.
|
|
41
|
+
- An empty database is initialized automatically. A history schema version other than 2 is rejected; see Migrations.
|
|
35
42
|
|
|
36
|
-
|
|
43
|
+
History rows are independent of core data. A core hard delete does not remove them; the application removes them when required.
|
|
37
44
|
|
|
38
|
-
|
|
45
|
+
## Migrations
|
|
46
|
+
|
|
47
|
+
Each release writes one history schema version and opens only that version. Opening an older
|
|
48
|
+
database fails with an error that points here; migrations never run when the adapter opens a database.
|
|
49
|
+
|
|
50
|
+
| History schema | Written by |
|
|
51
|
+
| --- | --- |
|
|
52
|
+
| v1 | 1.0.0-rc |
|
|
53
|
+
| v2 | 1.0.0 and later |
|
|
54
|
+
|
|
55
|
+
Stop writers, migrate the core schema of a shared file first with `migrateSQLiteCore` from
|
|
56
|
+
`@univerjs-pro/collaboration-database-sqlite`, then let `migrateSQLiteHistory` back up the file and
|
|
57
|
+
apply every missing step:
|
|
39
58
|
|
|
40
59
|
```ts
|
|
41
|
-
import {
|
|
60
|
+
import { migrateSQLiteHistory } from '@univerjs-pro/collaboration-history-database-sqlite';
|
|
42
61
|
|
|
43
|
-
await
|
|
62
|
+
const result = await migrateSQLiteHistory({ filename: './data/collaboration.sqlite' });
|
|
63
|
+
// { from: 1, to: 2, backupFilename: './data/collaboration.sqlite.history-v1.backup' }
|
|
44
64
|
```
|
|
45
65
|
|
|
46
|
-
|
|
66
|
+
The backup is a consistent copy written next to the database before anything changes; pass
|
|
67
|
+
`backupFilename` to choose its path. An existing backup file is never overwritten. A database that
|
|
68
|
+
already has the current version is left alone and gets no backup.
|
|
69
|
+
|
|
70
|
+
Each step runs in one transaction. If a step fails, the file stays at the last complete version and
|
|
71
|
+
the backup stays in place; calling `migrateSQLiteHistory` again continues from there.
|
|
72
|
+
|
|
73
|
+
### Data changed in v2
|
|
74
|
+
|
|
75
|
+
V1 stored every revision; v2 keeps only the boundary of each segment with its author, start time,
|
|
76
|
+
origin and additional fields. The migration requires that every segment starts at a stored revision
|
|
77
|
+
with valid facts, and that the core and Worktree schemas of the same file are already migrated,
|
|
78
|
+
because their migrations read creation facts from the v1 history rows.
|
package/README.zh-CN.md
CHANGED
|
@@ -2,11 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
[English](./README.md) | [简体中文](./README.zh-CN.md)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
`IHistoryDatabaseAdapter`。支持 Node.js 22 及以上,可与 Core、Comment 和 Worktree
|
|
7
|
-
共用一个数据库文件。
|
|
5
|
+
持久化 SQLite `IHistoryDatabaseAdapter`,使用稳定的 `libsql` 同步接口,支持 Node.js 22 及以上版本。
|
|
8
6
|
|
|
9
|
-
##
|
|
7
|
+
## 创建 Adapter
|
|
10
8
|
|
|
11
9
|
```bash
|
|
12
10
|
pnpm add \
|
|
@@ -14,36 +12,65 @@ pnpm add \
|
|
|
14
12
|
@univerjs-pro/collaboration-history-database-sqlite
|
|
15
13
|
```
|
|
16
14
|
|
|
15
|
+
调用方需要先创建数据库父目录:
|
|
16
|
+
|
|
17
17
|
```ts
|
|
18
|
-
import {
|
|
18
|
+
import { mkdir } from 'node:fs/promises';
|
|
19
19
|
import { SQLiteHistoryDatabaseAdapter } from '@univerjs-pro/collaboration-history-database-sqlite';
|
|
20
|
+
import { UniverHistoryService } from '@univerjs-pro/collaboration-history-service';
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
await mkdir('./data', { recursive: true });
|
|
23
|
+
|
|
24
|
+
const database = new SQLiteHistoryDatabaseAdapter({
|
|
22
25
|
filename: './data/collaboration.sqlite',
|
|
23
26
|
busyTimeoutMs: 5_000,
|
|
24
27
|
});
|
|
25
|
-
const historyService = new UniverHistoryService({
|
|
26
|
-
collabService,
|
|
27
|
-
dbAdapter: historyDatabase,
|
|
28
|
-
});
|
|
28
|
+
const historyService = new UniverHistoryService({ collabService, dbAdapter: database });
|
|
29
29
|
|
|
30
30
|
await historyService.dispose();
|
|
31
|
-
await
|
|
31
|
+
await database.dispose();
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
将 Adapter 作为 `dbAdapter` 传给 `UniverHistoryService`。同一个文件可以同时承载核心、Comment 和
|
|
35
|
+
Worktree 的 SQLite 适配器。
|
|
36
|
+
|
|
37
|
+
## SQLite 行为
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
- 不主动修改 `journal_mode`;需要 WAL、checkpoint 或备份策略时由应用在打开前配置。
|
|
40
|
+
- 分段起点保存在 `collaboration_history_records` 表;changeset 本身由 History 从核心适配器读取。
|
|
41
|
+
- schema 版本以 `('history', 2)` 写入 `collaboration_schema_versions`,该表与其他 Univer 协同组件共用,
|
|
42
|
+
不占用 `PRAGMA user_version`。
|
|
43
|
+
- 空库会自动初始化;版本不为 2 的 history schema 会被拒绝,见下文的迁移一节。
|
|
37
44
|
|
|
38
|
-
|
|
45
|
+
History 行独立于核心数据。核心的 hard delete 不会删除它们,需要时由应用自行清理。
|
|
39
46
|
|
|
40
|
-
|
|
47
|
+
## 迁移
|
|
48
|
+
|
|
49
|
+
每个版本只写入并打开一个 history schema 版本。打开旧库会报错并指向这里;打开数据库时从不自动迁移。
|
|
50
|
+
|
|
51
|
+
| History schema | 写入版本 |
|
|
52
|
+
| --- | --- |
|
|
53
|
+
| v1 | 1.0.0-rc |
|
|
54
|
+
| v2 | 1.0.0 及以后 |
|
|
55
|
+
|
|
56
|
+
先停止写入,共用文件时先用 `@univerjs-pro/collaboration-database-sqlite` 的 `migrateSQLiteCore`
|
|
57
|
+
迁移核心 schema,然后由 `migrateSQLiteHistory` 备份文件并补齐所有缺少的步骤:
|
|
41
58
|
|
|
42
59
|
```ts
|
|
43
|
-
import {
|
|
60
|
+
import { migrateSQLiteHistory } from '@univerjs-pro/collaboration-history-database-sqlite';
|
|
44
61
|
|
|
45
|
-
await
|
|
62
|
+
const result = await migrateSQLiteHistory({ filename: './data/collaboration.sqlite' });
|
|
63
|
+
// { from: 1, to: 2, backupFilename: './data/collaboration.sqlite.history-v1.backup' }
|
|
46
64
|
```
|
|
47
65
|
|
|
48
|
-
|
|
49
|
-
|
|
66
|
+
备份是在任何改动之前写到数据库旁边的一致副本,可用 `backupFilename` 指定路径;已有的备份文件永远不会被
|
|
67
|
+
覆盖。已经是当前版本的数据库原样保留,也不会生成备份。
|
|
68
|
+
|
|
69
|
+
每一步在一个事务内完成。某一步失败时,文件停在上一个完整版本,备份仍然保留;再次调用
|
|
70
|
+
`migrateSQLiteHistory` 会从那里继续。
|
|
71
|
+
|
|
72
|
+
### v2 改变的数据
|
|
73
|
+
|
|
74
|
+
v1 保存每一个 revision,v2 只保留每个分段的起点及其作者、起始时间、origin 和附加字段。迁移要求
|
|
75
|
+
每个分段的起点都是一条有效记录,并且同文件的核心和 Worktree schema 已经迁移完成,因为它们的迁移
|
|
76
|
+
会从 v1 的 history 行读取创建信息。
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,52 @@
|
|
|
1
|
-
Object['defineProperty'](exports,Symbol['toStringTag'],{'value':'Module'});var e=Object['create'],t=Object['defineProperty'],n=Object['getOwnPropertyDescriptor'],r=Object['getOwnPropertyNames'],i=Object['getPrototypeOf'],a=Object['prototype']['hasOwnProperty'],o=(_0x35b93d,_0x561392,_0x55a0e9,_0x4b681e)=>{if(_0x561392&&typeof _0x561392=='object'||typeof _0x561392=='function'){for(var _0x4062e9=r(_0x561392),_0x377ec2=0x0,_0x1f6ff5=_0x4062e9['length'],_0x595b66;_0x377ec2<_0x1f6ff5;_0x377ec2++)_0x595b66=_0x4062e9[_0x377ec2],!a['call'](_0x35b93d,_0x595b66)&&_0x595b66!==_0x55a0e9&&t(_0x35b93d,_0x595b66,{'get':(_0x2604a5=>_0x561392[_0x2604a5])['bind'](null,_0x595b66),'enumerable':!(_0x4b681e=n(_0x561392,_0x595b66))||_0x4b681e['enumerable']});}return _0x35b93d;},s=(_0x2ac626,_0x4dd5ac,_0x2f7058)=>(_0x2f7058=_0x2ac626==null?{}:e(i(_0x2ac626)),o(_0x4dd5ac||!_0x2ac626||!_0x2ac626['__esModule']?t(_0x2f7058,'default',{'value':_0x2ac626,'enumerable':!0x0}):_0x2f7058,_0x2ac626));let c=require('libsql');c=s(c,0x1);var l='\x0a\x20\x20CREATE\x20TABLE\x20collaboration_history_records\x20(\x0a\x20\x20\x20\x20unit_id\x20TEXT\x20NOT\x20NULL,\x0a\x20\x20\x20\x20start_revision\x20INTEGER\x20NOT\x20NULL\x20CHECK\x20(start_revision\x20>=\x201),\x0a\x20\x20\x20\x20user_id\x20TEXT\x20NOT\x20NULL,\x0a\x20\x20\x20\x20created_at_ms\x20INTEGER\x20NOT\x20NULL\x20CHECK\x20(created_at_ms\x20>=\x200),\x0a\x20\x20\x20\x20origin\x20INTEGER\x20NOT\x20NULL,\x0a\x20\x20\x20\x20additional_fields\x20TEXT,\x0a\x20\x20\x20\x20PRIMARY\x20KEY\x20(unit_id,\x20start_revision)\x0a\x20\x20);\x0a',u='\x0a\x20\x20CREATE\x20INDEX\x20collaboration_history_origin_lookup\x0a\x20\x20\x20\x20ON\x20collaboration_history_records(unit_id,\x20origin,\x20start_revision);\x0a\x20\x20CREATE\x20INDEX\x20collaboration_history_creator_lookup\x0a\x20\x20\x20\x20ON\x20collaboration_history_records(unit_id,\x20user_id,\x20start_revision);\x0a',d=class{['_database'];['_disposed']=!0x1;constructor(_0xeaf8f5){if(!_0xeaf8f5?.['filename'])throw TypeError('SQLiteHistoryDatabaseAdapter\x20requires\x20a\x20filename');let _0x4b5c11=_0xeaf8f5['busyTimeoutMs']??0x1388;if(!Number['isSafeInteger'](_0x4b5c11)||_0x4b5c11<0x0)throw TypeError('busyTimeoutMs\x20must\x20be\x20a\x20non-negative\x20safe\x20integer');this['_database']=new c['default'](_0xeaf8f5['filename'],{'timeout':_0x4b5c11});try{this['_database']['exec']('PRAGMA\x20busy_timeout\x20=\x20'+_0x4b5c11+';'),this['_initializeSchema']();}catch(_0x154dcb){throw this['_database']['close'](),_0x154dcb;}}async['getLatestRecord'](_0x4e053c,_0x12b9b1){this['_assertOpen']();let _0x4fd110=this['_latest'](_0x12b9b1);return _0x4fd110?f(_0x4fd110):null;}async['appendRecord'](_0x6797c0,_0x2ab840,_0x854e87){if(this['_assertOpen'](),!_0x2ab840['unitID']||!_0x2ab840['userID']||!Number['isSafeInteger'](_0x2ab840['startRevision'])||_0x2ab840['startRevision']<0x1||!Number['isSafeInteger'](_0x2ab840['createdAt'])||_0x2ab840['createdAt']<0x0)throw TypeError('History\x20record\x20is\x20invalid');return this['_transaction'](()=>{let _0x5ea647=this['_database']['prepare']('SELECT\x20*\x20FROM\x20collaboration_history_records\x0a\x20\x20\x20\x20\x20\x20\x20\x20WHERE\x20unit_id\x20=\x20?\x20AND\x20start_revision\x20=\x20?')['get'](_0x2ab840['unitID'],_0x2ab840['startRevision']);if(_0x5ea647)return{'status':_0x5ea647['user_id']===_0x2ab840['userID']&&_0x5ea647['created_at_ms']===_0x2ab840['createdAt']&&_0x5ea647['origin']===_0x2ab840['origin']&&_0x5ea647['additional_fields']===(_0x2ab840['additionalFields']??null)?'already-exists':'conflict'};let _0x1882de=this['_latest'](_0x2ab840['unitID'])?.['start_revision']??null;return _0x1882de!==_0x854e87['expectedLatestStartRevision']||_0x2ab840['startRevision']<=(_0x1882de??0x0)?{'status':'conflict'}:(this['_database']['prepare']('INSERT\x20INTO\x20collaboration_history_records\x0a\x20\x20\x20\x20\x20\x20\x20\x20(unit_id,\x20start_revision,\x20user_id,\x20created_at_ms,\x20origin,\x20additional_fields)\x0a\x20\x20\x20\x20\x20\x20\x20\x20VALUES\x20(?,\x20?,\x20?,\x20?,\x20?,\x20?)')['run'](_0x2ab840['unitID'],_0x2ab840['startRevision'],_0x2ab840['userID'],_0x2ab840['createdAt'],_0x2ab840['origin'],_0x2ab840['additionalFields']??null),{'status':'appended'});});}async['listRecords'](_0x10cf96,_0xd40245,_0x308aee){this['_assertOpen']();let _0x58b40f=[_0x308aee['throughRevision'],_0x308aee['throughRevision'],_0xd40245,_0x308aee['throughRevision']],_0x363ec2=[];_0x308aee['beforeRevision']!==void 0x0&&(_0x363ec2['push']('r.start_revision\x20<\x20?'),_0x58b40f['push'](_0x308aee['beforeRevision'])),_0x308aee['origin']&&(_0x363ec2['push']('r.origin\x20=\x20?'),_0x58b40f['push'](_0x308aee['origin'])),_0x308aee['userIDs']?.['length']&&(_0x363ec2['push']('r.user_id\x20IN\x20('+_0x308aee['userIDs']['map'](()=>'?')['join'](',\x20')+')'),_0x58b40f['push'](..._0x308aee['userIDs'])),_0x58b40f['push'](_0x308aee['length']+0x1);let _0x3df5cb=this['_database']['prepare']('\x0a\x20\x20\x20\x20\x20\x20SELECT\x20r.*,\x20MIN(?,\x20COALESCE((\x0a\x20\x20\x20\x20\x20\x20\x20\x20SELECT\x20next.start_revision\x20-\x201\x20FROM\x20collaboration_history_records\x20AS\x20next\x0a\x20\x20\x20\x20\x20\x20\x20\x20WHERE\x20next.unit_id\x20=\x20r.unit_id\x20AND\x20next.start_revision\x20>\x20r.start_revision\x0a\x20\x20\x20\x20\x20\x20\x20\x20ORDER\x20BY\x20next.start_revision\x20ASC\x20LIMIT\x201\x0a\x20\x20\x20\x20\x20\x20),\x20?))\x20AS\x20end_revision\x0a\x20\x20\x20\x20\x20\x20FROM\x20collaboration_history_records\x20AS\x20r\x0a\x20\x20\x20\x20\x20\x20WHERE\x20r.unit_id\x20=\x20?\x20AND\x20r.start_revision\x20<=\x20?\x0a\x20\x20\x20\x20\x20\x20\x20\x20'+(_0x363ec2['length']?'AND\x20'+_0x363ec2['join']('\x20AND\x20'):'')+'\x0a\x20\x20\x20\x20\x20\x20ORDER\x20BY\x20r.start_revision\x20DESC\x20LIMIT\x20?\x0a\x20\x20\x20\x20')['all'](..._0x58b40f);return{'records':_0x3df5cb['slice'](0x0,_0x308aee['length'])['map'](_0x22ab86=>({'record':f(_0x22ab86),'endRevision':_0x22ab86['end_revision']})),'hasMore':_0x3df5cb['length']>_0x308aee['length']};}async['listCreators'](_0x386171,_0x357db3,_0x164c91){this['_assertOpen']();let _0x5691e0=this['_database']['prepare']('SELECT\x20DISTINCT\x20user_id,\x20origin\x20FROM\x20collaboration_history_records\x0a\x20\x20\x20\x20\x20\x20WHERE\x20unit_id\x20=\x20?\x20AND\x20start_revision\x20<=\x20?\x20ORDER\x20BY\x20user_id,\x20origin')['all'](_0x357db3,_0x164c91['throughRevision']),_0x326203=new Map();for(let _0x3b4f76 of _0x5691e0){let _0xfaf3d6=_0x326203['get'](_0x3b4f76['user_id'])??[];_0xfaf3d6['push'](_0x3b4f76['origin']),_0x326203['set'](_0x3b4f76['user_id'],_0xfaf3d6);}return[..._0x326203]['map'](([_0x4ff9a9,_0x1651a3])=>({'userID':_0x4ff9a9,'origins':_0x1651a3}));}async['dispose'](){this['_disposed']||(this['_disposed']=!0x0,this['_database']['close']());}['_latest'](_0x377805){return this['_database']['prepare']('SELECT\x20*\x20FROM\x20collaboration_history_records\x0a\x20\x20\x20\x20\x20\x20WHERE\x20unit_id\x20=\x20?\x20ORDER\x20BY\x20start_revision\x20DESC\x20LIMIT\x201')['get'](_0x377805);}['_initializeSchema'](){this['_transaction'](()=>{let _0x26688b=_0x3f787b=>!!this['_database']['prepare']('SELECT\x201\x20FROM\x20sqlite_schema\x20WHERE\x20type\x20=\x20\x27table\x27\x20AND\x20name\x20=\x20?')['get'](_0x3f787b),_0x3ae3ac=_0x26688b('collaboration_history_revisions')||_0x26688b('collaboration_history_records');if(!_0x26688b('collaboration_schema_versions')){if(_0x3ae3ac)throw Error('SQLite\x20History\x20tables\x20exist\x20without\x20a\x20schema\x20version');this['_database']['exec']('CREATE\x20TABLE\x20collaboration_schema_versions\x20(\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20component\x20TEXT\x20PRIMARY\x20KEY,\x20version\x20INTEGER\x20NOT\x20NULL\x20CHECK\x20(version\x20>=\x201));');}let _0x112190=this['_database']['prepare']('SELECT\x20version\x20FROM\x20collaboration_schema_versions\x20WHERE\x20component\x20=\x20\x27history\x27')['get']();if(_0x112190){if(_0x112190['version']===0x1)throw Error('SQLite\x20History\x20schema\x20v1\x20requires\x20migrateSQLiteHistoryV1ToV2\x20before\x20opening\x20the\x20Adapter');if(_0x112190['version']!==0x2)throw Error('SQLite\x20History\x20schema\x20version\x20'+_0x112190['version']+'\x20is\x20not\x20supported');if(!_0x26688b('collaboration_history_records'))throw Error('SQLite\x20History\x20schema\x20v2\x20is\x20incomplete');this['_database']['prepare']('SELECT\x20unit_id,\x20start_revision,\x20user_id,\x20created_at_ms,\x20origin,\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20additional_fields\x20FROM\x20collaboration_history_records\x20LIMIT\x200')['all']();return;}if(_0x3ae3ac)throw Error('SQLite\x20History\x20tables\x20exist\x20without\x20a\x20schema\x20version');this['_database']['exec'](l+u+'INSERT\x20INTO\x20collaboration_schema_versions\x20(component,\x20version)\x20VALUES\x20(\x27history\x27,\x202);');});}['_transaction'](_0x35fc37){this['_database']['exec']('BEGIN\x20IMMEDIATE;');try{let _0x46f972=_0x35fc37();return this['_database']['exec']('COMMIT;'),_0x46f972;}catch(_0x2f6697){throw this['_database']['exec']('ROLLBACK;'),_0x2f6697;}}['_assertOpen'](){if(this['_disposed'])throw Error('SQLite\x20History\x20Database\x20Adapter\x20is\x20disposed');}};function f(_0x4189eb){return{'unitID':_0x4189eb['unit_id'],'startRevision':_0x4189eb['start_revision'],'userID':_0x4189eb['user_id'],'createdAt':_0x4189eb['created_at_ms'],'origin':_0x4189eb['origin'],..._0x4189eb['additional_fields']===null?{}:{'additionalFields':_0x4189eb['additional_fields']}};}async function p(_0x5a8e19){let _0x4ea97d=new c['default'](_0x5a8e19['filename'],{'fileMustExist':!0x0}),_0x2870d0=!0x1;try{if(_0x4ea97d['exec']('BEGIN\x20IMMEDIATE;'),_0x2870d0=!0x0,_0x4ea97d['prepare']('SELECT\x20version\x20FROM\x20collaboration_schema_versions\x20WHERE\x20component\x20=\x20\x27history\x27')['get']()?.['version']!==0x1)throw Error('History\x20v1-to-v2\x20migration\x20requires\x20schema\x20version\x201');if(_0x4ea97d['prepare']('SELECT\x20version\x20FROM\x20collaboration_schema_versions\x20WHERE\x20component\x20=\x20\x27core\x27')['get']()?.['version']===0x1)throw Error('Migrate\x20SQLite\x20Core\x20v1\x20before\x20History\x20so\x20creation\x20facts\x20remain\x20available');let _0x150e0a=_0x4ea97d['prepare']('SELECT\x20version\x20FROM\x20collaboration_schema_versions\x20WHERE\x20component\x20=\x20\x27worktree\x27')['get']();if(_0x150e0a&&_0x150e0a['version']<0x3)throw Error('Migrate\x20SQLite\x20Worktree\x20before\x20History\x20so\x20creation\x20facts\x20remain\x20available');if(_0x4ea97d['prepare']('SELECT\x201\x20FROM\x20collaboration_history_revisions\x20AS\x20r\x0a\x20\x20\x20\x20\x20\x20LEFT\x20JOIN\x20collaboration_history_revisions\x20AS\x20first\x0a\x20\x20\x20\x20\x20\x20\x20\x20ON\x20first.unit_id\x20=\x20r.unit_id\x20AND\x20first.revision\x20=\x20r.history_revision\x0a\x20\x20\x20\x20\x20\x20WHERE\x20r.history_revision\x20>\x20r.revision\x20OR\x20first.revision\x20IS\x20NULL\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20OR\x20first.history_revision\x20!=\x20first.revision\x20OR\x20first.user_id\x20=\x20\x27\x27\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20OR\x20typeof(first.committed_at)\x20!=\x20\x27integer\x27\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20OR\x20first.committed_at\x20<\x200\x20OR\x20first.committed_at\x20>\x209007199254740991\x0a\x20\x20\x20\x20\x20\x20LIMIT\x201')['get']())throw Error('History\x20v1\x20contains\x20a\x20missing\x20or\x20invalid\x20segment\x20starting\x20revision');_0x4ea97d['exec'](l),_0x4ea97d['exec']('INSERT\x20INTO\x20collaboration_history_records\x0a\x20\x20\x20\x20\x20\x20(unit_id,\x20start_revision,\x20user_id,\x20created_at_ms,\x20origin,\x20additional_fields)\x0a\x20\x20\x20\x20\x20\x20SELECT\x20unit_id,\x20revision,\x20user_id,\x20committed_at,\x20origin,\x20additional_fields\x0a\x20\x20\x20\x20\x20\x20FROM\x20collaboration_history_revisions\x20WHERE\x20revision\x20=\x20history_revision;\x0a\x20\x20\x20\x20\x20\x20DROP\x20TABLE\x20collaboration_history_revisions;'),_0x4ea97d['exec'](u),_0x4ea97d['exec']('UPDATE\x20collaboration_schema_versions\x20SET\x20version\x20=\x202\x20WHERE\x20component\x20=\x20\x27history\x27;\x20COMMIT;'),_0x2870d0=!0x1;}finally{try{_0x2870d0&&_0x4ea97d['exec']('ROLLBACK;');}finally{_0x4ea97d['close']();}}}exports['SQLiteHistoryDatabaseAdapter']=d,exports['migrateSQLiteHistoryV1ToV2']=p;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require("libsql");c=s(c,1);let l=require("node:fs");var u=`history`,d=`
|
|
2
|
+
CREATE TABLE collaboration_history_records (
|
|
3
|
+
unit_id TEXT NOT NULL,
|
|
4
|
+
start_revision INTEGER NOT NULL CHECK (start_revision >= 1),
|
|
5
|
+
user_id TEXT NOT NULL,
|
|
6
|
+
created_at_ms INTEGER NOT NULL CHECK (created_at_ms >= 0),
|
|
7
|
+
origin INTEGER NOT NULL,
|
|
8
|
+
additional_fields TEXT,
|
|
9
|
+
PRIMARY KEY (unit_id, start_revision)
|
|
10
|
+
);
|
|
11
|
+
`,f=`
|
|
12
|
+
CREATE INDEX collaboration_history_origin_lookup
|
|
13
|
+
ON collaboration_history_records(unit_id, origin, start_revision);
|
|
14
|
+
CREATE INDEX collaboration_history_creator_lookup
|
|
15
|
+
ON collaboration_history_records(unit_id, user_id, start_revision);
|
|
16
|
+
`,p=class{_database;_disposed=!1;constructor(e){if(!e.filename)throw TypeError(`SQLiteHistoryDatabaseAdapter requires a filename`);let t=e.busyTimeoutMs??5e3;this._database=new c.default(e.filename,{timeout:t});try{this._database.exec(`PRAGMA busy_timeout = ${t};`),this._initializeSchema()}catch(e){throw this._database.close(),e}}async getLatestRecord(e,t){this._assertOpen();let n=this._latest(t);return n?g(n):null}async appendRecord(e,t,n){return this._assertOpen(),m(t),this._transaction(()=>{let e=this._database.prepare(`SELECT * FROM collaboration_history_records WHERE unit_id = ? AND start_revision = ?`).get(t.unitID,t.startRevision);if(e)return{status:e.user_id===t.userID&&e.created_at_ms===t.createdAt&&e.origin===t.origin&&e.additional_fields===(t.additionalFields??null)?`already-exists`:`conflict`};let r=this._latest(t.unitID)?.start_revision??null;return r!==n.expectedLatestStartRevision||t.startRevision<=(r??0)?{status:`conflict`}:(this._database.prepare(`INSERT INTO collaboration_history_records
|
|
17
|
+
(unit_id, start_revision, user_id, created_at_ms, origin, additional_fields)
|
|
18
|
+
VALUES (?, ?, ?, ?, ?, ?)`).run(t.unitID,t.startRevision,t.userID,t.createdAt,t.origin,t.additionalFields??null),{status:`appended`})})}async listRecords(e,t,n){this._assertOpen();let r=[n.throughRevision,n.throughRevision,t,n.throughRevision],i=[];n.beforeRevision!==void 0&&(i.push(`r.start_revision < ?`),r.push(n.beforeRevision)),n.origin&&(i.push(`r.origin = ?`),r.push(n.origin)),n.userIDs?.length&&(i.push(`r.user_id IN (${n.userIDs.map(()=>`?`).join(`, `)})`),r.push(...n.userIDs)),r.push(n.length+1);let a=this._database.prepare(`
|
|
19
|
+
SELECT r.*, MIN(?, COALESCE((
|
|
20
|
+
SELECT next.start_revision - 1 FROM collaboration_history_records AS next
|
|
21
|
+
WHERE next.unit_id = r.unit_id AND next.start_revision > r.start_revision
|
|
22
|
+
ORDER BY next.start_revision ASC LIMIT 1
|
|
23
|
+
), ?)) AS end_revision
|
|
24
|
+
FROM collaboration_history_records AS r
|
|
25
|
+
WHERE r.unit_id = ? AND r.start_revision <= ?
|
|
26
|
+
${i.length?`AND ${i.join(` AND `)}`:``}
|
|
27
|
+
ORDER BY r.start_revision DESC LIMIT ?
|
|
28
|
+
`).all(...r);return{records:a.slice(0,n.length).map(e=>({record:g(e),endRevision:e.end_revision})),hasMore:a.length>n.length}}async listCreators(e,t,n){this._assertOpen();let r=this._database.prepare(`SELECT DISTINCT user_id, origin FROM collaboration_history_records
|
|
29
|
+
WHERE unit_id = ? AND start_revision <= ? ORDER BY user_id, origin`).all(t,n.throughRevision),i=new Map;for(let e of r){let t=i.get(e.user_id)??[];t.push(e.origin),i.set(e.user_id,t)}return[...i].map(([e,t])=>({userID:e,origins:t}))}async dispose(){this._disposed||(this._disposed=!0,this._database.close())}_latest(e){return this._database.prepare(`SELECT * FROM collaboration_history_records WHERE unit_id = ? ORDER BY start_revision DESC LIMIT 1`).get(e)}_initializeSchema(){this._transaction(()=>{this._database.exec(`
|
|
30
|
+
CREATE TABLE IF NOT EXISTS collaboration_schema_versions (
|
|
31
|
+
component TEXT PRIMARY KEY,
|
|
32
|
+
version INTEGER NOT NULL CHECK (version >= 1)
|
|
33
|
+
);
|
|
34
|
+
`);let e=this._readSchemaVersion();if(e!==2){if(e!==void 0)throw h(e);this._database.exec(`
|
|
35
|
+
${d}
|
|
36
|
+
${f}
|
|
37
|
+
INSERT INTO collaboration_schema_versions (component, version)
|
|
38
|
+
VALUES ('${u}', 2);
|
|
39
|
+
`)}})}_readSchemaVersion(){return this._database.prepare(`SELECT version FROM collaboration_schema_versions WHERE component = ?`).get(u)?.version}_transaction(e){this._database.exec(`BEGIN IMMEDIATE;`);try{let t=e();return this._database.exec(`COMMIT;`),t}catch(e){try{this._database.exec(`ROLLBACK;`)}catch{}throw e}}_assertOpen(){if(this._disposed)throw Error(`SQLite History Database Adapter is disposed`)}};function m(e){if(!e.unitID||!e.userID||!Number.isSafeInteger(e.startRevision)||e.startRevision<1||!Number.isSafeInteger(e.createdAt)||e.createdAt<0)throw TypeError(`History record is invalid`)}function h(e){return Error(`History schema version ${e} is not supported by this adapter (expected 2); ${e<2?`run migrateSQLiteHistory first`:`upgrade the adapter`}`)}function g(e){return{unitID:e.unit_id,startRevision:e.start_revision,userID:e.user_id,createdAt:e.created_at_ms,origin:e.origin,...e.additional_fields===null?{}:{additionalFields:e.additional_fields}}}function _(e,t){let n=new c.default(e,{fileMustExist:!0}),r=!1;try{n.exec(`BEGIN IMMEDIATE;`),r=!0,t(n),n.exec(`COMMIT;`),r=!1}finally{try{r&&n.exec(`ROLLBACK;`)}finally{n.close()}}}function v(e,t){return e.prepare(`SELECT version FROM collaboration_schema_versions WHERE component = ?`).get(t)?.version}async function y(e){_(e.filename,e=>{if(v(e,`history`)!==1)throw Error(`migrateSQLiteHistoryV1ToV2 requires history schema version 1`);if(v(e,`core`)===1)throw Error(`Migrate SQLite Core v1 before History so creation facts remain available`);let t=v(e,`worktree`);if(t!==void 0&&t<3)throw Error(`Migrate SQLite Worktree before History so creation facts remain available`);if(e.prepare(`SELECT 1 FROM collaboration_history_revisions AS r
|
|
40
|
+
LEFT JOIN collaboration_history_revisions AS first
|
|
41
|
+
ON first.unit_id = r.unit_id AND first.revision = r.history_revision
|
|
42
|
+
WHERE r.history_revision > r.revision OR first.revision IS NULL
|
|
43
|
+
OR first.history_revision != first.revision OR first.user_id = ''
|
|
44
|
+
OR typeof(first.committed_at) != 'integer'
|
|
45
|
+
OR first.committed_at < 0 OR first.committed_at > 9007199254740991
|
|
46
|
+
LIMIT 1`).get())throw Error(`History v1 contains a missing or invalid segment starting revision`);e.exec(d),e.exec(`
|
|
47
|
+
INSERT INTO collaboration_history_records
|
|
48
|
+
(unit_id, start_revision, user_id, created_at_ms, origin, additional_fields)
|
|
49
|
+
SELECT unit_id, revision, user_id, committed_at, origin, additional_fields
|
|
50
|
+
FROM collaboration_history_revisions WHERE revision = history_revision;
|
|
51
|
+
DROP TABLE collaboration_history_revisions;
|
|
52
|
+
`),e.exec(f),e.prepare(`UPDATE collaboration_schema_versions SET version = 2 WHERE component = ?`).run(u)})}var b=[y];async function x(e){let t=S(e.filename);if(t===2)return{from:t,to:t};if(t>2)throw Error(`History schema version ${t} is newer than this package supports (2)`);let n=e.backupFilename??`${e.filename}.history-v${t}.backup`;C(e.filename,n);for(let n of b.slice(t-1))await n(e);return{from:t,to:2,backupFilename:n}}function S(e){let t=new c.default(e,{fileMustExist:!0});try{let e=v(t,u);if(e===void 0||!Number.isInteger(e)||e<1)throw Error(`The database has no history schema version to migrate from`);return e}catch(e){throw e instanceof Error&&e.message.startsWith(`The database has no history`)?e:Error(`The database has no history schema version to migrate from`)}finally{t.close()}}function C(e,t){if((0,l.existsSync)(t))throw Error(`Backup target already exists: ${t}`);let n=new c.default(e,{fileMustExist:!0});try{n.prepare(`VACUUM INTO ?`).run(t)}finally{n.close()}}exports.SQLiteHistoryDatabaseAdapter=p,exports.migrateSQLiteHistory=x,exports.migrateSQLiteHistoryV1ToV2=y;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,17 +2,28 @@ import { AppendHistoryRecordResult, HistoryCreatorIndex, HistoryDatabaseContext,
|
|
|
2
2
|
|
|
3
3
|
//#region src/sqlite-history-database-adapter.d.ts
|
|
4
4
|
interface SQLiteHistoryDatabaseAdapterOptions {
|
|
5
|
+
/** SQLite filename or `:memory:`. The parent directory must already exist. */
|
|
5
6
|
readonly filename: string;
|
|
7
|
+
/** How long SQLite waits for another writer before reporting SQLITE_BUSY. Defaults to 5000. */
|
|
6
8
|
readonly busyTimeoutMs?: number;
|
|
7
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* SQLite implementation of the History persistence contract, built on the synchronous `libsql`
|
|
12
|
+
* API. It can share a database file with the other Univer collaboration SQLite adapters.
|
|
13
|
+
*
|
|
14
|
+
* The constructor initializes an empty database and rejects a database whose history schema
|
|
15
|
+
* version differs from the one this adapter writes; migrations are always explicit.
|
|
16
|
+
*/
|
|
8
17
|
declare class SQLiteHistoryDatabaseAdapter implements IHistoryDatabaseAdapter {
|
|
9
18
|
private readonly _database;
|
|
10
19
|
private _disposed;
|
|
11
20
|
constructor(options: SQLiteHistoryDatabaseAdapterOptions);
|
|
12
21
|
getLatestRecord(_context: HistoryDatabaseContext, unitID: string): Promise<HistoryRecord | null>;
|
|
22
|
+
/** Appends when the stored latest start revision equals the expected one; otherwise reports a conflict. */
|
|
13
23
|
appendRecord(_context: HistoryDatabaseContext, record: HistoryRecord, options: {
|
|
14
24
|
readonly expectedLatestStartRevision: number | null;
|
|
15
25
|
}): Promise<AppendHistoryRecordResult>;
|
|
26
|
+
/** Pages boundaries newest first; end revisions use the unfiltered successor, capped at throughRevision. */
|
|
16
27
|
listRecords(_context: HistoryDatabaseContext, unitID: string, options: ListHistoryRecordsOptions): Promise<ListHistoryRecordsResult>;
|
|
17
28
|
listCreators(_context: HistoryDatabaseContext, unitID: string, options: {
|
|
18
29
|
readonly throughRevision: number;
|
|
@@ -20,14 +31,41 @@ declare class SQLiteHistoryDatabaseAdapter implements IHistoryDatabaseAdapter {
|
|
|
20
31
|
dispose(): Promise<void>;
|
|
21
32
|
private _latest;
|
|
22
33
|
private _initializeSchema;
|
|
34
|
+
private _readSchemaVersion;
|
|
23
35
|
private _transaction;
|
|
24
36
|
private _assertOpen;
|
|
25
37
|
}
|
|
26
38
|
//#endregion
|
|
39
|
+
//#region src/migrations/migrate.d.ts
|
|
40
|
+
interface SQLiteHistoryMigrationOptions {
|
|
41
|
+
readonly filename: string;
|
|
42
|
+
/** Where the backup is written before any change. Defaults to `<filename>.history-v<from>.backup`. */
|
|
43
|
+
readonly backupFilename?: string;
|
|
44
|
+
}
|
|
45
|
+
interface SQLiteHistoryMigrationResult {
|
|
46
|
+
/** History schema version found in the file. */
|
|
47
|
+
readonly from: number;
|
|
48
|
+
/** History schema version after the call; always the version this package writes. */
|
|
49
|
+
readonly to: number;
|
|
50
|
+
/** Backup written before migrating; undefined when the file already had the current version. */
|
|
51
|
+
readonly backupFilename?: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Brings a database to the history schema version this package writes. The file is backed up first,
|
|
55
|
+
* then every missing step runs in its own transaction. Run the core migration of a shared file
|
|
56
|
+
* before this one. Stop writers before calling.
|
|
57
|
+
*/
|
|
58
|
+
declare function migrateSQLiteHistory(options: SQLiteHistoryMigrationOptions): Promise<SQLiteHistoryMigrationResult>;
|
|
59
|
+
//#endregion
|
|
27
60
|
//#region src/migrations/v1-to-v2.d.ts
|
|
28
61
|
interface SQLiteHistoryV1ToV2MigrationOptions {
|
|
29
62
|
readonly filename: string;
|
|
30
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Explicit, atomic migration from history schema v1 to v2. V1 stored every revision; v2 keeps only
|
|
66
|
+
* segment boundaries. Stop writers before calling, and migrate the core and Worktree schemas of the
|
|
67
|
+
* same file first: their migrations read creation facts from the v1 history rows.
|
|
68
|
+
*/
|
|
31
69
|
declare function migrateSQLiteHistoryV1ToV2(options: SQLiteHistoryV1ToV2MigrationOptions): Promise<void>;
|
|
32
70
|
//#endregion
|
|
33
|
-
export { SQLiteHistoryDatabaseAdapter, type SQLiteHistoryDatabaseAdapterOptions, type SQLiteHistoryV1ToV2MigrationOptions, migrateSQLiteHistoryV1ToV2 };
|
|
71
|
+
export { SQLiteHistoryDatabaseAdapter, type SQLiteHistoryDatabaseAdapterOptions, type SQLiteHistoryMigrationOptions, type SQLiteHistoryMigrationResult, type SQLiteHistoryV1ToV2MigrationOptions, migrateSQLiteHistory, migrateSQLiteHistoryV1ToV2 };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import cjsModule from "./index.cjs";
|
|
2
|
+
const facadeExport0 = cjsModule["SQLiteHistoryDatabaseAdapter"];
|
|
3
|
+
const facadeExport1 = cjsModule["migrateSQLiteHistory"];
|
|
4
|
+
const facadeExport2 = cjsModule["migrateSQLiteHistoryV1ToV2"];
|
|
5
|
+
export { facadeExport0 as SQLiteHistoryDatabaseAdapter, facadeExport1 as migrateSQLiteHistory, facadeExport2 as migrateSQLiteHistoryV1ToV2 };
|
package/package.json
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs-pro/collaboration-history-database-sqlite",
|
|
3
|
+
"description": "SQLite IHistoryDatabaseAdapter for Univer Collaboration built on libsql",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/dream-num/univer-collaboration-sdk-community.git",
|
|
7
|
+
"directory": "packages/collaboration-history-database-sqlite"
|
|
8
|
+
},
|
|
9
|
+
"license": "Apache-2.0",
|
|
3
10
|
"type": "module",
|
|
4
11
|
"engines": {
|
|
5
12
|
"node": ">=22"
|
|
6
13
|
},
|
|
7
|
-
"version": "1.
|
|
14
|
+
"version": "1.4.0",
|
|
8
15
|
"main": "./dist/index.cjs",
|
|
9
16
|
"module": "./dist/index.mjs",
|
|
10
17
|
"types": "./dist/index.d.ts",
|
|
@@ -19,8 +26,9 @@
|
|
|
19
26
|
"dist"
|
|
20
27
|
],
|
|
21
28
|
"dependencies": {
|
|
22
|
-
"@univerjs-pro/collaboration-history-service": "1.3.1",
|
|
23
|
-
"@univerjs/protocol": "1.3.1",
|
|
24
29
|
"libsql": "^0.5.29"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@univerjs-pro/collaboration-history-service": "1.4.0"
|
|
25
33
|
}
|
|
26
34
|
}
|