@samebase/alchemy-convex 0.4.1 → 0.6.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.
@@ -0,0 +1,162 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated utilities for implementing server-side Convex query and mutation functions.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import {
12
+ ActionBuilder,
13
+ HttpActionBuilder,
14
+ MutationBuilder,
15
+ QueryBuilder,
16
+ GenericActionCtx,
17
+ GenericMutationCtx,
18
+ GenericQueryCtx,
19
+ GenericDatabaseReader,
20
+ GenericDatabaseWriter,
21
+ } from "convex/server";
22
+ import type { DataModel } from "./dataModel.js";
23
+
24
+ /**
25
+ * Typesafe environment variables.
26
+ *
27
+ * This includes platform-provided env vars and any variables declared in
28
+ * `convex.config.ts`.
29
+ */
30
+ type Env = {
31
+ readonly CONVEX_CLOUD_URL: string;
32
+ readonly CONVEX_SITE_URL: string;
33
+ };
34
+
35
+ /**
36
+ * Define a query in this Convex app's public API.
37
+ *
38
+ * This function will be allowed to read your Convex database and will be accessible from the client.
39
+ *
40
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
41
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
42
+ */
43
+ export declare const query: QueryBuilder<DataModel, "public">;
44
+
45
+ /**
46
+ * Define a query that is only accessible from other Convex functions (but not from the client).
47
+ *
48
+ * This function will be allowed to read from your Convex database. It will not be accessible from the client.
49
+ *
50
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
51
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
52
+ */
53
+ export declare const internalQuery: QueryBuilder<DataModel, "internal">;
54
+
55
+ /**
56
+ * Define a mutation in this Convex app's public API.
57
+ *
58
+ * This function will be allowed to modify your Convex database and will be accessible from the client.
59
+ *
60
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
61
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
62
+ */
63
+ export declare const mutation: MutationBuilder<DataModel, "public">;
64
+
65
+ /**
66
+ * Define a mutation that is only accessible from other Convex functions (but not from the client).
67
+ *
68
+ * This function will be allowed to modify your Convex database. It will not be accessible from the client.
69
+ *
70
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
71
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
72
+ */
73
+ export declare const internalMutation: MutationBuilder<DataModel, "internal">;
74
+
75
+ /**
76
+ * Define an action in this Convex app's public API.
77
+ *
78
+ * An action is a function which can execute any JavaScript code, including non-deterministic
79
+ * code and code with side-effects, like calling third-party services.
80
+ * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
81
+ * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
82
+ *
83
+ * @param func - The action. It receives an {@link ActionCtx} as its first argument.
84
+ * @returns The wrapped action. Include this as an `export` to name it and make it accessible.
85
+ */
86
+ export declare const action: ActionBuilder<DataModel, "public">;
87
+
88
+ /**
89
+ * Define an action that is only accessible from other Convex functions (but not from the client).
90
+ *
91
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument.
92
+ * @returns The wrapped function. Include this as an `export` to name it and make it accessible.
93
+ */
94
+ export declare const internalAction: ActionBuilder<DataModel, "internal">;
95
+
96
+ /**
97
+ * Define an HTTP action.
98
+ *
99
+ * The wrapped function will be used to respond to HTTP requests received
100
+ * by a Convex deployment if the requests matches the path and method where
101
+ * this action is routed. Be sure to route your httpAction in `convex/http.js`.
102
+ *
103
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument
104
+ * and a Fetch API `Request` object as its second.
105
+ * @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
106
+ */
107
+ export declare const httpAction: HttpActionBuilder;
108
+
109
+ /**
110
+ * Typesafe environment variables.
111
+ *
112
+ * This includes platform-provided env vars and any variables declared in
113
+ * `convex.config.ts`.
114
+ */
115
+ export declare const env: Env;
116
+
117
+ /**
118
+ * A set of services for use within Convex query functions.
119
+ *
120
+ * The query context is passed as the first argument to any Convex query
121
+ * function run on the server.
122
+ *
123
+ * This differs from the {@link MutationCtx} because all of the services are
124
+ * read-only.
125
+ */
126
+ export type QueryCtx = GenericQueryCtx<DataModel>;
127
+
128
+ /**
129
+ * A set of services for use within Convex mutation functions.
130
+ *
131
+ * The mutation context is passed as the first argument to any Convex mutation
132
+ * function run on the server.
133
+ */
134
+ export type MutationCtx = GenericMutationCtx<DataModel>;
135
+
136
+ /**
137
+ * A set of services for use within Convex action functions.
138
+ *
139
+ * The action context is passed as the first argument to any Convex action
140
+ * function run on the server.
141
+ */
142
+ export type ActionCtx = GenericActionCtx<DataModel>;
143
+
144
+ /**
145
+ * An interface to read from the database within Convex query functions.
146
+ *
147
+ * The two entry points are {@link DatabaseReader.get}, which fetches a single
148
+ * document by its {@link Id}, or {@link DatabaseReader.query}, which starts
149
+ * building a query.
150
+ */
151
+ export type DatabaseReader = GenericDatabaseReader<DataModel>;
152
+
153
+ /**
154
+ * An interface to read from and write to the database within Convex mutation
155
+ * functions.
156
+ *
157
+ * Convex guarantees that all writes within a single mutation are
158
+ * executed atomically, so you never have to worry about partial writes leaving
159
+ * your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
160
+ * for the guarantees Convex provides your functions.
161
+ */
162
+ export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
@@ -0,0 +1,101 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated utilities for implementing server-side Convex query and mutation functions.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import {
12
+ actionGeneric,
13
+ httpActionGeneric,
14
+ queryGeneric,
15
+ mutationGeneric,
16
+ internalActionGeneric,
17
+ internalMutationGeneric,
18
+ internalQueryGeneric,
19
+ } from "convex/server";
20
+
21
+ /**
22
+ * Define a query in this Convex app's public API.
23
+ *
24
+ * This function will be allowed to read your Convex database and will be accessible from the client.
25
+ *
26
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
27
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
28
+ */
29
+ export const query = queryGeneric;
30
+
31
+ /**
32
+ * Define a query that is only accessible from other Convex functions (but not from the client).
33
+ *
34
+ * This function will be allowed to read from your Convex database. It will not be accessible from the client.
35
+ *
36
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
37
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
38
+ */
39
+ export const internalQuery = internalQueryGeneric;
40
+
41
+ /**
42
+ * Define a mutation in this Convex app's public API.
43
+ *
44
+ * This function will be allowed to modify your Convex database and will be accessible from the client.
45
+ *
46
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
47
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
48
+ */
49
+ export const mutation = mutationGeneric;
50
+
51
+ /**
52
+ * Define a mutation that is only accessible from other Convex functions (but not from the client).
53
+ *
54
+ * This function will be allowed to modify your Convex database. It will not be accessible from the client.
55
+ *
56
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
57
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
58
+ */
59
+ export const internalMutation = internalMutationGeneric;
60
+
61
+ /**
62
+ * Define an action in this Convex app's public API.
63
+ *
64
+ * An action is a function which can execute any JavaScript code, including non-deterministic
65
+ * code and code with side-effects, like calling third-party services.
66
+ * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
67
+ * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
68
+ *
69
+ * @param func - The action. It receives an {@link ActionCtx} as its first argument.
70
+ * @returns The wrapped action. Include this as an `export` to name it and make it accessible.
71
+ */
72
+ export const action = actionGeneric;
73
+
74
+ /**
75
+ * Define an action that is only accessible from other Convex functions (but not from the client).
76
+ *
77
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument.
78
+ * @returns The wrapped function. Include this as an `export` to name it and make it accessible.
79
+ */
80
+ export const internalAction = internalActionGeneric;
81
+
82
+ /**
83
+ * Define an HTTP action.
84
+ *
85
+ * The wrapped function will be used to respond to HTTP requests received
86
+ * by a Convex deployment if the requests matches the path and method where
87
+ * this action is routed. Be sure to route your httpAction in `convex/http.js`.
88
+ *
89
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument
90
+ * and a Fetch API `Request` object as its second.
91
+ * @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
92
+ */
93
+ export const httpAction = httpActionGeneric;
94
+
95
+ /**
96
+ * Typesafe environment variables.
97
+ *
98
+ * This includes platform-provided env vars and any variables declared in
99
+ * `convex.config.ts`.
100
+ */
101
+ export const env = process.env;
@@ -0,0 +1,19 @@
1
+ import { defineSchema, defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+
4
+ // `value` is the JSON text of Alchemy's encodeState(...): the same text that
5
+ // Alchemy's local state writes to a file. A Convex object would reject keys
6
+ // that Alchemy state can hold, such as keys that start with "$".
7
+ export default defineSchema({
8
+ resources: defineTable({
9
+ stack: v.string(),
10
+ stage: v.string(),
11
+ fqn: v.string(),
12
+ value: v.string(),
13
+ }).index("by_fqn", ["stack", "stage", "fqn"]),
14
+ outputs: defineTable({
15
+ stack: v.string(),
16
+ stage: v.string(),
17
+ value: v.string(),
18
+ }).index("by_stage", ["stack", "stage"]),
19
+ });
@@ -0,0 +1,219 @@
1
+ // The state functions of the alchemy-state project: one project for each
2
+ // Convex team, pushed by Convex.state() from this directory. Every function
3
+ // is internal: a public function of a root app answers anyone who knows the
4
+ // deployment URL, with no key (measured). An internal function answers only
5
+ // POST /api/function with a deploy key of the production deployment, the
6
+ // call that `convex run` makes.
7
+ //
8
+ // A stage holds one row for each resource of its stack, so the reads of one
9
+ // stage collect all rows. The function names are a wire contract: Samebase
10
+ // reads `stacks` and `getOutput` by name, and an older client calls an older
11
+ // set of names. Add functions; never rename or remove one.
12
+ import { v } from "convex/values";
13
+ import type { QueryCtx } from "./_generated/server.js";
14
+ import { internalMutation, internalQuery } from "./_generated/server.js";
15
+ import { STORE_VERSION } from "./version.ts";
16
+
17
+ /** The wire version of this store, compared with the client's STORE_VERSION. */
18
+ export const version = internalQuery({
19
+ args: {},
20
+ returns: v.number(),
21
+ handler: async () => STORE_VERSION,
22
+ });
23
+
24
+ const stage = { stack: v.string(), stage: v.string() };
25
+ const resource = { ...stage, fqn: v.string() };
26
+
27
+ const rowOf = (ctx: QueryCtx, args: { stack: string; stage: string; fqn: string }) =>
28
+ ctx.db
29
+ .query("resources")
30
+ .withIndex("by_fqn", (q) =>
31
+ q.eq("stack", args.stack).eq("stage", args.stage).eq("fqn", args.fqn),
32
+ )
33
+ .unique();
34
+
35
+ const rowsOf = (ctx: QueryCtx, args: { stack: string; stage: string }) =>
36
+ ctx.db
37
+ .query("resources")
38
+ .withIndex("by_fqn", (q) => q.eq("stack", args.stack).eq("stage", args.stage))
39
+ .collect();
40
+
41
+ const outputOf = (ctx: QueryCtx, args: { stack: string; stage: string }) =>
42
+ ctx.db
43
+ .query("outputs")
44
+ .withIndex("by_stage", (q) => q.eq("stack", args.stack).eq("stage", args.stage))
45
+ .unique();
46
+
47
+ /** The sorted union of two lists of names. */
48
+ const union = (a: readonly string[], b: readonly string[]) => [...new Set([...a, ...b])].sort();
49
+
50
+ /**
51
+ * The distinct names of one table in index order, with one index read for
52
+ * each name: `first(after)` is the first row whose name is above `after`.
53
+ */
54
+ const distinct = async (first: (after: string | undefined) => Promise<string | null>) => {
55
+ const names: string[] = [];
56
+ for (;;) {
57
+ const next = await first(names.at(-1));
58
+ if (next === null) return names;
59
+ names.push(next);
60
+ }
61
+ };
62
+
63
+ /** Stacks with at least one resource or one output, sorted. */
64
+ export const stacks = internalQuery({
65
+ args: {},
66
+ returns: v.array(v.string()),
67
+ handler: async (ctx) =>
68
+ union(
69
+ await distinct(
70
+ async (after) =>
71
+ (
72
+ await ctx.db
73
+ .query("resources")
74
+ .withIndex("by_fqn", (q) => (after === undefined ? q : q.gt("stack", after)))
75
+ .first()
76
+ )?.stack ?? null,
77
+ ),
78
+ await distinct(
79
+ async (after) =>
80
+ (
81
+ await ctx.db
82
+ .query("outputs")
83
+ .withIndex("by_stage", (q) => (after === undefined ? q : q.gt("stack", after)))
84
+ .first()
85
+ )?.stack ?? null,
86
+ ),
87
+ ),
88
+ });
89
+
90
+ /** Stages of a stack with at least one resource or one output, sorted. */
91
+ export const stages = internalQuery({
92
+ args: { stack: v.string() },
93
+ returns: v.array(v.string()),
94
+ handler: async (ctx, args) =>
95
+ union(
96
+ await distinct(
97
+ async (after) =>
98
+ (
99
+ await ctx.db
100
+ .query("resources")
101
+ .withIndex("by_fqn", (q) =>
102
+ after === undefined
103
+ ? q.eq("stack", args.stack)
104
+ : q.eq("stack", args.stack).gt("stage", after),
105
+ )
106
+ .first()
107
+ )?.stage ?? null,
108
+ ),
109
+ await distinct(
110
+ async (after) =>
111
+ (
112
+ await ctx.db
113
+ .query("outputs")
114
+ .withIndex("by_stage", (q) =>
115
+ after === undefined
116
+ ? q.eq("stack", args.stack)
117
+ : q.eq("stack", args.stack).gt("stage", after),
118
+ )
119
+ .first()
120
+ )?.stage ?? null,
121
+ ),
122
+ ),
123
+ });
124
+
125
+ export const list = internalQuery({
126
+ args: stage,
127
+ returns: v.array(v.string()),
128
+ handler: async (ctx, args) => (await rowsOf(ctx, args)).map((row) => row.fqn),
129
+ });
130
+
131
+ export const get = internalQuery({
132
+ args: resource,
133
+ returns: v.union(v.string(), v.null()),
134
+ handler: async (ctx, args) => (await rowOf(ctx, args))?.value ?? null,
135
+ });
136
+
137
+ /** Resources whose state has `status: "replaced"`. */
138
+ export const replaced = internalQuery({
139
+ args: stage,
140
+ returns: v.array(v.string()),
141
+ handler: async (ctx, args) =>
142
+ (await rowsOf(ctx, args))
143
+ .map((row) => row.value)
144
+ .filter((value) => {
145
+ const parsed: unknown = JSON.parse(value);
146
+ return (
147
+ typeof parsed === "object" &&
148
+ parsed !== null &&
149
+ "status" in parsed &&
150
+ parsed.status === "replaced"
151
+ );
152
+ }),
153
+ });
154
+
155
+ export const set = internalMutation({
156
+ args: { ...resource, value: v.string() },
157
+ returns: v.null(),
158
+ handler: async (ctx, args) => {
159
+ const row = await rowOf(ctx, args);
160
+ if (row === null) await ctx.db.insert("resources", args);
161
+ else await ctx.db.patch("resources", row._id, { value: args.value });
162
+ return null;
163
+ },
164
+ });
165
+
166
+ export const remove = internalMutation({
167
+ args: resource,
168
+ returns: v.null(),
169
+ handler: async (ctx, args) => {
170
+ const row = await rowOf(ctx, args);
171
+ if (row !== null) await ctx.db.delete("resources", row._id);
172
+ return null;
173
+ },
174
+ });
175
+
176
+ /** Deletes the resources and outputs of one stage, or of every stage of the stack. */
177
+ export const removeStack = internalMutation({
178
+ args: { stack: v.string(), stage: v.optional(v.string()) },
179
+ returns: v.null(),
180
+ handler: async (ctx, args) => {
181
+ const { stack } = args;
182
+ const resources = await ctx.db
183
+ .query("resources")
184
+ .withIndex("by_fqn", (q) =>
185
+ args.stage === undefined
186
+ ? q.eq("stack", stack)
187
+ : q.eq("stack", stack).eq("stage", args.stage),
188
+ )
189
+ .collect();
190
+ const outputs = await ctx.db
191
+ .query("outputs")
192
+ .withIndex("by_stage", (q) =>
193
+ args.stage === undefined
194
+ ? q.eq("stack", stack)
195
+ : q.eq("stack", stack).eq("stage", args.stage),
196
+ )
197
+ .collect();
198
+ for (const row of resources) await ctx.db.delete("resources", row._id);
199
+ for (const row of outputs) await ctx.db.delete("outputs", row._id);
200
+ return null;
201
+ },
202
+ });
203
+
204
+ export const getOutput = internalQuery({
205
+ args: stage,
206
+ returns: v.union(v.string(), v.null()),
207
+ handler: async (ctx, args) => (await outputOf(ctx, args))?.value ?? null,
208
+ });
209
+
210
+ export const setOutput = internalMutation({
211
+ args: { ...stage, value: v.string() },
212
+ returns: v.null(),
213
+ handler: async (ctx, args) => {
214
+ const row = await outputOf(ctx, args);
215
+ if (row === null) await ctx.db.insert("outputs", args);
216
+ else await ctx.db.patch("outputs", row._id, { value: args.value });
217
+ return null;
218
+ },
219
+ });
@@ -0,0 +1,6 @@
1
+ // The wire version of the store: the `state:version` query answers it, and
2
+ // Convex.state() pushes this directory when the store of the team answers an
3
+ // older one or has no `version` yet. Raise it when a function changes in a way
4
+ // that an older client cannot use. Never rename or remove a function: an
5
+ // older client, and Samebase, call the store by these names.
6
+ export const STORE_VERSION = 1;
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "alchemy-state",
3
+ "private": true,
4
+ "type": "module",
5
+ "peerDependencies": {
6
+ "convex": "^1.46.0"
7
+ }
8
+ }