effect-app 4.0.0-beta.25 → 4.0.0-beta.250
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/CHANGELOG.md +1131 -0
- package/dist/Array.d.ts +3 -2
- package/dist/Array.d.ts.map +1 -1
- package/dist/Array.js +4 -4
- package/dist/Chunk.d.ts +1 -1
- package/dist/Chunk.d.ts.map +1 -1
- package/dist/Config/SecretURL.d.ts +4 -2
- package/dist/Config/SecretURL.d.ts.map +1 -1
- package/dist/Config/SecretURL.js +3 -6
- package/dist/Config/internal/configSecretURL.d.ts +1 -1
- package/dist/Config/internal/configSecretURL.d.ts.map +1 -1
- package/dist/Config/internal/configSecretURL.js +2 -2
- package/dist/Config.d.ts +7 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +6 -0
- package/dist/ConfigProvider.d.ts +39 -0
- package/dist/ConfigProvider.d.ts.map +1 -0
- package/dist/ConfigProvider.js +42 -0
- package/dist/Context.d.ts +42 -0
- package/dist/Context.d.ts.map +1 -0
- package/dist/Context.js +67 -0
- package/dist/Effect.d.ts +13 -12
- package/dist/Effect.d.ts.map +1 -1
- package/dist/Effect.js +5 -8
- package/dist/Emailer.d.ts +51 -0
- package/dist/Emailer.d.ts.map +1 -0
- package/dist/Emailer.js +7 -0
- package/dist/Function.d.ts +1 -1
- package/dist/Function.d.ts.map +1 -1
- package/dist/Inputify.type.d.ts +1 -1
- package/dist/Layer.d.ts +11 -7
- package/dist/Layer.d.ts.map +1 -1
- package/dist/Layer.js +3 -2
- package/dist/Model/Repository/Registry.d.ts +21 -0
- package/dist/Model/Repository/Registry.d.ts.map +1 -0
- package/dist/Model/Repository/Registry.js +18 -0
- package/dist/Model/Repository/ext.d.ts +60 -0
- package/dist/Model/Repository/ext.d.ts.map +1 -0
- package/dist/Model/Repository/ext.js +122 -0
- package/dist/Model/Repository/internal/internal.d.ts +62 -0
- package/dist/Model/Repository/internal/internal.d.ts.map +1 -0
- package/dist/Model/Repository/internal/internal.js +398 -0
- package/dist/Model/Repository/legacy.d.ts +21 -0
- package/dist/Model/Repository/legacy.d.ts.map +1 -0
- package/dist/Model/Repository/legacy.js +2 -0
- package/dist/Model/Repository/makeRepo.d.ts +53 -0
- package/dist/Model/Repository/makeRepo.d.ts.map +1 -0
- package/dist/Model/Repository/makeRepo.js +27 -0
- package/dist/Model/Repository/service.d.ts +97 -0
- package/dist/Model/Repository/service.d.ts.map +1 -0
- package/dist/Model/Repository/service.js +2 -0
- package/dist/Model/Repository/validation.d.ts +71 -0
- package/dist/Model/Repository/validation.d.ts.map +1 -0
- package/dist/Model/Repository/validation.js +32 -0
- package/dist/Model/Repository.d.ts +7 -0
- package/dist/Model/Repository.d.ts.map +1 -0
- package/dist/Model/Repository.js +7 -0
- package/dist/Model/dsl.d.ts +33 -0
- package/dist/Model/dsl.d.ts.map +1 -0
- package/dist/Model/dsl.js +43 -0
- package/dist/Model/filter/filterApi.d.ts +30 -0
- package/dist/Model/filter/filterApi.d.ts.map +1 -0
- package/dist/Model/filter/filterApi.js +2 -0
- package/dist/Model/filter/types/errors.d.ts +29 -0
- package/dist/Model/filter/types/errors.d.ts.map +1 -0
- package/dist/Model/filter/types/errors.js +2 -0
- package/dist/Model/filter/types/fields.d.ts +15 -0
- package/dist/Model/filter/types/fields.d.ts.map +1 -0
- package/dist/Model/filter/types/fields.js +2 -0
- package/dist/Model/filter/types/path/common.d.ts +316 -0
- package/dist/Model/filter/types/path/common.d.ts.map +1 -0
- package/dist/Model/filter/types/path/common.js +2 -0
- package/dist/Model/filter/types/path/eager.d.ts +95 -0
- package/dist/Model/filter/types/path/eager.d.ts.map +1 -0
- package/dist/Model/filter/types/path/eager.js +31 -0
- package/dist/Model/filter/types/path/index.d.ts +4 -0
- package/dist/Model/filter/types/path/index.d.ts.map +1 -0
- package/dist/Model/filter/types/path/index.js +3 -0
- package/dist/Model/filter/types/utils.d.ts +79 -0
- package/dist/Model/filter/types/utils.d.ts.map +1 -0
- package/dist/Model/filter/types/utils.js +2 -0
- package/dist/Model/filter/types/validator.d.ts +30 -0
- package/dist/Model/filter/types/validator.d.ts.map +1 -0
- package/dist/Model/filter/types/validator.js +2 -0
- package/dist/Model/filter/types.d.ts +5 -0
- package/dist/Model/filter/types.d.ts.map +1 -0
- package/dist/Model/filter/types.js +7 -0
- package/dist/Model/query/dsl.d.ts +446 -0
- package/dist/Model/query/dsl.d.ts.map +1 -0
- package/dist/Model/query/dsl.js +342 -0
- package/dist/Model/query/new-kid-interpreter.d.ts +136 -0
- package/dist/Model/query/new-kid-interpreter.d.ts.map +1 -0
- package/dist/Model/query/new-kid-interpreter.js +336 -0
- package/dist/Model/query.d.ts +15 -0
- package/dist/Model/query.d.ts.map +1 -0
- package/dist/Model/query.js +3 -0
- package/dist/Model.d.ts +5 -0
- package/dist/Model.d.ts.map +1 -0
- package/dist/Model.js +5 -0
- package/dist/NonEmptySet.d.ts +4 -2
- package/dist/NonEmptySet.d.ts.map +1 -1
- package/dist/NonEmptySet.js +2 -2
- package/dist/Option.d.ts +2 -1
- package/dist/Option.d.ts.map +1 -1
- package/dist/Option.js +3 -1
- package/dist/Pure.d.ts +8 -6
- package/dist/Pure.d.ts.map +1 -1
- package/dist/Pure.js +17 -14
- package/dist/QueueMaker.d.ts +13 -0
- package/dist/QueueMaker.d.ts.map +1 -0
- package/dist/QueueMaker.js +4 -0
- package/dist/RequestContext.d.ts +103 -0
- package/dist/RequestContext.d.ts.map +1 -0
- package/dist/RequestContext.js +49 -0
- package/dist/Schema/Class.d.ts +66 -20
- package/dist/Schema/Class.d.ts.map +1 -1
- package/dist/Schema/Class.js +192 -23
- package/dist/Schema/FastCheck.d.ts +1 -1
- package/dist/Schema/FastCheck.d.ts.map +1 -1
- package/dist/Schema/Methods.d.ts +1 -1
- package/dist/Schema/SchemaParser.d.ts +5 -0
- package/dist/Schema/SchemaParser.d.ts.map +1 -0
- package/dist/Schema/SchemaParser.js +6 -0
- package/dist/Schema/SpecialJsonSchema.d.ts +34 -0
- package/dist/Schema/SpecialJsonSchema.d.ts.map +1 -0
- package/dist/Schema/SpecialJsonSchema.js +118 -0
- package/dist/Schema/SpecialOpenApi.d.ts +32 -0
- package/dist/Schema/SpecialOpenApi.d.ts.map +1 -0
- package/dist/Schema/SpecialOpenApi.js +123 -0
- package/dist/Schema/brand.d.ts +5 -3
- package/dist/Schema/brand.d.ts.map +1 -1
- package/dist/Schema/brand.js +3 -1
- package/dist/Schema/email.d.ts +1 -1
- package/dist/Schema/email.d.ts.map +1 -1
- package/dist/Schema/email.js +7 -4
- package/dist/Schema/ext.d.ts +339 -56
- package/dist/Schema/ext.d.ts.map +1 -1
- package/dist/Schema/ext.js +358 -53
- package/dist/Schema/moreStrings.d.ts +108 -26
- package/dist/Schema/moreStrings.d.ts.map +1 -1
- package/dist/Schema/moreStrings.js +45 -16
- package/dist/Schema/numbers.d.ts +55 -15
- package/dist/Schema/numbers.d.ts.map +1 -1
- package/dist/Schema/numbers.js +60 -12
- package/dist/Schema/phoneNumber.d.ts +1 -1
- package/dist/Schema/phoneNumber.d.ts.map +1 -1
- package/dist/Schema/phoneNumber.js +6 -3
- package/dist/Schema/schema.d.ts +1 -1
- package/dist/Schema/strings.d.ts +5 -5
- package/dist/Schema/strings.d.ts.map +1 -1
- package/dist/Schema/strings.js +1 -5
- package/dist/Schema.d.ts +214 -8
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +190 -11
- package/dist/Set.d.ts +5 -2
- package/dist/Set.d.ts.map +1 -1
- package/dist/Set.js +3 -2
- package/dist/Store.d.ts +166 -0
- package/dist/Store.d.ts.map +1 -0
- package/dist/Store.js +117 -0
- package/dist/TypeTest.d.ts +1 -1
- package/dist/Types.d.ts +1 -1
- package/dist/Widen.type.d.ts +1 -1
- package/dist/_ext/Array.d.ts +2 -2
- package/dist/_ext/Array.d.ts.map +1 -1
- package/dist/_ext/Array.js +4 -2
- package/dist/_ext/date.d.ts +1 -1
- package/dist/_ext/misc.d.ts +5 -2
- package/dist/_ext/misc.d.ts.map +1 -1
- package/dist/_ext/misc.js +4 -2
- package/dist/_ext/ord.ext.d.ts +3 -2
- package/dist/_ext/ord.ext.d.ts.map +1 -1
- package/dist/_ext/ord.ext.js +2 -2
- package/dist/builtin.d.ts +1 -1
- package/dist/builtin.d.ts.map +1 -1
- package/dist/client/InvalidationKeys.d.ts +29 -0
- package/dist/client/InvalidationKeys.d.ts.map +1 -0
- package/dist/client/InvalidationKeys.js +33 -0
- package/dist/client/apiClientFactory.d.ts +20 -32
- package/dist/client/apiClientFactory.d.ts.map +1 -1
- package/dist/client/apiClientFactory.js +104 -34
- package/dist/client/clientFor.d.ts +53 -19
- package/dist/client/clientFor.d.ts.map +1 -1
- package/dist/client/clientFor.js +9 -1
- package/dist/client/errors.d.ts +49 -25
- package/dist/client/errors.d.ts.map +1 -1
- package/dist/client/errors.js +43 -17
- package/dist/client/makeClient.d.ts +495 -33
- package/dist/client/makeClient.d.ts.map +1 -1
- package/dist/client/makeClient.js +66 -24
- package/dist/client.d.ts +6 -5
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +2 -1
- package/dist/faker.d.ts +1 -1
- package/dist/faker.d.ts.map +1 -1
- package/dist/http/Request.d.ts +2 -2
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +2 -2
- package/dist/http/internal/lib.d.ts +1 -1
- package/dist/http.d.ts +1 -1
- package/dist/ids.d.ts +40 -12
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js +25 -3
- package/dist/index.d.ts +7 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -8
- package/dist/logger.d.ts +1 -1
- package/dist/middleware.d.ts +14 -8
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +14 -8
- package/dist/rpc/Invalidation.d.ts +420 -0
- package/dist/rpc/Invalidation.d.ts.map +1 -0
- package/dist/rpc/Invalidation.js +168 -0
- package/dist/rpc/MiddlewareMaker.d.ts +12 -8
- package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
- package/dist/rpc/MiddlewareMaker.js +59 -38
- package/dist/rpc/RpcContextMap.d.ts +4 -4
- package/dist/rpc/RpcContextMap.d.ts.map +1 -1
- package/dist/rpc/RpcContextMap.js +4 -4
- package/dist/rpc/RpcMiddleware.d.ts +15 -11
- package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
- package/dist/rpc/RpcMiddleware.js +1 -1
- package/dist/rpc.d.ts +2 -2
- package/dist/rpc.d.ts.map +1 -1
- package/dist/rpc.js +2 -2
- package/dist/runtime.d.ts +19 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +40 -0
- package/dist/toast.d.ts +51 -0
- package/dist/toast.d.ts.map +1 -0
- package/dist/toast.js +34 -0
- package/dist/transform.d.ts +2 -2
- package/dist/transform.d.ts.map +1 -1
- package/dist/transform.js +4 -5
- package/dist/utils/effectify.d.ts +2 -2
- package/dist/utils/effectify.d.ts.map +1 -1
- package/dist/utils/effectify.js +2 -2
- package/dist/utils/extend.d.ts +1 -1
- package/dist/utils/extend.d.ts.map +1 -1
- package/dist/utils/gen.d.ts +5 -5
- package/dist/utils/gen.d.ts.map +1 -1
- package/dist/utils/logLevel.d.ts +3 -3
- package/dist/utils/logLevel.d.ts.map +1 -1
- package/dist/utils/logger.d.ts +5 -4
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +4 -4
- package/dist/utils.d.ts +40 -45
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +19 -27
- package/dist/validation/validators.d.ts +1 -1
- package/dist/validation/validators.d.ts.map +1 -1
- package/dist/validation.d.ts +1 -1
- package/dist/validation.d.ts.map +1 -1
- package/dist/withToast.d.ts +30 -0
- package/dist/withToast.d.ts.map +1 -0
- package/dist/withToast.js +64 -0
- package/package.json +158 -24
- package/src/Array.ts +3 -3
- package/src/Config/SecretURL.ts +5 -2
- package/src/Config/internal/configSecretURL.ts +1 -1
- package/src/Config.ts +14 -0
- package/src/ConfigProvider.ts +48 -0
- package/src/{ServiceMap.ts → Context.ts} +56 -63
- package/src/Effect.ts +12 -14
- package/src/Emailer.ts +51 -0
- package/src/Layer.ts +10 -6
- package/src/Model/Repository/Registry.ts +34 -0
- package/src/Model/Repository/ext.ts +375 -0
- package/src/Model/Repository/internal/internal.ts +692 -0
- package/src/Model/Repository/legacy.ts +29 -0
- package/src/Model/Repository/makeRepo.ts +144 -0
- package/src/Model/Repository/service.ts +639 -0
- package/src/Model/Repository/validation.ts +31 -0
- package/src/Model/Repository.ts +6 -0
- package/src/Model/dsl.ts +129 -0
- package/src/Model/filter/filterApi.ts +60 -0
- package/src/Model/filter/types/errors.ts +47 -0
- package/src/Model/filter/types/fields.ts +50 -0
- package/src/Model/filter/types/path/common.ts +404 -0
- package/src/Model/filter/types/path/eager.ts +297 -0
- package/src/Model/filter/types/path/index.ts +4 -0
- package/src/Model/filter/types/utils.ts +128 -0
- package/src/Model/filter/types/validator.ts +46 -0
- package/src/Model/filter/types.ts +6 -0
- package/src/Model/query/dsl.ts +2546 -0
- package/src/Model/query/new-kid-interpreter.ts +484 -0
- package/src/Model/query.ts +13 -0
- package/src/Model.ts +4 -0
- package/src/NonEmptySet.ts +3 -1
- package/src/Option.ts +2 -0
- package/src/Pure.ts +21 -19
- package/src/QueueMaker.ts +19 -0
- package/src/RequestContext.ts +62 -0
- package/src/Schema/Class.ts +274 -64
- package/src/Schema/SchemaParser.ts +12 -0
- package/src/Schema/SpecialJsonSchema.ts +139 -0
- package/src/Schema/SpecialOpenApi.ts +130 -0
- package/src/Schema/brand.ts +22 -2
- package/src/Schema/email.ts +7 -2
- package/src/Schema/ext.ts +443 -88
- package/src/Schema/moreStrings.ts +93 -37
- package/src/Schema/numbers.ts +64 -16
- package/src/Schema/phoneNumber.ts +5 -1
- package/src/Schema/strings.ts +4 -8
- package/src/Schema.ts +374 -10
- package/src/Set.ts +5 -1
- package/src/Store.ts +273 -0
- package/src/_ext/Array.ts +3 -1
- package/src/_ext/misc.ts +4 -1
- package/src/_ext/ord.ext.ts +2 -1
- package/src/client/InvalidationKeys.ts +50 -0
- package/src/client/apiClientFactory.ts +230 -131
- package/src/client/clientFor.ts +102 -31
- package/src/client/errors.ts +52 -26
- package/src/client/makeClient.ts +592 -71
- package/src/client.ts +5 -4
- package/src/http/Request.ts +1 -1
- package/src/ids.ts +25 -3
- package/src/index.ts +7 -10
- package/src/middleware.ts +13 -9
- package/src/rpc/Invalidation.ts +261 -0
- package/src/rpc/MiddlewareMaker.ts +83 -75
- package/src/rpc/README.md +2 -2
- package/src/rpc/RpcContextMap.ts +6 -5
- package/src/rpc/RpcMiddleware.ts +18 -12
- package/src/rpc.ts +1 -1
- package/src/runtime.ts +56 -0
- package/src/toast.ts +54 -0
- package/src/transform.ts +3 -3
- package/src/utils/effectify.ts +1 -1
- package/src/utils/gen.ts +8 -8
- package/src/utils/logLevel.ts +1 -1
- package/src/utils/logger.ts +4 -3
- package/src/utils.ts +62 -139
- package/src/withToast.ts +133 -0
- package/test/dist/rpc-dynamic-middleware.test.d.ts.map +1 -0
- package/test/dist/rpc.test.d.ts.map +1 -1
- package/test/dist/secretURL.test.d.ts.map +1 -0
- package/test/dist/special.test.d.ts.map +1 -0
- package/test/dist/stream-error.types.d.ts +2 -0
- package/test/dist/stream-error.types.d.ts.map +1 -0
- package/test/dist/stream-error.types.js +27 -0
- package/test/moreStrings.test.ts +1 -1
- package/test/rpc.test.ts +46 -6
- package/test/schema.test.ts +459 -30
- package/test/secretURL.test.ts +160 -0
- package/test/special.test.ts +1026 -0
- package/test/utils.test.ts +7 -7
- package/tsconfig.base.json +6 -5
- package/tsconfig.json +2 -1
- package/tsconfig.json.bak +2 -2
- package/tsconfig.src.json +29 -29
- package/tsconfig.test.json +2 -2
- package/dist/Operations.d.ts +0 -123
- package/dist/Operations.d.ts.map +0 -1
- package/dist/Operations.js +0 -29
- package/dist/ServiceMap.d.ts +0 -44
- package/dist/ServiceMap.d.ts.map +0 -1
- package/dist/ServiceMap.js +0 -91
- package/eslint.config.mjs +0 -26
- package/src/Operations.ts +0 -55
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
import * as Equivalence from "effect/Equivalence"
|
|
4
|
+
import { flow, pipe } from "effect/Function"
|
|
5
|
+
import * as Pipeable from "effect/Pipeable"
|
|
6
|
+
import * as PubSub from "effect/PubSub"
|
|
7
|
+
import * as Result from "effect/Result"
|
|
8
|
+
import * as SchemaAST from "effect/SchemaAST"
|
|
9
|
+
import * as Unify from "effect/Unify"
|
|
10
|
+
import * as Array from "../../../Array.js"
|
|
11
|
+
import type { NonEmptyReadonlyArray } from "../../../Array.js"
|
|
12
|
+
import { toNonEmptyArray } from "../../../Array.js"
|
|
13
|
+
import * as Chunk from "../../../Chunk.js"
|
|
14
|
+
import { NotFoundError } from "../../../client/errors.js"
|
|
15
|
+
import * as Context from "../../../Context.js"
|
|
16
|
+
import * as Effect from "../../../Effect.js"
|
|
17
|
+
import { flatMapOption } from "../../../Effect.js"
|
|
18
|
+
import * as Option from "../../../Option.js"
|
|
19
|
+
import * as S from "../../../Schema.js"
|
|
20
|
+
import { type Codec, NonNegativeInt } from "../../../Schema.js"
|
|
21
|
+
import { type FilterArgs, getContextMap, type PersistenceModelType, type StoreConfig, StoreMaker } from "../../../Store.js"
|
|
22
|
+
import type { FieldValues } from "../../filter/types.js"
|
|
23
|
+
import * as Q from "../../query.js"
|
|
24
|
+
import type { Repository } from "../service.js"
|
|
25
|
+
import { ValidationError, ValidationResult } from "../validation.js"
|
|
26
|
+
|
|
27
|
+
const dedupe = Array.dedupeWith(Equivalence.String)
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A base implementation to create a repository.
|
|
31
|
+
*/
|
|
32
|
+
export function makeRepoInternal<
|
|
33
|
+
Evt = never
|
|
34
|
+
>() {
|
|
35
|
+
return <
|
|
36
|
+
ItemType extends string,
|
|
37
|
+
R,
|
|
38
|
+
Encoded extends FieldValues,
|
|
39
|
+
T,
|
|
40
|
+
IdKey extends keyof T & keyof Encoded
|
|
41
|
+
>(
|
|
42
|
+
name: ItemType,
|
|
43
|
+
schema: S.Codec<T, Encoded, R>,
|
|
44
|
+
mapFrom: (pm: Encoded) => Encoded,
|
|
45
|
+
mapTo: (e: Encoded, etag: string | undefined) => PersistenceModelType<Encoded>,
|
|
46
|
+
idKey: IdKey
|
|
47
|
+
) => {
|
|
48
|
+
type PM = PersistenceModelType<Encoded>
|
|
49
|
+
function mapToPersistenceModel(
|
|
50
|
+
e: Encoded,
|
|
51
|
+
getEtag: (id: string) => string | undefined
|
|
52
|
+
): PM {
|
|
53
|
+
return mapTo(e, getEtag(e[idKey]))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function mapReverse(
|
|
57
|
+
{ _etag, ...e }: PM,
|
|
58
|
+
setEtag: (id: string, eTag: string | undefined) => void
|
|
59
|
+
): Encoded {
|
|
60
|
+
setEtag((e as any)[idKey], _etag)
|
|
61
|
+
return mapFrom(e as unknown as Encoded)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const mkStore = makeStore<Encoded>()(name, schema, mapTo, idKey)
|
|
65
|
+
|
|
66
|
+
function make<RInitial = never, E = never, RPublish = never, RCtx = never>(
|
|
67
|
+
args: [Evt] extends [never] ? {
|
|
68
|
+
schemaContext?: Context.Context<RCtx>
|
|
69
|
+
makeInitial?: Effect.Effect<readonly T[], E, RInitial> | undefined
|
|
70
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
71
|
+
partitionValue?: (e?: Encoded) => string
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
: {
|
|
75
|
+
schemaContext?: Context.Context<RCtx>
|
|
76
|
+
publishEvents: (evt: NonEmptyReadonlyArray<Evt>) => Effect.Effect<void, never, RPublish>
|
|
77
|
+
makeInitial?: Effect.Effect<readonly T[], E, RInitial> | undefined
|
|
78
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
79
|
+
partitionValue?: (e?: Encoded) => string
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
) {
|
|
83
|
+
return Effect
|
|
84
|
+
.gen(function*() {
|
|
85
|
+
const rctx: Context.Context<RCtx> = args.schemaContext ?? Context.empty() as any
|
|
86
|
+
const provideRctx = Effect.provide(rctx)
|
|
87
|
+
const encodeMany = flow(
|
|
88
|
+
S.encodeEffect(S.Array(schema)),
|
|
89
|
+
provideRctx,
|
|
90
|
+
Effect.withSpan("encodeMany", { attributes: { "app.entity": name } }, { captureStackTrace: false })
|
|
91
|
+
)
|
|
92
|
+
const decode = flow(S.decodeEffectConcurrently(schema), provideRctx)
|
|
93
|
+
const decodeMany = flow(
|
|
94
|
+
S.decodeEffectConcurrently(S.Array(schema)),
|
|
95
|
+
provideRctx
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
const store = yield* mkStore(args.makeInitial, args.config)
|
|
99
|
+
const cms = Effect.map(getContextMap.pipe(Effect.orDie), (_) => ({
|
|
100
|
+
get: (id: string) => _.get(`${name}.${id}`),
|
|
101
|
+
set: (id: string, etag: string | undefined) => _.set(`${name}.${id}`, etag)
|
|
102
|
+
}))
|
|
103
|
+
|
|
104
|
+
const pub = "publishEvents" in args
|
|
105
|
+
? args.publishEvents
|
|
106
|
+
: () => Effect.void
|
|
107
|
+
const changeFeed = yield* PubSub.unbounded<[T[], "save" | "remove"]>()
|
|
108
|
+
|
|
109
|
+
const allE = cms
|
|
110
|
+
.pipe(Effect.flatMap((cm) => Effect.map(store.all, (_) => _.map((_) => mapReverse(_, cm.set)))))
|
|
111
|
+
|
|
112
|
+
const all = Effect
|
|
113
|
+
.flatMap(
|
|
114
|
+
allE,
|
|
115
|
+
(_) => decodeMany(_).pipe(Effect.orDie)
|
|
116
|
+
)
|
|
117
|
+
.pipe(
|
|
118
|
+
Effect.map((_) => _ as T[]),
|
|
119
|
+
Effect.withSpan("Repository.all", {
|
|
120
|
+
kind: "client",
|
|
121
|
+
attributes: { "app.entity": name }
|
|
122
|
+
}, { captureStackTrace: false })
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
const fieldsSchema = schema as unknown as { fields: any }
|
|
126
|
+
// assumes the id field never needs a service...
|
|
127
|
+
const i = ("fields" in fieldsSchema ? S.Struct(fieldsSchema["fields"]) as unknown as typeof schema : schema)
|
|
128
|
+
.pipe((_) => {
|
|
129
|
+
let ast = _.ast
|
|
130
|
+
if (ast._tag === "Declaration") ast = ast.typeParameters[0]!
|
|
131
|
+
|
|
132
|
+
const pickIdFromAst = (a: SchemaAST.AST) => {
|
|
133
|
+
// Unwrap Declaration (e.g. TaggedClass) to get the underlying Objects AST
|
|
134
|
+
let inner = a
|
|
135
|
+
if (inner._tag === "Declaration") inner = inner.typeParameters[0]!
|
|
136
|
+
// Pick from the original AST to preserve the full encoding chain (e.g. decodeTo transformations).
|
|
137
|
+
// Using toEncoded would lose transformation info needed to encode Type -> Encoded.
|
|
138
|
+
if (SchemaAST.isObjects(inner)) {
|
|
139
|
+
const field = inner.propertySignatures.find((_) => _.name === idKey)
|
|
140
|
+
if (field) {
|
|
141
|
+
return S.Struct({ [idKey]: S.make(field.type) }) as unknown as Codec<T, Encoded>
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return S.make(a) as unknown as Codec<T, Encoded>
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return ast._tag === "Union"
|
|
148
|
+
// we need to get the Objects (TypeLiteral), in case of class it has encoding chain...
|
|
149
|
+
? S.Union(
|
|
150
|
+
ast.types.map((_) => pickIdFromAst(_))
|
|
151
|
+
)
|
|
152
|
+
: pickIdFromAst(ast)
|
|
153
|
+
})
|
|
154
|
+
const encodeId = flow(S.encodeEffect(i), provideRctx)
|
|
155
|
+
const encodeIdOnly = (id: string) =>
|
|
156
|
+
encodeId({ [idKey]: id } as any).pipe(
|
|
157
|
+
Effect.map((_: Record<string, unknown>) => _[idKey as string] as Encoded[IdKey])
|
|
158
|
+
)
|
|
159
|
+
const findEId = Effect.fnUntraced(function*(id: Encoded[IdKey]) {
|
|
160
|
+
yield* Effect.annotateCurrentSpan({ "app.entity.id": id })
|
|
161
|
+
|
|
162
|
+
return yield* Effect.flatMap(
|
|
163
|
+
store.find(id),
|
|
164
|
+
(item) =>
|
|
165
|
+
Effect.gen(function*() {
|
|
166
|
+
const { set } = yield* cms
|
|
167
|
+
return item.pipe(Option.map((_) => mapReverse(_, set)))
|
|
168
|
+
})
|
|
169
|
+
)
|
|
170
|
+
})
|
|
171
|
+
// TODO: select the particular field, instead of as struct
|
|
172
|
+
const findE = Effect.fnUntraced(function*(id: T[IdKey]) {
|
|
173
|
+
yield* Effect.annotateCurrentSpan({ "app.entity.id": id })
|
|
174
|
+
|
|
175
|
+
return yield* pipe(
|
|
176
|
+
encodeId({ [idKey]: id } as any),
|
|
177
|
+
Effect.orDie,
|
|
178
|
+
Effect.map((_) => (_ as any)[idKey]),
|
|
179
|
+
Effect.flatMap(findEId)
|
|
180
|
+
)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
const find = Effect.fn("Repository.find", {
|
|
184
|
+
kind: "client",
|
|
185
|
+
attributes: { "app.entity": name }
|
|
186
|
+
})(function*(id: T[IdKey]) {
|
|
187
|
+
yield* Effect.annotateCurrentSpan({ "app.entity.id": id })
|
|
188
|
+
return yield* flatMapOption(findE(id), (_) => Effect.orDie(decode(_)))
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
const saveAllE = (a: Iterable<Encoded>) =>
|
|
192
|
+
flatMapOption(
|
|
193
|
+
Effect
|
|
194
|
+
.sync(() => toNonEmptyArray([...a])),
|
|
195
|
+
(a) =>
|
|
196
|
+
Effect.gen(function*() {
|
|
197
|
+
const { get, set } = yield* cms
|
|
198
|
+
const items = a.map((_) => mapToPersistenceModel(_, get))
|
|
199
|
+
const ret = yield* store.batchSet(items)
|
|
200
|
+
ret.forEach((_) => set(_[idKey], _._etag))
|
|
201
|
+
})
|
|
202
|
+
)
|
|
203
|
+
.pipe(Effect.asVoid)
|
|
204
|
+
|
|
205
|
+
const saveAll = (a: Iterable<T>) =>
|
|
206
|
+
encodeMany(Array.fromIterable(a))
|
|
207
|
+
.pipe(
|
|
208
|
+
Effect.orDie,
|
|
209
|
+
Effect.andThen(saveAllE)
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
const saveAndPublish = Effect.fn("Repository.saveAndPublish", { attributes: { "app.entity": name } })(
|
|
213
|
+
function*(items: Iterable<T>, events: Iterable<Evt> = []) {
|
|
214
|
+
const it = Chunk.fromIterable(items)
|
|
215
|
+
const evts = [...events]
|
|
216
|
+
yield* Effect.annotateCurrentSpan({
|
|
217
|
+
"app.entity.ids": Chunk.map(it, (_) => _[idKey]),
|
|
218
|
+
"app.event.count": evts.length
|
|
219
|
+
})
|
|
220
|
+
return yield* saveAll(it)
|
|
221
|
+
.pipe(
|
|
222
|
+
Effect.andThen(Effect.sync(() => toNonEmptyArray(evts))),
|
|
223
|
+
// TODO: for full consistency the events should be stored within the same database transaction, and then picked up.
|
|
224
|
+
(_) => flatMapOption(_, pub),
|
|
225
|
+
Effect.andThen(PubSub.publish(changeFeed, [Chunk.toArray(it), "save"] as [T[], "save" | "remove"])),
|
|
226
|
+
Effect.asVoid
|
|
227
|
+
)
|
|
228
|
+
}
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
const removeAndPublish = Effect.fn("Repository.removeAndPublish", { attributes: { "app.entity": name } })(
|
|
232
|
+
function*(a: Iterable<T>, events: Iterable<Evt> = []) {
|
|
233
|
+
const { set } = yield* cms
|
|
234
|
+
const it = [...a]
|
|
235
|
+
const evts = [...events]
|
|
236
|
+
yield* Effect.annotateCurrentSpan({
|
|
237
|
+
"app.entity.ids": it.map((_) => _[idKey]),
|
|
238
|
+
"app.event.count": evts.length
|
|
239
|
+
})
|
|
240
|
+
const items = yield* encodeMany(it).pipe(Effect.orDie)
|
|
241
|
+
if (Array.isReadonlyArrayNonEmpty(items)) {
|
|
242
|
+
yield* store.batchRemove(
|
|
243
|
+
items.map((_) => (_[idKey])),
|
|
244
|
+
args.config?.partitionValue?.(items[0])
|
|
245
|
+
)
|
|
246
|
+
for (const e of items) {
|
|
247
|
+
set(e[idKey], undefined)
|
|
248
|
+
}
|
|
249
|
+
yield* Effect
|
|
250
|
+
.sync(() => toNonEmptyArray(evts))
|
|
251
|
+
// TODO: for full consistency the events should be stored within the same database transaction, and then picked up.
|
|
252
|
+
.pipe((_) => flatMapOption(_, pub))
|
|
253
|
+
|
|
254
|
+
yield* PubSub.publish(changeFeed, [it, "remove"] as [T[], "save" | "remove"])
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
const removeById = Effect.fn("Repository.removeById", { attributes: { "app.entity": name } })(
|
|
260
|
+
function*(idOrIds: T[IdKey] | ReadonlyArray<T[IdKey]>) {
|
|
261
|
+
const ids = globalThis.Array.isArray(idOrIds)
|
|
262
|
+
? idOrIds as readonly T[IdKey][]
|
|
263
|
+
: [idOrIds as T[IdKey]]
|
|
264
|
+
if (!Array.isReadonlyArrayNonEmpty(ids)) {
|
|
265
|
+
return
|
|
266
|
+
}
|
|
267
|
+
const { set } = yield* cms
|
|
268
|
+
const eids = yield* Effect.forEach(ids, (_) => encodeIdOnly(_ as any)).pipe(Effect.orDie)
|
|
269
|
+
yield* Effect.annotateCurrentSpan({ "app.entity.ids": eids })
|
|
270
|
+
yield* store.batchRemove(eids)
|
|
271
|
+
for (const id of eids) {
|
|
272
|
+
set(id, undefined)
|
|
273
|
+
}
|
|
274
|
+
yield* PubSub.publish(changeFeed, [[], "remove"] as [T[], "save" | "remove"])
|
|
275
|
+
}
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
const parseMany = Effect.fn("parseMany", {
|
|
279
|
+
attributes: { "app.entity": name, "app.query.mode": "transform" }
|
|
280
|
+
})(
|
|
281
|
+
function*(items: readonly PM[]) {
|
|
282
|
+
const cm = yield* cms
|
|
283
|
+
return yield* decodeMany(items.map((_) => mapReverse(_, cm.set))).pipe(Effect.orDie)
|
|
284
|
+
}
|
|
285
|
+
)
|
|
286
|
+
const decodeManyCache = new WeakMap<
|
|
287
|
+
S.Codec<any, any, any>,
|
|
288
|
+
(i: readonly any[]) => Effect.Effect<any, any, any>
|
|
289
|
+
>()
|
|
290
|
+
const getDecodeMany = (s: S.Codec<any, Encoded, any>) => {
|
|
291
|
+
let dec = decodeManyCache.get(s)
|
|
292
|
+
if (!dec) {
|
|
293
|
+
dec = S.decodeEffectConcurrently(S.Array(s))
|
|
294
|
+
decodeManyCache.set(s, dec)
|
|
295
|
+
}
|
|
296
|
+
return dec
|
|
297
|
+
}
|
|
298
|
+
const parseMany2 = Effect.fn("parseMany", {
|
|
299
|
+
attributes: { "app.entity": name, "app.query.mode": "transform" }
|
|
300
|
+
})(
|
|
301
|
+
function*<A, R>(items: readonly PM[], schema: S.Codec<A, Encoded, R>) {
|
|
302
|
+
const cm = yield* cms
|
|
303
|
+
return yield* getDecodeMany(schema)(items.map((_) => mapReverse(_, cm.set))).pipe(Effect.orDie)
|
|
304
|
+
}
|
|
305
|
+
)
|
|
306
|
+
const filter = <U extends keyof Encoded = keyof Encoded>(args: FilterArgs<Encoded, U>) =>
|
|
307
|
+
store
|
|
308
|
+
.filter(
|
|
309
|
+
// always enforce id and _etag because they are system fields, required for etag tracking etc
|
|
310
|
+
{
|
|
311
|
+
...args,
|
|
312
|
+
select: args.select
|
|
313
|
+
? dedupe([...args.select, idKey, "_etag" as any])
|
|
314
|
+
: undefined
|
|
315
|
+
} as typeof args
|
|
316
|
+
)
|
|
317
|
+
.pipe(
|
|
318
|
+
Effect.tap((items) =>
|
|
319
|
+
Effect.map(cms, ({ set }) => items.forEach((_) => set((_ as Encoded)[idKey], (_ as PM)._etag)))
|
|
320
|
+
)
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
// TODO: For raw we should use S.from, and drop the R...
|
|
324
|
+
const query: {
|
|
325
|
+
<A, R, From extends FieldValues>(
|
|
326
|
+
q: Q.QueryProjection<Encoded extends From ? From : never, A, R>
|
|
327
|
+
): Effect.Effect<readonly A[], S.SchemaError, Exclude<R, RCtx>>
|
|
328
|
+
<A, R, EncodedRefined extends Encoded = Encoded>(
|
|
329
|
+
q: Q.QAll<NoInfer<Encoded>, NoInfer<EncodedRefined>, A, R>
|
|
330
|
+
): Effect.Effect<readonly A[], never, Exclude<R, RCtx>>
|
|
331
|
+
} = (<A, R, EncodedRefined extends Encoded = Encoded>(q: Q.QAll<Encoded, EncodedRefined, A, R>) => {
|
|
332
|
+
const a = Q.toFilter(q, schema)
|
|
333
|
+
// Mode dispatch — see `Q.project` JSDoc for the contract:
|
|
334
|
+
// aggregate: GROUP BY + aggregate functions at DB level; decode raw rows with schema; SchemaError surfaces.
|
|
335
|
+
// project : decode raw encoded rows with schema; no PM reverse-mapping; SchemaError surfaces.
|
|
336
|
+
// collect : same as project, but schema yields Option and None rows are dropped.
|
|
337
|
+
// transform: PM reverse-map (re-inject _etag/PM state from cms cache) then decode; orDie.
|
|
338
|
+
const eff = a.mode === "aggregate"
|
|
339
|
+
? store
|
|
340
|
+
// `a.select` contains `{ key, aggregate }` items not expressible in FilterFunc<Encoded, U>'s
|
|
341
|
+
// `U extends keyof Encoded` generic. Cast is unavoidable until FilterFunc supports aggregate mode.
|
|
342
|
+
.filter(a as any)
|
|
343
|
+
// Decode raw aggregate rows directly — no PM reverse-mapping, no id/_etag needed.
|
|
344
|
+
.pipe(
|
|
345
|
+
Effect.andThen(
|
|
346
|
+
flow(
|
|
347
|
+
S.decodeEffectConcurrently(S.Array(a.schema ?? schema)),
|
|
348
|
+
provideRctx,
|
|
349
|
+
Effect.withSpan("parseMany", {
|
|
350
|
+
attributes: { "app.entity": name, "app.query.mode": "aggregate" }
|
|
351
|
+
})
|
|
352
|
+
)
|
|
353
|
+
)
|
|
354
|
+
)
|
|
355
|
+
: a.mode === "project"
|
|
356
|
+
? filter(a)
|
|
357
|
+
// TODO: mapFrom but need to support per field and dependencies
|
|
358
|
+
.pipe(
|
|
359
|
+
Effect.andThen(
|
|
360
|
+
flow(
|
|
361
|
+
S.decodeEffectConcurrently(S.Array(a.schema ?? schema)),
|
|
362
|
+
provideRctx,
|
|
363
|
+
Effect.withSpan("parseMany", {
|
|
364
|
+
attributes: { "app.entity": name, "app.query.mode": "project" }
|
|
365
|
+
})
|
|
366
|
+
)
|
|
367
|
+
)
|
|
368
|
+
)
|
|
369
|
+
: a.mode === "collect"
|
|
370
|
+
? filter(a)
|
|
371
|
+
// TODO: mapFrom but need to support per field and dependencies
|
|
372
|
+
.pipe(
|
|
373
|
+
Effect.flatMap(flow(
|
|
374
|
+
S.decodeEffectConcurrently(S.Array(a.schema)),
|
|
375
|
+
Effect.map(Array.getSomes),
|
|
376
|
+
provideRctx,
|
|
377
|
+
Effect.withSpan("parseMany", {
|
|
378
|
+
attributes: { "app.entity": name, "app.query.mode": "collect" }
|
|
379
|
+
})
|
|
380
|
+
))
|
|
381
|
+
)
|
|
382
|
+
: Effect.flatMap(
|
|
383
|
+
filter(a),
|
|
384
|
+
(_) =>
|
|
385
|
+
Unify.unify(
|
|
386
|
+
a.schema
|
|
387
|
+
// TODO: partial may not match?
|
|
388
|
+
? parseMany2(_ as any, a.schema as any)
|
|
389
|
+
: parseMany(_ as any)
|
|
390
|
+
)
|
|
391
|
+
)
|
|
392
|
+
return pipe(
|
|
393
|
+
a.ttype === "one"
|
|
394
|
+
? Effect.flatMap(
|
|
395
|
+
eff,
|
|
396
|
+
flow(
|
|
397
|
+
Array.head,
|
|
398
|
+
Option.match({
|
|
399
|
+
onNone: () => Effect.fail(new NotFoundError({ id: "query", /* TODO */ type: name })),
|
|
400
|
+
onSome: Effect.succeed
|
|
401
|
+
})
|
|
402
|
+
)
|
|
403
|
+
)
|
|
404
|
+
: a.ttype === "count"
|
|
405
|
+
? Effect
|
|
406
|
+
.map(eff, (_) => NonNegativeInt(_.length))
|
|
407
|
+
.pipe(Effect.orDie)
|
|
408
|
+
: eff,
|
|
409
|
+
Effect.tap((r) =>
|
|
410
|
+
Effect.annotateCurrentSpan({
|
|
411
|
+
"app.query.ttype": a.ttype,
|
|
412
|
+
"app.query.mode": a.mode,
|
|
413
|
+
"db.response.returned_rows": Array.isArray(r) ? r.length : 1
|
|
414
|
+
})
|
|
415
|
+
),
|
|
416
|
+
Effect.withSpan("Repository.query", {
|
|
417
|
+
kind: "client",
|
|
418
|
+
attributes: { "app.entity": name }
|
|
419
|
+
}, { captureStackTrace: false })
|
|
420
|
+
)
|
|
421
|
+
}) as any
|
|
422
|
+
|
|
423
|
+
const validateSample = Effect.fn("Repository.validateSample", { attributes: { "app.entity": name } })(
|
|
424
|
+
function*(options?: {
|
|
425
|
+
percentage?: number
|
|
426
|
+
maxItems?: number
|
|
427
|
+
}) {
|
|
428
|
+
const percentage = options?.percentage ?? 0.1 // default 10%
|
|
429
|
+
const maxItems = options?.maxItems
|
|
430
|
+
|
|
431
|
+
// 1. get all IDs with projection (bypasses main schema decode)
|
|
432
|
+
const allIds = yield* store
|
|
433
|
+
.filter({
|
|
434
|
+
t: null as unknown as Encoded,
|
|
435
|
+
select: [idKey as keyof Encoded]
|
|
436
|
+
})
|
|
437
|
+
.pipe(Effect.withSpan("Repository.filter", {
|
|
438
|
+
kind: "client",
|
|
439
|
+
attributes: { "app.entity": name }
|
|
440
|
+
}, { captureStackTrace: false }))
|
|
441
|
+
|
|
442
|
+
// 2. random subset
|
|
443
|
+
const shuffled = [...allIds].sort(() => Math.random() - 0.5)
|
|
444
|
+
const sampleSize = Math.min(
|
|
445
|
+
maxItems ?? Infinity,
|
|
446
|
+
Math.ceil(allIds.length * percentage)
|
|
447
|
+
)
|
|
448
|
+
const sample = shuffled.slice(0, sampleSize)
|
|
449
|
+
|
|
450
|
+
// 3. validate each item
|
|
451
|
+
const errors: ValidationError[] = []
|
|
452
|
+
|
|
453
|
+
for (const item of sample) {
|
|
454
|
+
const id = item[idKey]
|
|
455
|
+
const rawResult = yield* store.find(id).pipe(
|
|
456
|
+
Effect.withSpan("Repository.find", {
|
|
457
|
+
kind: "client",
|
|
458
|
+
attributes: { "app.entity": name, "app.entity.id": id }
|
|
459
|
+
}, { captureStackTrace: false })
|
|
460
|
+
)
|
|
461
|
+
|
|
462
|
+
if (Option.isNone(rawResult)) continue
|
|
463
|
+
|
|
464
|
+
const rawData = rawResult.value as Encoded
|
|
465
|
+
const jitMResult = mapFrom(rawData) // apply jitM
|
|
466
|
+
|
|
467
|
+
const decodeResult = yield* S.decodeEffectConcurrently(schema)(jitMResult).pipe(
|
|
468
|
+
Effect.result,
|
|
469
|
+
provideRctx
|
|
470
|
+
)
|
|
471
|
+
|
|
472
|
+
if (Result.isFailure(decodeResult)) {
|
|
473
|
+
errors.push(
|
|
474
|
+
ValidationError.make({
|
|
475
|
+
id,
|
|
476
|
+
rawData,
|
|
477
|
+
jitMResult,
|
|
478
|
+
error: decodeResult.failure
|
|
479
|
+
})
|
|
480
|
+
)
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return ValidationResult.make({
|
|
485
|
+
total: NonNegativeInt(allIds.length),
|
|
486
|
+
sampled: NonNegativeInt(sample.length),
|
|
487
|
+
valid: NonNegativeInt(sample.length - errors.length),
|
|
488
|
+
errors
|
|
489
|
+
})
|
|
490
|
+
}
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
const r = {
|
|
494
|
+
changeFeed,
|
|
495
|
+
itemType: name,
|
|
496
|
+
idKey,
|
|
497
|
+
find,
|
|
498
|
+
all,
|
|
499
|
+
saveAndPublish,
|
|
500
|
+
removeAndPublish,
|
|
501
|
+
removeById,
|
|
502
|
+
seedNamespace: (namespace: string) => store.seedNamespace(namespace),
|
|
503
|
+
validateSample,
|
|
504
|
+
queryRaw<A, Out, QR>(schema: S.Codec<A, Out, QR>, q: Q.RawQuery<Encoded, Out>) {
|
|
505
|
+
const dec = S.decodeEffectConcurrently(S.Array(schema))
|
|
506
|
+
return store.queryRaw(q).pipe(
|
|
507
|
+
Effect.flatMap(dec),
|
|
508
|
+
Effect.withSpan("Repository.queryRaw", {
|
|
509
|
+
kind: "client",
|
|
510
|
+
attributes: { "app.entity": name }
|
|
511
|
+
}, { captureStackTrace: false })
|
|
512
|
+
)
|
|
513
|
+
},
|
|
514
|
+
query(q: any) {
|
|
515
|
+
// eslint-disable-next-line prefer-rest-params
|
|
516
|
+
return query(typeof q === "function" ? Pipeable.pipeArguments(Q.make(), arguments) : q) as any
|
|
517
|
+
},
|
|
518
|
+
/**
|
|
519
|
+
* @internal
|
|
520
|
+
*/
|
|
521
|
+
mapped: <A, R>(schema: S.Codec<A, any, R>) => {
|
|
522
|
+
const dec = S.decodeEffectConcurrently(schema)
|
|
523
|
+
const encMany = S.encodeEffect(S.Array(schema))
|
|
524
|
+
const decMany = S.decodeEffectConcurrently(S.Array(schema))
|
|
525
|
+
const spanAttrs = { kind: "client" as const, attributes: { "app.entity": name } }
|
|
526
|
+
return {
|
|
527
|
+
all: allE.pipe(
|
|
528
|
+
Effect.flatMap(decMany),
|
|
529
|
+
Effect.map((_) => _ as any[]),
|
|
530
|
+
Effect.withSpan("Repository.mapped.all", spanAttrs, { captureStackTrace: false })
|
|
531
|
+
),
|
|
532
|
+
find: (id: T[IdKey]) =>
|
|
533
|
+
flatMapOption(findE(id), dec).pipe(
|
|
534
|
+
Effect.withSpan("Repository.mapped.find", {
|
|
535
|
+
...spanAttrs,
|
|
536
|
+
attributes: { ...spanAttrs.attributes, "app.entity.id": id }
|
|
537
|
+
}, { captureStackTrace: false })
|
|
538
|
+
),
|
|
539
|
+
// query: (q: any) => {
|
|
540
|
+
// const a = Q.toFilter(q)
|
|
541
|
+
|
|
542
|
+
// return filter(a)
|
|
543
|
+
// .pipe(
|
|
544
|
+
// Effect.flatMap(decMany),
|
|
545
|
+
// Effect.map((_) => _ as any[]),
|
|
546
|
+
// Effect.withSpan("Repository.mapped.query [effect-app/infra]", {
|
|
547
|
+
// captureStackTrace: false,
|
|
548
|
+
// attributes: {
|
|
549
|
+
// "repository.model_name": name,
|
|
550
|
+
// query: { ...a, schema: a.schema ? "__SCHEMA__" : a.schema, filter: a.filter.build() }
|
|
551
|
+
// }
|
|
552
|
+
// })
|
|
553
|
+
// )
|
|
554
|
+
// },
|
|
555
|
+
save: (...xes: any[]) =>
|
|
556
|
+
Effect.flatMap(encMany(xes), (_) => saveAllE(_)).pipe(
|
|
557
|
+
Effect.withSpan("Repository.mapped.save", spanAttrs, { captureStackTrace: false })
|
|
558
|
+
)
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return r as Repository<T, Encoded, Evt, ItemType, IdKey, Exclude<R, RCtx>, RPublish, RCtx>
|
|
563
|
+
})
|
|
564
|
+
.pipe(Effect
|
|
565
|
+
// .withSpan("Repository.make [effect-app/infra]", { attributes: { "repository.model_name": name } })
|
|
566
|
+
.withLogSpan("Repository.make: " + name))
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
return {
|
|
570
|
+
make,
|
|
571
|
+
Q: Q.make<Encoded>()
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const pluralize = (s: string) =>
|
|
577
|
+
s.endsWith("s")
|
|
578
|
+
? s + "es"
|
|
579
|
+
: s.endsWith("y")
|
|
580
|
+
? s.substring(0, s.length - 1) + "ies"
|
|
581
|
+
: s + "s"
|
|
582
|
+
|
|
583
|
+
export function makeStore<Encoded extends FieldValues>() {
|
|
584
|
+
return <
|
|
585
|
+
ItemType extends string,
|
|
586
|
+
R,
|
|
587
|
+
E,
|
|
588
|
+
T,
|
|
589
|
+
IdKey extends keyof Encoded
|
|
590
|
+
>(
|
|
591
|
+
name: ItemType,
|
|
592
|
+
schema: S.Codec<T, E, R>,
|
|
593
|
+
mapTo: (e: E, etag: string | undefined) => Encoded,
|
|
594
|
+
idKey: IdKey
|
|
595
|
+
) => {
|
|
596
|
+
function makeStore<RInitial = never, EInitial = never>(
|
|
597
|
+
makeInitial?: Effect.Effect<readonly T[], EInitial, RInitial>,
|
|
598
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
599
|
+
partitionValue?: (e?: Encoded) => string
|
|
600
|
+
}
|
|
601
|
+
) {
|
|
602
|
+
function encodeToEncoded() {
|
|
603
|
+
const getEtag = () => undefined
|
|
604
|
+
return (t: T) =>
|
|
605
|
+
S.encodeEffect(schema)(t).pipe(
|
|
606
|
+
Effect.orDie,
|
|
607
|
+
Effect.map((_) => mapToPersistenceModel(_, getEtag))
|
|
608
|
+
)
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function mapToPersistenceModel(
|
|
612
|
+
e: E,
|
|
613
|
+
getEtag: (id: string) => string | undefined
|
|
614
|
+
): Encoded {
|
|
615
|
+
return mapTo(e, getEtag((e as any)[idKey] as string))
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return Effect.gen(function*() {
|
|
619
|
+
const { make } = yield* StoreMaker
|
|
620
|
+
|
|
621
|
+
const store = yield* make<IdKey, Encoded, RInitial | R, EInitial>(
|
|
622
|
+
pluralize(name),
|
|
623
|
+
idKey,
|
|
624
|
+
makeInitial
|
|
625
|
+
? makeInitial
|
|
626
|
+
.pipe(
|
|
627
|
+
Effect.flatMap(Effect.forEach(encodeToEncoded())),
|
|
628
|
+
Effect.withSpan("Repository.makeInitial", {
|
|
629
|
+
attributes: { "app.entity": name }
|
|
630
|
+
}, {
|
|
631
|
+
captureStackTrace: false
|
|
632
|
+
})
|
|
633
|
+
)
|
|
634
|
+
: undefined,
|
|
635
|
+
{
|
|
636
|
+
...config,
|
|
637
|
+
partitionValue: config?.partitionValue
|
|
638
|
+
?? ((_) => "primary") /*(isIntegrationEvent(r) ? r.companyId : r.id*/
|
|
639
|
+
}
|
|
640
|
+
)
|
|
641
|
+
|
|
642
|
+
return store
|
|
643
|
+
})
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
return makeStore
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
export interface Repos<
|
|
651
|
+
T,
|
|
652
|
+
Encoded extends { id: string },
|
|
653
|
+
RSchema,
|
|
654
|
+
Evt,
|
|
655
|
+
ItemType extends string,
|
|
656
|
+
IdKey extends keyof T,
|
|
657
|
+
RPublish
|
|
658
|
+
> {
|
|
659
|
+
make<RInitial = never, E = never, R2 = never>(
|
|
660
|
+
args: [Evt] extends [never] ? {
|
|
661
|
+
makeInitial?: Effect.Effect<readonly T[], E, RInitial> | undefined
|
|
662
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
663
|
+
partitionValue?: (e?: Encoded) => string
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
: {
|
|
667
|
+
publishEvents: (evt: NonEmptyReadonlyArray<Evt>) => Effect.Effect<void, never, R2>
|
|
668
|
+
makeInitial?: Effect.Effect<readonly T[], E, RInitial> | undefined
|
|
669
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
670
|
+
partitionValue?: (e?: Encoded) => string
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
): Effect.Effect<Repository<T, Encoded, Evt, ItemType, IdKey, RSchema, RPublish>, E, StoreMaker | RInitial | R2>
|
|
674
|
+
makeWith<Out, RInitial = never, E = never, R2 = never>(
|
|
675
|
+
args: [Evt] extends [never] ? {
|
|
676
|
+
makeInitial?: Effect.Effect<readonly T[], E, RInitial> | undefined
|
|
677
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
678
|
+
partitionValue?: (e?: Encoded) => string
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
: {
|
|
682
|
+
publishEvents: (evt: NonEmptyReadonlyArray<Evt>) => Effect.Effect<void, never, R2>
|
|
683
|
+
makeInitial?: Effect.Effect<readonly T[], E, RInitial> | undefined
|
|
684
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
685
|
+
partitionValue?: (e?: Encoded) => string
|
|
686
|
+
}
|
|
687
|
+
},
|
|
688
|
+
f: (r: Repository<T, Encoded, Evt, ItemType, IdKey, RSchema, RPublish>) => Out
|
|
689
|
+
): Effect.Effect<Out, E, StoreMaker | RInitial | R2>
|
|
690
|
+
readonly Q: ReturnType<typeof Q.make<Encoded>>
|
|
691
|
+
readonly type: Repository<T, Encoded, Evt, ItemType, IdKey, RSchema, RPublish>
|
|
692
|
+
}
|