effect-app 4.0.0-beta.27 → 4.0.0-beta.272
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 +1275 -0
- package/dist/Array.d.ts +2 -1
- package/dist/Array.d.ts.map +1 -1
- package/dist/Array.js +4 -4
- package/dist/Chunk.d.ts.map +1 -1
- package/dist/Config/SecretURL.d.ts +3 -1
- package/dist/Config/SecretURL.d.ts.map +1 -1
- package/dist/Config/SecretURL.js +3 -6
- package/dist/Config/internal/configSecretURL.d.ts.map +1 -1
- package/dist/Config/internal/configSecretURL.js +2 -2
- package/dist/Config.d.ts +2 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +8 -0
- package/dist/ConfigProvider.d.ts +2 -0
- package/dist/ConfigProvider.d.ts.map +1 -0
- package/dist/ConfigProvider.js +6 -0
- package/dist/{ServiceMap.d.ts → Context.d.ts} +18 -20
- package/dist/Context.d.ts.map +1 -0
- package/dist/Context.js +67 -0
- package/dist/Effect.d.ts +10 -9
- 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.map +1 -1
- package/dist/Layer.d.ts +10 -6
- package/dist/Layer.d.ts.map +1 -1
- package/dist/Layer.js +3 -2
- package/dist/Model/Repository/Registry.d.ts +22 -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 +63 -0
- package/dist/Model/Repository/internal/internal.d.ts.map +1 -0
- package/dist/Model/Repository/internal/internal.js +430 -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 +54 -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 +121 -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 +58 -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 +94 -0
- package/dist/Model/filter/types/path/eager.d.ts.map +1 -0
- package/dist/Model/filter/types/path/eager.js +36 -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 +493 -0
- package/dist/Model/query/dsl.d.ts.map +1 -0
- package/dist/Model/query/dsl.js +376 -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 +3 -1
- package/dist/NonEmptySet.d.ts.map +1 -1
- package/dist/NonEmptySet.js +2 -2
- package/dist/Option.d.ts +1 -0
- package/dist/Option.d.ts.map +1 -1
- package/dist/Option.js +3 -1
- package/dist/Pure.d.ts +7 -5
- 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 +154 -0
- package/dist/RequestContext.d.ts.map +1 -0
- package/dist/RequestContext.js +54 -0
- package/dist/Schema/Class.d.ts +160 -19
- package/dist/Schema/Class.d.ts.map +1 -1
- package/dist/Schema/Class.js +260 -17
- package/dist/Schema/FastCheck.d.ts.map +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 +4 -2
- package/dist/Schema/brand.d.ts.map +1 -1
- package/dist/Schema/brand.js +3 -1
- package/dist/Schema/email.d.ts.map +1 -1
- package/dist/Schema/email.js +7 -4
- package/dist/Schema/ext.d.ts +338 -55
- package/dist/Schema/ext.d.ts.map +1 -1
- package/dist/Schema/ext.js +358 -53
- package/dist/Schema/moreStrings.d.ts +82 -36
- package/dist/Schema/moreStrings.d.ts.map +1 -1
- package/dist/Schema/moreStrings.js +49 -42
- package/dist/Schema/numbers.d.ts +34 -21
- package/dist/Schema/numbers.d.ts.map +1 -1
- package/dist/Schema/numbers.js +55 -12
- package/dist/Schema/phoneNumber.d.ts.map +1 -1
- package/dist/Schema/phoneNumber.js +6 -3
- package/dist/Schema/strings.d.ts +18 -4
- package/dist/Schema/strings.d.ts.map +1 -1
- package/dist/Schema/strings.js +1 -5
- package/dist/Schema.d.ts +213 -7
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +190 -11
- package/dist/Set.d.ts +4 -1
- package/dist/Set.d.ts.map +1 -1
- package/dist/Set.js +3 -2
- package/dist/Store.d.ts +170 -0
- package/dist/Store.d.ts.map +1 -0
- package/dist/Store.js +121 -0
- package/dist/_ext/Array.d.ts +1 -1
- package/dist/_ext/Array.d.ts.map +1 -1
- package/dist/_ext/Array.js +4 -2
- package/dist/_ext/misc.d.ts +4 -1
- package/dist/_ext/misc.d.ts.map +1 -1
- package/dist/_ext/misc.js +4 -2
- package/dist/_ext/ord.ext.d.ts +2 -1
- package/dist/_ext/ord.ext.d.ts.map +1 -1
- package/dist/_ext/ord.ext.js +2 -2
- 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 +19 -31
- package/dist/client/apiClientFactory.d.ts.map +1 -1
- package/dist/client/apiClientFactory.js +104 -34
- package/dist/client/clientFor.d.ts +52 -18
- package/dist/client/clientFor.d.ts.map +1 -1
- package/dist/client/clientFor.js +9 -1
- package/dist/client/errors.d.ts +82 -27
- package/dist/client/errors.d.ts.map +1 -1
- package/dist/client/errors.js +75 -19
- package/dist/client/makeClient.d.ts +494 -32
- package/dist/client/makeClient.d.ts.map +1 -1
- package/dist/client/makeClient.js +66 -24
- package/dist/client.d.ts +1 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +2 -1
- package/dist/faker.d.ts.map +1 -1
- package/dist/http/Request.d.ts +1 -1
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +2 -2
- package/dist/ids.d.ts +42 -14
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js +30 -5
- package/dist/index.d.ts +6 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -8
- package/dist/middleware.d.ts +13 -7
- 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 +11 -7
- package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
- package/dist/rpc/MiddlewareMaker.js +59 -38
- package/dist/rpc/RpcContextMap.d.ts +3 -3
- package/dist/rpc/RpcContextMap.d.ts.map +1 -1
- package/dist/rpc/RpcContextMap.js +4 -4
- package/dist/rpc/RpcMiddleware.d.ts +14 -10
- package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
- package/dist/rpc/RpcMiddleware.js +1 -1
- package/dist/rpc.d.ts +1 -1
- 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/setupRequest.d.ts +19 -0
- package/dist/setupRequest.d.ts.map +1 -0
- package/dist/setupRequest.js +69 -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 +1 -1
- package/dist/transform.d.ts.map +1 -1
- package/dist/transform.js +4 -5
- package/dist/utils/effectify.d.ts +1 -1
- package/dist/utils/effectify.d.ts.map +1 -1
- package/dist/utils/effectify.js +2 -2
- package/dist/utils/extend.d.ts.map +1 -1
- package/dist/utils/gen.d.ts +4 -4
- package/dist/utils/gen.d.ts.map +1 -1
- package/dist/utils/logLevel.d.ts +2 -2
- package/dist/utils/logLevel.d.ts.map +1 -1
- package/dist/utils/logger.d.ts +4 -3
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +4 -4
- package/dist/utils.d.ts +34 -39
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +33 -37
- package/dist/validation/validators.d.ts.map +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 +22 -247
- package/src/Array.ts +5 -5
- package/src/Chunk.ts +3 -3
- package/src/Config/SecretURL.ts +6 -3
- package/src/Config/internal/configSecretURL.ts +2 -2
- package/src/Config.ts +7 -0
- package/src/ConfigProvider.ts +5 -0
- package/src/{ServiceMap.ts → Context.ts} +56 -63
- package/src/Effect.ts +14 -16
- package/src/Emailer.ts +51 -0
- package/src/Inputify.type.ts +1 -1
- package/src/Layer.ts +11 -7
- package/src/Model/Repository/Registry.ts +35 -0
- package/src/Model/Repository/ext.ts +375 -0
- package/src/Model/Repository/internal/internal.ts +741 -0
- package/src/Model/Repository/legacy.ts +29 -0
- package/src/Model/Repository/makeRepo.ts +145 -0
- package/src/Model/Repository/service.ts +676 -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 +329 -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 +2694 -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 +6 -4
- package/src/Option.ts +2 -0
- package/src/Pure.ts +22 -20
- package/src/QueueMaker.ts +19 -0
- package/src/RequestContext.ts +95 -0
- package/src/Schema/Class.ts +593 -59
- 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 +9 -4
- package/src/Schema/ext.ts +446 -91
- package/src/Schema/moreStrings.ts +147 -68
- package/src/Schema/numbers.ts +97 -28
- package/src/Schema/phoneNumber.ts +9 -5
- package/src/Schema/strings.ts +23 -14
- package/src/Schema.ts +389 -25
- package/src/Set.ts +6 -2
- package/src/Store.ts +277 -0
- package/src/_ext/Array.ts +4 -2
- 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 +234 -135
- package/src/client/clientFor.ts +105 -34
- package/src/client/errors.ts +100 -29
- package/src/client/makeClient.ts +594 -73
- package/src/client.ts +5 -4
- package/src/http/Request.ts +3 -3
- package/src/http.ts +1 -1
- package/src/ids.ts +33 -6
- package/src/index.ts +20 -23
- package/src/middleware.ts +13 -9
- package/src/rpc/Invalidation.ts +261 -0
- package/src/rpc/MiddlewareMaker.ts +88 -80
- package/src/rpc/README.md +2 -2
- package/src/rpc/RpcContextMap.ts +7 -6
- package/src/rpc/RpcMiddleware.ts +19 -13
- package/src/rpc.ts +4 -4
- package/src/runtime.ts +56 -0
- package/src/setupRequest.ts +134 -0
- package/src/toast.ts +54 -0
- package/src/transform.ts +4 -4
- 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 +85 -158
- package/src/validation.ts +2 -2
- package/src/withToast.ts +133 -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/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 +1258 -0
- package/test/utils.test.ts +7 -7
- package/tsconfig.base.json +6 -5
- package/tsconfig.json +3 -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.map +0 -1
- package/dist/ServiceMap.js +0 -91
- package/eslint.config.mjs +0 -26
- package/src/Operations.ts +0 -55
package/src/Emailer.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as Data from "effect/Data"
|
|
2
|
+
import type { NonEmptyReadonlyArray } from "./Array.ts"
|
|
3
|
+
import * as Context from "./Context.ts"
|
|
4
|
+
import type * as Effect from "./Effect.ts"
|
|
5
|
+
import type { Email } from "./Schema.ts"
|
|
6
|
+
|
|
7
|
+
export class SendMailError extends Data.TaggedError("SendMailError")<{
|
|
8
|
+
readonly raw: Error
|
|
9
|
+
}> {}
|
|
10
|
+
|
|
11
|
+
export class Emailer extends Context.Opaque<Emailer, {
|
|
12
|
+
sendMail: (msg: EmailMsgOptionalFrom) => Effect.Effect<void, SendMailError>
|
|
13
|
+
}>()("effect-app/Emailer") {}
|
|
14
|
+
|
|
15
|
+
export type EmailData = Email | {
|
|
16
|
+
name?: string
|
|
17
|
+
email: Email
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface EmailContentPart {
|
|
21
|
+
type: string
|
|
22
|
+
value: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type EmailRecipients = EmailData | NonEmptyReadonlyArray<EmailData>
|
|
26
|
+
|
|
27
|
+
export interface EmailMsgBase {
|
|
28
|
+
readonly to: EmailRecipients
|
|
29
|
+
readonly cc?: EmailRecipients
|
|
30
|
+
readonly bcc?: EmailRecipients
|
|
31
|
+
readonly from: EmailData
|
|
32
|
+
readonly replyTo?: EmailData
|
|
33
|
+
readonly subject?: string
|
|
34
|
+
/**
|
|
35
|
+
* should multiple `to` addresess be considered multiple emails?
|
|
36
|
+
* defaults to `true`, not to leak email addresses
|
|
37
|
+
*/
|
|
38
|
+
readonly isMultiple?: boolean
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type EmailContent =
|
|
42
|
+
| { text: string }
|
|
43
|
+
| { html: string }
|
|
44
|
+
| { templateId: string }
|
|
45
|
+
| { content: NonEmptyReadonlyArray<EmailContentPart> }
|
|
46
|
+
|
|
47
|
+
export type EmailMsg =
|
|
48
|
+
& EmailMsgBase
|
|
49
|
+
& EmailContent
|
|
50
|
+
|
|
51
|
+
export type EmailMsgOptionalFrom = Omit<EmailMsgBase, "from"> & Partial<Pick<EmailMsg, "from">> & EmailContent
|
package/src/Inputify.type.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { BuiltInObject } from "effect-app/utils"
|
|
2
2
|
|
|
3
|
-
import type { Primitive } from "./Widen.type.
|
|
3
|
+
import type { Primitive } from "./Widen.type.ts"
|
|
4
4
|
|
|
5
5
|
// Get rid of Date | string, and replace with Date | null
|
|
6
6
|
export type Inputify<T> = Date extends T ? string extends T ? Date | null
|
package/src/Layer.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import type * as Array from "effect/Array"
|
|
2
|
+
import * as Effect from "effect/Effect"
|
|
3
3
|
import { dual } from "effect/Function"
|
|
4
|
-
import
|
|
4
|
+
import * as Layer from "effect/Layer"
|
|
5
|
+
import type * as Scope from "effect/Scope"
|
|
6
|
+
import type * as Types from "effect/Types"
|
|
7
|
+
import type * as Context from "./Context.ts"
|
|
8
|
+
import { type EffectGenUtils } from "./utils/gen.ts"
|
|
5
9
|
|
|
6
10
|
export * from "effect/Layer"
|
|
7
11
|
|
|
@@ -11,13 +15,13 @@ type MakeEff<S, E, R> = {
|
|
|
11
15
|
readonly make: Effect.Effect<S, E, R>
|
|
12
16
|
}
|
|
13
17
|
type MakeGen<S, E = never, R = never> = {
|
|
14
|
-
readonly make: () => Generator<
|
|
18
|
+
readonly make: () => Generator<Effect.Effect<any, E, R>, S>
|
|
15
19
|
}
|
|
16
20
|
type MakeGenNo<S> = {
|
|
17
21
|
readonly make: () => Generator<unknown, S>
|
|
18
22
|
}
|
|
19
23
|
type MakeErr<Opts> = Opts extends { make: () => any } ? EffectGenUtils.Error<Opts["make"]> : never
|
|
20
|
-
type MakeContext<Opts> = Opts extends { make: () => any } ? EffectGenUtils.
|
|
24
|
+
type MakeContext<Opts> = Opts extends { make: () => any } ? EffectGenUtils.Context<Opts["make"]> : never
|
|
21
25
|
|
|
22
26
|
type DependenciesOpt = { dependencies?: Array.NonEmptyReadonlyArray<Layer.Any> }
|
|
23
27
|
type Dependencies = { dependencies: Array.NonEmptyReadonlyArray<Layer.Any> }
|
|
@@ -41,12 +45,12 @@ type PackedOrUnpackedLayer<I, Opts> = Opts extends Dependencies ? PackedLayers<I
|
|
|
41
45
|
|
|
42
46
|
export const make: {
|
|
43
47
|
<I, S>(
|
|
44
|
-
tag:
|
|
48
|
+
tag: Context.Service<I, S>
|
|
45
49
|
): <Opts extends Make<Types.NoInfer<S>, any, any>>(
|
|
46
50
|
options: Opts
|
|
47
51
|
) => PackedOrUnpackedLayer<I, Opts>
|
|
48
52
|
<I, S, Opts extends Make<Types.NoInfer<S>, any, any>>(
|
|
49
|
-
tag:
|
|
53
|
+
tag: Context.Service<I, S>,
|
|
50
54
|
options: Opts
|
|
51
55
|
): PackedOrUnpackedLayer<I, Opts>
|
|
52
56
|
} = dual(2, (tag, options) => {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { DatabaseError } from "../../client/errors.ts"
|
|
2
|
+
import * as Context from "../../Context.ts"
|
|
3
|
+
import * as Effect from "../../Effect.ts"
|
|
4
|
+
|
|
5
|
+
export interface RegisteredRepository {
|
|
6
|
+
readonly seedNamespace: (namespace: string) => Effect.Effect<void, DatabaseError>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const make = Effect.sync(() => {
|
|
10
|
+
const repos = new Map<string, RegisteredRepository>()
|
|
11
|
+
return {
|
|
12
|
+
register(modelName: string, repo: RegisteredRepository) {
|
|
13
|
+
repos.set(modelName, repo)
|
|
14
|
+
},
|
|
15
|
+
seedNamespace: (namespace: string) =>
|
|
16
|
+
Effect.suspend(() =>
|
|
17
|
+
Effect.forEach(
|
|
18
|
+
repos.values(),
|
|
19
|
+
(r) => r.seedNamespace(namespace),
|
|
20
|
+
{ concurrency: "unbounded", discard: true }
|
|
21
|
+
)
|
|
22
|
+
),
|
|
23
|
+
get entries(): ReadonlyMap<string, RegisteredRepository> {
|
|
24
|
+
return repos
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
export class RepositoryRegistry extends Context.Opaque<RepositoryRegistry, {
|
|
30
|
+
readonly register: (modelName: string, repo: RegisteredRepository) => void
|
|
31
|
+
readonly seedNamespace: (namespace: string) => Effect.Effect<void, DatabaseError>
|
|
32
|
+
readonly entries: ReadonlyMap<string, RegisteredRepository>
|
|
33
|
+
}>()("effect-app/RepositoryRegistry", { make }) {}
|
|
34
|
+
|
|
35
|
+
export const RepositoryRegistryLive = RepositoryRegistry.toLayer(RepositoryRegistry.make)
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
3
|
+
import * as Exit from "effect/Exit"
|
|
4
|
+
import * as Request from "effect/Request"
|
|
5
|
+
import * as RequestResolver from "effect/RequestResolver"
|
|
6
|
+
import * as Array from "../../Array.ts"
|
|
7
|
+
import type { NonEmptyArray } from "../../Array.ts"
|
|
8
|
+
import { type InvalidStateError, NotFoundError, type OptimisticConcurrencyException } from "../../client/errors.ts"
|
|
9
|
+
import * as Effect from "../../Effect.ts"
|
|
10
|
+
import * as Option from "../../Option.ts"
|
|
11
|
+
import { type FixEnv, type PureEnv, runTerm } from "../../Pure.ts"
|
|
12
|
+
import { AnyPureDSL } from "../dsl.ts"
|
|
13
|
+
import type { FieldValues } from "../filter/types.ts"
|
|
14
|
+
import type { Query, QueryEnd, QueryWhere } from "../query.ts"
|
|
15
|
+
import * as Q from "../query.ts"
|
|
16
|
+
import type { Repository } from "./service.ts"
|
|
17
|
+
|
|
18
|
+
interface BatchOptions {
|
|
19
|
+
readonly batch?: true | number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const asReadonlyArray = <T>(itemOrItems: T | ReadonlyArray<T>): ReadonlyArray<T> =>
|
|
23
|
+
globalThis.Array.isArray(itemOrItems)
|
|
24
|
+
? itemOrItems as ReadonlyArray<T>
|
|
25
|
+
: [itemOrItems as T]
|
|
26
|
+
|
|
27
|
+
const getBatchSize = (batch?: true | number) =>
|
|
28
|
+
batch === true
|
|
29
|
+
? 100
|
|
30
|
+
: typeof batch === "number" && Number.isFinite(batch) && batch > 0
|
|
31
|
+
? Math.floor(batch)
|
|
32
|
+
: undefined
|
|
33
|
+
|
|
34
|
+
export const extendRepo = <
|
|
35
|
+
T,
|
|
36
|
+
Encoded extends FieldValues,
|
|
37
|
+
Evt,
|
|
38
|
+
ItemType extends string,
|
|
39
|
+
IdKey extends keyof T & keyof Encoded,
|
|
40
|
+
RSchema,
|
|
41
|
+
RPublish,
|
|
42
|
+
RProvided = never
|
|
43
|
+
>(
|
|
44
|
+
repo: Repository<T, Encoded, Evt, ItemType, IdKey, RSchema, RPublish, RProvided>
|
|
45
|
+
) => {
|
|
46
|
+
const get = (id: T[IdKey]) =>
|
|
47
|
+
repo.find(id).pipe(
|
|
48
|
+
Effect.flatMap(Option.match({
|
|
49
|
+
onNone: () => Effect.fail(new NotFoundError<ItemType>({ type: repo.itemType, id })),
|
|
50
|
+
onSome: Effect.succeed
|
|
51
|
+
}))
|
|
52
|
+
)
|
|
53
|
+
function saveManyWithPure_<
|
|
54
|
+
R,
|
|
55
|
+
A,
|
|
56
|
+
E,
|
|
57
|
+
S1 extends T,
|
|
58
|
+
S2 extends T
|
|
59
|
+
>(
|
|
60
|
+
items: Iterable<S1>,
|
|
61
|
+
pure: Effect.Effect<A, E, FixEnv<R, Evt, readonly S1[], readonly S2[]>>
|
|
62
|
+
) {
|
|
63
|
+
return saveAllWithEffectInt(
|
|
64
|
+
runTerm(pure, [...items])
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function saveWithPure_<
|
|
69
|
+
R,
|
|
70
|
+
A,
|
|
71
|
+
E,
|
|
72
|
+
S1 extends T,
|
|
73
|
+
S2 extends T
|
|
74
|
+
>(
|
|
75
|
+
item: S1,
|
|
76
|
+
pure: Effect.Effect<A, E, FixEnv<R, Evt, S1, S2>>
|
|
77
|
+
) {
|
|
78
|
+
return saveAllWithEffectInt(
|
|
79
|
+
runTerm(pure, item)
|
|
80
|
+
.pipe(Effect
|
|
81
|
+
.map(([item, events, a]) => [[item], events, a]))
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function saveAllWithEffectInt<
|
|
86
|
+
P extends T,
|
|
87
|
+
R,
|
|
88
|
+
E,
|
|
89
|
+
A
|
|
90
|
+
>(
|
|
91
|
+
gen: Effect.Effect<readonly [Iterable<P>, Iterable<Evt>, A], E, R>
|
|
92
|
+
) {
|
|
93
|
+
return Effect.flatMap(gen, ([items, events, a]) => repo.saveAndPublish(items, events).pipe(Effect.map(() => a)))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function saveManyWithPureBatched_<
|
|
97
|
+
R,
|
|
98
|
+
A,
|
|
99
|
+
E,
|
|
100
|
+
S1 extends T,
|
|
101
|
+
S2 extends T
|
|
102
|
+
>(
|
|
103
|
+
items: Iterable<S1>,
|
|
104
|
+
pure: Effect.Effect<A, E, FixEnv<R, Evt, readonly S1[], readonly S2[]>>,
|
|
105
|
+
batchSize = 100
|
|
106
|
+
) {
|
|
107
|
+
return Effect.forEach(
|
|
108
|
+
Array.chunksOf(items, batchSize),
|
|
109
|
+
(batch) =>
|
|
110
|
+
saveAllWithEffectInt(
|
|
111
|
+
runTerm(pure, batch)
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const queryAndSavePure: {
|
|
117
|
+
<A, E2, R2, T2 extends T>(
|
|
118
|
+
q: (
|
|
119
|
+
q: Query<Encoded>
|
|
120
|
+
) => QueryEnd<Encoded, "one">,
|
|
121
|
+
pure: Effect.Effect<A, E2, FixEnv<R2, Evt, T, T2>>
|
|
122
|
+
): Effect.Effect<
|
|
123
|
+
A,
|
|
124
|
+
InvalidStateError | OptimisticConcurrencyException | NotFoundError<ItemType> | E2,
|
|
125
|
+
Exclude<R2, {
|
|
126
|
+
env: PureEnv<Evt, T, T2>
|
|
127
|
+
}>
|
|
128
|
+
>
|
|
129
|
+
<A, E2, R2, T2 extends T>(
|
|
130
|
+
q: (
|
|
131
|
+
q: Query<Encoded>
|
|
132
|
+
) =>
|
|
133
|
+
| Query<Encoded>
|
|
134
|
+
| QueryWhere<Encoded>
|
|
135
|
+
| QueryEnd<Encoded>,
|
|
136
|
+
pure: Effect.Effect<A, E2, FixEnv<R2, Evt, readonly T[], readonly T2[]>>
|
|
137
|
+
): Effect.Effect<
|
|
138
|
+
A,
|
|
139
|
+
InvalidStateError | OptimisticConcurrencyException | E2,
|
|
140
|
+
| RSchema
|
|
141
|
+
| RPublish
|
|
142
|
+
| Exclude<R2, {
|
|
143
|
+
env: PureEnv<Evt, readonly T[], readonly T2[]>
|
|
144
|
+
}>
|
|
145
|
+
>
|
|
146
|
+
<A, E2, R2, T2 extends T>(
|
|
147
|
+
q: (
|
|
148
|
+
q: Query<Encoded>
|
|
149
|
+
) =>
|
|
150
|
+
| Query<Encoded>
|
|
151
|
+
| QueryWhere<Encoded>
|
|
152
|
+
| QueryEnd<Encoded>,
|
|
153
|
+
pure: Effect.Effect<A, E2, FixEnv<R2, Evt, readonly T[], readonly T2[]>>,
|
|
154
|
+
batch: "batched" | number
|
|
155
|
+
): Effect.Effect<
|
|
156
|
+
A[],
|
|
157
|
+
InvalidStateError | OptimisticConcurrencyException | E2,
|
|
158
|
+
| RSchema
|
|
159
|
+
| RPublish
|
|
160
|
+
| Exclude<R2, {
|
|
161
|
+
env: PureEnv<Evt, readonly T[], readonly T2[]>
|
|
162
|
+
}>
|
|
163
|
+
>
|
|
164
|
+
} = (q, pure, batch?: "batched" | number) =>
|
|
165
|
+
repo.query(q).pipe(
|
|
166
|
+
Effect.andThen((_) =>
|
|
167
|
+
Array.isArray(_)
|
|
168
|
+
? batch === undefined
|
|
169
|
+
? saveManyWithPure_(_ as any, pure as any)
|
|
170
|
+
: saveManyWithPureBatched_(_ as any, pure as any, batch === "batched" ? 100 : batch)
|
|
171
|
+
: saveWithPure_(_ as any, pure as any)
|
|
172
|
+
)
|
|
173
|
+
) as any
|
|
174
|
+
|
|
175
|
+
const saveManyWithPure: {
|
|
176
|
+
<R, A, E, S1 extends T, S2 extends T>(
|
|
177
|
+
items: Iterable<S1>,
|
|
178
|
+
pure: Effect.Effect<A, E, FixEnv<R, Evt, readonly S1[], readonly S2[]>>
|
|
179
|
+
): Effect.Effect<
|
|
180
|
+
A,
|
|
181
|
+
InvalidStateError | OptimisticConcurrencyException | E,
|
|
182
|
+
| RSchema
|
|
183
|
+
| RPublish
|
|
184
|
+
| Exclude<R, {
|
|
185
|
+
env: PureEnv<Evt, readonly S1[], readonly S2[]>
|
|
186
|
+
}>
|
|
187
|
+
>
|
|
188
|
+
<R, A, E, S1 extends T, S2 extends T>(
|
|
189
|
+
items: Iterable<S1>,
|
|
190
|
+
pure: Effect.Effect<A, E, FixEnv<R, Evt, readonly S1[], readonly S2[]>>,
|
|
191
|
+
batch: "batched" | number
|
|
192
|
+
): Effect.Effect<
|
|
193
|
+
A[],
|
|
194
|
+
InvalidStateError | OptimisticConcurrencyException | E,
|
|
195
|
+
| RSchema
|
|
196
|
+
| RPublish
|
|
197
|
+
| Exclude<R, {
|
|
198
|
+
env: PureEnv<Evt, readonly S1[], readonly S2[]>
|
|
199
|
+
}>
|
|
200
|
+
>
|
|
201
|
+
} = (items, pure, batch?: "batched" | number) =>
|
|
202
|
+
batch
|
|
203
|
+
? Effect.forEach(
|
|
204
|
+
Array.chunksOf(items, batch === "batched" ? 100 : batch),
|
|
205
|
+
(batch) =>
|
|
206
|
+
saveAllWithEffectInt(
|
|
207
|
+
runTerm(pure, batch)
|
|
208
|
+
)
|
|
209
|
+
)
|
|
210
|
+
: saveAllWithEffectInt(
|
|
211
|
+
runTerm(pure, [...items])
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
const byIdAndSaveWithPure: {
|
|
215
|
+
<R, A, E, S2 extends T>(
|
|
216
|
+
id: T[IdKey],
|
|
217
|
+
pure: Effect.Effect<A, E, FixEnv<R, Evt, T, S2>>
|
|
218
|
+
): Effect.Effect<
|
|
219
|
+
A,
|
|
220
|
+
InvalidStateError | OptimisticConcurrencyException | NotFoundError<ItemType> | E,
|
|
221
|
+
| RSchema
|
|
222
|
+
| RPublish
|
|
223
|
+
| Exclude<R, {
|
|
224
|
+
env: PureEnv<Evt, T, S2>
|
|
225
|
+
}>
|
|
226
|
+
>
|
|
227
|
+
} = (id, pure): any => get(id).pipe(Effect.flatMap((item) => saveWithPure_(item, pure)))
|
|
228
|
+
|
|
229
|
+
type Req =
|
|
230
|
+
& Request.Request<T, NotFoundError<ItemType>>
|
|
231
|
+
& { _tag: `Get${ItemType}`; id: T[IdKey] }
|
|
232
|
+
const _request = Request.tagged<Req>(`Get${repo.itemType}`)
|
|
233
|
+
|
|
234
|
+
const requestResolver = RequestResolver
|
|
235
|
+
.make((
|
|
236
|
+
entries: NonEmptyArray<Request.Entry<Req>>,
|
|
237
|
+
_key: unknown
|
|
238
|
+
) =>
|
|
239
|
+
(repo.query(Q.where(repo.idKey as any, "in" as any, entries.map((_) => _.request.id)) as any) as Effect.Effect<
|
|
240
|
+
readonly T[]
|
|
241
|
+
>)
|
|
242
|
+
// TODO
|
|
243
|
+
.pipe(
|
|
244
|
+
Effect.andThen((items) =>
|
|
245
|
+
Effect.forEach(entries, (entry) =>
|
|
246
|
+
Request.complete(
|
|
247
|
+
Array
|
|
248
|
+
.findFirst(items, (_) => _[repo.idKey] === entry.request.id)
|
|
249
|
+
.pipe(Option.match({
|
|
250
|
+
onNone: () => Exit.fail(new NotFoundError({ type: repo.itemType, id: entry.request.id })),
|
|
251
|
+
onSome: Exit.succeed
|
|
252
|
+
}))
|
|
253
|
+
)(entry), { discard: true })
|
|
254
|
+
),
|
|
255
|
+
Effect
|
|
256
|
+
.catchCause((cause) =>
|
|
257
|
+
Effect.forEach(entries, (entry) => Request.complete(Exit.failCause(cause))(entry), { discard: true })
|
|
258
|
+
)
|
|
259
|
+
)
|
|
260
|
+
)
|
|
261
|
+
.pipe(
|
|
262
|
+
RequestResolver.batchN(20)
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
const exts = {
|
|
266
|
+
request: (id: T[IdKey]) => Effect.request(_request({ id }), requestResolver),
|
|
267
|
+
get,
|
|
268
|
+
log: (evt: Evt) => AnyPureDSL.log(evt),
|
|
269
|
+
/**
|
|
270
|
+
* Enables chunked writes for large batches via `options.batch`.
|
|
271
|
+
* Note: batching breaks transactional properties because chunks are saved independently.
|
|
272
|
+
*/
|
|
273
|
+
save: ((itemOrItems: T | ReadonlyArray<T>, options?: BatchOptions) => {
|
|
274
|
+
const items = asReadonlyArray(itemOrItems)
|
|
275
|
+
if (!Array.isReadonlyArrayNonEmpty(items)) {
|
|
276
|
+
return Effect.void
|
|
277
|
+
}
|
|
278
|
+
const batchSize = getBatchSize(options?.batch)
|
|
279
|
+
if (batchSize === undefined) {
|
|
280
|
+
return repo.saveAndPublish(items)
|
|
281
|
+
}
|
|
282
|
+
return Effect.forEach(
|
|
283
|
+
Array.chunksOf(items, batchSize),
|
|
284
|
+
(batch) => repo.saveAndPublish(batch),
|
|
285
|
+
{ discard: true }
|
|
286
|
+
)
|
|
287
|
+
}) as (
|
|
288
|
+
itemOrItems: T | ReadonlyArray<T>,
|
|
289
|
+
options?: BatchOptions
|
|
290
|
+
) => Effect.Effect<
|
|
291
|
+
void,
|
|
292
|
+
InvalidStateError | OptimisticConcurrencyException,
|
|
293
|
+
RSchema | RPublish
|
|
294
|
+
>,
|
|
295
|
+
saveWithEvents: (events: Iterable<Evt>) => (...items: NonEmptyArray<T>) => repo.saveAndPublish(items, events),
|
|
296
|
+
/**
|
|
297
|
+
* Enables chunked deletes for large batches via `options.batch`.
|
|
298
|
+
* Note: batching breaks transactional properties because chunks are removed independently.
|
|
299
|
+
*/
|
|
300
|
+
remove: ((itemOrItems: T | ReadonlyArray<T>, options?: BatchOptions) => {
|
|
301
|
+
const items = asReadonlyArray(itemOrItems)
|
|
302
|
+
if (!Array.isReadonlyArrayNonEmpty(items)) {
|
|
303
|
+
return Effect.void
|
|
304
|
+
}
|
|
305
|
+
const batchSize = getBatchSize(options?.batch)
|
|
306
|
+
if (batchSize === undefined) {
|
|
307
|
+
return repo.removeAndPublish(items)
|
|
308
|
+
}
|
|
309
|
+
return Effect.forEach(
|
|
310
|
+
Array.chunksOf(items, batchSize),
|
|
311
|
+
(batch) => repo.removeAndPublish(batch),
|
|
312
|
+
{ discard: true }
|
|
313
|
+
)
|
|
314
|
+
}) as (
|
|
315
|
+
itemOrItems: T | ReadonlyArray<T>,
|
|
316
|
+
options?: BatchOptions
|
|
317
|
+
) => Effect.Effect<void, never, RSchema | RPublish>,
|
|
318
|
+
/**
|
|
319
|
+
* Enables chunked deletes for large batches via `options.batch`.
|
|
320
|
+
* Note: batching breaks transactional properties because chunks are removed independently.
|
|
321
|
+
*/
|
|
322
|
+
removeById: ((idOrIds: T[IdKey] | ReadonlyArray<T[IdKey]>, options?: BatchOptions) => {
|
|
323
|
+
const ids = asReadonlyArray(idOrIds)
|
|
324
|
+
if (!Array.isReadonlyArrayNonEmpty(ids)) {
|
|
325
|
+
return Effect.void
|
|
326
|
+
}
|
|
327
|
+
const batchSize = getBatchSize(options?.batch)
|
|
328
|
+
if (batchSize === undefined) {
|
|
329
|
+
return repo.removeById(ids)
|
|
330
|
+
}
|
|
331
|
+
return Effect.forEach(
|
|
332
|
+
Array.chunksOf(ids, batchSize),
|
|
333
|
+
(batch) => repo.removeById(batch),
|
|
334
|
+
{ discard: true }
|
|
335
|
+
)
|
|
336
|
+
}) as (
|
|
337
|
+
idOrIds: T[IdKey] | ReadonlyArray<T[IdKey]>,
|
|
338
|
+
options?: BatchOptions
|
|
339
|
+
) => Effect.Effect<void, never, RSchema>,
|
|
340
|
+
queryAndSavePure,
|
|
341
|
+
saveManyWithPure,
|
|
342
|
+
byIdAndSaveWithPure,
|
|
343
|
+
saveWithPure: <
|
|
344
|
+
R,
|
|
345
|
+
A,
|
|
346
|
+
E,
|
|
347
|
+
S1 extends T,
|
|
348
|
+
S2 extends T
|
|
349
|
+
>(
|
|
350
|
+
item: S1,
|
|
351
|
+
pure: Effect.Effect<A, E, FixEnv<R, Evt, S1, S2>>
|
|
352
|
+
) =>
|
|
353
|
+
saveAllWithEffectInt(
|
|
354
|
+
runTerm(pure, item)
|
|
355
|
+
.pipe(Effect.map(([item, events, a]) => [[item], events, a]))
|
|
356
|
+
)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return {
|
|
360
|
+
...repo,
|
|
361
|
+
...exts
|
|
362
|
+
} as Repository<T, Encoded, Evt, ItemType, IdKey, RSchema, RPublish, RProvided> & typeof exts
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
366
|
+
export interface ExtendedRepository<
|
|
367
|
+
T,
|
|
368
|
+
Encoded extends FieldValues,
|
|
369
|
+
Evt,
|
|
370
|
+
ItemType extends string,
|
|
371
|
+
IdKey extends keyof T & keyof Encoded,
|
|
372
|
+
RSchema,
|
|
373
|
+
RPublish,
|
|
374
|
+
RProvided = never
|
|
375
|
+
> extends ReturnType<typeof extendRepo<T, Encoded, Evt, ItemType, IdKey, RSchema, RPublish, RProvided>> {}
|