@confect/server 1.0.0-next.3 → 1.0.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.
@@ -1,48 +1,14 @@
1
1
  import type * as FunctionSpec from "@confect/core/FunctionSpec";
2
2
  import type * as GroupSpec from "@confect/core/GroupSpec";
3
3
  import type * as Spec from "@confect/core/Spec";
4
- import {
5
- actionGeneric,
6
- type DefaultFunctionArgs,
7
- type FunctionVisibility,
8
- type GenericActionCtx,
9
- type GenericMutationCtx,
10
- type GenericQueryCtx,
11
- internalActionGeneric,
12
- internalMutationGeneric,
13
- internalQueryGeneric,
14
- mutationGeneric,
15
- queryGeneric,
16
- type RegisteredAction,
17
- type RegisteredMutation,
18
- type RegisteredQuery,
19
- } from "convex/server";
20
- import { Effect, Layer, Match, pipe, Ref, Schema, type Types } from "effect";
21
- import * as ActionCtx from "./ActionCtx";
22
- import * as ActionRunner from "./ActionRunner";
4
+ import type { Layer } from "effect";
5
+ import { Effect, Match, Ref, type Types } from "effect";
23
6
  import type * as Api from "./Api";
24
- import * as Auth from "./Auth";
25
- import * as DatabaseReader from "./DatabaseReader";
26
- import type * as DatabaseSchema from "./DatabaseSchema";
27
- import * as DatabaseWriter from "./DatabaseWriter";
28
- import type * as DataModel from "./DataModel";
29
7
  import * as Impl from "./Impl";
30
8
  import { mapLeaves } from "./internal/utils";
31
- import * as MutationCtx from "./MutationCtx";
32
- import * as MutationRunner from "./MutationRunner";
33
- import * as QueryCtx from "./QueryCtx";
34
- import * as QueryRunner from "./QueryRunner";
9
+ import type * as RegisteredFunction from "./RegisteredFunction";
35
10
  import * as Registry from "./Registry";
36
11
  import * as RegistryItem from "./RegistryItem";
37
- import * as Scheduler from "./Scheduler";
38
- import * as SchemaToValidator from "./SchemaToValidator";
39
- import { StorageActionWriter, StorageReader, StorageWriter } from "./Storage";
40
- import * as VectorSearch from "./VectorSearch";
41
-
42
- export type RegisteredFunction =
43
- | RegisteredQuery<FunctionVisibility, DefaultFunctionArgs, any>
44
- | RegisteredMutation<FunctionVisibility, DefaultFunctionArgs, any>
45
- | RegisteredAction<FunctionVisibility, DefaultFunctionArgs, any>;
46
12
 
47
13
  export type RegisteredFunctions<Spec_ extends Spec.AnyWithProps> =
48
14
  Types.Simplify<RegisteredFunctionsHelper<Spec.Groups<Spec_>>>;
@@ -80,11 +46,15 @@ type RegisteredFunctionsHelper<Groups extends GroupSpec.AnyWithProps> = {
80
46
  };
81
47
 
82
48
  export interface AnyWithProps {
83
- readonly [key: string]: RegisteredFunction | AnyWithProps;
49
+ readonly [key: string]: RegisteredFunction.RegisteredFunction | AnyWithProps;
84
50
  }
85
51
 
86
52
  export const make = <Api_ extends Api.AnyWithProps>(
87
53
  impl: Layer.Layer<Impl.Impl<Api_, "Finalized">>,
54
+ makeRegisteredFunction: (
55
+ api: Api_,
56
+ registryItem: RegistryItem.AnyWithProps,
57
+ ) => RegisteredFunction.RegisteredFunction,
88
58
  ) =>
89
59
  Effect.gen(function* () {
90
60
  const registry = yield* Registry.Registry;
@@ -100,274 +70,14 @@ export const make = <Api_ extends Api.AnyWithProps>(
100
70
  ),
101
71
  Match.when("Finalized", () =>
102
72
  Effect.succeed(
103
- mapLeaves<RegistryItem.AnyWithProps, RegisteredFunction>(
104
- functionImplItems,
105
- RegistryItem.isRegistryItem,
106
- (registryItem) => makeRegisteredFunction(api, registryItem),
73
+ mapLeaves<
74
+ RegistryItem.AnyWithProps,
75
+ RegisteredFunction.RegisteredFunction
76
+ >(functionImplItems, RegistryItem.isRegistryItem, (registryItem) =>
77
+ makeRegisteredFunction(api, registryItem),
107
78
  ) as RegisteredFunctions<Api_["spec"]>,
108
79
  ),
109
80
  ),
110
81
  Match.exhaustive,
111
82
  );
112
83
  }).pipe(Effect.provide(impl), Effect.runSync);
113
-
114
- const makeRegisteredFunction = <Api_ extends Api.AnyWithProps>(
115
- api: Api_,
116
- { function_, handler }: RegistryItem.AnyWithProps,
117
- ): RegisteredFunction =>
118
- Match.value(function_.functionType).pipe(
119
- Match.when("query", () => {
120
- const genericFunction = Match.value(function_.functionVisibility).pipe(
121
- Match.when("public", () => queryGeneric),
122
- Match.when("internal", () => internalQueryGeneric),
123
- Match.exhaustive,
124
- );
125
-
126
- return genericFunction(
127
- queryFunction(api.databaseSchema, {
128
- args: function_.args,
129
- returns: function_.returns,
130
- handler,
131
- }),
132
- );
133
- }),
134
- Match.when("mutation", () => {
135
- const genericFunction = Match.value(function_.functionVisibility).pipe(
136
- Match.when("public", () => mutationGeneric),
137
- Match.when("internal", () => internalMutationGeneric),
138
- Match.exhaustive,
139
- );
140
-
141
- return genericFunction(
142
- mutationFunction(api.databaseSchema, {
143
- args: function_.args,
144
- returns: function_.returns,
145
- handler,
146
- }),
147
- );
148
- }),
149
- Match.when("action", () => {
150
- const genericFunction = Match.value(function_.functionVisibility).pipe(
151
- Match.when("public", () => actionGeneric),
152
- Match.when("internal", () => internalActionGeneric),
153
- Match.exhaustive,
154
- );
155
-
156
- return genericFunction(
157
- actionFunction({
158
- args: function_.args,
159
- returns: function_.returns,
160
- handler,
161
- }),
162
- );
163
- }),
164
- Match.exhaustive,
165
- );
166
-
167
- const queryFunction = <
168
- Schema extends DatabaseSchema.AnyWithProps,
169
- Args,
170
- ConvexArgs extends DefaultFunctionArgs,
171
- Returns,
172
- ConvexReturns,
173
- E,
174
- >(
175
- schema: Schema,
176
- {
177
- args,
178
- returns,
179
- handler,
180
- }: {
181
- args: Schema.Schema<Args, ConvexArgs>;
182
- returns: Schema.Schema<Returns, ConvexReturns>;
183
- handler: (
184
- a: Args,
185
- ) => Effect.Effect<
186
- Returns,
187
- E,
188
- | DatabaseReader.DatabaseReader<Schema>
189
- | Auth.Auth
190
- | StorageReader
191
- | QueryRunner.QueryRunner
192
- | QueryCtx.QueryCtx<DataModel.ToConvex<DataModel.FromSchema<Schema>>>
193
- >;
194
- },
195
- ) => ({
196
- args: SchemaToValidator.compileArgsSchema(args),
197
- returns: SchemaToValidator.compileReturnsSchema(returns),
198
- handler: (
199
- ctx: GenericQueryCtx<DataModel.ToConvex<DataModel.FromSchema<Schema>>>,
200
- actualArgs: ConvexArgs,
201
- ): Promise<ConvexReturns> =>
202
- pipe(
203
- actualArgs,
204
- Schema.decode(args),
205
- Effect.orDie,
206
- Effect.andThen((decodedArgs) =>
207
- pipe(
208
- handler(decodedArgs),
209
- Effect.provide(
210
- Layer.mergeAll(
211
- DatabaseReader.layer(schema, ctx.db),
212
- Auth.layer(ctx.auth),
213
- StorageReader.layer(ctx.storage),
214
- QueryRunner.layer(ctx.runQuery),
215
- Layer.succeed(
216
- QueryCtx.QueryCtx<
217
- DataModel.ToConvex<DataModel.FromSchema<Schema>>
218
- >(),
219
- ctx,
220
- ),
221
- ),
222
- ),
223
- ),
224
- ),
225
- Effect.andThen((convexReturns) =>
226
- Schema.encodeUnknown(returns)(convexReturns),
227
- ),
228
- Effect.runPromise,
229
- ),
230
- });
231
-
232
- export const mutationLayer = <Schema extends DatabaseSchema.AnyWithProps>(
233
- schema: Schema,
234
- ctx: GenericMutationCtx<DataModel.ToConvex<DataModel.FromSchema<Schema>>>,
235
- ) =>
236
- Layer.mergeAll(
237
- DatabaseReader.layer(schema, ctx.db),
238
- DatabaseWriter.layer(schema, ctx.db),
239
- Auth.layer(ctx.auth),
240
- Scheduler.layer(ctx.scheduler),
241
- StorageReader.layer(ctx.storage),
242
- StorageWriter.layer(ctx.storage),
243
- QueryRunner.layer(ctx.runQuery),
244
- MutationRunner.layer(ctx.runMutation),
245
- Layer.succeed(
246
- MutationCtx.MutationCtx<
247
- DataModel.ToConvex<DataModel.FromSchema<Schema>>
248
- >(),
249
- ctx,
250
- ),
251
- );
252
-
253
- export type MutationServices<Schema extends DatabaseSchema.AnyWithProps> =
254
- | DatabaseReader.DatabaseReader<Schema>
255
- | DatabaseWriter.DatabaseWriter<Schema>
256
- | Auth.Auth
257
- | Scheduler.Scheduler
258
- | StorageReader
259
- | StorageWriter
260
- | QueryRunner.QueryRunner
261
- | MutationRunner.MutationRunner
262
- | MutationCtx.MutationCtx<DataModel.ToConvex<DataModel.FromSchema<Schema>>>;
263
-
264
- const mutationFunction = <
265
- Schema extends DatabaseSchema.AnyWithProps,
266
- Args,
267
- ConvexArgs extends DefaultFunctionArgs,
268
- Returns,
269
- ConvexReturns,
270
- E,
271
- >(
272
- schema: Schema,
273
- {
274
- args,
275
- returns,
276
- handler,
277
- }: {
278
- args: Schema.Schema<Args, ConvexArgs>;
279
- returns: Schema.Schema<Returns, ConvexReturns>;
280
- handler: (a: Args) => Effect.Effect<Returns, E, MutationServices<Schema>>;
281
- },
282
- ) => ({
283
- args: SchemaToValidator.compileArgsSchema(args),
284
- returns: SchemaToValidator.compileReturnsSchema(returns),
285
- handler: (
286
- ctx: GenericMutationCtx<DataModel.ToConvex<DataModel.FromSchema<Schema>>>,
287
- actualArgs: ConvexArgs,
288
- ): Promise<ConvexReturns> =>
289
- pipe(
290
- actualArgs,
291
- Schema.decode(args),
292
- Effect.orDie,
293
- Effect.andThen((decodedArgs) =>
294
- handler(decodedArgs).pipe(Effect.provide(mutationLayer(schema, ctx))),
295
- ),
296
- Effect.andThen((convexReturns) =>
297
- Schema.encodeUnknown(returns)(convexReturns),
298
- ),
299
- Effect.runPromise,
300
- ),
301
- });
302
-
303
- const actionFunction = <
304
- Schema extends DatabaseSchema.AnyWithProps,
305
- Args,
306
- ConvexArgs extends DefaultFunctionArgs,
307
- Returns,
308
- ConvexReturns,
309
- E,
310
- >({
311
- args,
312
- returns,
313
- handler,
314
- }: {
315
- args: Schema.Schema<Args, ConvexArgs>;
316
- returns: Schema.Schema<Returns, ConvexReturns>;
317
- handler: (
318
- a: Args,
319
- ) => Effect.Effect<
320
- Returns,
321
- E,
322
- | Scheduler.Scheduler
323
- | Auth.Auth
324
- | StorageReader
325
- | StorageWriter
326
- | StorageActionWriter
327
- | QueryRunner.QueryRunner
328
- | MutationRunner.MutationRunner
329
- | ActionRunner.ActionRunner
330
- | VectorSearch.VectorSearch<DataModel.FromSchema<Schema>>
331
- | ActionCtx.ActionCtx<DataModel.ToConvex<DataModel.FromSchema<Schema>>>
332
- >;
333
- }) => ({
334
- args: SchemaToValidator.compileArgsSchema(args),
335
- returns: SchemaToValidator.compileReturnsSchema(returns),
336
- handler: (
337
- ctx: GenericActionCtx<DataModel.ToConvex<DataModel.FromSchema<Schema>>>,
338
- actualArgs: ConvexArgs,
339
- ): Promise<ConvexReturns> =>
340
- pipe(
341
- actualArgs,
342
- Schema.decode(args),
343
- Effect.orDie,
344
- Effect.andThen((decodedArgs) =>
345
- pipe(
346
- handler(decodedArgs),
347
- Effect.provide(
348
- Layer.mergeAll(
349
- Scheduler.layer(ctx.scheduler),
350
- Auth.layer(ctx.auth),
351
- StorageReader.layer(ctx.storage),
352
- StorageWriter.layer(ctx.storage),
353
- StorageActionWriter.layer(ctx.storage),
354
- QueryRunner.layer(ctx.runQuery),
355
- MutationRunner.layer(ctx.runMutation),
356
- ActionRunner.layer(ctx.runAction),
357
- VectorSearch.layer(ctx.vectorSearch),
358
- Layer.succeed(
359
- ActionCtx.ActionCtx<
360
- DataModel.ToConvex<DataModel.FromSchema<Schema>>
361
- >(),
362
- ctx,
363
- ),
364
- ),
365
- ),
366
- ),
367
- ),
368
- Effect.andThen((convexReturns) =>
369
- Schema.encodeUnknown(returns)(convexReturns),
370
- ),
371
- Effect.runPromise,
372
- ),
373
- });
@@ -0,0 +1,68 @@
1
+ import { NodeContext } from "@effect/platform-node";
2
+ import {
3
+ actionGeneric,
4
+ type DefaultFunctionArgs,
5
+ internalActionGeneric,
6
+ } from "convex/server";
7
+ import type { Effect } from "effect";
8
+ import { Layer, Match, type Schema } from "effect";
9
+ import type * as Api from "./Api";
10
+ import type * as DatabaseSchema from "./DatabaseSchema";
11
+ import * as RegisteredFunction from "./RegisteredFunction";
12
+ import type * as RegistryItem from "./RegistryItem";
13
+
14
+ export const make = <Api_ extends Api.AnyWithPropsWithRuntime<"Node">>(
15
+ api: Api_,
16
+ { function_, handler }: RegistryItem.AnyWithProps,
17
+ ): RegisteredFunction.RegisteredFunction => {
18
+ const genericFunction = Match.value(function_.functionVisibility).pipe(
19
+ Match.when("public", () => actionGeneric),
20
+ Match.when("internal", () => internalActionGeneric),
21
+ Match.exhaustive,
22
+ );
23
+
24
+ return genericFunction(
25
+ nodeActionFunction(api.databaseSchema, {
26
+ args: function_.args,
27
+ returns: function_.returns,
28
+ handler,
29
+ }),
30
+ );
31
+ };
32
+
33
+ const nodeActionFunction = <
34
+ DatabaseSchema_ extends DatabaseSchema.AnyWithProps,
35
+ Args,
36
+ ConvexArgs extends DefaultFunctionArgs,
37
+ Returns,
38
+ ConvexReturns,
39
+ E,
40
+ >(
41
+ databaseSchema: DatabaseSchema_,
42
+ {
43
+ args,
44
+ returns,
45
+ handler,
46
+ }: {
47
+ args: Schema.Schema<Args, ConvexArgs>;
48
+ returns: Schema.Schema<Returns, ConvexReturns>;
49
+ handler: (
50
+ a: Args,
51
+ ) => Effect.Effect<
52
+ Returns,
53
+ E,
54
+ | RegisteredFunction.ActionServices<DatabaseSchema_>
55
+ | NodeContext.NodeContext
56
+ >;
57
+ },
58
+ ) =>
59
+ RegisteredFunction.actionFunctionBase({
60
+ args,
61
+ returns,
62
+ handler,
63
+ createLayer: (ctx) =>
64
+ Layer.mergeAll(
65
+ RegisteredFunction.actionLayer(databaseSchema, ctx),
66
+ NodeContext.layer,
67
+ ),
68
+ });
package/src/index.ts CHANGED
@@ -18,6 +18,8 @@ export * as OrderedQuery from "./OrderedQuery";
18
18
  export * as QueryCtx from "./QueryCtx";
19
19
  export * as QueryInitializer from "./QueryInitializer";
20
20
  export * as QueryRunner from "./QueryRunner";
21
+ export * as RegisteredConvexFunction from "./RegisteredConvexFunction";
22
+ export * as RegisteredFunction from "./RegisteredFunction";
21
23
  export * as RegisteredFunctions from "./RegisteredFunctions";
22
24
  export * as Registry from "./Registry";
23
25
  export * as RegistryItem from "./RegistryItem";
package/src/node.ts ADDED
@@ -0,0 +1 @@
1
+ export * as RegisteredNodeFunction from "./RegisteredNodeFunction";