@effect-app/infra 2.10.0 → 2.11.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.
@@ -0,0 +1,481 @@
1
+ import type {} from "effect/Equal"
2
+ import type {} from "effect/Hash"
3
+ import type { NonEmptyReadonlyArray } from "effect-app"
4
+ import { Array, Chunk, Context, Effect, Equivalence, flow, Option, pipe, Pipeable, PubSub, S, Unify } from "effect-app"
5
+ import { toNonEmptyArray } from "effect-app/Array"
6
+ import { NotFoundError } from "effect-app/client"
7
+ import { flatMapOption } from "effect-app/Effect"
8
+ import type { Schema } from "effect-app/Schema"
9
+ import { NonNegativeInt } from "effect-app/Schema"
10
+ import { setupRequestContextFromCurrent } from "../../../api/setupRequest.js"
11
+ import type { FilterArgs, PersistenceModelType, StoreConfig } from "../../../Store.js"
12
+ import { StoreMaker } from "../../../Store.js"
13
+ import { getContextMap } from "../../../Store/ContextMapContainer.js"
14
+ import type { FieldValues } from "../../filter/types.js"
15
+ import * as Q from "../../query.js"
16
+ import type { Repository } from "../service.js"
17
+
18
+ const dedupe = Array.dedupeWith(Equivalence.string)
19
+
20
+ /**
21
+ * A base implementation to create a repository.
22
+ */
23
+ export function makeRepoInternal<
24
+ Evt = never
25
+ >() {
26
+ return <
27
+ ItemType extends string,
28
+ R,
29
+ Encoded extends { id: string },
30
+ T,
31
+ IdKey extends keyof T
32
+ >(
33
+ name: ItemType,
34
+ schema: S.Schema<T, Encoded, R>,
35
+ mapFrom: (pm: Encoded) => Encoded,
36
+ mapTo: (e: Encoded, etag: string | undefined) => PersistenceModelType<Encoded>,
37
+ idKey: IdKey
38
+ ) => {
39
+ type PM = PersistenceModelType<Encoded>
40
+ function mapToPersistenceModel(
41
+ e: Encoded,
42
+ getEtag: (id: string) => string | undefined
43
+ ): PM {
44
+ return mapTo(e, getEtag(e.id))
45
+ }
46
+
47
+ function mapReverse(
48
+ { _etag, ...e }: PM,
49
+ setEtag: (id: string, eTag: string | undefined) => void
50
+ ): Encoded {
51
+ setEtag(e.id, _etag)
52
+ return mapFrom(e as unknown as Encoded)
53
+ }
54
+
55
+ const mkStore = makeStore<Encoded>()(name, schema, mapTo)
56
+
57
+ function make<RInitial = never, E = never, RPublish = never, RCtx = never>(
58
+ args: [Evt] extends [never] ? {
59
+ schemaContext?: Context.Context<RCtx>
60
+ makeInitial?: Effect<readonly T[], E, RInitial>
61
+ config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
62
+ partitionValue?: (a: Encoded) => string
63
+ }
64
+ }
65
+ : {
66
+ schemaContext?: Context.Context<RCtx>
67
+ publishEvents: (evt: NonEmptyReadonlyArray<Evt>) => Effect<void, never, RPublish>
68
+ makeInitial?: Effect<readonly T[], E, RInitial>
69
+ config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
70
+ partitionValue?: (a: Encoded) => string
71
+ }
72
+ }
73
+ ) {
74
+ return Effect
75
+ .gen(function*() {
76
+ const rctx: Context<RCtx> = args.schemaContext ?? Context.empty() as any
77
+ const provideRctx = Effect.provide(rctx)
78
+ const encodeMany = flow(
79
+ S.encode(S.Array(schema)),
80
+ provideRctx,
81
+ Effect.withSpan("encodeMany", { captureStackTrace: false })
82
+ )
83
+ const decode = flow(S.decode(schema), provideRctx)
84
+ const decodeMany = flow(
85
+ S.decode(S.Array(schema)),
86
+ provideRctx,
87
+ Effect.withSpan("decodeMany", { captureStackTrace: false })
88
+ )
89
+
90
+ const store = yield* mkStore(args.makeInitial, args.config)
91
+ const cms = Effect.andThen(getContextMap.pipe(Effect.orDie), (_) => ({
92
+ get: (id: string) => _.get(`${name}.${id}`),
93
+ set: (id: string, etag: string | undefined) => _.set(`${name}.${id}`, etag)
94
+ }))
95
+
96
+ const pub = "publishEvents" in args
97
+ ? args.publishEvents
98
+ : () => Effect.void
99
+ const changeFeed = yield* PubSub.unbounded<[T[], "save" | "remove"]>()
100
+
101
+ const allE = cms
102
+ .pipe(Effect.flatMap((cm) => Effect.map(store.all, (_) => _.map((_) => mapReverse(_, cm.set)))))
103
+
104
+ const all = Effect
105
+ .flatMap(
106
+ allE,
107
+ (_) => decodeMany(_).pipe(Effect.orDie)
108
+ )
109
+ .pipe(Effect.map((_) => _ as T[]))
110
+
111
+ const fieldsSchema = schema as unknown as { fields: any }
112
+ // assumes the id field never needs a service...
113
+ const i = ("fields" in fieldsSchema ? S.Struct(fieldsSchema["fields"]) as unknown as typeof schema : schema)
114
+ .pipe((_) => {
115
+ let ast = _.ast
116
+ if (ast._tag === "Declaration") ast = ast.typeParameters[0]!
117
+
118
+ const s = S.make(ast) as unknown as Schema<T, Encoded, R>
119
+
120
+ return ast._tag === "Union"
121
+ // we need to get the TypeLiteral, incase of class it's behind a transform...
122
+ ? S.Union(
123
+ ...ast.types.map((_) =>
124
+ (S.make(_._tag === "Transformation" ? _.from : _) as unknown as Schema<T, Encoded>)
125
+ .pipe(S.pick(idKey as any))
126
+ )
127
+ )
128
+ : s.pipe(S.pick(idKey as any))
129
+ })
130
+ const encodeId = flow(S.encode(i), provideRctx)
131
+ function findEId(id: Encoded["id"]) {
132
+ return Effect.flatMap(
133
+ store.find(id),
134
+ (item) =>
135
+ Effect.gen(function*() {
136
+ const { set } = yield* cms
137
+ return item.pipe(Option.map((_) => mapReverse(_, set)))
138
+ })
139
+ )
140
+ }
141
+ function findE(id: T[IdKey]) {
142
+ return pipe(
143
+ encodeId({ [idKey]: id } as any),
144
+ Effect.orDie,
145
+ // we will have idKey because the transform is undone again by the encode schema mumbo jumbo above
146
+ // TODO: make reliable. (Security: isin: PrimaryKey(ISIN), idKey: "isin", does end up with "id")
147
+ Effect.map((_) => (_ as any)[idKey] ?? (_ as any).id),
148
+ Effect.flatMap(findEId)
149
+ )
150
+ }
151
+
152
+ function find(id: T[IdKey]) {
153
+ return Effect.flatMapOption(findE(id), (_) => Effect.orDie(decode(_)))
154
+ }
155
+
156
+ const saveAllE = (a: Iterable<Encoded>) =>
157
+ Effect
158
+ .flatMapOption(
159
+ Effect
160
+ .sync(() => toNonEmptyArray([...a])),
161
+ (a) =>
162
+ Effect.gen(function*() {
163
+ const { get, set } = yield* cms
164
+ const items = a.map((_) => mapToPersistenceModel(_, get))
165
+ const ret = yield* store.batchSet(items)
166
+ ret.forEach((_) => set(_.id, _._etag))
167
+ })
168
+ )
169
+ .pipe(Effect.asVoid)
170
+
171
+ const saveAll = (a: Iterable<T>) =>
172
+ encodeMany(Array.fromIterable(a))
173
+ .pipe(
174
+ Effect.orDie,
175
+ Effect.andThen(saveAllE)
176
+ )
177
+
178
+ const saveAndPublish = (items: Iterable<T>, events: Iterable<Evt> = []) => {
179
+ return Effect
180
+ .suspend(() => {
181
+ const it = Chunk.fromIterable(items)
182
+ return saveAll(it)
183
+ .pipe(
184
+ Effect.andThen(Effect.sync(() => toNonEmptyArray([...events]))),
185
+ // TODO: for full consistency the events should be stored within the same database transaction, and then picked up.
186
+ (_) => Effect.flatMapOption(_, pub),
187
+ Effect.andThen(changeFeed.publish([Chunk.toArray(it), "save"])),
188
+ Effect.asVoid
189
+ )
190
+ })
191
+ .pipe(Effect.withSpan("saveAndPublish", { captureStackTrace: false }))
192
+ }
193
+
194
+ function removeAndPublish(a: Iterable<T>, events: Iterable<Evt> = []) {
195
+ return Effect.gen(function*() {
196
+ const { get, set } = yield* cms
197
+ const it = [...a]
198
+ const items = yield* encodeMany(it).pipe(Effect.orDie)
199
+ // TODO: we should have a batchRemove on store so the adapter can actually batch...
200
+ for (const e of items) {
201
+ yield* store.remove(mapToPersistenceModel(e, get))
202
+ set(e.id, undefined)
203
+ }
204
+ yield* Effect
205
+ .sync(() => toNonEmptyArray([...events]))
206
+ // TODO: for full consistency the events should be stored within the same database transaction, and then picked up.
207
+ .pipe((_) => Effect.flatMapOption(_, pub))
208
+
209
+ yield* changeFeed.publish([it, "remove"])
210
+ })
211
+ }
212
+
213
+ const parseMany = (items: readonly PM[]) =>
214
+ Effect
215
+ .flatMap(cms, (cm) =>
216
+ decodeMany(items.map((_) => mapReverse(_, cm.set)))
217
+ .pipe(Effect.orDie, Effect.withSpan("parseMany", { captureStackTrace: false })))
218
+ const parseMany2 = <A, R>(
219
+ items: readonly PM[],
220
+ schema: S.Schema<A, Encoded, R>
221
+ ) =>
222
+ Effect
223
+ .flatMap(cms, (cm) =>
224
+ S
225
+ .decode(S.Array(schema))(
226
+ items.map((_) => mapReverse(_, cm.set))
227
+ )
228
+ .pipe(Effect.orDie, Effect.withSpan("parseMany2", { captureStackTrace: false })))
229
+ const filter = <U extends keyof Encoded = keyof Encoded>(args: FilterArgs<Encoded, U>) =>
230
+ store
231
+ .filter(
232
+ // always enforce id and _etag because they are system fields, required for etag tracking etc
233
+ {
234
+ ...args,
235
+ select: args.select
236
+ ? dedupe([...args.select, "id", "_etag" as any])
237
+ : undefined
238
+ } as typeof args
239
+ )
240
+ .pipe(
241
+ Effect.tap((items) =>
242
+ Effect.map(cms, ({ set }) => items.forEach((_) => set((_ as Encoded).id, (_ as PM)._etag)))
243
+ )
244
+ )
245
+
246
+ // TODO: For raw we should use S.from, and drop the R...
247
+ const query: {
248
+ <A, R, From extends FieldValues>(
249
+ q: Q.QueryProjection<Encoded extends From ? From : never, A, R>
250
+ ): Effect.Effect<readonly A[], S.ParseResult.ParseError, R>
251
+ <A, R, EncodedRefined extends Encoded = Encoded>(
252
+ q: Q.QAll<NoInfer<Encoded>, NoInfer<EncodedRefined>, A, R>
253
+ ): Effect.Effect<readonly A[], never, R>
254
+ } = (<A, R, EncodedRefined extends Encoded = Encoded>(q: Q.QAll<Encoded, EncodedRefined, A, R>) => {
255
+ const a = Q.toFilter(q)
256
+ const eff = a.mode === "project"
257
+ ? filter(a)
258
+ // TODO: mapFrom but need to support per field and dependencies
259
+ .pipe(
260
+ Effect.andThen(flow(S.decode(S.Array(a.schema ?? schema)), provideRctx))
261
+ )
262
+ : a.mode === "collect"
263
+ ? filter(a)
264
+ // TODO: mapFrom but need to support per field and dependencies
265
+ .pipe(
266
+ Effect.flatMap(flow(
267
+ S.decode(S.Array(a.schema)),
268
+ Effect.map(Array.getSomes),
269
+ provideRctx
270
+ ))
271
+ )
272
+ : Effect.flatMap(
273
+ filter(a),
274
+ (_) =>
275
+ Unify.unify(
276
+ a.schema
277
+ // TODO: partial may not match?
278
+ ? parseMany2(_ as any, a.schema as any)
279
+ : parseMany(_ as any)
280
+ )
281
+ )
282
+ return pipe(
283
+ a.ttype === "one"
284
+ ? Effect.andThen(
285
+ eff,
286
+ flow(
287
+ Array.head,
288
+ Effect.mapError(() => new NotFoundError({ id: "query", /* TODO */ type: name }))
289
+ )
290
+ )
291
+ : a.ttype === "count"
292
+ ? Effect
293
+ .andThen(eff, (_) => NonNegativeInt(_.length))
294
+ .pipe(Effect.catchTag("ParseError", (e) => Effect.die(e)))
295
+ : eff,
296
+ Effect.withSpan("Repository.query [effect-app/infra]", {
297
+ captureStackTrace: false,
298
+ attributes: {
299
+ "repository.model_name": name,
300
+ query: { ...a, schema: a.schema ? "__SCHEMA__" : a.schema, filter: a.filter }
301
+ }
302
+ })
303
+ )
304
+ }) as any
305
+
306
+ const r: Repository<T, Encoded, Evt, ItemType, IdKey, Exclude<R, RCtx>, RPublish> = {
307
+ changeFeed,
308
+ itemType: name,
309
+ idKey,
310
+ find,
311
+ all,
312
+ saveAndPublish,
313
+ removeAndPublish,
314
+ query(q: any) {
315
+ // eslint-disable-next-line prefer-rest-params
316
+ return query(typeof q === "function" ? Pipeable.pipeArguments(Q.make(), arguments) : q) as any
317
+ },
318
+ /**
319
+ * @internal
320
+ */
321
+ mapped: <A, R>(schema: S.Schema<A, any, R>) => {
322
+ const dec = S.decode(schema)
323
+ const encMany = S.encode(S.Array(schema))
324
+ const decMany = S.decode(S.Array(schema))
325
+ return {
326
+ all: allE.pipe(
327
+ Effect.flatMap(decMany),
328
+ Effect.map((_) => _ as any[])
329
+ ),
330
+ find: (id: T[IdKey]) => flatMapOption(findE(id), dec),
331
+ // query: (q: any) => {
332
+ // const a = Q.toFilter(q)
333
+
334
+ // return filter(a)
335
+ // .pipe(
336
+ // Effect.flatMap(decMany),
337
+ // Effect.map((_) => _ as any[]),
338
+ // Effect.withSpan("Repository.mapped.query [effect-app/infra]", {
339
+ // captureStackTrace: false,
340
+ // attributes: {
341
+ // "repository.model_name": name,
342
+ // query: { ...a, schema: a.schema ? "__SCHEMA__" : a.schema, filter: a.filter.build() }
343
+ // }
344
+ // })
345
+ // )
346
+ // },
347
+ save: (...xes: any[]) =>
348
+ Effect.flatMap(encMany(xes), (_) => saveAllE(_)).pipe(
349
+ Effect.withSpan("mapped.save", { captureStackTrace: false })
350
+ )
351
+ }
352
+ }
353
+ }
354
+ return r
355
+ })
356
+ .pipe(Effect
357
+ // .withSpan("Repository.make [effect-app/infra]", { attributes: { "repository.model_name": name } })
358
+ .withLogSpan("Repository.make: " + name))
359
+ }
360
+
361
+ return {
362
+ make,
363
+ Q: Q.make<Encoded>()
364
+ }
365
+ }
366
+ }
367
+
368
+ const pluralize = (s: string) =>
369
+ s.endsWith("s")
370
+ ? s + "es"
371
+ : s.endsWith("y")
372
+ ? s.substring(0, s.length - 1) + "ies"
373
+ : s + "s"
374
+
375
+ export function makeStore<
376
+ Encoded extends { id: string }
377
+ >() {
378
+ return <
379
+ ItemType extends string,
380
+ R,
381
+ E extends { id: string },
382
+ T
383
+ >(
384
+ name: ItemType,
385
+ schema: S.Schema<T, E, R>,
386
+ mapTo: (e: E, etag: string | undefined) => Encoded
387
+ ) => {
388
+ function encodeToEncoded() {
389
+ const getEtag = () => undefined
390
+ return (t: T) =>
391
+ S.encode(schema)(t).pipe(
392
+ Effect.orDie,
393
+ Effect.map((_) => mapToPersistenceModel(_, getEtag))
394
+ )
395
+ }
396
+
397
+ function mapToPersistenceModel(
398
+ e: E,
399
+ getEtag: (id: string) => string | undefined
400
+ ): Encoded {
401
+ return mapTo(e, getEtag(e.id))
402
+ }
403
+
404
+ function makeStore<RInitial = never, EInitial = never>(
405
+ makeInitial?: Effect<readonly T[], EInitial, RInitial>,
406
+ config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
407
+ partitionValue?: (a: Encoded) => string
408
+ }
409
+ ) {
410
+ return Effect.gen(function*() {
411
+ const { make } = yield* StoreMaker
412
+
413
+ const store = yield* make<Encoded, string, RInitial | R, EInitial>(
414
+ pluralize(name),
415
+ makeInitial
416
+ ? makeInitial
417
+ .pipe(
418
+ Effect.flatMap(Effect.forEach(encodeToEncoded())),
419
+ setupRequestContextFromCurrent("Repository.makeInitial [effect-app/infra]", {
420
+ attributes: { "repository.model_name": name }
421
+ })
422
+ )
423
+ : undefined,
424
+ {
425
+ ...config,
426
+ partitionValue: config?.partitionValue
427
+ ?? ((_) => "primary") /*(isIntegrationEvent(r) ? r.companyId : r.id*/
428
+ }
429
+ )
430
+
431
+ return store
432
+ })
433
+ }
434
+
435
+ return makeStore
436
+ }
437
+ }
438
+
439
+ export interface Repos<
440
+ T,
441
+ Encoded extends { id: string },
442
+ RSchema,
443
+ Evt,
444
+ ItemType extends string,
445
+ IdKey extends keyof T,
446
+ RPublish
447
+ > {
448
+ make<RInitial = never, E = never, R2 = never>(
449
+ args: [Evt] extends [never] ? {
450
+ makeInitial?: Effect<readonly T[], E, RInitial>
451
+ config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
452
+ partitionValue?: (a: Encoded) => string
453
+ }
454
+ }
455
+ : {
456
+ publishEvents: (evt: NonEmptyReadonlyArray<Evt>) => Effect<void, never, R2>
457
+ makeInitial?: Effect<readonly T[], E, RInitial>
458
+ config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
459
+ partitionValue?: (a: Encoded) => string
460
+ }
461
+ }
462
+ ): Effect<Repository<T, Encoded, Evt, ItemType, IdKey, RSchema, RPublish>, E, StoreMaker | RInitial | R2>
463
+ makeWith<Out, RInitial = never, E = never, R2 = never>(
464
+ args: [Evt] extends [never] ? {
465
+ makeInitial?: Effect<readonly T[], E, RInitial>
466
+ config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
467
+ partitionValue?: (a: Encoded) => string
468
+ }
469
+ }
470
+ : {
471
+ publishEvents: (evt: NonEmptyReadonlyArray<Evt>) => Effect<void, never, R2>
472
+ makeInitial?: Effect<readonly T[], E, RInitial>
473
+ config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
474
+ partitionValue?: (a: Encoded) => string
475
+ }
476
+ },
477
+ f: (r: Repository<T, Encoded, Evt, ItemType, IdKey, RSchema, RPublish>) => Out
478
+ ): Effect<Out, E, StoreMaker | RInitial | R2>
479
+ readonly Q: ReturnType<typeof Q.make<Encoded>>
480
+ readonly type: Repository<T, Encoded, Evt, ItemType, IdKey, RSchema, RPublish>
481
+ }