@gaganref/confect-server 10.3.1-fork.7 → 10.3.1-fork.70
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/dist/ActionCall.d.ts +30 -0
- package/dist/ActionCall.d.ts.map +1 -0
- package/dist/ActionCall.js +41 -0
- package/dist/ActionCall.js.map +1 -0
- package/dist/ActionRunner.d.ts +1 -1
- package/dist/ActionRunner.d.ts.map +1 -1
- package/dist/ActionRunner.js +3 -3
- package/dist/ActionRunner.js.map +1 -1
- package/dist/AiGatewayDecisionClient.js +1 -1
- package/dist/AiGatewayLanguageClient.js +1 -1
- package/dist/Auth.d.ts +31 -2
- package/dist/Auth.d.ts.map +1 -1
- package/dist/Auth.js +23 -3
- package/dist/Auth.js.map +1 -1
- package/dist/Callback.d.ts +1 -1
- package/dist/Callback.d.ts.map +1 -1
- package/dist/Callback.js +6 -6
- package/dist/Callback.js.map +1 -1
- package/dist/ConvexConfigProvider.js +2 -2
- package/dist/ConvexFunctionDefinitions.d.ts.map +1 -1
- package/dist/ConvexFunctionDefinitions.js +4 -4
- package/dist/ConvexFunctionDefinitions.js.map +1 -1
- package/dist/ConvexLogger.js +1 -1
- package/dist/CronJobs.d.ts.map +1 -1
- package/dist/CronJobs.js +7 -6
- package/dist/CronJobs.js.map +1 -1
- package/dist/DatabaseSchema.js +2 -2
- package/dist/DatabaseWriter.d.ts +36 -2
- package/dist/DatabaseWriter.d.ts.map +1 -1
- package/dist/DatabaseWriter.js +55 -8
- package/dist/DatabaseWriter.js.map +1 -1
- package/dist/ExecutionMetadata.js +1 -1
- package/dist/FunctionImpl.js +2 -2
- package/dist/FunctionRegistryItem.d.ts +1 -0
- package/dist/FunctionRegistryItem.d.ts.map +1 -1
- package/dist/FunctionRegistryItem.js +2 -1
- package/dist/FunctionRegistryItem.js.map +1 -1
- package/dist/GroupImpl.d.ts +21 -2
- package/dist/GroupImpl.d.ts.map +1 -1
- package/dist/GroupImpl.js +21 -22
- package/dist/GroupImpl.js.map +1 -1
- package/dist/GroupRegistry.d.ts +1 -0
- package/dist/GroupRegistry.d.ts.map +1 -1
- package/dist/GroupRegistry.js +10 -3
- package/dist/GroupRegistry.js.map +1 -1
- package/dist/Handler.d.ts +8 -4
- package/dist/Handler.d.ts.map +1 -1
- package/dist/Handler.js.map +1 -1
- package/dist/HttpRouter.d.ts +2 -0
- package/dist/HttpRouter.d.ts.map +1 -1
- package/dist/HttpRouter.js +17 -8
- package/dist/HttpRouter.js.map +1 -1
- package/dist/MiddlewareImpl.js +1 -1
- package/dist/MutationRunner.d.ts +1 -1
- package/dist/MutationRunner.d.ts.map +1 -1
- package/dist/MutationRunner.js +3 -3
- package/dist/MutationRunner.js.map +1 -1
- package/dist/MutationTransactionContext.d.ts.map +1 -1
- package/dist/MutationTransactionContext.js +4 -3
- package/dist/MutationTransactionContext.js.map +1 -1
- package/dist/OfficialComponent.d.ts +60 -0
- package/dist/OfficialComponent.d.ts.map +1 -0
- package/dist/OfficialComponent.js +72 -0
- package/dist/OfficialComponent.js.map +1 -0
- package/dist/OrderedQuery.d.ts +4 -5
- package/dist/OrderedQuery.d.ts.map +1 -1
- package/dist/OrderedQuery.js.map +1 -1
- package/dist/QueryInitializer.js +2 -2
- package/dist/QueryRunner.js +2 -2
- package/dist/QueryStream.d.ts +120 -12
- package/dist/QueryStream.d.ts.map +1 -1
- package/dist/QueryStream.js +143 -41
- package/dist/QueryStream.js.map +1 -1
- package/dist/QueryStreamCursor.js +1 -1
- package/dist/QueryStreamIndexPrefix.js +1 -1
- package/dist/QueryStreamIndexRange.js +2 -2
- package/dist/QueryStreamKey.js +1 -1
- package/dist/QueryStreamKeyBounds.js +2 -2
- package/dist/QueryStreamKeyLayout.js +2 -2
- package/dist/QueryStreamKeyValues.js +2 -2
- package/dist/QueryStreamPagination.js +1 -1
- package/dist/QueryStreamReadBudget.d.ts +3 -2
- package/dist/QueryStreamReadBudget.d.ts.map +1 -1
- package/dist/QueryStreamReadBudget.js +8 -5
- package/dist/QueryStreamReadBudget.js.map +1 -1
- package/dist/QueryTransactionContext.js +2 -2
- package/dist/RegisteredConvexFunction.d.ts +10 -5
- package/dist/RegisteredConvexFunction.d.ts.map +1 -1
- package/dist/RegisteredConvexFunction.js +45 -29
- package/dist/RegisteredConvexFunction.js.map +1 -1
- package/dist/RegisteredFunction.d.ts +39 -22
- package/dist/RegisteredFunction.d.ts.map +1 -1
- package/dist/RegisteredFunction.js +155 -42
- package/dist/RegisteredFunction.js.map +1 -1
- package/dist/RegisteredNodeFunction.d.ts +1 -1
- package/dist/RegisteredNodeFunction.d.ts.map +1 -1
- package/dist/RegisteredNodeFunction.js +10 -6
- package/dist/RegisteredNodeFunction.js.map +1 -1
- package/dist/RegistryItems.d.ts +7 -0
- package/dist/RegistryItems.d.ts.map +1 -1
- package/dist/RegistryItems.js +8 -3
- package/dist/RegistryItems.js.map +1 -1
- package/dist/RequestMetadata.js +1 -1
- package/dist/Scheduler.d.ts +1 -1
- package/dist/Scheduler.d.ts.map +1 -1
- package/dist/Scheduler.js +14 -13
- package/dist/Scheduler.js.map +1 -1
- package/dist/Storage.js +1 -1
- package/dist/StorageActionWriter.js +1 -1
- package/dist/StorageReader.js +1 -1
- package/dist/StorageWriter.js +1 -1
- package/dist/TransactionMetadata.js +1 -1
- package/dist/VectorSearch.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -18
- package/dist/internal/AiGatewayDecisionClient.js +1 -1
- package/dist/internal/AiGatewayLanguageClient.js +1 -1
- package/dist/internal/AiGatewayServiceToken.js +1 -1
- package/dist/internal/DocumentPatch.d.ts +16 -0
- package/dist/internal/DocumentPatch.d.ts.map +1 -0
- package/dist/internal/DocumentPatch.js +36 -0
- package/dist/internal/DocumentPatch.js.map +1 -0
- package/dist/internal/observability.d.ts +5 -0
- package/dist/internal/observability.d.ts.map +1 -0
- package/dist/internal/observability.js +21 -0
- package/dist/internal/observability.js.map +1 -0
- package/dist/internal/reservedArgument.d.ts +8 -0
- package/dist/internal/reservedArgument.d.ts.map +1 -0
- package/dist/internal/reservedArgument.js +30 -0
- package/dist/internal/reservedArgument.js.map +1 -0
- package/package.json +5 -5
- package/src/ActionCall.ts +83 -0
- package/src/ActionRunner.ts +3 -2
- package/src/Auth.ts +49 -1
- package/src/Callback.ts +3 -2
- package/src/ConvexFunctionDefinitions.ts +2 -1
- package/src/CronJobs.ts +2 -1
- package/src/DatabaseWriter.ts +183 -25
- package/src/FunctionRegistryItem.ts +4 -0
- package/src/GroupImpl.ts +61 -8
- package/src/GroupRegistry.ts +7 -3
- package/src/Handler.ts +21 -5
- package/src/HttpRouter.ts +54 -4
- package/src/MutationRunner.ts +3 -2
- package/src/MutationTransactionContext.ts +2 -1
- package/src/OfficialComponent.ts +168 -0
- package/src/OrderedQuery.ts +4 -5
- package/src/QueryStream.ts +391 -61
- package/src/QueryStreamReadBudget.ts +19 -6
- package/src/RegisteredConvexFunction.ts +35 -2
- package/src/RegisteredFunction.ts +251 -45
- package/src/RegisteredNodeFunction.ts +11 -1
- package/src/RegistryItems.ts +31 -0
- package/src/Scheduler.ts +17 -19
- package/src/index.ts +2 -0
- package/src/internal/DocumentPatch.ts +87 -0
- package/src/internal/observability.ts +27 -0
- package/src/internal/reservedArgument.ts +56 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as ReservedArgument from "./internal/reservedArgument.js";
|
|
2
|
+
import * as ActionCall from "@confect/core/ActionCall";
|
|
3
|
+
import type * as Ref from "@confect/core/Ref";
|
|
4
|
+
import type { FunctionReference } from "convex/server";
|
|
5
|
+
import * as Context from "effect/Context";
|
|
6
|
+
import * as Data from "effect/Data";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as ActionCtx from "./ActionCtx.js";
|
|
9
|
+
import * as ActionRunner from "./ActionRunner.js";
|
|
10
|
+
import type * as DatabaseSchema from "./DatabaseSchema.js";
|
|
11
|
+
import type * as DataModel from "./DataModel.js";
|
|
12
|
+
import type * as Handler from "./Handler.js";
|
|
13
|
+
import * as MiddlewareImpl from "./MiddlewareImpl.js";
|
|
14
|
+
import * as MutationRunner from "./MutationRunner.js";
|
|
15
|
+
import * as QueryRunner from "./QueryRunner.js";
|
|
16
|
+
import * as Scheduler from "./Scheduler.js";
|
|
17
|
+
|
|
18
|
+
export class ActionCallAlreadyUsed extends Data.TaggedError(
|
|
19
|
+
"ActionCallAlreadyUsed",
|
|
20
|
+
)<{}> {}
|
|
21
|
+
export class ActionCallNotUsed extends Data.TaggedError(
|
|
22
|
+
"ActionCallNotUsed",
|
|
23
|
+
)<{}> {}
|
|
24
|
+
export class MissingFunctionType extends Data.TaggedError(
|
|
25
|
+
"MissingFunctionType",
|
|
26
|
+
)<{}> {}
|
|
27
|
+
export class NestedActionNotAllowed extends Data.TaggedError(
|
|
28
|
+
"NestedActionNotAllowed",
|
|
29
|
+
)<{}> {}
|
|
30
|
+
|
|
31
|
+
export class SchedulingNotAllowed extends Data.TaggedError(
|
|
32
|
+
"SchedulingNotAllowed",
|
|
33
|
+
)<{}> {}
|
|
34
|
+
|
|
35
|
+
export const layer = <S extends DatabaseSchema.AnyWithProps>(schema: S) =>
|
|
36
|
+
MiddlewareImpl.make(schema, ActionCall.Middleware, (effect) =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
const ctx = yield* ActionCtx.ActionCtx<
|
|
39
|
+
DataModel.ToConvex<DataModel.FromSchema<S>>,
|
|
40
|
+
DatabaseSchema.Scope<S>
|
|
41
|
+
>();
|
|
42
|
+
let claimed = false;
|
|
43
|
+
const call: ActionCall.Call = <
|
|
44
|
+
Target extends Ref.AnyQuery | Ref.AnyMutation,
|
|
45
|
+
>(
|
|
46
|
+
ref: Target,
|
|
47
|
+
...args: Ref.OptionalArgs<NoInfer<Target>>
|
|
48
|
+
) =>
|
|
49
|
+
Effect.suspend(() => {
|
|
50
|
+
if (claimed) return Effect.die(new ActionCallAlreadyUsed());
|
|
51
|
+
claimed = true;
|
|
52
|
+
if (ref.functionType !== "query" && ref.functionType !== "mutation")
|
|
53
|
+
return Effect.die(new MissingFunctionType());
|
|
54
|
+
return ReservedArgument.runWithCodec(
|
|
55
|
+
ref,
|
|
56
|
+
(args[0] ?? {}) as Ref.Args<Target>,
|
|
57
|
+
(native, encoded) =>
|
|
58
|
+
ref.functionType === "query"
|
|
59
|
+
? ctx.runQuery(
|
|
60
|
+
native as FunctionReference<"query">,
|
|
61
|
+
encoded as never,
|
|
62
|
+
)
|
|
63
|
+
: ctx.runMutation(
|
|
64
|
+
native as FunctionReference<"mutation">,
|
|
65
|
+
encoded as never,
|
|
66
|
+
),
|
|
67
|
+
);
|
|
68
|
+
}).pipe(Effect.withSpan("ActionCall.call"));
|
|
69
|
+
return yield* effect.pipe(
|
|
70
|
+
Effect.provideService(ActionCall.ActionCall, call),
|
|
71
|
+
Effect.updateContext(
|
|
72
|
+
(context: Context.Context<Handler.ActionServices<S>>) =>
|
|
73
|
+
Context.omit(
|
|
74
|
+
QueryRunner.QueryRunner,
|
|
75
|
+
MutationRunner.MutationRunner,
|
|
76
|
+
ActionRunner.ActionRunner,
|
|
77
|
+
ActionCtx.ActionCtx(),
|
|
78
|
+
Scheduler.Scheduler,
|
|
79
|
+
)(context),
|
|
80
|
+
),
|
|
81
|
+
);
|
|
82
|
+
}),
|
|
83
|
+
);
|
package/src/ActionRunner.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as ReservedArgument from "./internal/reservedArgument.js";
|
|
2
|
+
import type * as Ref from "@confect/core/Ref";
|
|
2
3
|
import { type GenericActionCtx } from "convex/server";
|
|
3
4
|
import * as Effect from "effect/Effect";
|
|
4
5
|
import * as Context from "effect/Context";
|
|
@@ -18,7 +19,7 @@ const make = (runAction: GenericActionCtx<any>["runAction"]) => ({
|
|
|
18
19
|
Ref.Error<Action> | Schema.SchemaError
|
|
19
20
|
> =>
|
|
20
21
|
run(
|
|
21
|
-
|
|
22
|
+
ReservedArgument.runWithCodec(
|
|
22
23
|
action,
|
|
23
24
|
(args[0] ?? {}) as Ref.Args<Action>,
|
|
24
25
|
(functionReference, encodedArgs) =>
|
package/src/Auth.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Auth as ConvexAuth,
|
|
3
|
+
MutationAuth as ConvexMutationAuth,
|
|
4
|
+
SignJwtOptions,
|
|
5
|
+
UserIdentity,
|
|
6
|
+
} from "convex/server";
|
|
2
7
|
import * as Context from "effect/Context";
|
|
3
8
|
import { flow } from "effect/Function";
|
|
4
9
|
import * as Effect from "effect/Effect";
|
|
@@ -26,8 +31,51 @@ export class Auth extends Context.Service<Auth, ReturnType<typeof make>>()(
|
|
|
26
31
|
"@confect/server/Auth",
|
|
27
32
|
) {}
|
|
28
33
|
|
|
34
|
+
/**
|
|
35
|
+
* The outcome of Convex's identity verification for the current request.
|
|
36
|
+
*/
|
|
37
|
+
export type Identity =
|
|
38
|
+
| { readonly _tag: "Absent" }
|
|
39
|
+
| { readonly _tag: "Valid"; readonly identity: UserIdentity }
|
|
40
|
+
| { readonly _tag: "Invalid" };
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Read HTTP identity without failing for missing or rejected credentials.
|
|
44
|
+
* Convex verifies the credential. Rejected reads expose no token or error
|
|
45
|
+
* detail.
|
|
46
|
+
*/
|
|
47
|
+
export const getUserIdentity: Effect.Effect<Identity, never, Auth> = Auth.pipe(
|
|
48
|
+
Effect.flatMap((auth) =>
|
|
49
|
+
auth.getUserIdentity.pipe(
|
|
50
|
+
Effect.map((identity): Identity => ({ _tag: "Valid", identity })),
|
|
51
|
+
Effect.catchTag("NoUserIdentityFoundError", () =>
|
|
52
|
+
Effect.succeed<Identity>({ _tag: "Absent" }),
|
|
53
|
+
),
|
|
54
|
+
Effect.catchDefect(() => Effect.succeed<Identity>({ _tag: "Invalid" })),
|
|
55
|
+
),
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
|
|
29
59
|
export const layer = (auth: ConvexAuth) => Layer.succeed(Auth, make(auth));
|
|
30
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Mutation-only deployment signing, available in applications and components.
|
|
63
|
+
*/
|
|
64
|
+
export class MutationAuth extends Context.Service<
|
|
65
|
+
MutationAuth,
|
|
66
|
+
{
|
|
67
|
+
readonly signJwt: (options: SignJwtOptions) => Effect.Effect<string>;
|
|
68
|
+
}
|
|
69
|
+
>()("@confect/server/Auth/MutationAuth") {}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* SDK rejections remain defects, as with the other native auth operations.
|
|
73
|
+
*/
|
|
74
|
+
export const mutationLayer = (auth: ConvexMutationAuth) =>
|
|
75
|
+
Layer.succeed(MutationAuth, {
|
|
76
|
+
signJwt: (options) => Effect.promise(() => auth.signJwt(options)),
|
|
77
|
+
});
|
|
78
|
+
|
|
31
79
|
export type ForTarget<Target extends DatabaseSchema.Target> =
|
|
32
80
|
Target["kind"] extends "component" ? never : Auth;
|
|
33
81
|
|
package/src/Callback.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as Callback from "@confect/core/Callback";
|
|
2
2
|
import * as MiddlewareSpec from "@confect/core/MiddlewareSpec";
|
|
3
|
-
import * as Ref from "@confect/core/Ref";
|
|
3
|
+
import type * as Ref from "@confect/core/Ref";
|
|
4
|
+
import { getFunctionReference } from "@confect/core/RefInternal";
|
|
4
5
|
import {
|
|
5
6
|
compileAst,
|
|
6
7
|
UnsupportedPropertySignatureKeyTypeError,
|
|
@@ -305,7 +306,7 @@ export const create = Effect.fn("Callback.create")(function* <
|
|
|
305
306
|
yield* Effect.fromResult(verify(contract, ref));
|
|
306
307
|
}
|
|
307
308
|
const text = yield* Effect.promise(() =>
|
|
308
|
-
createFunctionHandle(
|
|
309
|
+
createFunctionHandle(getFunctionReference(ref)),
|
|
309
310
|
);
|
|
310
311
|
return yield* Schema.decodeEffect(Callback.handle(contract))(text).pipe(
|
|
311
312
|
Effect.orDie,
|
|
@@ -63,7 +63,7 @@ export const buildForGroup = <Group extends GroupSpec.AnyWithProps>(
|
|
|
63
63
|
databaseSchema: DatabaseSchema.AnyWithProps,
|
|
64
64
|
groupLayer: Layer.Layer<GroupImpl.GroupImpl<"Finalized">>,
|
|
65
65
|
): ForGroup<Group> => {
|
|
66
|
-
const { functionRegistryItems, middlewareRegistryItems } =
|
|
66
|
+
const { functionRegistryItems, middlewareRegistryItems, observability } =
|
|
67
67
|
GroupRegistry.make(groupLayer);
|
|
68
68
|
return mapLeaves<
|
|
69
69
|
FunctionRegistryItem.AnyWithProps,
|
|
@@ -83,6 +83,7 @@ export const buildForGroup = <Group extends GroupSpec.AnyWithProps>(
|
|
|
83
83
|
databaseSchema,
|
|
84
84
|
confect,
|
|
85
85
|
ResolvedMiddleware.resolve(confect, middlewareRegistryItems),
|
|
86
|
+
observability,
|
|
86
87
|
),
|
|
87
88
|
),
|
|
88
89
|
Match.exhaustive,
|
package/src/CronJobs.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Ref } from "@confect/core";
|
|
2
|
+
import { getFunctionReference } from "@confect/core/RefInternal";
|
|
2
3
|
import type { CronJob as ConvexCronJob } from "convex/server";
|
|
3
4
|
import { cronJobs as makeConvexCrons, type Crons } from "convex/server";
|
|
4
5
|
import { pipe } from "effect/Function";
|
|
@@ -35,7 +36,7 @@ const Proto = {
|
|
|
35
36
|
crons: { ...this.convexCronJobs.crons },
|
|
36
37
|
});
|
|
37
38
|
|
|
38
|
-
const functionReference =
|
|
39
|
+
const functionReference = getFunctionReference(cronJob.ref);
|
|
39
40
|
const encodedArgs = Ref.encodeArgsSync(cronJob.ref, cronJob.args);
|
|
40
41
|
|
|
41
42
|
Match.value(cronJob.schedule).pipe(
|
package/src/DatabaseWriter.ts
CHANGED
|
@@ -10,11 +10,14 @@ import { pipe } from "effect/Function";
|
|
|
10
10
|
import * as Context from "effect/Context";
|
|
11
11
|
import * as Effect from "effect/Effect";
|
|
12
12
|
import * as Layer from "effect/Layer";
|
|
13
|
+
import * as Predicate from "effect/Predicate";
|
|
13
14
|
import * as Record from "effect/Record";
|
|
15
|
+
import * as Result from "effect/Result";
|
|
14
16
|
import * as DatabaseSchema from "./DatabaseSchema.js";
|
|
15
17
|
import type * as DataModel from "./DataModel.js";
|
|
16
18
|
import type { DocumentByName as DocumentByName_ } from "./DataModel.js";
|
|
17
19
|
import * as Document from "./Document.js";
|
|
20
|
+
import * as DocumentPatch from "./internal/DocumentPatch.js";
|
|
18
21
|
import * as QueryInitializer from "./QueryInitializer.js";
|
|
19
22
|
import type * as Table from "./Table.js";
|
|
20
23
|
|
|
@@ -97,11 +100,54 @@ export type DatabaseWriterTag<
|
|
|
97
100
|
DatabaseWriterService<DatabaseSchema_, Docs>
|
|
98
101
|
>;
|
|
99
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Decoded documents surrounding one successful write.
|
|
105
|
+
*/
|
|
106
|
+
export type Change<S extends DatabaseSchema.AnyWithProps> = {
|
|
107
|
+
[Name in DataModel.TableNames<DataModel.FromSchema<S>>]: {
|
|
108
|
+
readonly tableName: Name;
|
|
109
|
+
} & (
|
|
110
|
+
| {
|
|
111
|
+
readonly operation: "insert";
|
|
112
|
+
readonly oldDoc: null;
|
|
113
|
+
readonly newDoc: DocumentByName_<DataModel.FromSchema<S>, Name>;
|
|
114
|
+
}
|
|
115
|
+
| {
|
|
116
|
+
readonly operation: "update";
|
|
117
|
+
readonly oldDoc: DocumentByName_<DataModel.FromSchema<S>, Name>;
|
|
118
|
+
readonly newDoc: DocumentByName_<DataModel.FromSchema<S>, Name>;
|
|
119
|
+
}
|
|
120
|
+
| {
|
|
121
|
+
readonly operation: "delete";
|
|
122
|
+
readonly oldDoc: DocumentByName_<DataModel.FromSchema<S>, Name>;
|
|
123
|
+
readonly newDoc: null;
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
}[DataModel.TableNames<DataModel.FromSchema<S>>];
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Runs after the write, in its transaction. A defect aborts the mutation.
|
|
130
|
+
*
|
|
131
|
+
* Only writes through this writer are observed. Raw `ctx.db`, `MutationCtx`
|
|
132
|
+
* writes and a second `DatabaseWriter.make` bypass the hook, so a host that
|
|
133
|
+
* relies on it keeps raw writers out of its functions. Component writers and
|
|
134
|
+
* nested `runMutation` calls each need their own hook installation.
|
|
135
|
+
*
|
|
136
|
+
* Concurrent writes to one document (for example under `Effect.all`) can
|
|
137
|
+
* interleave the read-write-read steps. Await them sequentially when the hook
|
|
138
|
+
* needs exact per-write snapshots. The writer takes no lock, because a hook
|
|
139
|
+
* that writes through the same writer would deadlock under one.
|
|
140
|
+
*/
|
|
141
|
+
export type Hook<S extends DatabaseSchema.AnyWithProps> = (
|
|
142
|
+
change: Change<S>,
|
|
143
|
+
) => Effect.Effect<void>;
|
|
144
|
+
|
|
100
145
|
export const make = <DatabaseSchema_ extends DatabaseSchema.AnyWithProps>(
|
|
101
146
|
databaseSchema: DatabaseSchema_,
|
|
102
147
|
convexDatabaseWriter: GenericDatabaseWriter<
|
|
103
148
|
DataModel.ToConvex<DataModel.FromSchema<DatabaseSchema_>>
|
|
104
149
|
>,
|
|
150
|
+
hook?: Hook<DatabaseSchema_>,
|
|
105
151
|
): DatabaseWriterService<DatabaseSchema_> => {
|
|
106
152
|
type DataModel_ = DataModel.FromSchema<DatabaseSchema_>;
|
|
107
153
|
|
|
@@ -131,38 +177,108 @@ export const make = <DatabaseSchema_ extends DatabaseSchema.AnyWithProps>(
|
|
|
131
177
|
return id as unknown as DataModel.Id<DataModel_, TableName>;
|
|
132
178
|
});
|
|
133
179
|
|
|
180
|
+
// A codec whose encoding spans fields cannot encode a patch on its own, so
|
|
181
|
+
// the patch is merged into the decoded document and the result replaces it.
|
|
182
|
+
const replacePatched = (
|
|
183
|
+
id: DataModel.Id<DataModel_, TableName>,
|
|
184
|
+
patchedValues: PatchValue<
|
|
185
|
+
Document.WithoutSystemFields<DocumentByName_<DataModel_, TableName>>
|
|
186
|
+
>,
|
|
187
|
+
) =>
|
|
188
|
+
Effect.gen(function* () {
|
|
189
|
+
const originalDecodedDoc = yield* QueryInitializer.getById<
|
|
190
|
+
Table.AnyWithProps,
|
|
191
|
+
TableName
|
|
192
|
+
>(
|
|
193
|
+
convexDatabaseWriter as any,
|
|
194
|
+
tableDef,
|
|
195
|
+
)(id as unknown as GenericId<TableName>);
|
|
196
|
+
|
|
197
|
+
const updatedEncodedDoc = yield* pipe(
|
|
198
|
+
patchedValues,
|
|
199
|
+
Record.reduce(originalDecodedDoc, (acc, value, key) =>
|
|
200
|
+
value === undefined
|
|
201
|
+
? Record.remove(acc, key)
|
|
202
|
+
: Record.set(acc, key, value),
|
|
203
|
+
),
|
|
204
|
+
Document.encode(tableDef),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
yield* Effect.promise(() =>
|
|
208
|
+
convexDatabaseWriter.replace(
|
|
209
|
+
id as unknown as GenericId<TableName>,
|
|
210
|
+
updatedEncodedDoc as Expand<
|
|
211
|
+
BetterOmit<
|
|
212
|
+
DocumentByName<DataModel.ToConvex<DataModel_>, TableName>,
|
|
213
|
+
"_creationTime" | "_id"
|
|
214
|
+
>
|
|
215
|
+
>,
|
|
216
|
+
),
|
|
217
|
+
);
|
|
218
|
+
});
|
|
219
|
+
|
|
134
220
|
const patch = Effect.fn("DatabaseWriter.patch")(function* (
|
|
135
221
|
id: DataModel.Id<DataModel_, TableName>,
|
|
136
222
|
patchedValues: PatchValue<
|
|
137
223
|
Document.WithoutSystemFields<DocumentByName_<DataModel_, TableName>>
|
|
138
224
|
>,
|
|
139
225
|
) {
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
convexDatabaseWriter as any,
|
|
145
|
-
tableDef,
|
|
146
|
-
)(id as unknown as GenericId<TableName>);
|
|
226
|
+
const encodePatch = DocumentPatch.encoder(tableDef);
|
|
227
|
+
if (encodePatch === undefined) {
|
|
228
|
+
return yield* replacePatched(id, patchedValues);
|
|
229
|
+
}
|
|
147
230
|
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
231
|
+
const storedDoc = yield* pipe(
|
|
232
|
+
Effect.promise(() =>
|
|
233
|
+
convexDatabaseWriter.get(id as unknown as GenericId<TableName>),
|
|
234
|
+
),
|
|
235
|
+
Effect.andThen((value) =>
|
|
236
|
+
Effect.fromResult(
|
|
237
|
+
Result.fromNullishOr(
|
|
238
|
+
value,
|
|
239
|
+
() =>
|
|
240
|
+
new QueryInitializer.GetByIdFailure({
|
|
241
|
+
tableName,
|
|
242
|
+
id,
|
|
243
|
+
}),
|
|
244
|
+
),
|
|
245
|
+
),
|
|
246
|
+
),
|
|
247
|
+
);
|
|
248
|
+
// The patch must encode and the patched document must decode. When
|
|
249
|
+
// either fails, the stored document decides whose fault that is.
|
|
250
|
+
const encodedPatch = yield* pipe(
|
|
251
|
+
encodePatch(patchedValues),
|
|
252
|
+
Effect.mapError(({ message }) => message),
|
|
253
|
+
Effect.tap((delta) =>
|
|
254
|
+
pipe(
|
|
255
|
+
{ ...storedDoc, ...delta },
|
|
256
|
+
Record.filter(Predicate.isNotUndefined),
|
|
257
|
+
Document.decode(tableDef),
|
|
258
|
+
Effect.mapError(({ parseError }) => parseError),
|
|
259
|
+
),
|
|
260
|
+
),
|
|
261
|
+
Effect.catch((parseError) =>
|
|
262
|
+
pipe(
|
|
263
|
+
Document.decode(storedDoc, tableDef),
|
|
264
|
+
Effect.andThen(
|
|
265
|
+
Effect.fail(
|
|
266
|
+
new Document.DocumentEncodeError({
|
|
267
|
+
tableName,
|
|
268
|
+
id,
|
|
269
|
+
parseError,
|
|
270
|
+
}),
|
|
271
|
+
),
|
|
272
|
+
),
|
|
273
|
+
),
|
|
154
274
|
),
|
|
155
|
-
Document.encode(tableDef),
|
|
156
275
|
);
|
|
157
276
|
|
|
158
277
|
yield* Effect.promise(() =>
|
|
159
|
-
convexDatabaseWriter.
|
|
278
|
+
convexDatabaseWriter.patch(
|
|
160
279
|
id as unknown as GenericId<TableName>,
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
DocumentByName<DataModel.ToConvex<DataModel_>, TableName>,
|
|
164
|
-
"_creationTime" | "_id"
|
|
165
|
-
>
|
|
280
|
+
encodedPatch as Partial<
|
|
281
|
+
DocumentByName<DataModel.ToConvex<DataModel_>, TableName>
|
|
166
282
|
>,
|
|
167
283
|
),
|
|
168
284
|
);
|
|
@@ -194,11 +310,52 @@ export const make = <DatabaseSchema_ extends DatabaseSchema.AnyWithProps>(
|
|
|
194
310
|
convexDatabaseWriter.delete(id as unknown as GenericId<TableName>),
|
|
195
311
|
);
|
|
196
312
|
|
|
313
|
+
const accessor = { insert, patch, replace, delete: delete_ };
|
|
314
|
+
if (hook === undefined) return accessor;
|
|
315
|
+
|
|
316
|
+
const read = (id: DataModel.Id<DataModel_, TableName>) =>
|
|
317
|
+
QueryInitializer.getById<Table.AnyWithProps, TableName>(
|
|
318
|
+
convexDatabaseWriter as any,
|
|
319
|
+
tableDef,
|
|
320
|
+
)(id as unknown as GenericId<TableName>);
|
|
321
|
+
const notify = (
|
|
322
|
+
operation: "insert" | "update" | "delete",
|
|
323
|
+
oldDoc: unknown,
|
|
324
|
+
newDoc: unknown,
|
|
325
|
+
) =>
|
|
326
|
+
hook({ tableName, operation, oldDoc, newDoc } as Change<DatabaseSchema_>);
|
|
327
|
+
const update =
|
|
328
|
+
<A extends ReadonlyArray<unknown>, E, BeforeError>(
|
|
329
|
+
before: (
|
|
330
|
+
id: DataModel.Id<DataModel_, TableName>,
|
|
331
|
+
) => Effect.Effect<unknown, BeforeError>,
|
|
332
|
+
write: (
|
|
333
|
+
id: DataModel.Id<DataModel_, TableName>,
|
|
334
|
+
...args: A
|
|
335
|
+
) => Effect.Effect<void, E>,
|
|
336
|
+
) =>
|
|
337
|
+
(id: DataModel.Id<DataModel_, TableName>, ...args: A) =>
|
|
338
|
+
Effect.gen(function* () {
|
|
339
|
+
const oldDoc = yield* before(id);
|
|
340
|
+
yield* write(id, ...args);
|
|
341
|
+
yield* notify("update", oldDoc, yield* read(id).pipe(Effect.orDie));
|
|
342
|
+
});
|
|
343
|
+
|
|
197
344
|
return {
|
|
198
|
-
insert
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
345
|
+
insert: (document: Parameters<typeof insert>[0]) =>
|
|
346
|
+
Effect.gen(function* () {
|
|
347
|
+
const id = yield* insert(document);
|
|
348
|
+
yield* notify("insert", null, yield* read(id).pipe(Effect.orDie));
|
|
349
|
+
return id;
|
|
350
|
+
}),
|
|
351
|
+
patch: update(read, patch),
|
|
352
|
+
replace: update((id) => read(id).pipe(Effect.orDie), replace),
|
|
353
|
+
delete: (id: DataModel.Id<DataModel_, TableName>) =>
|
|
354
|
+
Effect.gen(function* () {
|
|
355
|
+
const oldDoc = yield* read(id).pipe(Effect.orDie);
|
|
356
|
+
yield* delete_(id);
|
|
357
|
+
yield* notify("delete", oldDoc, null);
|
|
358
|
+
}),
|
|
202
359
|
};
|
|
203
360
|
};
|
|
204
361
|
|
|
@@ -225,8 +382,9 @@ export const layer = <DatabaseSchema_ extends DatabaseSchema.AnyWithProps>(
|
|
|
225
382
|
convexDatabaseWriter: GenericDatabaseWriter<
|
|
226
383
|
DataModel.ToConvex<DataModel.FromSchema<DatabaseSchema_>>
|
|
227
384
|
>,
|
|
385
|
+
hook?: Hook<DatabaseSchema_>,
|
|
228
386
|
) =>
|
|
229
387
|
Layer.succeed(
|
|
230
388
|
DatabaseWriter<DatabaseSchema_>(),
|
|
231
|
-
make(databaseSchema, convexDatabaseWriter),
|
|
389
|
+
make(databaseSchema, convexDatabaseWriter, hook),
|
|
232
390
|
);
|
|
@@ -29,6 +29,7 @@ export interface ConfectFunctionRegistryItem {
|
|
|
29
29
|
readonly [TypeId]: TypeId;
|
|
30
30
|
readonly _tag: "Confect";
|
|
31
31
|
readonly name: string;
|
|
32
|
+
readonly reservedArgument?: string;
|
|
32
33
|
readonly functionVisibility: FunctionVisibility;
|
|
33
34
|
readonly functionType: FunctionType;
|
|
34
35
|
readonly args: Schema.Codec<any, any>;
|
|
@@ -64,6 +65,9 @@ export const make = ({
|
|
|
64
65
|
const item = Object.assign(Object.create(FunctionRegistryItemProto), {
|
|
65
66
|
_tag: "Confect" as const,
|
|
66
67
|
name: functionSpec.name,
|
|
68
|
+
...(functionSpec.reservedArgument === undefined
|
|
69
|
+
? {}
|
|
70
|
+
: { reservedArgument: functionSpec.reservedArgument }),
|
|
67
71
|
functionVisibility: functionSpec.functionVisibility,
|
|
68
72
|
functionType: functionSpec.runtimeAndFunctionType.functionType,
|
|
69
73
|
middlewareAttachments: [
|
package/src/GroupImpl.ts
CHANGED
|
@@ -5,6 +5,7 @@ import * as RegistryItems from "./RegistryItems.js";
|
|
|
5
5
|
import * as Array from "effect/Array";
|
|
6
6
|
import * as Context from "effect/Context";
|
|
7
7
|
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as Data from "effect/Data";
|
|
8
9
|
import * as Layer from "effect/Layer";
|
|
9
10
|
import * as Option from "effect/Option";
|
|
10
11
|
import * as Predicate from "effect/Predicate";
|
|
@@ -111,10 +112,20 @@ export const make = <
|
|
|
111
112
|
FunctionImpl.FromGroupSpec<Group> | MiddlewareImpl.FromGroupSpec<Group>
|
|
112
113
|
>;
|
|
113
114
|
|
|
114
|
-
const
|
|
115
|
-
context
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
const findGroupImpl = <S>(context: Context.Context<S>): Option.Option<Any> =>
|
|
116
|
+
Array.findFirst(context.mapUnsafe.values(), isGroupImpl);
|
|
117
|
+
|
|
118
|
+
export class MissingRequiredMiddlewareError extends Data.TaggedError(
|
|
119
|
+
"MissingRequiredMiddlewareError",
|
|
120
|
+
)<{
|
|
121
|
+
readonly missing: ReadonlyArray<RegistryItems.MissingMiddleware>;
|
|
122
|
+
}> {
|
|
123
|
+
override get message(): string {
|
|
124
|
+
return `GroupImpl.finalize: ${this.missing.map(({ functionName, middlewareKey }) => `${functionName} requires middleware "${middlewareKey}"`).join("\n")}`;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
type Finalizable = Layer.Layer<AnyUnfinalized> | Layer.Layer<AnyFinalized>;
|
|
118
129
|
|
|
119
130
|
/**
|
|
120
131
|
* Mark a `GroupImpl` layer as fully implemented. The parameter type defaults
|
|
@@ -127,20 +138,52 @@ const findUnfinalizedGroupImpl = <S>(
|
|
|
127
138
|
* registered into this group's scope are snapshotted onto the produced service
|
|
128
139
|
* value's `registeredFunctionNames` field, so consumers can verify impl
|
|
129
140
|
* completeness against a `GroupSpec`'s expected functions without having to
|
|
130
|
-
* inspect the `Registry` themselves.
|
|
141
|
+
* inspect the `Registry` themselves. Pass `{ required }` to check every
|
|
142
|
+
* registered function's middleware at layer build, including internal
|
|
143
|
+
* functions. This form also accepts a finalized layer so generated registration
|
|
144
|
+
* can enforce the backend declaration without changing authored impls. Pass
|
|
145
|
+
* `groupPath` to qualify uncovered function names in the diagnostic.
|
|
131
146
|
*/
|
|
132
|
-
export
|
|
147
|
+
export function finalize(options: {
|
|
148
|
+
readonly groupPath?: string;
|
|
149
|
+
readonly required: ReadonlyArray<
|
|
150
|
+
Pick<MiddlewareSpec.AnyMiddlewareSpec, "key">
|
|
151
|
+
>;
|
|
152
|
+
}): (group: Finalizable) => Layer.Layer<GroupImpl<"Finalized">>;
|
|
153
|
+
export function finalize(
|
|
133
154
|
group: Layer.Layer<GroupImpl<"Unfinalized">>,
|
|
155
|
+
): Layer.Layer<GroupImpl<"Finalized">>;
|
|
156
|
+
export function finalize(
|
|
157
|
+
input:
|
|
158
|
+
| Finalizable
|
|
159
|
+
| {
|
|
160
|
+
readonly groupPath?: string;
|
|
161
|
+
readonly required: ReadonlyArray<
|
|
162
|
+
Pick<MiddlewareSpec.AnyMiddlewareSpec, "key">
|
|
163
|
+
>;
|
|
164
|
+
},
|
|
165
|
+
):
|
|
166
|
+
| Layer.Layer<GroupImpl<"Finalized">>
|
|
167
|
+
| ((group: Finalizable) => Layer.Layer<GroupImpl<"Finalized">>) {
|
|
168
|
+
return "required" in input
|
|
169
|
+
? (group) => finalizeWith(group, input.required, input.groupPath)
|
|
170
|
+
: finalizeWith(input, []);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const finalizeWith = (
|
|
174
|
+
group: Layer.Layer<never>,
|
|
175
|
+
required: ReadonlyArray<Pick<MiddlewareSpec.AnyMiddlewareSpec, "key">>,
|
|
176
|
+
groupPath = "",
|
|
134
177
|
): Layer.Layer<GroupImpl<"Finalized">> =>
|
|
135
178
|
Layer.flatMap(group, (context): Layer.Layer<GroupImpl<"Finalized">> =>
|
|
136
|
-
|
|
179
|
+
findGroupImpl(context).pipe(
|
|
137
180
|
Option.match({
|
|
138
181
|
onNone: () =>
|
|
139
182
|
Layer.effect(
|
|
140
183
|
GroupImpl<"Finalized">({ finalizationStatus: "Finalized" }),
|
|
141
184
|
Effect.die(
|
|
142
185
|
new Error(
|
|
143
|
-
"GroupImpl.finalize: no
|
|
186
|
+
"GroupImpl.finalize: no GroupImpl service was found in the layer's context.",
|
|
144
187
|
),
|
|
145
188
|
),
|
|
146
189
|
),
|
|
@@ -150,6 +193,16 @@ export const finalize = (
|
|
|
150
193
|
Effect.gen(function* () {
|
|
151
194
|
const registry = yield* Registry.Registry;
|
|
152
195
|
const items = yield* Ref.get(registry);
|
|
196
|
+
const missing = RegistryItems.missingRequired(
|
|
197
|
+
items,
|
|
198
|
+
required,
|
|
199
|
+
groupPath,
|
|
200
|
+
);
|
|
201
|
+
if (missing.length > 0) {
|
|
202
|
+
return yield* Effect.die(
|
|
203
|
+
new MissingRequiredMiddlewareError({ missing }),
|
|
204
|
+
);
|
|
205
|
+
}
|
|
153
206
|
return {
|
|
154
207
|
[TypeId]: TypeId,
|
|
155
208
|
finalizationStatus: "Finalized" as const,
|
package/src/GroupRegistry.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as RegisteredFunction from "./RegisteredFunction.js";
|
|
1
2
|
import * as Effect from "effect/Effect";
|
|
2
3
|
import type * as Layer from "effect/Layer";
|
|
3
4
|
import * as Ref from "effect/Ref";
|
|
@@ -9,9 +10,12 @@ import * as MiddlewareRegistryItem from "./MiddlewareRegistryItem.js";
|
|
|
9
10
|
export const make = (
|
|
10
11
|
groupLayer: Layer.Layer<GroupImpl.GroupImpl<"Finalized">>,
|
|
11
12
|
) => {
|
|
12
|
-
const registryItems = Effect.gen(function* () {
|
|
13
|
+
const { registryItems, observability } = Effect.gen(function* () {
|
|
13
14
|
const registry = yield* Registry.Registry;
|
|
14
|
-
return
|
|
15
|
+
return {
|
|
16
|
+
registryItems: yield* Ref.get(registry),
|
|
17
|
+
observability: yield* RegisteredFunction.Observability,
|
|
18
|
+
};
|
|
15
19
|
}).pipe(
|
|
16
20
|
Effect.provide(groupLayer),
|
|
17
21
|
Effect.provideService(
|
|
@@ -21,7 +25,7 @@ export const make = (
|
|
|
21
25
|
Effect.runSync,
|
|
22
26
|
);
|
|
23
27
|
|
|
24
|
-
return partitionRegistryItems(registryItems);
|
|
28
|
+
return { ...partitionRegistryItems(registryItems), observability };
|
|
25
29
|
};
|
|
26
30
|
|
|
27
31
|
const partitionRegistryItems = (registryItems: RegistryItems.RegistryItems) => {
|