effect-zero 0.5.0 → 0.6.0-beta.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 +56 -38
- package/dist/client-transaction.d.ts +11 -9
- package/dist/client-transaction.d.ts.map +1 -1
- package/dist/client-transaction.js +4 -1
- package/dist/client-transaction.js.map +1 -1
- package/dist/client.d.ts +68 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +7 -10
- package/dist/client.js.map +1 -1
- package/dist/client.test.d.ts +2 -0
- package/dist/client.test.d.ts.map +1 -0
- package/dist/client.test.js +19 -0
- package/dist/client.test.js.map +1 -0
- package/dist/internal/server-synchronization-context.d.ts.map +1 -1
- package/dist/internal/server-synchronization-context.js +2 -2
- package/dist/internal/server-synchronization-context.js.map +1 -1
- package/dist/internal/server.d.ts +13 -28
- package/dist/internal/server.d.ts.map +1 -1
- package/dist/internal/server.js +5 -19
- package/dist/internal/server.js.map +1 -1
- package/dist/internal/server.test.js +5 -3
- package/dist/internal/server.test.js.map +1 -1
- package/dist/server-transaction.d.ts +82 -109
- package/dist/server-transaction.d.ts.map +1 -1
- package/dist/server-transaction.js +26 -74
- package/dist/server-transaction.js.map +1 -1
- package/dist/server.d.ts +207 -354
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +60 -80
- package/dist/server.js.map +1 -1
- package/dist/types/ast.d.ts +4 -0
- package/dist/types/ast.d.ts.map +1 -0
- package/dist/types/ast.js +4 -0
- package/dist/types/ast.js.map +1 -0
- package/dist/types/{queries.d.ts → custom-queries.d.ts} +19 -1
- package/dist/types/custom-queries.d.ts.map +1 -0
- package/dist/types/{queries.js → custom-queries.js} +5 -6
- package/dist/types/custom-queries.js.map +1 -0
- package/dist/types/mutate-server.d.ts +160 -0
- package/dist/types/mutate-server.d.ts.map +1 -0
- package/dist/types/mutate-server.js +29 -0
- package/dist/types/mutate-server.js.map +1 -0
- package/dist/types/mutation-id.d.ts +7 -0
- package/dist/types/mutation-id.d.ts.map +1 -0
- package/dist/types/mutation-id.js +8 -0
- package/dist/types/mutation-id.js.map +1 -0
- package/dist/types/mutation.d.ts +79 -0
- package/dist/types/mutation.d.ts.map +1 -0
- package/dist/types/mutation.js +125 -0
- package/dist/types/mutation.js.map +1 -0
- package/dist/types/push.d.ts +100 -85
- package/dist/types/push.d.ts.map +1 -1
- package/dist/types/push.js +7 -134
- package/dist/types/push.js.map +1 -1
- package/dist/types/query-server.d.ts +144 -0
- package/dist/types/query-server.d.ts.map +1 -0
- package/dist/types/query-server.js +19 -0
- package/dist/types/query-server.js.map +1 -0
- package/dist/types.test.js +4 -4
- package/dist/types.test.js.map +1 -1
- package/package.json +35 -5
- package/src/client-transaction.ts +5 -1
- package/src/client.ts +14 -15
- package/src/internal/server-synchronization-context.ts +37 -39
- package/src/internal/server.ts +8 -31
- package/src/server-transaction.ts +55 -101
- package/src/server.ts +147 -151
- package/src/types/ast.ts +4 -0
- package/src/types/{queries.ts → custom-queries.ts} +4 -6
- package/src/types/mutate-server.ts +37 -0
- package/src/types/mutation-id.ts +10 -0
- package/src/types/mutation.ts +155 -0
- package/src/types/push.ts +7 -167
- package/src/types/query-server.ts +27 -0
- package/dist/types/queries.d.ts.map +0 -1
- package/dist/types/queries.js.map +0 -1
|
@@ -1,93 +1,45 @@
|
|
|
1
1
|
import * as Cause from "effect/Cause";
|
|
2
2
|
import * as Ctx from "effect/Context";
|
|
3
3
|
import * as Data from "effect/Data";
|
|
4
|
-
import * as Deferred from "effect/Deferred";
|
|
5
4
|
import * as Effect from "effect/Effect";
|
|
6
|
-
import * as Exit from "effect/Exit";
|
|
7
5
|
import * as Layer from "effect/Layer";
|
|
8
6
|
import * as Predicate from "effect/Predicate";
|
|
9
|
-
import
|
|
10
|
-
import * as ServerSynchronizationContext from "./internal/server-synchronization-context.js";
|
|
7
|
+
import * as ClientTransaction from "./client-transaction.js";
|
|
11
8
|
import { prefixId } from "./internal/utils.js";
|
|
9
|
+
export const DatabaseSymbol = Symbol.for(prefixId("Database"));
|
|
10
|
+
export const ServerTransactionCallbackSymbol = Symbol.for(prefixId("ServerTransactionCallback"));
|
|
12
11
|
export const make = (id, database, clientTransaction) => {
|
|
13
12
|
class Context extends Ctx.Service()(`${id}/ServerTransactionContext`) {
|
|
14
13
|
}
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
class ServerTransactionCallback extends Ctx.Service()(`${id}/ServerTransactionCallback`) {
|
|
15
|
+
}
|
|
16
|
+
const use = Effect.fn(function* (fn) {
|
|
17
|
+
const transaction = yield* Context;
|
|
17
18
|
return yield* Effect.tryPromise({
|
|
18
|
-
try: (signal) => fn(
|
|
19
|
-
catch: (error) => new
|
|
19
|
+
try: (signal) => fn(transaction, { signal }),
|
|
20
|
+
catch: (error) => new TransactionUseError({ cause: Cause.fail(error) }),
|
|
20
21
|
});
|
|
21
22
|
});
|
|
22
23
|
const execute = Effect.fn(function* (effect) {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
Layer.succeed(clientTransaction.Context, transaction),
|
|
31
|
-
]), (effect) => Effect.runPromiseExitWith(ctx)(effect, { signal }));
|
|
32
|
-
Deferred.doneUnsafe(result, exit);
|
|
33
|
-
if (Exit.isFailure(exit)) {
|
|
34
|
-
// This error's purpose is to differentiate between "external" errors
|
|
35
|
-
// that originate from the user-defined mutator code and "internal" errors
|
|
36
|
-
// that originate from our own code and the Zero API.
|
|
37
|
-
// Both types are caught in the "catch" block below, but at this point we only need to handle
|
|
38
|
-
// the "internal" errors wrapping them in a `ZeroDatabaseError`, because "external" errors
|
|
39
|
-
// are already covered by passing the Exit result to the Deferred, which is why
|
|
40
|
-
// we have the ServerTransactionUserError silenced below in the pipe.
|
|
41
|
-
throw new ServerTransactionUserError();
|
|
42
|
-
}
|
|
43
|
-
return exit.value;
|
|
44
|
-
}, transactionInput),
|
|
45
|
-
catch: (error) => {
|
|
46
|
-
if (ServerTransactionUserError.is(error)) {
|
|
47
|
-
return error;
|
|
48
|
-
}
|
|
49
|
-
// This is for errors that occur when calling `database.transaction` despite the provided `effect` succeeding.
|
|
50
|
-
// This can be caused by e.g. the database connection timing out or other database-related issues.
|
|
51
|
-
return new ZeroDatabaseError({ cause: Cause.fail(error) });
|
|
52
|
-
},
|
|
53
|
-
}).pipe(Effect.catchTag("ServerTransactionUserError", () => Effect.void));
|
|
54
|
-
return yield* Deferred.await(result);
|
|
55
|
-
}, ServerSynchronizationContext.guard);
|
|
56
|
-
const checkAndIncrementLastMutationId = Effect.gen(function* () {
|
|
57
|
-
const { transactionHooks } = yield* Context;
|
|
58
|
-
const { clientID, mutationID: receivedMutationID } = yield* ServerTransactionInput;
|
|
59
|
-
const { lastMutationID } = yield* Effect.tryPromise({
|
|
60
|
-
try: () => transactionHooks.updateClientMutationID(),
|
|
61
|
-
catch: (error) => new UpdateClientMutationIdError({ cause: Cause.fail(error) }),
|
|
62
|
-
});
|
|
63
|
-
if (receivedMutationID < lastMutationID) {
|
|
64
|
-
return yield* new MutationAlreadyProcessedError({
|
|
65
|
-
clientID,
|
|
66
|
-
received: receivedMutationID,
|
|
67
|
-
actual: lastMutationID,
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
if (receivedMutationID > lastMutationID) {
|
|
71
|
-
return yield* new OutOfOrderMutationError({
|
|
72
|
-
clientID,
|
|
73
|
-
receivedMutationID,
|
|
74
|
-
lastMutationID,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
24
|
+
const { transact } = yield* ServerTransactionCallback;
|
|
25
|
+
return yield* transact((transaction) => effect.pipe(
|
|
26
|
+
// Provide the live transaction under both the server- and client-transaction tags.
|
|
27
|
+
Effect.provide([
|
|
28
|
+
Layer.succeed(Context, transaction),
|
|
29
|
+
Layer.succeed(clientTransaction[ClientTransaction.ContextSymbol], transaction),
|
|
30
|
+
])));
|
|
77
31
|
});
|
|
78
|
-
return {
|
|
32
|
+
return {
|
|
33
|
+
use,
|
|
34
|
+
execute,
|
|
35
|
+
[DatabaseSymbol]: database,
|
|
36
|
+
[ServerTransactionCallbackSymbol]: ServerTransactionCallback,
|
|
37
|
+
};
|
|
79
38
|
};
|
|
80
|
-
class
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
class ZeroDatabaseError extends Data.TaggedError("ZeroDatabaseError") {
|
|
85
|
-
}
|
|
86
|
-
const ServerTransactionUserErrorTypeId = Symbol.for(prefixId("ServerTransactionUserError"));
|
|
87
|
-
class ServerTransactionUserError extends Data.TaggedError("ServerTransactionUserError") {
|
|
88
|
-
[ServerTransactionUserErrorTypeId] = ServerTransactionUserErrorTypeId;
|
|
89
|
-
static is(e) {
|
|
90
|
-
return Predicate.hasProperty(e, ServerTransactionUserErrorTypeId);
|
|
39
|
+
class TransactionUseError extends Data.TaggedError("TransactionUseError") {
|
|
40
|
+
get message() {
|
|
41
|
+
const error = Cause.squash(this.cause);
|
|
42
|
+
return Predicate.isError(error) ? error.message : "Internal error";
|
|
91
43
|
}
|
|
92
44
|
}
|
|
93
45
|
//# sourceMappingURL=server-transaction.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-transaction.js","sourceRoot":"","sources":["../src/server-transaction.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,aAAa,CAAC;AACpC,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"server-transaction.js","sourceRoot":"","sources":["../src/server-transaction.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,aAAa,CAAC;AACpC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AAExC,OAAO,KAAK,KAAK,MAAM,cAAc,CAA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