@samebase/alchemy-convex 0.4.0 → 0.5.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,169 @@
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 type {
12
+ ActionBuilder,
13
+ HttpActionBuilder,
14
+ MutationBuilder,
15
+ QueryBuilder,
16
+ GenericActionCtx,
17
+ GenericMutationCtx,
18
+ GenericQueryCtx,
19
+ GenericDatabaseReader,
20
+ GenericDatabaseWriter,
21
+ } from "convex/server";
22
+ import {
23
+ actionGeneric,
24
+ httpActionGeneric,
25
+ queryGeneric,
26
+ mutationGeneric,
27
+ internalActionGeneric,
28
+ internalMutationGeneric,
29
+ internalQueryGeneric,
30
+ } from "convex/server";
31
+ import type { DataModel } from "./dataModel.js";
32
+
33
+ /**
34
+ * Typesafe environment variables.
35
+ *
36
+ * This includes platform-provided env vars and any variables declared in
37
+ * `convex.config.ts`.
38
+ */
39
+ type Env = {
40
+ readonly CONVEX_CLOUD_URL: string;
41
+ readonly CONVEX_SITE_URL: string;
42
+ };
43
+
44
+ /**
45
+ * Define a query in this Convex app's public API.
46
+ *
47
+ * This function will be allowed to read your Convex database and will be accessible from the client.
48
+ *
49
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
50
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
51
+ */
52
+ export const query: QueryBuilder<DataModel, "public"> = queryGeneric;
53
+
54
+ /**
55
+ * Define a query that is only accessible from other Convex functions (but not from the client).
56
+ *
57
+ * This function will be allowed to read from your Convex database. It will not be accessible from the client.
58
+ *
59
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
60
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
61
+ */
62
+ export const internalQuery: QueryBuilder<DataModel, "internal"> =
63
+ internalQueryGeneric;
64
+
65
+ /**
66
+ * Define a mutation in this Convex app's public API.
67
+ *
68
+ * This function will be allowed to modify your Convex database and will 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 const mutation: MutationBuilder<DataModel, "public"> = mutationGeneric;
74
+
75
+ /**
76
+ * Define a mutation that is only accessible from other Convex functions (but not from the client).
77
+ *
78
+ * This function will be allowed to modify your Convex database. It will not be accessible from the client.
79
+ *
80
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
81
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
82
+ */
83
+ export const internalMutation: MutationBuilder<DataModel, "internal"> =
84
+ internalMutationGeneric;
85
+
86
+ /**
87
+ * Define an action in this Convex app's public API.
88
+ *
89
+ * An action is a function which can execute any JavaScript code, including non-deterministic
90
+ * code and code with side-effects, like calling third-party services.
91
+ * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
92
+ * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
93
+ *
94
+ * @param func - The action. It receives an {@link ActionCtx} as its first argument.
95
+ * @returns The wrapped action. Include this as an `export` to name it and make it accessible.
96
+ */
97
+ export const action: ActionBuilder<DataModel, "public"> = actionGeneric;
98
+
99
+ /**
100
+ * Define an action that is only accessible from other Convex functions (but not from the client).
101
+ *
102
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument.
103
+ * @returns The wrapped function. Include this as an `export` to name it and make it accessible.
104
+ */
105
+ export const internalAction: ActionBuilder<DataModel, "internal"> =
106
+ internalActionGeneric;
107
+
108
+ /**
109
+ * Define an HTTP action.
110
+ *
111
+ * The wrapped function will be used to respond to HTTP requests received
112
+ * by a Convex deployment if the requests matches the path and method where
113
+ * this action is routed. Be sure to route your httpAction in `convex/http.js`.
114
+ *
115
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument
116
+ * and a Fetch API `Request` object as its second.
117
+ * @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
118
+ */
119
+ export const httpAction: HttpActionBuilder = httpActionGeneric;
120
+ export const env: Env = (globalThis as unknown as { process: { env: Env } })
121
+ .process.env;
122
+
123
+ /**
124
+ * A set of services for use within Convex query functions.
125
+ *
126
+ * The query context is passed as the first argument to any Convex query
127
+ * function run on the server.
128
+ *
129
+ * If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
130
+ */
131
+ export type QueryCtx = GenericQueryCtx<DataModel>;
132
+
133
+ /**
134
+ * A set of services for use within Convex mutation functions.
135
+ *
136
+ * The mutation context is passed as the first argument to any Convex mutation
137
+ * function run on the server.
138
+ *
139
+ * If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
140
+ */
141
+ export type MutationCtx = GenericMutationCtx<DataModel>;
142
+
143
+ /**
144
+ * A set of services for use within Convex action functions.
145
+ *
146
+ * The action context is passed as the first argument to any Convex action
147
+ * function run on the server.
148
+ */
149
+ export type ActionCtx = GenericActionCtx<DataModel>;
150
+
151
+ /**
152
+ * An interface to read from the database within Convex query functions.
153
+ *
154
+ * The two entry points are {@link DatabaseReader.get}, which fetches a single
155
+ * document by its {@link Id}, or {@link DatabaseReader.query}, which starts
156
+ * building a query.
157
+ */
158
+ export type DatabaseReader = GenericDatabaseReader<DataModel>;
159
+
160
+ /**
161
+ * An interface to read from and write to the database within Convex mutation
162
+ * functions.
163
+ *
164
+ * Convex guarantees that all writes within a single mutation are
165
+ * executed atomically, so you never have to worry about partial writes leaving
166
+ * your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
167
+ * for the guarantees Convex provides your functions.
168
+ */
169
+ export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
@@ -0,0 +1,5 @@
1
+ // The Alchemy state store of a Convex app. Mount it with its default name:
2
+ // `Convex.state()` calls it at the component path "alchemyState".
3
+ import { defineComponent } from "convex/server";
4
+
5
+ export default defineComponent("alchemyState");
@@ -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,169 @@
1
+ // The state functions. A component function is not part of the public API of
2
+ // the app: only POST /api/function with a deploy key reaches it, and that key
3
+ // needs `deployment:data:view` for a query and `deployment:data:write` for a
4
+ // mutation (measured).
5
+ //
6
+ // A stage holds one row for each resource of its stack, so the reads of one
7
+ // stage collect all rows.
8
+ import { v } from "convex/values";
9
+ import type { QueryCtx } from "./_generated/server.js";
10
+ import { mutation, query } from "./_generated/server.js";
11
+
12
+ const stage = { stack: v.string(), stage: v.string() };
13
+ const resource = { ...stage, fqn: v.string() };
14
+
15
+ const rowOf = (ctx: QueryCtx, args: { stack: string; stage: string; fqn: string }) =>
16
+ ctx.db
17
+ .query("resources")
18
+ .withIndex("by_fqn", (q) =>
19
+ q.eq("stack", args.stack).eq("stage", args.stage).eq("fqn", args.fqn),
20
+ )
21
+ .unique();
22
+
23
+ const rowsOf = (ctx: QueryCtx, args: { stack: string; stage: string }) =>
24
+ ctx.db
25
+ .query("resources")
26
+ .withIndex("by_fqn", (q) => q.eq("stack", args.stack).eq("stage", args.stage))
27
+ .collect();
28
+
29
+ const outputOf = (ctx: QueryCtx, args: { stack: string; stage: string }) =>
30
+ ctx.db
31
+ .query("outputs")
32
+ .withIndex("by_stage", (q) => q.eq("stack", args.stack).eq("stage", args.stage))
33
+ .unique();
34
+
35
+ /** Stacks with at least one resource, in index order: one index read for each stack. */
36
+ export const stacks = query({
37
+ args: {},
38
+ returns: v.array(v.string()),
39
+ handler: async (ctx) => {
40
+ const names: string[] = [];
41
+ for (;;) {
42
+ const last = names.at(-1);
43
+ const next = await ctx.db
44
+ .query("resources")
45
+ .withIndex("by_fqn", (q) => (last === undefined ? q : q.gt("stack", last)))
46
+ .first();
47
+ if (next === null) return names;
48
+ names.push(next.stack);
49
+ }
50
+ },
51
+ });
52
+
53
+ /** Stages of a stack with at least one resource, in index order. */
54
+ export const stages = query({
55
+ args: { stack: v.string() },
56
+ returns: v.array(v.string()),
57
+ handler: async (ctx, args) => {
58
+ const names: string[] = [];
59
+ for (;;) {
60
+ const last = names.at(-1);
61
+ const next = await ctx.db
62
+ .query("resources")
63
+ .withIndex("by_fqn", (q) =>
64
+ last === undefined
65
+ ? q.eq("stack", args.stack)
66
+ : q.eq("stack", args.stack).gt("stage", last),
67
+ )
68
+ .first();
69
+ if (next === null) return names;
70
+ names.push(next.stage);
71
+ }
72
+ },
73
+ });
74
+
75
+ export const list = query({
76
+ args: stage,
77
+ returns: v.array(v.string()),
78
+ handler: async (ctx, args) => (await rowsOf(ctx, args)).map((row) => row.fqn),
79
+ });
80
+
81
+ export const get = query({
82
+ args: resource,
83
+ returns: v.union(v.string(), v.null()),
84
+ handler: async (ctx, args) => (await rowOf(ctx, args))?.value ?? null,
85
+ });
86
+
87
+ /** Resources whose state has `status: "replaced"`. */
88
+ export const replaced = query({
89
+ args: stage,
90
+ returns: v.array(v.string()),
91
+ handler: async (ctx, args) =>
92
+ (await rowsOf(ctx, args))
93
+ .map((row) => row.value)
94
+ .filter((value) => {
95
+ const parsed: unknown = JSON.parse(value);
96
+ return (
97
+ typeof parsed === "object" &&
98
+ parsed !== null &&
99
+ "status" in parsed &&
100
+ parsed.status === "replaced"
101
+ );
102
+ }),
103
+ });
104
+
105
+ export const set = mutation({
106
+ args: { ...resource, value: v.string() },
107
+ returns: v.null(),
108
+ handler: async (ctx, args) => {
109
+ const row = await rowOf(ctx, args);
110
+ if (row === null) await ctx.db.insert("resources", args);
111
+ else await ctx.db.patch("resources", row._id, { value: args.value });
112
+ return null;
113
+ },
114
+ });
115
+
116
+ export const remove = mutation({
117
+ args: resource,
118
+ returns: v.null(),
119
+ handler: async (ctx, args) => {
120
+ const row = await rowOf(ctx, args);
121
+ if (row !== null) await ctx.db.delete("resources", row._id);
122
+ return null;
123
+ },
124
+ });
125
+
126
+ /** Deletes the resources and outputs of one stage, or of every stage of the stack. */
127
+ export const removeStack = mutation({
128
+ args: { stack: v.string(), stage: v.optional(v.string()) },
129
+ returns: v.null(),
130
+ handler: async (ctx, args) => {
131
+ const { stack } = args;
132
+ const resources = await ctx.db
133
+ .query("resources")
134
+ .withIndex("by_fqn", (q) =>
135
+ args.stage === undefined
136
+ ? q.eq("stack", stack)
137
+ : q.eq("stack", stack).eq("stage", args.stage),
138
+ )
139
+ .collect();
140
+ const outputs = await ctx.db
141
+ .query("outputs")
142
+ .withIndex("by_stage", (q) =>
143
+ args.stage === undefined
144
+ ? q.eq("stack", stack)
145
+ : q.eq("stack", stack).eq("stage", args.stage),
146
+ )
147
+ .collect();
148
+ for (const row of resources) await ctx.db.delete("resources", row._id);
149
+ for (const row of outputs) await ctx.db.delete("outputs", row._id);
150
+ return null;
151
+ },
152
+ });
153
+
154
+ export const getOutput = query({
155
+ args: stage,
156
+ returns: v.union(v.string(), v.null()),
157
+ handler: async (ctx, args) => (await outputOf(ctx, args))?.value ?? null,
158
+ });
159
+
160
+ export const setOutput = mutation({
161
+ args: { ...stage, value: v.string() },
162
+ returns: v.null(),
163
+ handler: async (ctx, args) => {
164
+ const row = await outputOf(ctx, args);
165
+ if (row === null) await ctx.db.insert("outputs", args);
166
+ else await ctx.db.patch("outputs", row._id, { value: args.value });
167
+ return null;
168
+ },
169
+ });
package/src/index.ts CHANGED
@@ -28,6 +28,7 @@ export {
28
28
  ProjectTeamChange,
29
29
  } from "./Project.ts";
30
30
  export { Providers, providers } from "./Providers.ts";
31
+ export { STATE_DEPLOY_KEY_ENV, state } from "./State.ts";
31
32
  export {
32
33
  CodePushFailed,
33
34
  ConvexAppNotFound,