@loomwork/plugin-sqlite 0.0.0-stage → 0.1.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 +202 -0
- package/NOTICE +8 -0
- package/README.md +62 -2
- package/dist/SqliteArchiveStore.d.ts +22 -0
- package/dist/SqliteArchiveStore.d.ts.map +1 -0
- package/dist/SqliteArchiveStore.js +129 -0
- package/dist/SqliteArchiveStore.js.map +1 -0
- package/dist/SqliteBranchRegistry.d.ts +26 -0
- package/dist/SqliteBranchRegistry.d.ts.map +1 -0
- package/dist/SqliteBranchRegistry.js +154 -0
- package/dist/SqliteBranchRegistry.js.map +1 -0
- package/dist/SqliteContentStore.d.ts +55 -0
- package/dist/SqliteContentStore.d.ts.map +1 -0
- package/dist/SqliteContentStore.js +150 -0
- package/dist/SqliteContentStore.js.map +1 -0
- package/dist/SqliteDurableStateStore.d.ts +26 -0
- package/dist/SqliteDurableStateStore.d.ts.map +1 -0
- package/dist/SqliteDurableStateStore.js +81 -0
- package/dist/SqliteDurableStateStore.js.map +1 -0
- package/dist/SqliteEventStore.d.ts +55 -0
- package/dist/SqliteEventStore.d.ts.map +1 -0
- package/dist/SqliteEventStore.js +297 -0
- package/dist/SqliteEventStore.js.map +1 -0
- package/dist/SqliteLeaseStore.d.ts +25 -0
- package/dist/SqliteLeaseStore.d.ts.map +1 -0
- package/dist/SqliteLeaseStore.js +107 -0
- package/dist/SqliteLeaseStore.js.map +1 -0
- package/dist/SqliteMemoryStore.d.ts +52 -0
- package/dist/SqliteMemoryStore.d.ts.map +1 -0
- package/dist/SqliteMemoryStore.js +149 -0
- package/dist/SqliteMemoryStore.js.map +1 -0
- package/dist/SqliteReadModelStore.d.ts +38 -0
- package/dist/SqliteReadModelStore.d.ts.map +1 -0
- package/dist/SqliteReadModelStore.js +292 -0
- package/dist/SqliteReadModelStore.js.map +1 -0
- package/dist/SqliteSnapshotStore.d.ts +31 -0
- package/dist/SqliteSnapshotStore.d.ts.map +1 -0
- package/dist/SqliteSnapshotStore.js +101 -0
- package/dist/SqliteSnapshotStore.js.map +1 -0
- package/dist/SqliteTimerStore.d.ts +19 -0
- package/dist/SqliteTimerStore.d.ts.map +1 -0
- package/dist/SqliteTimerStore.js +106 -0
- package/dist/SqliteTimerStore.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/preset.d.ts +31 -0
- package/dist/preset.d.ts.map +1 -0
- package/dist/preset.js +51 -0
- package/dist/preset.js.map +1 -0
- package/dist/schema.d.ts +3 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +103 -0
- package/dist/schema.js.map +1 -0
- package/dist/snapshotSchema.d.ts +3 -0
- package/dist/snapshotSchema.d.ts.map +1 -0
- package/dist/snapshotSchema.js +19 -0
- package/dist/snapshotSchema.js.map +1 -0
- package/package.json +62 -3
- package/src/SqliteArchiveStore.ts +189 -0
- package/src/SqliteBranchRegistry.ts +199 -0
- package/src/SqliteContentStore.ts +195 -0
- package/src/SqliteDurableStateStore.ts +101 -0
- package/src/SqliteEventStore.ts +414 -0
- package/src/SqliteLeaseStore.ts +165 -0
- package/src/SqliteMemoryStore.ts +160 -0
- package/src/SqliteReadModelStore.ts +378 -0
- package/src/SqliteSnapshotStore.ts +157 -0
- package/src/SqliteTimerStore.ts +124 -0
- package/src/index.ts +13 -0
- package/src/preset.ts +66 -0
- package/src/schema.ts +100 -0
- package/src/snapshotSchema.ts +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
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.
|
|
202
|
+
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,63 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @loomwork/plugin-sqlite
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The zero-server durable backend for Loom. It implements the storage SPIs on embedded SQLite (via
|
|
4
|
+
`better-sqlite3`): `SqliteEventStore`, `SqliteSnapshotStore`, `SqliteBranchRegistry`,
|
|
5
|
+
`SqliteTimerStore`, `SqliteLeaseStore`, `SqliteArchiveStore` (the compaction and inbox archive, ADR 0138/0139),
|
|
6
|
+
`SqliteDurableStateStore`, `SqliteReadModelStore`, `SqliteContentStore` and `SqliteMemoryStore`, behind the same interfaces as the in-memory
|
|
7
|
+
reference. The native driver lives here so the kernel packages stay free of it.
|
|
8
|
+
|
|
9
|
+
## When to use it
|
|
10
|
+
|
|
11
|
+
- Local development, demos and the CLI: `loom logs`, `loom debug` and friends read the `.db` files it
|
|
12
|
+
writes.
|
|
13
|
+
- Single-node production where one machine owns the log.
|
|
14
|
+
- Not for multi-node deployments: SQLite leases have `host` scope (one machine). Use
|
|
15
|
+
[`@loomwork/plugin-postgres`](../postgres/README.md) for a shared log.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
Not published to npm yet: `"@loomwork/plugin-sqlite": "workspace:*"` in this workspace, or a `link:`
|
|
20
|
+
override from a sibling checkout (see [AGENTS.md](../../AGENTS.md)). `better-sqlite3` is a native
|
|
21
|
+
module, so `pnpm install` must be allowed to build it.
|
|
22
|
+
|
|
23
|
+
## Entry points
|
|
24
|
+
|
|
25
|
+
`@loomwork/plugin-sqlite` (single entry). The one most apps use is the preset
|
|
26
|
+
`sqliteStorage({ dir?, branching?, snapshotCodec? })`; the store classes are exported for hand-wiring and tests.
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { defineConfig } from "@loomwork/app";
|
|
32
|
+
import { sqliteStorage } from "@loomwork/plugin-sqlite";
|
|
33
|
+
|
|
34
|
+
const app = defineConfig({
|
|
35
|
+
storage: sqliteStorage({ dir: ".loom", branching: true }),
|
|
36
|
+
surfaces: ["default"],
|
|
37
|
+
flows: [],
|
|
38
|
+
});
|
|
39
|
+
// ... run flows; the log is in .loom/events.db (snapshots.db, branches.db alongside)
|
|
40
|
+
await app.close();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Key concepts
|
|
44
|
+
|
|
45
|
+
- `dir` defaults to `.loom`; the preset creates `events.db`, `snapshots.db`, `archive.db` (the archive store
|
|
46
|
+
compaction and the inbox trim move events into) and (with `branching: true`) `branches.db`.
|
|
47
|
+
- `snapshotCodec: "zstd"` opts in to zstd-compressed snapshot states (default `"none"`, JSON text; needs
|
|
48
|
+
Node >= 22.15). Old snapshots stay readable ([ADR 0141](../../docs/adr/0141-snapshot-zstd-codec.md)).
|
|
49
|
+
- The preset also exposes a `leaseStore` over the same `events.db`, so heartbeat leases and the log
|
|
50
|
+
share one failure domain. `defineConfig` picks it up automatically.
|
|
51
|
+
- Point the CLI at the file: `npx loom logs --db .loom/events.db <executionId>`.
|
|
52
|
+
- `SqliteMemoryStore` and `SqliteContentStore` back `memoryStore` and the content seam
|
|
53
|
+
([ADR 0046](../../docs/adr/0046-content-store-seam-thin-authoritative-log.md)).
|
|
54
|
+
- A storage backend is a preset, never a `kind`
|
|
55
|
+
([ADR 0043](../../docs/adr/0043-plugin-packaging-and-1p-3p-separation.md)); the SPIs are verified by
|
|
56
|
+
[`@loomwork/conformance`](../../packages/conformance/README.md).
|
|
57
|
+
|
|
58
|
+
## See also
|
|
59
|
+
|
|
60
|
+
- [API reference: `@loomwork/plugin-sqlite`](../../docs/api-reference.org) (single-node durability).
|
|
61
|
+
- [User guide, section 9: Configuration reference](../../docs/user-guide.md#9-configuration-reference),
|
|
62
|
+
[plugins](../../docs/plugins.md), [getting started, section 5](../../docs/getting-started.md).
|
|
63
|
+
- [`@loomwork/plugin-postgres`](../postgres/README.md), [`@loomwork/app`](../../packages/app/README.md).
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ArchiveExecutionPage, type ArchiveSegment, type ArchiveSegmentHeader, type ArchiveStore } from "@loomwork/event-runtime";
|
|
2
|
+
export declare class SqliteArchiveStore implements ArchiveStore {
|
|
3
|
+
private readonly db;
|
|
4
|
+
constructor(dbPath: string);
|
|
5
|
+
append(executionId: string, segment: ArchiveSegment): Promise<void>;
|
|
6
|
+
private insert;
|
|
7
|
+
/** ADR 0139 N1: one transaction - compare the headers, delete the key's segments, insert the new ones. */
|
|
8
|
+
replaceSegments(executionId: string, expected: readonly ArchiveSegmentHeader[], next: readonly ArchiveSegment[]): Promise<void>;
|
|
9
|
+
private headersSync;
|
|
10
|
+
headers(executionId: string): Promise<ArchiveSegmentHeader[]>;
|
|
11
|
+
listExecutions(opts?: {
|
|
12
|
+
cursor?: string;
|
|
13
|
+
limit?: number;
|
|
14
|
+
}): Promise<ArchiveExecutionPage>;
|
|
15
|
+
read(executionId: string, range?: {
|
|
16
|
+
readonly from?: number;
|
|
17
|
+
readonly through?: number;
|
|
18
|
+
}): Promise<ArchiveSegment[]>;
|
|
19
|
+
deleteExecution(executionId: string): Promise<void>;
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=SqliteArchiveStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SqliteArchiveStore.d.ts","sourceRoot":"","sources":["../src/SqliteArchiveStore.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,oBAAoB,EACzB,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACzB,KAAK,YAAY,EAGlB,MAAM,yBAAyB,CAAC;AAuCjC,qBAAa,kBAAmB,YAAW,YAAY;IACrD,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAoB;gBAE3B,MAAM,EAAE,MAAM;IAgCpB,MAAM,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAUzE,OAAO,CAAC,MAAM;IAqBd,0GAA0G;IACpG,eAAe,CACnB,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,SAAS,oBAAoB,EAAE,EACzC,IAAI,EAAE,SAAS,cAAc,EAAE,GAC9B,OAAO,CAAC,IAAI,CAAC;IAShB,OAAO,CAAC,WAAW;IAiBb,OAAO,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,EAAE,CAAC;IAI7D,cAAc,CAClB,IAAI,GAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAO,GAC7C,OAAO,CAAC,oBAAoB,CAAC;IAgB1B,IAAI,CACR,WAAW,EAAE,MAAM,EACnB,KAAK,GAAE;QAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAO,GAChE,OAAO,CAAC,cAAc,EAAE,CAAC;IAatB,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAInD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SqliteArchiveStore = void 0;
|
|
7
|
+
const event_runtime_1 = require("@loomwork/event-runtime");
|
|
8
|
+
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
|
9
|
+
const kindOf = (k) => k === "legacy-checkpoint" ? { kind: "legacy-checkpoint" } : {};
|
|
10
|
+
const senderOf = (d) => d === null ? {} : { senderDigest: d };
|
|
11
|
+
const toSegment = (r) => {
|
|
12
|
+
const body = JSON.parse(r.body);
|
|
13
|
+
return {
|
|
14
|
+
from: r.from_seq,
|
|
15
|
+
through: r.through_seq,
|
|
16
|
+
count: r.count,
|
|
17
|
+
digest: r.digest,
|
|
18
|
+
incarnation: r.incarnation,
|
|
19
|
+
...kindOf(r.kind),
|
|
20
|
+
...senderOf(r.sender_digest),
|
|
21
|
+
envelopes: body.envelopes,
|
|
22
|
+
...(body.sealed !== undefined && { sealed: body.sealed }),
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
class SqliteArchiveStore {
|
|
26
|
+
db;
|
|
27
|
+
constructor(dbPath) {
|
|
28
|
+
this.db = new better_sqlite3_1.default(dbPath);
|
|
29
|
+
try {
|
|
30
|
+
this.db.pragma("journal_mode = WAL");
|
|
31
|
+
this.db.pragma("synchronous = NORMAL");
|
|
32
|
+
this.db.exec(`
|
|
33
|
+
CREATE TABLE IF NOT EXISTS archive_segments (
|
|
34
|
+
execution_id TEXT NOT NULL,
|
|
35
|
+
from_seq INTEGER NOT NULL,
|
|
36
|
+
through_seq INTEGER NOT NULL,
|
|
37
|
+
count INTEGER NOT NULL,
|
|
38
|
+
digest TEXT NOT NULL,
|
|
39
|
+
incarnation TEXT NOT NULL,
|
|
40
|
+
kind TEXT,
|
|
41
|
+
sender_digest TEXT,
|
|
42
|
+
body TEXT NOT NULL,
|
|
43
|
+
PRIMARY KEY (execution_id, from_seq)
|
|
44
|
+
);
|
|
45
|
+
`);
|
|
46
|
+
// ADR 0139 N7: a table an earlier build created gains the column (CREATE IF NOT EXISTS never does).
|
|
47
|
+
const cols = this.db.prepare("PRAGMA table_info(archive_segments)").all();
|
|
48
|
+
if (!cols.some((c) => c.name === "sender_digest")) {
|
|
49
|
+
this.db.exec("ALTER TABLE archive_segments ADD COLUMN sender_digest TEXT");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
this.db.close();
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async append(executionId, segment) {
|
|
58
|
+
// One transaction: the classify read and the insert see the same rows.
|
|
59
|
+
const tx = this.db.transaction(() => {
|
|
60
|
+
const stored = this.headersSync(executionId);
|
|
61
|
+
if ((0, event_runtime_1.classifyAppend)(executionId, stored, segment) === "noop")
|
|
62
|
+
return;
|
|
63
|
+
this.insert(executionId, segment);
|
|
64
|
+
});
|
|
65
|
+
tx.immediate();
|
|
66
|
+
}
|
|
67
|
+
insert(executionId, segment) {
|
|
68
|
+
this.db
|
|
69
|
+
.prepare("INSERT INTO archive_segments (execution_id, from_seq, through_seq, count, digest, incarnation, kind, sender_digest, body) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
|
70
|
+
.run(executionId, segment.from, segment.through, segment.count, segment.digest, segment.incarnation, segment.kind ?? null, segment.senderDigest ?? null, JSON.stringify({
|
|
71
|
+
envelopes: segment.envelopes,
|
|
72
|
+
...(segment.sealed !== undefined && { sealed: segment.sealed }),
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
/** ADR 0139 N1: one transaction - compare the headers, delete the key's segments, insert the new ones. */
|
|
76
|
+
async replaceSegments(executionId, expected, next) {
|
|
77
|
+
const tx = this.db.transaction(() => {
|
|
78
|
+
(0, event_runtime_1.assertReplaceable)(executionId, this.headersSync(executionId), expected, next);
|
|
79
|
+
this.db.prepare("DELETE FROM archive_segments WHERE execution_id = ?").run(executionId);
|
|
80
|
+
for (const seg of next)
|
|
81
|
+
this.insert(executionId, seg);
|
|
82
|
+
});
|
|
83
|
+
tx.immediate();
|
|
84
|
+
}
|
|
85
|
+
headersSync(executionId) {
|
|
86
|
+
return this.db
|
|
87
|
+
.prepare("SELECT from_seq, through_seq, count, digest, incarnation, kind, sender_digest FROM archive_segments WHERE execution_id = ? ORDER BY from_seq")
|
|
88
|
+
.all(executionId)
|
|
89
|
+
.map((r) => ({
|
|
90
|
+
from: r.from_seq,
|
|
91
|
+
through: r.through_seq,
|
|
92
|
+
count: r.count,
|
|
93
|
+
digest: r.digest,
|
|
94
|
+
incarnation: r.incarnation,
|
|
95
|
+
...kindOf(r.kind),
|
|
96
|
+
...senderOf(r.sender_digest),
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
async headers(executionId) {
|
|
100
|
+
return this.headersSync(executionId);
|
|
101
|
+
}
|
|
102
|
+
async listExecutions(opts = {}) {
|
|
103
|
+
const limit = opts.limit ?? 500;
|
|
104
|
+
// BINARY collation = code point order for well-formed ids (the SPI's order).
|
|
105
|
+
const ids = this.db
|
|
106
|
+
.prepare("SELECT DISTINCT execution_id FROM archive_segments WHERE execution_id > ? ORDER BY execution_id LIMIT ?")
|
|
107
|
+
.all(opts.cursor ?? "", limit + 1)
|
|
108
|
+
.map((r) => r.execution_id);
|
|
109
|
+
// An empty-string id sorts first and `> ''` would skip it; it names no execution, so that is moot.
|
|
110
|
+
const page = ids.slice(0, limit);
|
|
111
|
+
return ids.length > limit
|
|
112
|
+
? { executionIds: page, nextCursor: page[page.length - 1] }
|
|
113
|
+
: { executionIds: page };
|
|
114
|
+
}
|
|
115
|
+
async read(executionId, range = {}) {
|
|
116
|
+
return this.db
|
|
117
|
+
.prepare("SELECT * FROM archive_segments WHERE execution_id = ? AND through_seq >= ? AND from_seq <= ? ORDER BY from_seq")
|
|
118
|
+
.all(executionId, range.from ?? Number.MIN_SAFE_INTEGER, range.through ?? Number.MAX_SAFE_INTEGER)
|
|
119
|
+
.map(toSegment);
|
|
120
|
+
}
|
|
121
|
+
async deleteExecution(executionId) {
|
|
122
|
+
this.db.prepare("DELETE FROM archive_segments WHERE execution_id = ?").run(executionId);
|
|
123
|
+
}
|
|
124
|
+
async close() {
|
|
125
|
+
this.db.close();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.SqliteArchiveStore = SqliteArchiveStore;
|
|
129
|
+
//# sourceMappingURL=SqliteArchiveStore.js.map
|
|
@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D,CAAC,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAC1F,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;IAClB,CAAC;CACF;AA9ID,gDA8IC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type BranchRef, type BranchRegistry, type LineageRecord, type ListRefsOptions, type PromoteOptions } from "@loomwork/event-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* SQLite-backed {@link BranchRegistry}. Lives in @loomwork/event-runtime because
|
|
4
|
+
* refs describe event-log topology; point it at the same DB file as the
|
|
5
|
+
* SqliteEventStore (migrate() creates the `refs`/`lineage` tables idempotently).
|
|
6
|
+
*/
|
|
7
|
+
export declare class SqliteBranchRegistry implements BranchRegistry {
|
|
8
|
+
private readonly db;
|
|
9
|
+
constructor(dbPath: string);
|
|
10
|
+
putRef(ref: BranchRef): Promise<void>;
|
|
11
|
+
/** Synchronous upsert — `putRef`'s body, callable from inside a transaction. */
|
|
12
|
+
private writeRef;
|
|
13
|
+
getRef(name: string): Promise<BranchRef | null>;
|
|
14
|
+
listRefs(options?: ListRefsOptions): Promise<BranchRef[]>;
|
|
15
|
+
moveTip(name: string, tip: number): Promise<void>;
|
|
16
|
+
promote(fromName: string, toName: string, options?: PromoteOptions): Promise<void>;
|
|
17
|
+
/** Synchronous ref read — the transaction body cannot await. */
|
|
18
|
+
private readRef;
|
|
19
|
+
deleteRef(name: string): Promise<void>;
|
|
20
|
+
recordLineage(rec: Omit<LineageRecord, "createdAt">): Promise<void>;
|
|
21
|
+
removeLineage(executionId: string): Promise<void>;
|
|
22
|
+
getLineage(executionId: string): Promise<LineageRecord | null>;
|
|
23
|
+
children(executionId: string): Promise<LineageRecord[]>;
|
|
24
|
+
close(): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=SqliteBranchRegistry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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vD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SqliteBranchRegistry = void 0;
|
|
7
|
+
const event_runtime_1 = require("@loomwork/event-runtime");
|
|
8
|
+
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
|
9
|
+
const schema_1 = require("./schema");
|
|
10
|
+
function rowToRef(row) {
|
|
11
|
+
return {
|
|
12
|
+
name: row.name,
|
|
13
|
+
executionId: row.execution_id,
|
|
14
|
+
tip: row.tip,
|
|
15
|
+
...(row.parent_execution_id !== null && { parentExecutionId: row.parent_execution_id }),
|
|
16
|
+
...(row.fork_sequence !== null && { forkSequence: row.fork_sequence }),
|
|
17
|
+
createdAt: row.created_at,
|
|
18
|
+
updatedAt: row.updated_at,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function rowToLineage(row) {
|
|
22
|
+
return {
|
|
23
|
+
executionId: row.execution_id,
|
|
24
|
+
parentExecutionId: row.parent_execution_id,
|
|
25
|
+
forkSequence: row.fork_sequence,
|
|
26
|
+
createdAt: row.created_at,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* SQLite-backed {@link BranchRegistry}. Lives in @loomwork/event-runtime because
|
|
31
|
+
* refs describe event-log topology; point it at the same DB file as the
|
|
32
|
+
* SqliteEventStore (migrate() creates the `refs`/`lineage` tables idempotently).
|
|
33
|
+
*/
|
|
34
|
+
class SqliteBranchRegistry {
|
|
35
|
+
db;
|
|
36
|
+
constructor(dbPath) {
|
|
37
|
+
this.db = new better_sqlite3_1.default(dbPath);
|
|
38
|
+
try {
|
|
39
|
+
this.db.pragma("journal_mode = WAL");
|
|
40
|
+
this.db.pragma("synchronous = NORMAL");
|
|
41
|
+
(0, schema_1.migrate)(this.db);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
this.db.close(); // don't leak the file handle if setup fails on a bad/locked file
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async putRef(ref) {
|
|
49
|
+
this.writeRef(ref);
|
|
50
|
+
}
|
|
51
|
+
/** Synchronous upsert — `putRef`'s body, callable from inside a transaction. */
|
|
52
|
+
writeRef(ref) {
|
|
53
|
+
this.db
|
|
54
|
+
.prepare(`
|
|
55
|
+
INSERT INTO refs (name, execution_id, tip, parent_execution_id, fork_sequence, created_at, updated_at)
|
|
56
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
57
|
+
ON CONFLICT(name) DO UPDATE SET
|
|
58
|
+
execution_id = excluded.execution_id,
|
|
59
|
+
tip = excluded.tip,
|
|
60
|
+
parent_execution_id = excluded.parent_execution_id,
|
|
61
|
+
fork_sequence = excluded.fork_sequence,
|
|
62
|
+
updated_at = excluded.updated_at
|
|
63
|
+
`)
|
|
64
|
+
.run(ref.name, ref.executionId, ref.tip, ref.parentExecutionId ?? null, ref.forkSequence ?? null, ref.createdAt, ref.updatedAt);
|
|
65
|
+
}
|
|
66
|
+
async getRef(name) {
|
|
67
|
+
const row = this.db.prepare("SELECT * FROM refs WHERE name = ?").get(name);
|
|
68
|
+
return row ? rowToRef(row) : null;
|
|
69
|
+
}
|
|
70
|
+
async listRefs(options) {
|
|
71
|
+
(0, event_runtime_1.assertListRefsLimit)(options?.limit);
|
|
72
|
+
const after = options?.after ?? null;
|
|
73
|
+
// `LIMIT -1` is SQLite for "no limit", so the guard above is load-bearing: an
|
|
74
|
+
// unvalidated negative would return the whole table to a caller asking for a page.
|
|
75
|
+
const rows = this.db
|
|
76
|
+
.prepare(`SELECT * FROM refs
|
|
77
|
+
WHERE (? IS NULL OR name > ?)
|
|
78
|
+
ORDER BY name ASC
|
|
79
|
+
LIMIT ?`)
|
|
80
|
+
.all(after, after, options?.limit ?? -1);
|
|
81
|
+
return rows.map(rowToRef);
|
|
82
|
+
}
|
|
83
|
+
async moveTip(name, tip) {
|
|
84
|
+
this.db
|
|
85
|
+
.prepare("UPDATE refs SET tip = ?, updated_at = ? WHERE name = ?")
|
|
86
|
+
.run(tip, Date.now(), name);
|
|
87
|
+
}
|
|
88
|
+
async promote(fromName, toName, options) {
|
|
89
|
+
// better-sqlite3 is synchronous, so wrapping the read-modify-write in a transaction
|
|
90
|
+
// makes the compare-and-set atomic against other connections to the same file.
|
|
91
|
+
const cas = options !== undefined && "expect" in options;
|
|
92
|
+
const expected = options?.expect ?? null;
|
|
93
|
+
const run = this.db.transaction(() => {
|
|
94
|
+
const from = this.readRef(fromName);
|
|
95
|
+
if (!from) {
|
|
96
|
+
throw new Error(`Cannot promote: ref "${fromName}" does not exist`);
|
|
97
|
+
}
|
|
98
|
+
const existing = this.readRef(toName);
|
|
99
|
+
if (cas) {
|
|
100
|
+
const actual = existing?.executionId ?? null;
|
|
101
|
+
if (actual !== expected) {
|
|
102
|
+
throw new event_runtime_1.BranchPromotionConflictError(toName, expected, actual);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const now = Date.now();
|
|
106
|
+
this.writeRef({
|
|
107
|
+
name: toName,
|
|
108
|
+
executionId: from.executionId,
|
|
109
|
+
tip: from.tip,
|
|
110
|
+
...(from.parentExecutionId !== undefined && { parentExecutionId: from.parentExecutionId }),
|
|
111
|
+
...(from.forkSequence !== undefined && { forkSequence: from.forkSequence }),
|
|
112
|
+
createdAt: existing?.createdAt ?? now,
|
|
113
|
+
updatedAt: now,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
run();
|
|
117
|
+
}
|
|
118
|
+
/** Synchronous ref read — the transaction body cannot await. */
|
|
119
|
+
readRef(name) {
|
|
120
|
+
const row = this.db.prepare("SELECT * FROM refs WHERE name = ?").get(name);
|
|
121
|
+
return row ? rowToRef(row) : null;
|
|
122
|
+
}
|
|
123
|
+
async deleteRef(name) {
|
|
124
|
+
this.db.prepare("DELETE FROM refs WHERE name = ?").run(name);
|
|
125
|
+
}
|
|
126
|
+
async recordLineage(rec) {
|
|
127
|
+
this.db
|
|
128
|
+
.prepare(`
|
|
129
|
+
INSERT OR REPLACE INTO lineage (execution_id, parent_execution_id, fork_sequence, created_at)
|
|
130
|
+
VALUES (?, ?, ?, ?)
|
|
131
|
+
`)
|
|
132
|
+
.run(rec.executionId, rec.parentExecutionId, rec.forkSequence, Date.now());
|
|
133
|
+
}
|
|
134
|
+
async removeLineage(executionId) {
|
|
135
|
+
this.db.prepare("DELETE FROM lineage WHERE execution_id = ?").run(executionId);
|
|
136
|
+
}
|
|
137
|
+
async getLineage(executionId) {
|
|
138
|
+
const row = this.db
|
|
139
|
+
.prepare("SELECT * FROM lineage WHERE execution_id = ?")
|
|
140
|
+
.get(executionId);
|
|
141
|
+
return row ? rowToLineage(row) : null;
|
|
142
|
+
}
|
|
143
|
+
async children(executionId) {
|
|
144
|
+
const rows = this.db
|
|
145
|
+
.prepare("SELECT * FROM lineage WHERE parent_execution_id = ? ORDER BY created_at ASC")
|
|
146
|
+
.all(executionId);
|
|
147
|
+
return rows.map(rowToLineage);
|
|
148
|
+
}
|
|
149
|
+
async close() {
|
|
150
|
+
this.db.close();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
exports.SqliteBranchRegistry = SqliteBranchRegistry;
|
|
154
|
+
//# sourceMappingURL=SqliteBranchRegistry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SqliteBranchRegistry.js","sourceRoot":"","sources":["../src/SqliteBranchRegistry.ts"],"names":[],"mappings":";;;;;;AAAA,2DAQiC;AACjC,oEAAsC;AACtC,qCAAmC;AAmBnC,SAAS,QAAQ,CAAC,GAAW;IAC3B,OAAO;QACL,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,WAAW,EAAE,GAAG,CAAC,YAAY;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