@effect-app/infra 0.221.0 → 0.223.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/_cjs/services/Operations.cjs +3 -2
- package/_cjs/services/Operations.cjs.map +1 -1
- package/_cjs/services/Repository/ext.cjs.map +1 -1
- package/_cjs/services/RepositoryBase.cjs +6 -4
- package/_cjs/services/RepositoryBase.cjs.map +1 -1
- package/_cjs/services/Store/Cosmos.cjs.map +1 -1
- package/_cjs/services/Store/Disk.cjs.map +1 -1
- package/_cjs/services/Store/Memory.cjs +1 -0
- package/_cjs/services/Store/Memory.cjs.map +1 -1
- package/_cjs/services/Store/service.cjs.map +1 -1
- package/_cjs/services/Store/utils.cjs.map +1 -1
- package/dist/services/Operations.d.ts +1 -0
- package/dist/services/Operations.d.ts.map +1 -1
- package/dist/services/Operations.js +4 -3
- package/dist/services/Repository/ext.d.ts +42 -15
- package/dist/services/Repository/ext.d.ts.map +1 -1
- package/dist/services/Repository/ext.js +1 -1
- package/dist/services/Repository/service.d.ts +3 -2
- package/dist/services/Repository/service.d.ts.map +1 -1
- package/dist/services/RepositoryBase.d.ts +81 -86
- package/dist/services/RepositoryBase.d.ts.map +1 -1
- package/dist/services/RepositoryBase.js +11 -7
- package/dist/services/Store/Cosmos.d.ts.map +1 -1
- package/dist/services/Store/Cosmos.js +1 -1
- package/dist/services/Store/Disk.d.ts +3 -1
- package/dist/services/Store/Disk.d.ts.map +1 -1
- package/dist/services/Store/Disk.js +1 -1
- package/dist/services/Store/Memory.d.ts +9 -3
- package/dist/services/Store/Memory.d.ts.map +1 -1
- package/dist/services/Store/Memory.js +2 -2
- package/dist/services/Store/service.d.ts +37 -18
- package/dist/services/Store/service.d.ts.map +1 -1
- package/dist/services/Store/service.js +1 -1
- package/dist/services/Store/utils.d.ts +6 -2
- package/dist/services/Store/utils.d.ts.map +1 -1
- package/dist/services/Store/utils.js +1 -1
- package/dist/services/query/new-kid-interpreter.d.ts +1 -1
- package/package.json +10 -10
- package/src/services/Operations.ts +4 -2
- package/src/services/Repository/ext.ts +28 -29
- package/src/services/Repository/service.ts +2 -3
- package/src/services/RepositoryBase.ts +177 -169
- package/src/services/Store/Cosmos.ts +13 -12
- package/src/services/Store/Disk.ts +12 -11
- package/src/services/Store/Memory.ts +13 -12
- package/src/services/Store/index.test.ts.bak +3 -3
- package/src/services/Store/service.ts +22 -19
- package/src/services/Store/utils.ts +2 -2
- package/test/query.test.ts +1 -1
|
@@ -36,33 +36,33 @@ import * as Q from "./query.js"
|
|
|
36
36
|
import { ContextMapContainer } from "./Store/ContextMapContainer.js"
|
|
37
37
|
import type * as QB from "./Store/filterApi/query.js"
|
|
38
38
|
|
|
39
|
-
export interface Mapped1<
|
|
40
|
-
all: Effect<
|
|
41
|
-
save: (...xes: readonly
|
|
39
|
+
export interface Mapped1<A, Encoded extends { id: string }, R> {
|
|
40
|
+
all: Effect<A[], ParseResult.ParseError, R>
|
|
41
|
+
save: (...xes: readonly A[]) => Effect<void, OptimisticConcurrencyException | ParseResult.ParseError, R>
|
|
42
42
|
query: (
|
|
43
|
-
b: (fn: QB.FilterTest<
|
|
44
|
-
) => Effect<
|
|
45
|
-
find: (id:
|
|
43
|
+
b: (fn: QB.FilterTest<Encoded>, fields: QB.Filter<Encoded, never>) => QB.QueryBuilder<Encoded>
|
|
44
|
+
) => Effect<A[], ParseResult.ParseError, R>
|
|
45
|
+
find: (id: Encoded["id"]) => Effect<Option<A>, ParseResult.ParseError, R>
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
// TODO: auto use project, and select fields from the From side of schema only
|
|
49
|
-
export interface Mapped2<
|
|
50
|
-
all: Effect<
|
|
49
|
+
export interface Mapped2<A, Encoded extends { id: string }, R> {
|
|
50
|
+
all: Effect<A[], ParseResult.ParseError, R>
|
|
51
51
|
query: (
|
|
52
|
-
b: (fn: QB.FilterTest<
|
|
53
|
-
) => Effect<
|
|
52
|
+
b: (fn: QB.FilterTest<Encoded>, fields: QB.Filter<Encoded, never>) => QB.QueryBuilder<Encoded>
|
|
53
|
+
) => Effect<A[], ParseResult.ParseError, R>
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
export interface Mapped<
|
|
57
|
-
<
|
|
56
|
+
export interface Mapped<Encoded extends { id: string }, OriginalEncoded extends { id: string } = Encoded> {
|
|
57
|
+
<A, R>(schema: S.Schema<A, OriginalEncoded, R>): Mapped1<A, Encoded, R>
|
|
58
58
|
// TODO: constrain on From having to contain only fields that fit OriginalFrom
|
|
59
|
-
<
|
|
59
|
+
<A, From, R>(schema: S.Schema<A, From, R>): Mapped2<A, Encoded, R>
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
export interface MM<Repo,
|
|
63
|
-
<
|
|
64
|
-
// TODO: constrain on From having to contain only fields that fit
|
|
65
|
-
<
|
|
62
|
+
export interface MM<Repo, Encoded extends { id: string }, OriginalEncoded extends { id: string }> {
|
|
63
|
+
<A, R>(schema: S.Schema<A, OriginalEncoded, R>): Effect<Mapped1<A, Encoded, R>, never, Repo>
|
|
64
|
+
// TODO: constrain on From having to contain only fields that fit OriginalEncoded
|
|
65
|
+
<A, Encoded2, R>(schema: S.Schema<A, Encoded2, R>): Effect<Mapped2<A, Encoded, R>, never, Repo>
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
/**
|
|
@@ -70,7 +70,7 @@ export interface MM<Repo, PM extends { id: string }, OriginalFrom> {
|
|
|
70
70
|
*/
|
|
71
71
|
export abstract class RepositoryBaseC<
|
|
72
72
|
T extends { id: unknown },
|
|
73
|
-
|
|
73
|
+
Encoded extends { id: string },
|
|
74
74
|
Evt,
|
|
75
75
|
ItemType extends string
|
|
76
76
|
> {
|
|
@@ -88,10 +88,10 @@ export abstract class RepositoryBaseC<
|
|
|
88
88
|
) => Effect<void>
|
|
89
89
|
|
|
90
90
|
abstract readonly query: {
|
|
91
|
-
<A, R,
|
|
91
|
+
<A, R, Encoded2 extends FieldValues, TType extends "one" | "many" | "count" = "many">(
|
|
92
92
|
q: (
|
|
93
|
-
initial: Query<Omit<
|
|
94
|
-
) => QueryProjection<Omit<
|
|
93
|
+
initial: Query<Omit<Encoded, "_etag">>
|
|
94
|
+
) => QueryProjection<Omit<Encoded, "_etag"> extends Encoded2 ? Encoded2 : never, A, R, TType>
|
|
95
95
|
): Effect.Effect<
|
|
96
96
|
TType extends "many" ? readonly A[] : TType extends "count" ? NonNegativeInt : A,
|
|
97
97
|
| (TType extends "many" ? never : NotFoundError<ItemType>)
|
|
@@ -99,24 +99,24 @@ export abstract class RepositoryBaseC<
|
|
|
99
99
|
R
|
|
100
100
|
>
|
|
101
101
|
<R = never, TType extends "one" | "many" = "many">(
|
|
102
|
-
q: (initial: Query<Omit<
|
|
102
|
+
q: (initial: Query<Omit<Encoded, "_etag">>) => QAll<Omit<Encoded, "_etag">, T, R, TType>
|
|
103
103
|
): Effect.Effect<TType extends "many" ? readonly T[] : T, TType extends "many" ? never : NotFoundError<ItemType>, R>
|
|
104
|
-
// <R = never>(q: QAll<Omit<
|
|
105
|
-
// <A, R,
|
|
106
|
-
// q: QueryProjection<Omit<
|
|
104
|
+
// <R = never>(q: QAll<Omit<Encoded, "_etag">, T, R>): Effect.Effect<readonly T[], never, R>
|
|
105
|
+
// <A, R, Encoded2 extends FieldValues>(
|
|
106
|
+
// q: QueryProjection<Omit<Encoded, "_etag"> extends Encoded2 ? Encoded2 : never, A, R>
|
|
107
107
|
// ): Effect.Effect<readonly A[], S.ParseResult.ParseError, R>
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
/** @deprecated use query */
|
|
111
|
-
abstract readonly mapped: Mapped<
|
|
111
|
+
abstract readonly mapped: Mapped<Encoded>
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
export abstract class RepositoryBaseC1<
|
|
115
115
|
T extends { id: unknown },
|
|
116
|
-
|
|
116
|
+
Encoded extends { id: string },
|
|
117
117
|
Evt,
|
|
118
118
|
ItemType extends string
|
|
119
|
-
> extends RepositoryBaseC<T,
|
|
119
|
+
> extends RepositoryBaseC<T, Encoded, Evt, ItemType> {
|
|
120
120
|
constructor(
|
|
121
121
|
public readonly itemType: ItemType
|
|
122
122
|
) {
|
|
@@ -126,13 +126,13 @@ export abstract class RepositoryBaseC1<
|
|
|
126
126
|
|
|
127
127
|
export class RepositoryBaseC2<
|
|
128
128
|
T extends { id: unknown },
|
|
129
|
-
|
|
129
|
+
Encoded extends { id: string },
|
|
130
130
|
Evt,
|
|
131
131
|
ItemType extends string
|
|
132
|
-
> extends RepositoryBaseC1<T,
|
|
132
|
+
> extends RepositoryBaseC1<T, Encoded, Evt, ItemType> {
|
|
133
133
|
constructor(
|
|
134
134
|
itemType: ItemType,
|
|
135
|
-
protected readonly impl: Repository<T,
|
|
135
|
+
protected readonly impl: Repository<T, Encoded, Evt, ItemType>
|
|
136
136
|
) {
|
|
137
137
|
super(itemType)
|
|
138
138
|
this.saveAndPublish = this.impl.saveAndPublish
|
|
@@ -155,10 +155,10 @@ export class RepositoryBaseC2<
|
|
|
155
155
|
|
|
156
156
|
export class RepositoryBaseC3<
|
|
157
157
|
T extends { id: unknown },
|
|
158
|
-
|
|
158
|
+
Encoded extends { id: string },
|
|
159
159
|
Evt,
|
|
160
160
|
ItemType extends string
|
|
161
|
-
> extends RepositoryBaseC2<T,
|
|
161
|
+
> extends RepositoryBaseC2<T, Encoded, Evt, ItemType> {
|
|
162
162
|
get(id: T["id"]) {
|
|
163
163
|
return Effect.andThen(
|
|
164
164
|
this
|
|
@@ -181,8 +181,8 @@ export class RepositoryBaseC3<
|
|
|
181
181
|
readonly queryAndSavePure: {
|
|
182
182
|
<A, E2, R2, T2 extends T>(
|
|
183
183
|
q: (
|
|
184
|
-
q: Query<Omit<
|
|
185
|
-
) => QueryEnd<Omit<
|
|
184
|
+
q: Query<Omit<Encoded, "_etag">>
|
|
185
|
+
) => QueryEnd<Omit<Encoded, "_etag">, "one">,
|
|
186
186
|
pure: Effect<A, E2, FixEnv<R2, Evt, T, T2>>
|
|
187
187
|
): Effect.Effect<
|
|
188
188
|
A,
|
|
@@ -193,8 +193,11 @@ export class RepositoryBaseC3<
|
|
|
193
193
|
>
|
|
194
194
|
<A, E2, R2, T2 extends T>(
|
|
195
195
|
q: (
|
|
196
|
-
q: Query<Omit<
|
|
197
|
-
) =>
|
|
196
|
+
q: Query<Omit<Encoded, "_etag">>
|
|
197
|
+
) =>
|
|
198
|
+
| Query<Omit<Encoded, "_etag">>
|
|
199
|
+
| QueryWhere<Omit<Encoded, "_etag">>
|
|
200
|
+
| QueryEnd<Omit<Encoded, "_etag">, "many">,
|
|
198
201
|
pure: Effect<A, E2, FixEnv<R2, Evt, readonly T[], readonly T2[]>>
|
|
199
202
|
): Effect.Effect<
|
|
200
203
|
A,
|
|
@@ -205,8 +208,11 @@ export class RepositoryBaseC3<
|
|
|
205
208
|
>
|
|
206
209
|
<A, E2, R2, T2 extends T>(
|
|
207
210
|
q: (
|
|
208
|
-
q: Query<Omit<
|
|
209
|
-
) =>
|
|
211
|
+
q: Query<Omit<Encoded, "_etag">>
|
|
212
|
+
) =>
|
|
213
|
+
| Query<Omit<Encoded, "_etag">>
|
|
214
|
+
| QueryWhere<Omit<Encoded, "_etag">>
|
|
215
|
+
| QueryEnd<Omit<Encoded, "_etag">, "many">,
|
|
210
216
|
pure: Effect<A, E2, FixEnv<R2, Evt, readonly T[], readonly T2[]>>,
|
|
211
217
|
batch: "batched" | number
|
|
212
218
|
): Effect.Effect<
|
|
@@ -303,27 +309,26 @@ export class RepositoryBaseC3<
|
|
|
303
309
|
}
|
|
304
310
|
}
|
|
305
311
|
|
|
306
|
-
type Exact<A, B> = [A] extends [B] ? [B] extends [A] ? true : false : false
|
|
307
312
|
/**
|
|
308
313
|
* A base implementation to create a repository.
|
|
309
314
|
*/
|
|
310
315
|
export function makeRepo<
|
|
311
|
-
PM extends { id: string; _etag: string | undefined },
|
|
312
316
|
Evt = never
|
|
313
317
|
>() {
|
|
314
318
|
return <
|
|
315
319
|
ItemType extends string,
|
|
316
320
|
R,
|
|
317
|
-
|
|
321
|
+
Encoded extends { id: string },
|
|
318
322
|
T extends { id: unknown }
|
|
319
323
|
>(
|
|
320
324
|
name: ItemType,
|
|
321
|
-
schema: S.Schema<T,
|
|
322
|
-
mapFrom: (pm:
|
|
323
|
-
mapTo: (e:
|
|
325
|
+
schema: S.Schema<T, Encoded, R>,
|
|
326
|
+
mapFrom: (pm: Encoded) => Encoded,
|
|
327
|
+
mapTo: (e: Encoded, etag: string | undefined) => PersistenceModelType<Encoded>
|
|
324
328
|
) => {
|
|
329
|
+
type PM = PersistenceModelType<Encoded>
|
|
325
330
|
function mapToPersistenceModel(
|
|
326
|
-
e:
|
|
331
|
+
e: Encoded,
|
|
327
332
|
getEtag: (id: string) => string | undefined
|
|
328
333
|
): PM {
|
|
329
334
|
return mapTo(e, getEtag(e.id))
|
|
@@ -332,25 +337,25 @@ export function makeRepo<
|
|
|
332
337
|
function mapReverse(
|
|
333
338
|
{ _etag, ...e }: PM,
|
|
334
339
|
setEtag: (id: string, eTag: string | undefined) => void
|
|
335
|
-
):
|
|
340
|
+
): Encoded {
|
|
336
341
|
setEtag(e.id, _etag)
|
|
337
|
-
return mapFrom(e)
|
|
342
|
+
return mapFrom(e as unknown as Encoded)
|
|
338
343
|
}
|
|
339
344
|
|
|
340
|
-
const mkStore = makeStore<
|
|
345
|
+
const mkStore = makeStore<Encoded>()(name, schema, mapTo)
|
|
341
346
|
|
|
342
347
|
function make<RInitial = never, E = never, R2 = never>(
|
|
343
348
|
args: [Evt] extends [never] ? {
|
|
344
349
|
makeInitial?: Effect<readonly T[], E, RInitial>
|
|
345
|
-
config?: Omit<StoreConfig<
|
|
346
|
-
partitionValue?: (a:
|
|
350
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
351
|
+
partitionValue?: (a: Encoded) => string
|
|
347
352
|
}
|
|
348
353
|
}
|
|
349
354
|
: {
|
|
350
355
|
publishEvents: (evt: NonEmptyReadonlyArray<Evt>) => Effect<void, never, R2>
|
|
351
356
|
makeInitial?: Effect<readonly T[], E, RInitial>
|
|
352
|
-
config?: Omit<StoreConfig<
|
|
353
|
-
partitionValue?: (a:
|
|
357
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
358
|
+
partitionValue?: (a: Encoded) => string
|
|
354
359
|
}
|
|
355
360
|
}
|
|
356
361
|
) {
|
|
@@ -389,28 +394,31 @@ export function makeRepo<
|
|
|
389
394
|
.pipe(Effect.map((_) => _ as T[]))
|
|
390
395
|
|
|
391
396
|
const fieldsSchema = schema as unknown as { fields: any }
|
|
392
|
-
const i = ("fields" in fieldsSchema ? S.struct(fieldsSchema["fields"]) as unknown as typeof schema : schema)
|
|
393
|
-
_
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
._tag === "Transformation"
|
|
402
|
-
? (S
|
|
403
|
-
.make(
|
|
404
|
-
_
|
|
397
|
+
const i = ("fields" in fieldsSchema ? S.struct(fieldsSchema["fields"]) as unknown as typeof schema : schema)
|
|
398
|
+
.pipe((_) =>
|
|
399
|
+
_.ast._tag === "Union"
|
|
400
|
+
// we need to get the TypeLiteral, incase of class it's behind a transform...
|
|
401
|
+
? S.union(..._.ast.types.map((_) =>
|
|
402
|
+
(S.make(_._tag === "Transformation" ? _.from : _) as unknown as Schema<T, Encoded>)
|
|
403
|
+
.pipe(S.pick("id"))
|
|
404
|
+
))
|
|
405
|
+
: _
|
|
405
406
|
.ast
|
|
406
|
-
.
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
407
|
+
._tag === "Transformation"
|
|
408
|
+
? (S
|
|
409
|
+
.make(
|
|
410
|
+
_
|
|
411
|
+
.ast
|
|
412
|
+
.from
|
|
413
|
+
) as unknown as Schema<T, Encoded>)
|
|
414
|
+
.pipe(S
|
|
415
|
+
.pick("id"))
|
|
416
|
+
: _
|
|
417
|
+
.pipe(S
|
|
418
|
+
.pick("id"))
|
|
419
|
+
)
|
|
412
420
|
const encodeId = flow(S.encode(i), Effect.provide(rctx))
|
|
413
|
-
function findEId(id:
|
|
421
|
+
function findEId(id: Encoded["id"]) {
|
|
414
422
|
return Effect.flatMap(
|
|
415
423
|
store.find(id),
|
|
416
424
|
(item) =>
|
|
@@ -433,7 +441,7 @@ export function makeRepo<
|
|
|
433
441
|
return Effect.flatMapOption(findE(id), (_) => Effect.orDie(decode(_)))
|
|
434
442
|
}
|
|
435
443
|
|
|
436
|
-
const saveAllE = (a: Iterable<
|
|
444
|
+
const saveAllE = (a: Iterable<Encoded>) =>
|
|
437
445
|
Effect
|
|
438
446
|
.flatMapOption(
|
|
439
447
|
Effect
|
|
@@ -495,31 +503,31 @@ export function makeRepo<
|
|
|
495
503
|
.pipe(Effect.orDie, Effect.withSpan("parseMany")))
|
|
496
504
|
const parseMany2 = <A, R>(
|
|
497
505
|
items: readonly PM[],
|
|
498
|
-
schema: S.Schema<A,
|
|
506
|
+
schema: S.Schema<A, Encoded, R>
|
|
499
507
|
) =>
|
|
500
508
|
Effect
|
|
501
509
|
.flatMap(cms, (cm) =>
|
|
502
510
|
S
|
|
503
511
|
.decode(S.array(schema))(
|
|
504
|
-
items.map((_) => mapReverse(_, cm.set)
|
|
512
|
+
items.map((_) => mapReverse(_, cm.set))
|
|
505
513
|
)
|
|
506
514
|
.pipe(Effect.orDie, Effect.withSpan("parseMany2")))
|
|
507
|
-
const filter = <U extends keyof
|
|
515
|
+
const filter = <U extends keyof Encoded = keyof Encoded>(args: FilterArgs<Encoded, U>) =>
|
|
508
516
|
store
|
|
509
517
|
.filter(args)
|
|
510
518
|
.pipe(Effect.tap((items) =>
|
|
511
519
|
args.select
|
|
512
520
|
? Effect.unit
|
|
513
|
-
: Effect.map(cms, ({ set }) => items.forEach((_) => set((_ as
|
|
521
|
+
: Effect.map(cms, ({ set }) => items.forEach((_) => set((_ as Encoded).id, (_ as PM)._etag)))
|
|
514
522
|
))
|
|
515
523
|
|
|
516
524
|
// TODO: For raw we should use S.from, and drop the R...
|
|
517
525
|
const query: {
|
|
518
526
|
<A, R, From extends FieldValues>(
|
|
519
|
-
q: QueryProjection<
|
|
527
|
+
q: QueryProjection<Encoded extends From ? From : never, A, R>
|
|
520
528
|
): Effect.Effect<readonly A[], S.ParseResult.ParseError, R>
|
|
521
|
-
<A, R>(q: QAll<NoInfer<
|
|
522
|
-
} = (<A, R>(q: QAll<
|
|
529
|
+
<A, R>(q: QAll<NoInfer<Encoded>, A, R>): Effect.Effect<readonly A[], never, R>
|
|
530
|
+
} = (<A, R>(q: QAll<Encoded, A, R>) => {
|
|
523
531
|
const a = Q.toFilter(q)
|
|
524
532
|
const eff = a.mode === "project"
|
|
525
533
|
? filter(a)
|
|
@@ -540,8 +548,9 @@ export function makeRepo<
|
|
|
540
548
|
(_) =>
|
|
541
549
|
Unify.unify(
|
|
542
550
|
a.schema
|
|
543
|
-
|
|
544
|
-
|
|
551
|
+
// TODO: partial may not match?
|
|
552
|
+
? parseMany2(_ as any, a.schema as any)
|
|
553
|
+
: parseMany(_ as any)
|
|
545
554
|
)
|
|
546
555
|
)
|
|
547
556
|
return pipe(
|
|
@@ -567,7 +576,7 @@ export function makeRepo<
|
|
|
567
576
|
)
|
|
568
577
|
}) as any
|
|
569
578
|
|
|
570
|
-
const r: Repository<T,
|
|
579
|
+
const r: Repository<T, Encoded, Evt, ItemType> = {
|
|
571
580
|
changeFeed,
|
|
572
581
|
itemType: name,
|
|
573
582
|
find,
|
|
@@ -588,7 +597,7 @@ export function makeRepo<
|
|
|
588
597
|
Effect.flatMap(decMany),
|
|
589
598
|
Effect.map((_) => _ as any[])
|
|
590
599
|
),
|
|
591
|
-
find: (id:
|
|
600
|
+
find: (id: Encoded["id"]) => flatMapOption(findE(id), dec),
|
|
592
601
|
query: (b: any) =>
|
|
593
602
|
filter({ filter: b })
|
|
594
603
|
.pipe(
|
|
@@ -608,7 +617,7 @@ export function makeRepo<
|
|
|
608
617
|
|
|
609
618
|
return {
|
|
610
619
|
make,
|
|
611
|
-
Q: Q.make<Omit<
|
|
620
|
+
Q: Q.make<Omit<Encoded, "_etag">>()
|
|
612
621
|
}
|
|
613
622
|
}
|
|
614
623
|
}
|
|
@@ -621,7 +630,7 @@ const pluralize = (s: string) =>
|
|
|
621
630
|
: s + "s"
|
|
622
631
|
|
|
623
632
|
export function makeStore<
|
|
624
|
-
|
|
633
|
+
Encoded extends { id: string }
|
|
625
634
|
>() {
|
|
626
635
|
return <
|
|
627
636
|
ItemType extends string,
|
|
@@ -631,9 +640,9 @@ export function makeStore<
|
|
|
631
640
|
>(
|
|
632
641
|
name: ItemType,
|
|
633
642
|
schema: S.Schema<T, E, R>,
|
|
634
|
-
mapTo: (e: E, etag: string | undefined) =>
|
|
643
|
+
mapTo: (e: E, etag: string | undefined) => Encoded
|
|
635
644
|
) => {
|
|
636
|
-
function
|
|
645
|
+
function encodeToEncoded() {
|
|
637
646
|
const getEtag = () => undefined
|
|
638
647
|
return (t: T) =>
|
|
639
648
|
S.encode(schema)(t).pipe(
|
|
@@ -645,26 +654,26 @@ export function makeStore<
|
|
|
645
654
|
function mapToPersistenceModel(
|
|
646
655
|
e: E,
|
|
647
656
|
getEtag: (id: string) => string | undefined
|
|
648
|
-
):
|
|
657
|
+
): Encoded {
|
|
649
658
|
return mapTo(e, getEtag(e.id))
|
|
650
659
|
}
|
|
651
660
|
|
|
652
661
|
function makeStore<RInitial = never, EInitial = never>(
|
|
653
662
|
makeInitial?: Effect<readonly T[], EInitial, RInitial>,
|
|
654
|
-
config?: Omit<StoreConfig<
|
|
655
|
-
partitionValue?: (a:
|
|
663
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
664
|
+
partitionValue?: (a: Encoded) => string
|
|
656
665
|
}
|
|
657
666
|
) {
|
|
658
667
|
return Effect.gen(function*($) {
|
|
659
668
|
const { make } = yield* $(StoreMaker)
|
|
660
669
|
|
|
661
670
|
const store = yield* $(
|
|
662
|
-
make<
|
|
671
|
+
make<Encoded, string, R | RInitial, EInitial>(
|
|
663
672
|
pluralize(name),
|
|
664
673
|
makeInitial
|
|
665
674
|
? makeInitial
|
|
666
675
|
.pipe(
|
|
667
|
-
Effect.flatMap(Effect.forEach(
|
|
676
|
+
Effect.flatMap(Effect.forEach(encodeToEncoded())),
|
|
668
677
|
Effect.withSpan("Repository.makeInitial [effect-app/infra]", {
|
|
669
678
|
attributes: { "repository.model_name": name }
|
|
670
679
|
})
|
|
@@ -687,7 +696,7 @@ export function makeStore<
|
|
|
687
696
|
|
|
688
697
|
export interface Repos<
|
|
689
698
|
T extends { id: unknown },
|
|
690
|
-
|
|
699
|
+
Encoded extends { id: string },
|
|
691
700
|
R,
|
|
692
701
|
Evt,
|
|
693
702
|
ItemType extends string
|
|
@@ -695,41 +704,41 @@ export interface Repos<
|
|
|
695
704
|
make<RInitial = never, E = never, R2 = never>(
|
|
696
705
|
args: [Evt] extends [never] ? {
|
|
697
706
|
makeInitial?: Effect<readonly T[], E, RInitial>
|
|
698
|
-
config?: Omit<StoreConfig<
|
|
699
|
-
partitionValue?: (a:
|
|
707
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
708
|
+
partitionValue?: (a: Encoded) => string
|
|
700
709
|
}
|
|
701
710
|
}
|
|
702
711
|
: {
|
|
703
712
|
publishEvents: (evt: NonEmptyReadonlyArray<Evt>) => Effect<void, never, R2>
|
|
704
713
|
makeInitial?: Effect<readonly T[], E, RInitial>
|
|
705
|
-
config?: Omit<StoreConfig<
|
|
706
|
-
partitionValue?: (a:
|
|
714
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
715
|
+
partitionValue?: (a: Encoded) => string
|
|
707
716
|
}
|
|
708
717
|
}
|
|
709
|
-
): Effect<Repository<T,
|
|
718
|
+
): Effect<Repository<T, Encoded, Evt, ItemType>, E, StoreMaker | ContextMapContainer | R | RInitial | R2>
|
|
710
719
|
makeWith<Out, RInitial = never, E = never, R2 = never>(
|
|
711
720
|
args: [Evt] extends [never] ? {
|
|
712
721
|
makeInitial?: Effect<readonly T[], E, RInitial>
|
|
713
|
-
config?: Omit<StoreConfig<
|
|
714
|
-
partitionValue?: (a:
|
|
722
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
723
|
+
partitionValue?: (a: Encoded) => string
|
|
715
724
|
}
|
|
716
725
|
}
|
|
717
726
|
: {
|
|
718
727
|
publishEvents: (evt: NonEmptyReadonlyArray<Evt>) => Effect<void, never, R2>
|
|
719
728
|
makeInitial?: Effect<readonly T[], E, RInitial>
|
|
720
|
-
config?: Omit<StoreConfig<
|
|
721
|
-
partitionValue?: (a:
|
|
729
|
+
config?: Omit<StoreConfig<Encoded>, "partitionValue"> & {
|
|
730
|
+
partitionValue?: (a: Encoded) => string
|
|
722
731
|
}
|
|
723
732
|
},
|
|
724
|
-
f: (r: Repository<T,
|
|
733
|
+
f: (r: Repository<T, Encoded, Evt, ItemType>) => Out
|
|
725
734
|
): Effect<Out, E, StoreMaker | ContextMapContainer | R | RInitial | R2>
|
|
726
|
-
readonly Q: ReturnType<typeof Q.make<
|
|
727
|
-
readonly type: Repository<T,
|
|
735
|
+
readonly Q: ReturnType<typeof Q.make<Encoded>>
|
|
736
|
+
readonly type: Repository<T, Encoded, Evt, ItemType>
|
|
728
737
|
}
|
|
729
738
|
|
|
730
739
|
export type GetRepoType<T> = T extends { type: infer R } ? R : never
|
|
731
740
|
|
|
732
|
-
export interface RepoFunctions<T extends { id: unknown },
|
|
741
|
+
export interface RepoFunctions<T extends { id: unknown }, Encoded extends { id: string }, Evt, ItemType, Service> {
|
|
733
742
|
all: Effect<readonly T[], never, Service>
|
|
734
743
|
find: (id: T["id"]) => Effect<Option<T>, never, Service>
|
|
735
744
|
removeById: (id: T["id"]) => Effect<void, NotFoundError<ItemType>, Service>
|
|
@@ -747,8 +756,8 @@ export interface RepoFunctions<T extends { id: unknown }, PM extends { id: strin
|
|
|
747
756
|
queryAndSavePure: {
|
|
748
757
|
<A, E2, R2, T2 extends T>(
|
|
749
758
|
q: (
|
|
750
|
-
q: Query<Omit<
|
|
751
|
-
) => QueryEnd<Omit<
|
|
759
|
+
q: Query<Omit<Encoded, "_etag">>
|
|
760
|
+
) => QueryEnd<Omit<Encoded, "_etag">, "one">,
|
|
752
761
|
pure: Effect<A, E2, FixEnv<R2, Evt, T, T2>>
|
|
753
762
|
): Effect.Effect<
|
|
754
763
|
A,
|
|
@@ -760,8 +769,11 @@ export interface RepoFunctions<T extends { id: unknown }, PM extends { id: strin
|
|
|
760
769
|
>
|
|
761
770
|
<A, E2, R2, T2 extends T>(
|
|
762
771
|
q: (
|
|
763
|
-
q: Query<Omit<
|
|
764
|
-
) =>
|
|
772
|
+
q: Query<Omit<Encoded, "_etag">>
|
|
773
|
+
) =>
|
|
774
|
+
| Query<Omit<Encoded, "_etag">>
|
|
775
|
+
| QueryWhere<Omit<Encoded, "_etag">>
|
|
776
|
+
| QueryEnd<Omit<Encoded, "_etag">, "many">,
|
|
765
777
|
pure: Effect<A, E2, FixEnv<R2, Evt, readonly T[], readonly T2[]>>
|
|
766
778
|
): Effect.Effect<
|
|
767
779
|
A,
|
|
@@ -773,8 +785,11 @@ export interface RepoFunctions<T extends { id: unknown }, PM extends { id: strin
|
|
|
773
785
|
>
|
|
774
786
|
<A, E2, R2, T2 extends T>(
|
|
775
787
|
q: (
|
|
776
|
-
q: Query<Omit<
|
|
777
|
-
) =>
|
|
788
|
+
q: Query<Omit<Encoded, "_etag">>
|
|
789
|
+
) =>
|
|
790
|
+
| Query<Omit<Encoded, "_etag">>
|
|
791
|
+
| QueryWhere<Omit<Encoded, "_etag">>
|
|
792
|
+
| QueryEnd<Omit<Encoded, "_etag">, "many">,
|
|
778
793
|
pure: Effect<A, E2, FixEnv<R2, Evt, readonly T[], readonly T2[]>>,
|
|
779
794
|
batchSize: number
|
|
780
795
|
): Effect.Effect<
|
|
@@ -790,8 +805,8 @@ export interface RepoFunctions<T extends { id: unknown }, PM extends { id: strin
|
|
|
790
805
|
readonly query: {
|
|
791
806
|
<A, R, From extends FieldValues, TType extends "one" | "many" | "count" = "many">(
|
|
792
807
|
q: (
|
|
793
|
-
initial: Query<Omit<
|
|
794
|
-
) => QueryProjection<Omit<
|
|
808
|
+
initial: Query<Omit<Encoded, "_etag">>
|
|
809
|
+
) => QueryProjection<Omit<Encoded, "_etag"> extends From ? From : never, A, R, TType>
|
|
795
810
|
): Effect.Effect<
|
|
796
811
|
TType extends "many" ? readonly A[] : TType extends "count" ? NonNegativeInt : A,
|
|
797
812
|
| (TType extends "many" ? never : NotFoundError<ItemType>)
|
|
@@ -799,15 +814,15 @@ export interface RepoFunctions<T extends { id: unknown }, PM extends { id: strin
|
|
|
799
814
|
Service | R
|
|
800
815
|
>
|
|
801
816
|
<R = never, TType extends "one" | "many" = "many">(
|
|
802
|
-
q: (initial: Query<Omit<
|
|
817
|
+
q: (initial: Query<Omit<Encoded, "_etag">>) => QAll<Omit<Encoded, "_etag">, T, R, TType>
|
|
803
818
|
): Effect.Effect<
|
|
804
819
|
TType extends "many" ? readonly T[] : T,
|
|
805
820
|
TType extends "many" ? never : NotFoundError<ItemType>,
|
|
806
821
|
Service | R
|
|
807
822
|
>
|
|
808
|
-
// <R = never>(q: QAll<Omit<
|
|
823
|
+
// <R = never>(q: QAll<Omit<Encoded, "_etag">, T, R>): Effect.Effect<readonly T[], never, Service | R>
|
|
809
824
|
// <A, R, From extends FieldValues>(
|
|
810
|
-
// q: QueryProjection<Omit<
|
|
825
|
+
// q: QueryProjection<Omit<Encoded, "_etag"> extends From ? From : never, A, R>
|
|
811
826
|
// ): Effect.Effect<readonly A[], S.ParseResult.ParseError, Service | R>
|
|
812
827
|
}
|
|
813
828
|
|
|
@@ -850,7 +865,7 @@ export interface RepoFunctions<T extends { id: unknown }, PM extends { id: strin
|
|
|
850
865
|
}
|
|
851
866
|
|
|
852
867
|
/** @experimental */
|
|
853
|
-
mapped: MM<Service,
|
|
868
|
+
mapped: MM<Service, Encoded, Omit<Encoded, "_etag">>
|
|
854
869
|
|
|
855
870
|
use: <X>(
|
|
856
871
|
body: (_: Service) => X
|
|
@@ -893,41 +908,39 @@ const makeRepoFunctions = (tag: any) => {
|
|
|
893
908
|
|
|
894
909
|
export const RepositoryBaseImpl = <Service>() => {
|
|
895
910
|
return <
|
|
896
|
-
PM extends { id: string; _etag: string | undefined },
|
|
897
911
|
Evt = never
|
|
898
912
|
>() =>
|
|
899
|
-
<ItemType extends string, R,
|
|
913
|
+
<ItemType extends string, R, Encoded extends { id: string }, T extends { id: unknown }>(
|
|
900
914
|
itemType: ItemType,
|
|
901
|
-
schema: S.Schema<T,
|
|
902
|
-
jitM?: (pm:
|
|
903
|
-
):
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
: never =>
|
|
915
|
+
schema: S.Schema<T, Encoded, R>,
|
|
916
|
+
jitM?: (pm: Encoded) => Encoded
|
|
917
|
+
):
|
|
918
|
+
& (abstract new() => RepositoryBaseC1<T, Encoded, Evt, ItemType>)
|
|
919
|
+
& Context.Tag<Service, Service>
|
|
920
|
+
& Repos<
|
|
921
|
+
T,
|
|
922
|
+
Encoded,
|
|
923
|
+
R,
|
|
924
|
+
Evt,
|
|
925
|
+
ItemType
|
|
926
|
+
>
|
|
927
|
+
& RepoFunctions<T, Encoded, Evt, ItemType, Service> =>
|
|
915
928
|
{
|
|
916
|
-
const mkRepo = makeRepo<
|
|
929
|
+
const mkRepo = makeRepo<Evt>()(
|
|
917
930
|
itemType,
|
|
918
931
|
schema,
|
|
919
|
-
jitM ? (pm) => jitM(pm as unknown as
|
|
932
|
+
jitM ? (pm) => jitM(pm as unknown as Encoded) : (pm) => pm as any,
|
|
920
933
|
(e, _etag) => ({ ...e, _etag })
|
|
921
934
|
)
|
|
922
|
-
abstract class Cls extends RepositoryBaseC1<T,
|
|
935
|
+
abstract class Cls extends RepositoryBaseC1<T, Encoded, Evt, ItemType> {
|
|
923
936
|
constructor() {
|
|
924
937
|
super(itemType)
|
|
925
938
|
}
|
|
926
939
|
static readonly make = mkRepo.make
|
|
927
940
|
static readonly makeWith = ((a: any, b: any) => Effect.map(mkRepo.make(a), b)) as any
|
|
928
941
|
|
|
929
|
-
static readonly Q = Q.make<
|
|
930
|
-
static readonly type: Repository<T,
|
|
942
|
+
static readonly Q = Q.make<Encoded>()
|
|
943
|
+
static readonly type: Repository<T, Encoded, Evt, ItemType> = undefined as any
|
|
931
944
|
}
|
|
932
945
|
const limit = Error.stackTraceLimit
|
|
933
946
|
Error.stackTraceLimit = 2
|
|
@@ -937,48 +950,43 @@ export const RepositoryBaseImpl = <Service>() => {
|
|
|
937
950
|
}
|
|
938
951
|
}
|
|
939
952
|
|
|
940
|
-
export const RepositoryDefaultImpl = <Service>() => {
|
|
941
|
-
return <
|
|
942
|
-
PM extends { id: string; _etag: string | undefined },
|
|
943
|
-
Evt = never
|
|
944
|
-
>() =>
|
|
945
|
-
<ItemType extends string, R, From extends { id: string }, T extends { id: unknown }>(
|
|
953
|
+
export const RepositoryDefaultImpl = <Service, Evt = never>() => {
|
|
954
|
+
return <ItemType extends string, R, Encoded extends { id: string }, T extends { id: unknown }>(
|
|
946
955
|
itemType: ItemType,
|
|
947
|
-
schema: S.Schema<T,
|
|
948
|
-
jitM?: (pm:
|
|
949
|
-
):
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
: never =>
|
|
956
|
+
schema: S.Schema<T, Encoded, R>,
|
|
957
|
+
jitM?: (pm: Encoded) => Encoded
|
|
958
|
+
):
|
|
959
|
+
& (abstract new(
|
|
960
|
+
impl: Repository<T, Encoded, Evt, ItemType>
|
|
961
|
+
) => RepositoryBaseC3<T, Encoded, Evt, ItemType>)
|
|
962
|
+
& Context.Tag<Service, Service>
|
|
963
|
+
& Repos<
|
|
964
|
+
T,
|
|
965
|
+
Encoded,
|
|
966
|
+
R,
|
|
967
|
+
Evt,
|
|
968
|
+
ItemType
|
|
969
|
+
>
|
|
970
|
+
& RepoFunctions<T, Encoded, Evt, ItemType, Service> =>
|
|
963
971
|
{
|
|
964
|
-
const mkRepo = makeRepo<
|
|
972
|
+
const mkRepo = makeRepo<Evt>()(
|
|
965
973
|
itemType,
|
|
966
974
|
schema,
|
|
967
|
-
jitM ? (pm) => jitM(pm
|
|
975
|
+
jitM ? (pm) => jitM(pm) : (pm) => pm,
|
|
968
976
|
(e, _etag) => ({ ...e, _etag })
|
|
969
977
|
)
|
|
970
|
-
abstract class Cls extends RepositoryBaseC3<T,
|
|
978
|
+
abstract class Cls extends RepositoryBaseC3<T, Encoded, Evt, ItemType> {
|
|
971
979
|
constructor(
|
|
972
|
-
impl: Repository<T,
|
|
980
|
+
impl: Repository<T, Encoded, Evt, ItemType>
|
|
973
981
|
) {
|
|
974
982
|
super(itemType, impl)
|
|
975
983
|
}
|
|
976
984
|
static readonly make = mkRepo.make
|
|
977
985
|
static readonly makeWith = ((a: any, b: any) => Effect.map(mkRepo.make(a), b)) as any
|
|
978
986
|
|
|
979
|
-
static readonly Q = Q.make<
|
|
987
|
+
static readonly Q = Q.make<Encoded>()
|
|
980
988
|
|
|
981
|
-
static readonly type: Repository<T,
|
|
989
|
+
static readonly type: Repository<T, Encoded, Evt, ItemType> = undefined as any
|
|
982
990
|
}
|
|
983
991
|
const limit = Error.stackTraceLimit
|
|
984
992
|
Error.stackTraceLimit = 2
|