@samebase/alchemy-convex 0.34.0 → 0.35.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/README.md +12 -9
- package/lib/{State-CuktZaOh.js → State-ceE-WFI3.js} +52 -51
- package/lib/{State-CuktZaOh.js.map → State-ceE-WFI3.js.map} +1 -1
- package/lib/cli.js +1 -1
- package/lib/index.d.ts +10 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/package.json +1 -1
- package/src/RunRecord.ts +62 -5
- package/src/State.ts +13 -67
- package/src/store/component/_generated/api.ts +0 -2
- package/src/store/component/_generated/component.ts +5 -74
- package/src/store/component/reader.ts +46 -55
- package/src/store/component/schema.ts +4 -2
- package/src/store/component/state.ts +23 -72
- package/src/store/convex/state.ts +8 -25
- package/src/store/convex/version.ts +3 -1
- package/src/store/component/runRecord.ts +0 -57
|
@@ -5,12 +5,11 @@
|
|
|
5
5
|
//
|
|
6
6
|
// A stage holds one row for each resource of its stack, so the reads of one
|
|
7
7
|
// stage collect all rows.
|
|
8
|
-
import { v, type
|
|
9
|
-
import type {
|
|
8
|
+
import { v, type Validator } from "convex/values";
|
|
9
|
+
import type { QueryCtx } from "./_generated/server.js";
|
|
10
10
|
// oxlint-disable-next-line samebase/no-raw-public-convex-function-builders -- Store entrypoints have no app wrapper; the reader and the deploy key enforce access.
|
|
11
11
|
import { mutation, query } from "./_generated/server.js";
|
|
12
12
|
import { generationOf } from "./keys.ts";
|
|
13
|
-
import { runChanged, runRecord } from "./runRecord.ts";
|
|
14
13
|
|
|
15
14
|
const stage = { stack: v.string(), stage: v.string() };
|
|
16
15
|
const resource = { ...stage, fqn: v.string() };
|
|
@@ -35,41 +34,6 @@ const outputOf = (ctx: QueryCtx, args: { stack: string; stage: string }) =>
|
|
|
35
34
|
.withIndex("by_stage", (q) => q.eq("stack", args.stack).eq("stage", args.stage))
|
|
36
35
|
.unique();
|
|
37
36
|
|
|
38
|
-
/**
|
|
39
|
-
* The run record of a write, or null for a write without one. The app's
|
|
40
|
-
* functions take it as an optional field; across the component boundary it is
|
|
41
|
-
* a required field, because the generated component API types an optional
|
|
42
|
-
* field without `undefined`.
|
|
43
|
-
*/
|
|
44
|
-
const run = v.union(runRecord, v.null());
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* A resource write and its current run become visible in one transaction. The output row of the
|
|
48
|
-
* stage changes only when the run changes (see `runChanged`). Alchemy writes the resources of a
|
|
49
|
-
* deploy at the same time, and a write to the one output row of the stage made all of them conflict
|
|
50
|
-
* until Convex stopped the retries. A write of the same run only reads the row, and reads do not
|
|
51
|
-
* conflict. So `recordedAt` stays the time of the first write of the run, until `setOutput` writes
|
|
52
|
-
* it again.
|
|
53
|
-
*/
|
|
54
|
-
const writeCurrentRun = async (
|
|
55
|
-
ctx: MutationCtx,
|
|
56
|
-
args: { stack: string; stage: string; run: Infer<typeof run> },
|
|
57
|
-
) => {
|
|
58
|
-
const output = await outputOf(ctx, args);
|
|
59
|
-
if (output === null) {
|
|
60
|
-
if (args.run !== null)
|
|
61
|
-
await ctx.db.insert("outputs", {
|
|
62
|
-
stack: args.stack,
|
|
63
|
-
stage: args.stage,
|
|
64
|
-
currentRun: args.run,
|
|
65
|
-
});
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
// A write without a run record clears the record of the previous run.
|
|
69
|
-
if (runChanged(output.currentRun, args.run))
|
|
70
|
-
await ctx.db.patch("outputs", output._id, { currentRun: args.run ?? undefined });
|
|
71
|
-
};
|
|
72
|
-
|
|
73
37
|
/** The sorted union of two lists of names. */
|
|
74
38
|
const union = (a: readonly string[], b: readonly string[]) => [...new Set([...a, ...b])].sort();
|
|
75
39
|
|
|
@@ -178,8 +142,9 @@ export const replaced = query({
|
|
|
178
142
|
}),
|
|
179
143
|
});
|
|
180
144
|
|
|
145
|
+
/** A resource write reads and writes only the row of that resource. */
|
|
181
146
|
export const set = mutation({
|
|
182
|
-
args: { ...resource, value: v.string()
|
|
147
|
+
args: { ...resource, value: v.string() },
|
|
183
148
|
returns: v.null(),
|
|
184
149
|
handler: async (ctx, args) => {
|
|
185
150
|
const row = await rowOf(ctx, args);
|
|
@@ -191,18 +156,16 @@ export const set = mutation({
|
|
|
191
156
|
value: args.value,
|
|
192
157
|
});
|
|
193
158
|
else await ctx.db.patch("resources", row._id, { value: args.value });
|
|
194
|
-
await writeCurrentRun(ctx, args);
|
|
195
159
|
return null;
|
|
196
160
|
},
|
|
197
161
|
});
|
|
198
162
|
|
|
199
163
|
export const remove = mutation({
|
|
200
|
-
args:
|
|
164
|
+
args: resource,
|
|
201
165
|
returns: v.null(),
|
|
202
166
|
handler: async (ctx, args) => {
|
|
203
167
|
const row = await rowOf(ctx, args);
|
|
204
168
|
if (row !== null) await ctx.db.delete("resources", row._id);
|
|
205
|
-
await writeCurrentRun(ctx, args);
|
|
206
169
|
return null;
|
|
207
170
|
},
|
|
208
171
|
});
|
|
@@ -238,23 +201,13 @@ export const getOutput = query({
|
|
|
238
201
|
});
|
|
239
202
|
|
|
240
203
|
export const setOutput = mutation({
|
|
241
|
-
args: { ...stage, value: v.string()
|
|
204
|
+
args: { ...stage, value: v.string() },
|
|
242
205
|
returns: v.null(),
|
|
243
206
|
handler: async (ctx, args) => {
|
|
244
207
|
const row = await outputOf(ctx, args);
|
|
245
|
-
|
|
246
|
-
if (row !== null)
|
|
247
|
-
await ctx.db.patch("outputs", row._id, {
|
|
248
|
-
value: args.value,
|
|
249
|
-
currentRun: args.run ?? undefined,
|
|
250
|
-
});
|
|
208
|
+
if (row !== null) await ctx.db.patch("outputs", row._id, { value: args.value });
|
|
251
209
|
else
|
|
252
|
-
await ctx.db.insert("outputs", {
|
|
253
|
-
stack: args.stack,
|
|
254
|
-
stage: args.stage,
|
|
255
|
-
value: args.value,
|
|
256
|
-
...(args.run === null ? {} : { currentRun: args.run }),
|
|
257
|
-
});
|
|
210
|
+
await ctx.db.insert("outputs", { stack: args.stack, stage: args.stage, value: args.value });
|
|
258
211
|
return null;
|
|
259
212
|
},
|
|
260
213
|
});
|
|
@@ -286,7 +239,7 @@ export const resourcePage = query({
|
|
|
286
239
|
},
|
|
287
240
|
});
|
|
288
241
|
|
|
289
|
-
/** One bounded page of the outputs. A row
|
|
242
|
+
/** One bounded page of the outputs. A row that store version 6 left without a value is left out. */
|
|
290
243
|
export const outputPage = query({
|
|
291
244
|
args: { cursor: v.union(v.string(), v.null()) },
|
|
292
245
|
returns: pageOf(v.object({ ...stage, value: v.string() })),
|
|
@@ -310,10 +263,7 @@ export const outputPage = query({
|
|
|
310
263
|
*/
|
|
311
264
|
const replaceResult = v.union(v.literal("replaced"), v.literal("changed"), v.literal("stale"));
|
|
312
265
|
|
|
313
|
-
/**
|
|
314
|
-
* Replaces the value of a row that still holds `expected`, for a reseal of `generation`: the row
|
|
315
|
-
* keeps its run record.
|
|
316
|
-
*/
|
|
266
|
+
/** Replaces the value of a row that still holds `expected`, for a reseal of `generation`. */
|
|
317
267
|
export const replaceResource = mutation({
|
|
318
268
|
args: { ...resource, expected: v.string(), value: v.string(), generation: v.number() },
|
|
319
269
|
returns: replaceResult,
|
|
@@ -339,18 +289,20 @@ export const replaceOutput = mutation({
|
|
|
339
289
|
},
|
|
340
290
|
});
|
|
341
291
|
|
|
342
|
-
/**
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
),
|
|
292
|
+
/**
|
|
293
|
+
* One stage as the store holds it: its resources in fqn order and its output, as JSON texts, with
|
|
294
|
+
* `output: null` for a stage without one. {@link readStack} and the repository reader give stages in
|
|
295
|
+
* this shape, and the shape is Alchemy state only: a reader takes anything else from the output.
|
|
296
|
+
*/
|
|
297
|
+
export const storedStage = v.object({
|
|
298
|
+
name: v.string(),
|
|
299
|
+
resources: v.array(v.object({ fqn: v.string(), value: v.string() })),
|
|
300
|
+
output: v.union(v.string(), v.null()),
|
|
352
301
|
});
|
|
353
302
|
|
|
303
|
+
/** The reply of {@link readStack}. */
|
|
304
|
+
export const stackReply = v.object({ stages: v.array(storedStage) });
|
|
305
|
+
|
|
354
306
|
/**
|
|
355
307
|
* One call for a reader: every stage of a stack in name order, each with its
|
|
356
308
|
* resources in fqn order and its output, as the JSON texts that the rows
|
|
@@ -381,12 +333,11 @@ export const readStack = query({
|
|
|
381
333
|
stages: stages.map((stage) => {
|
|
382
334
|
const output = outputs.find((row) => row.stage === stage);
|
|
383
335
|
return {
|
|
384
|
-
stage,
|
|
336
|
+
name: stage,
|
|
385
337
|
resources: resources
|
|
386
338
|
.filter((row) => row.stage === stage)
|
|
387
339
|
.map((row) => ({ fqn: row.fqn, value: row.value })),
|
|
388
340
|
output: output?.value ?? null,
|
|
389
|
-
record: output?.currentRun ?? null,
|
|
390
341
|
};
|
|
391
342
|
}),
|
|
392
343
|
};
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
//
|
|
5
5
|
// The rows are in the alchemyState component (src/store/component). Each
|
|
6
6
|
// function here forwards to the component function of the same name, with the
|
|
7
|
-
// same results; an absent `
|
|
7
|
+
// same results; an absent `stage` goes to the component as null. The functions
|
|
8
|
+
// hold Alchemy's state interface and nothing else, the same as the Cloudflare
|
|
9
|
+
// state store of Alchemy.
|
|
8
10
|
// These names and arguments are the wire contract of STORE_VERSION. Samebase
|
|
9
11
|
// reads `stacks`, `getOutput`, and `readStack` by name: a renamed function is
|
|
10
12
|
// a version bump of the store and of Samebase.
|
|
@@ -14,7 +16,6 @@
|
|
|
14
16
|
// open them for the run. `readStack` gives the rows as they are stored.
|
|
15
17
|
import type { FunctionReturnType } from "convex/server";
|
|
16
18
|
import { v } from "convex/values";
|
|
17
|
-
import { runRecord } from "../component/runRecord.ts";
|
|
18
19
|
import { stackReply } from "../component/state.ts";
|
|
19
20
|
import { components, internal } from "./_generated/api.js";
|
|
20
21
|
import {
|
|
@@ -119,13 +120,7 @@ export const replaced = internalQuery({
|
|
|
119
120
|
},
|
|
120
121
|
});
|
|
121
122
|
|
|
122
|
-
const setArgs = {
|
|
123
|
-
stack: v.string(),
|
|
124
|
-
stage: v.string(),
|
|
125
|
-
fqn: v.string(),
|
|
126
|
-
value: v.string(),
|
|
127
|
-
run: v.optional(runRecord),
|
|
128
|
-
};
|
|
123
|
+
const setArgs = { stack: v.string(), stage: v.string(), fqn: v.string(), value: v.string() };
|
|
129
124
|
|
|
130
125
|
export const set = internalAction({
|
|
131
126
|
args: setArgs,
|
|
@@ -147,22 +142,16 @@ export const writeResource = internalMutation({
|
|
|
147
142
|
stage: args.stage,
|
|
148
143
|
fqn: args.fqn,
|
|
149
144
|
value: args.value,
|
|
150
|
-
run: args.run ?? null,
|
|
151
145
|
});
|
|
152
146
|
return true;
|
|
153
147
|
},
|
|
154
148
|
});
|
|
155
149
|
|
|
156
150
|
export const remove = internalMutation({
|
|
157
|
-
args: { stack: v.string(), stage: v.string(), fqn: v.string()
|
|
151
|
+
args: { stack: v.string(), stage: v.string(), fqn: v.string() },
|
|
158
152
|
returns: v.null(),
|
|
159
153
|
handler: (ctx, args) =>
|
|
160
|
-
ctx.runMutation(state.remove, {
|
|
161
|
-
stack: args.stack,
|
|
162
|
-
stage: args.stage,
|
|
163
|
-
fqn: args.fqn,
|
|
164
|
-
run: args.run ?? null,
|
|
165
|
-
}),
|
|
154
|
+
ctx.runMutation(state.remove, { stack: args.stack, stage: args.stage, fqn: args.fqn }),
|
|
166
155
|
});
|
|
167
156
|
|
|
168
157
|
/** Deletes the resources and outputs of one stage, or of every stage of the stack. */
|
|
@@ -183,12 +172,7 @@ export const getOutput = internalQuery({
|
|
|
183
172
|
},
|
|
184
173
|
});
|
|
185
174
|
|
|
186
|
-
const setOutputArgs = {
|
|
187
|
-
stack: v.string(),
|
|
188
|
-
stage: v.string(),
|
|
189
|
-
value: v.string(),
|
|
190
|
-
run: v.optional(runRecord),
|
|
191
|
-
};
|
|
175
|
+
const setOutputArgs = { stack: v.string(), stage: v.string(), value: v.string() };
|
|
192
176
|
|
|
193
177
|
export const setOutput = internalAction({
|
|
194
178
|
args: setOutputArgs,
|
|
@@ -209,13 +193,12 @@ export const writeOutput = internalMutation({
|
|
|
209
193
|
stack: args.stack,
|
|
210
194
|
stage: args.stage,
|
|
211
195
|
value: args.value,
|
|
212
|
-
run: args.run ?? null,
|
|
213
196
|
});
|
|
214
197
|
return true;
|
|
215
198
|
},
|
|
216
199
|
});
|
|
217
200
|
|
|
218
|
-
/** Every stage of a stack with its resources
|
|
201
|
+
/** Every stage of a stack with its resources and output, in one call. Since store version 2. */
|
|
219
202
|
export const readStack = internalQuery({
|
|
220
203
|
args: { stack: v.string() },
|
|
221
204
|
returns: stackReply,
|
|
@@ -13,4 +13,6 @@
|
|
|
13
13
|
// 5: rows hold their secrets sealed: `set` and `setOutput` are actions, with `sealing`, `chooseKey`,
|
|
14
14
|
// `rotateKey`, and `reseal` (0.19.0).
|
|
15
15
|
// 6: a run record's `source` is `github-actions` or `checkout`, no longer `laptop` (0.32.0).
|
|
16
|
-
|
|
16
|
+
// 7: Alchemy's state interface only: `set`, `remove`, and `setOutput` take no `run`, `readStack`
|
|
17
|
+
// and `reader:repository` give stages as `{ name, resources, output }` (0.35.0).
|
|
18
|
+
export const STORE_VERSION = 7;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { v, type GenericValidator, type Infer } from "convex/values";
|
|
2
|
-
|
|
3
|
-
export const runRecord = v.object({
|
|
4
|
-
version: v.literal(1),
|
|
5
|
-
repository: v.string(),
|
|
6
|
-
stackFile: v.string(),
|
|
7
|
-
branch: v.string(),
|
|
8
|
-
pullRequest: v.union(v.number(), v.null()),
|
|
9
|
-
headCommit: v.string(),
|
|
10
|
-
runUrl: v.union(v.string(), v.null()),
|
|
11
|
-
source: v.union(v.literal("github-actions"), v.literal("checkout")),
|
|
12
|
-
recordedAt: v.string(),
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
type RunRecord = Infer<typeof runRecord>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* True when a write with `next` must replace `current`, the record of the stage. Records that differ
|
|
19
|
-
* only in `recordedAt`, the time of one write, name the same run. Two checkout deploys of one commit
|
|
20
|
-
* have no `runUrl`, so they are the same run too. A write without a record (`null`) clears one.
|
|
21
|
-
*/
|
|
22
|
-
export const runChanged = (current: RunRecord | undefined, next: RunRecord | null) => {
|
|
23
|
-
if (next === null || current === undefined) return next !== null || current !== undefined;
|
|
24
|
-
const other = new Map(Object.entries(next));
|
|
25
|
-
return Object.entries(current).some(
|
|
26
|
-
([field, value]) => field !== "recordedAt" && other.get(field) !== value,
|
|
27
|
-
);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/** Whether `value` passes `validator`. Only the kinds of {@link runRecord} pass; another kind fails. */
|
|
31
|
-
export const validate = <V extends GenericValidator>(
|
|
32
|
-
validator: V,
|
|
33
|
-
value: unknown,
|
|
34
|
-
// eslint-disable-next-line samebase/no-hand-written-type-predicate -- A component imports no app package, and the switch walks the validator itself.
|
|
35
|
-
): value is Infer<V> => {
|
|
36
|
-
switch (validator.kind) {
|
|
37
|
-
case "object": {
|
|
38
|
-
if (typeof value !== "object" || value === null) return false;
|
|
39
|
-
const fields = new Map(Object.entries(value));
|
|
40
|
-
return Object.entries(validator.fields).every(([key, field]) =>
|
|
41
|
-
validate(field, fields.get(key)),
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
case "union":
|
|
45
|
-
return validator.members.some((member) => validate(member, value));
|
|
46
|
-
case "literal":
|
|
47
|
-
return value === validator.value;
|
|
48
|
-
case "string":
|
|
49
|
-
return typeof value === "string";
|
|
50
|
-
case "float64":
|
|
51
|
-
return typeof value === "number";
|
|
52
|
-
case "null":
|
|
53
|
-
return value === null;
|
|
54
|
-
default:
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
};
|