-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathquery.test.ts
More file actions
1153 lines (991 loc) · 30.9 KB
/
query.test.ts
File metadata and controls
1153 lines (991 loc) · 30.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable unused-imports/no-unused-vars */
/* eslint-disable @typescript-eslint/no-empty-object-type */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Context, Effect, flow, Layer, Option, pipe, S, Struct } from "effect-app"
import { inspect } from "util"
import { expect, expectTypeOf, it } from "vitest"
import { setupRequestContextFromCurrent } from "../src/api/setupRequest.js"
import { and, count, make, one, or, order, page, project, type QueryEnd, type QueryProjection, type QueryWhere, toFilter, where } from "../src/Model/query.js"
import { makeRepo } from "../src/Model/Repository.js"
import { memFilter, MemoryStoreLive } from "../src/Store/Memory.js"
const str = S.Struct({ _tag: S.Literal("string"), value: S.String })
const num = S.Struct({ _tag: S.Literal("number"), value: S.Number })
const someUnion = S.Union(str, num)
export class Something extends S.Class<Something>()({
id: S.StringId.withDefault,
displayName: S.NonEmptyString255,
name: S.NullOr(S.NonEmptyString255).withDefault,
n: S.Date.withDefault,
union: someUnion.pipe(S.withDefaultConstructor(() => ({ _tag: "string" as const, value: "hi" })))
}) {}
export declare namespace Something {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface Encoded extends S.Schema.Encoded<typeof Something> {}
}
const MakeSomeService = Effect.succeed({ a: 1 })
export class SomeService extends Context.TagMakeId("SomeService", MakeSomeService)<SomeService>() {}
const q = make<Something.Encoded>()
.pipe( // provided automatically inside Repo.q2()
where("displayName", "Verona"),
or(
where("displayName", "Riley"),
and("n", "gt", "2021-01-01T00:00:00Z") // TODO: work with To type translation, so Date?
),
order("displayName"),
page({ take: 10 }),
// for projection performance benefit, this should be limited to the fields interested, and leads to SELECT fields
project(
S.transformToOrFail(
S.Struct(Struct.pick(Something.fields, "id", "displayName")),
S.Struct(Struct.pick(Something.fields, "id", "displayName")),
(_) => Effect.andThen(SomeService, _)
)
)
)
const items = [
new Something({ displayName: S.NonEmptyString255("Verona"), n: new Date("2020-01-01T00:00:00Z") }),
new Something({ displayName: S.NonEmptyString255("Riley") }),
new Something({
displayName: S.NonEmptyString255("Riley"),
n: new Date("2020-01-01T00:00:00Z"),
union: { _tag: "number", value: 1 }
})
]
// TODO: .merge queries?
// where(x, y).or(a, b) + where(z, v) = (where(x, y) or(a,b)) and where(z, v)) ?
it("merge", () => {
const a = make().pipe(where("a", "b"), or("c", "d"))
const b = make().pipe(where("d", "e"), or("f", "g"))
const merge = (b: any) => (a: any) => pipe(a, and(() => b))
const r = pipe(a, merge(b), toFilter, (_) => _.filter)
// TODO: instead this should probably scope the first where/or together e.g (where x, or y) and (...)
const expected = make().pipe(
where("a", "b"),
or("c", "d"),
and(where("d", "e"), or("f", "g")),
toFilter,
(_) => _.filter
)
console.log(JSON.stringify({ r, expected }, undefined, 2))
expect(r).toEqual(expected)
})
it("works", () => {
console.log("raw", inspect(q, undefined, 25))
const interpreted = toFilter(q)
console.log("interpreted", inspect(interpreted, undefined, 25))
const processed = memFilter(interpreted)(items.map((_) =>
S.encodeUnknownSync(S.Struct({
...Something.omit("displayName"),
displayName: S.Literal("Verona", "Riley")
}))(_)
))
expect(processed).toEqual(items.slice(0, 2).toReversed().map(Struct.pick("id", "displayName")))
})
class SomethingRepo extends Effect.Service<SomethingRepo>()("SomethingRepo", {
strict: false,
effect: Effect.gen(function*() {
return yield* makeRepo("Something", Something, {})
})
}) {
static readonly Test = Layer
.effect(
SomethingRepo,
Effect.gen(function*() {
return SomethingRepo.make(yield* makeRepo("Something", Something, { makeInitial: Effect.sync(() => items) }))
})
)
.pipe(
Layer.provide(MemoryStoreLive)
)
}
it("works with repo", () =>
Effect
.gen(function*() {
const somethingRepo = yield* SomethingRepo
yield* somethingRepo.saveAndPublish(items)
const q0 = yield* somethingRepo.query(one)
expectTypeOf(q0).toEqualTypeOf<Something>()
const q1 = yield* somethingRepo.query(() => q)
const q2 = yield* somethingRepo
.query(
where("displayName", "Verona"),
or(
where("displayName", "Riley"),
and("n", "gt", "2021-01-01T00:00:00Z") // TODO: work with To type translation, so Date?
),
order("displayName"),
page({ take: 10 }),
// for projection performance benefit, this should be limited to the fields interested, and leads to SELECT fields
project(
S.transformToOrFail(
S.Struct(Struct.pick(Something.fields, "displayName")),
S.Struct(Struct.pick(Something.fields, "displayName")),
(_) => Effect.andThen(SomeService, _)
)
)
)
const smtArr = yield* somethingRepo
.query(
flow(where("displayName", "Verona"))
)
expectTypeOf(smtArr).toEqualTypeOf<readonly Something[]>()
expect(q1).toEqual(items.slice(0, 2).toReversed().map(Struct.pick("id", "displayName")))
expect(q2).toEqual(items.slice(0, 2).toReversed().map(Struct.pick("displayName")))
})
.pipe(
Effect.provide(Layer.mergeAll(SomethingRepo.Test, SomeService.toLayer())),
Effect.provide(MemoryStoreLive),
setupRequestContextFromCurrent(),
Effect.runPromise
))
it("collect", () =>
Effect
.gen(function*() {
const somethingRepo = yield* SomethingRepo
yield* somethingRepo.saveAndPublish(items)
expect(
yield* somethingRepo
.query(
where("displayName", "Riley"), // TODO: work with To type translation, so Date?
// one,
// for projection performance benefit, this should be limited to the fields interested, and leads to SELECT fields
project(
S.transformTo(
S.encodedSchema(S.Struct({
...Struct.pick(Something.fields, "n"),
displayName: S.String
})),
S.typeSchema(S.Option(S.String)),
(_) =>
_.displayName === "Riley" && _.n === "2020-01-01T00:00:00.000Z"
? Option.some(`${_.displayName}-${_.n}`)
: Option.none()
),
"collect"
)
)
)
.toEqual(["Riley-2020-01-01T00:00:00.000Z"])
const queryRes = make<Something.Encoded>().pipe(
where("union._tag", "string"),
one
)
expectTypeOf(queryRes).toEqualTypeOf<
QueryEnd<{
readonly id: string
readonly displayName: string
readonly n: string
readonly union: {
readonly _tag: "string"
readonly value: string
}
readonly name: string | null
}, "one">
>()
const fromRepo = yield* somethingRepo.query(
where("union._tag", "string"),
one,
project(
S.Struct({
union: S.Struct({
_tag: S.Literal("string"),
value: S.String
})
})
)
)
const value = fromRepo.union.value
expectTypeOf(value).toEqualTypeOf<string>()
expect(value).toEqual("hi")
})
.pipe(
Effect.provide(Layer.mergeAll(SomethingRepo.Test, SomeService.toLayer())),
Effect.provide(MemoryStoreLive),
setupRequestContextFromCurrent(),
Effect.runPromise
))
class Person extends S.ExtendedTaggedClass<Person, Person.Encoded>()("person", {
id: S.String,
surname: S.String
}) {}
class Animal extends S.ExtendedTaggedClass<Animal, Animal.Encoded>()("animal", {
id: S.String,
surname: S.String
}) {}
class Test extends S.ExtendedTaggedClass<Test, Test.Encoded>()("test", {
id: S.String
}) {}
namespace Person {
export interface Encoded extends S.Struct.Encoded<typeof Person["fields"]> {}
}
namespace Animal {
export interface Encoded extends S.Struct.Encoded<typeof Animal["fields"]> {}
}
namespace Test {
export interface Encoded extends S.Struct.Encoded<typeof Test["fields"]> {}
}
const TestUnion = S.Union(Person, Animal, Test)
type TestUnion = typeof TestUnion.Type
namespace TestUnion {
export type Encoded = typeof TestUnion.Encoded
}
it(
"refine",
() =>
Effect
.gen(function*() {
const repo = yield* makeRepo("test", TestUnion, {})
const result = yield* repo.query(where("id", "123"), and("_tag", "animal"))
const result2 = yield* repo.query(where("_tag", "animal"))
expectTypeOf(result).toEqualTypeOf<readonly Animal[]>()
expectTypeOf(result2).toEqualTypeOf<readonly Animal[]>()
expect(result).toEqual([])
expect(result2).toEqual([])
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise)
)
it(
"refine2",
() =>
Effect
.gen(function*() {
class AA extends S.TaggedClass<AA>()("AA", {
id: S.String,
a: S.Unknown
}) {}
class BB extends S.TaggedClass<BB>()("BB", {
id: S.String,
b: S.Unknown
}) {}
class CC extends S.TaggedClass<CC>()("CC", {
id: S.String,
c: S.Unknown
}) {}
class DD extends S.TaggedClass<DD>()("DD", {
id: S.String,
d: S.Unknown
}) {}
// const repo = yield* makeRepo("test", S.Union(AA, BB, CC, DD), {})
type Union = AA | BB | CC | DD
const query1 = make<Union>().pipe(
where("id", "bla"),
and("_tag", "AA")
)
expectTypeOf(query1).toEqualTypeOf<
QueryWhere<Union, AA, true>
>()
const query2 = make<Union>().pipe(
where("_tag", "AA")
)
expectTypeOf(query2).toEqualTypeOf<QueryWhere<Union, AA, true>>()
const query2a = make<Union>().pipe(
where("c", "something")
)
expectTypeOf(query2a).toEqualTypeOf<
QueryWhere<Union, {
readonly id: string
readonly _tag: "CC"
readonly c: {} // from unknown to {} because "something" means that it's not null or undefined
}>
>()
const query3 = make<Union>().pipe(
where("_tag", "AA"),
or(
where("id", "test"),
and("_tag", "BB")
)
)
expectTypeOf(query3).toEqualTypeOf<
QueryWhere<
Union,
AA | BB
>
>()
const query3b = make<Union>().pipe(
where("_tag", "AA"),
or(
where("_tag", "BB")
)
)
expectTypeOf(query3b).toEqualTypeOf<QueryWhere<Union, AA | BB>>()
const query4 = make<Union>().pipe(
where("_tag", "AA"),
project(S.Struct({ id: S.String, a: S.Unknown }))
)
expectTypeOf(query4).toEqualTypeOf<
QueryProjection<
AA,
{
readonly id: string
readonly a: unknown
},
never,
"many",
true
>
>()
// eslint-disable-next-line unused-imports/no-unused-vars
const query5 = make<Union>().pipe(
where("id", "bla"),
// @ts-expect-error cannot project over fields that are not in common between the union members (you must refine the union first)
project(S.Struct({ id: S.String, a: S.Unknown }))
)
console.log(query5)
const query6 = make<Union>().pipe(
where("_tag", "neq", "AA")
)
expectTypeOf(query6).toEqualTypeOf<QueryWhere<Union, BB | CC | DD>>()
const query7 = make<Union>().pipe(
where("_tag", "AA"),
or(
where("id", "test"),
and("_tag", "neq", "BB")
)
)
expectTypeOf(query7).toEqualTypeOf<
QueryWhere<
Union,
AA | CC | DD
>
>()
const query8 = make<Union>().pipe(
where("_tag", "neq", "AA"),
and("_tag", "AA")
)
expectTypeOf(query8).toEqualTypeOf<QueryWhere<Union, never, true>>()
const query9 = make<Union>().pipe(
where("id", "AA"),
and("_tag", "AA"),
or(
where("_tag", "BB"),
or(
where("id", "test"),
and("_tag", "CC")
)
)
)
expectTypeOf(query9).toEqualTypeOf<
QueryWhere<
Union,
AA | BB | CC
>
>()
const query10 = make<Union>().pipe(
where("id", "AA"),
and("_tag", "AA"),
or(
where("id", "test"),
and("_tag", "BB")
),
order("id", "ASC"),
page({ take: 10 }),
count
)
expectTypeOf(query10).toEqualTypeOf<
QueryProjection<
AA | BB,
S.NonNegativeInt,
never,
"count"
>
>()
const query11 = make<Union>().pipe(
where("id", "AA"),
and("_tag", "AA"),
or(
where("id", "test"),
and("_tag", "BB")
),
order("id", "ASC"),
page({ take: 10 }),
one
)
expectTypeOf(query11).toEqualTypeOf<
QueryEnd<
AA | BB,
"one"
>
>()
expect([]).toEqual([])
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise)
)
it(
"refine2",
() =>
Effect
.gen(function*() {
class AA extends S.Class<AA>()({
id: S.Literal("AA"),
a: S.Unknown
}) {}
class BB extends S.Class<BB>()({
id: S.Literal("BB"),
b: S.Unknown
}) {}
class CC extends S.Class<CC>()({
id: S.Literal("CC"),
c: S.Unknown
}) {}
class DD extends S.Class<DD>()({
id: S.Literal("DD"),
d: S.Unknown
}) {}
type Union = AA | BB | CC | DD
const repo = yield* makeRepo("test", S.Union(AA, BB, CC, DD), {})
const query1 = make<Union>().pipe(
where("id", "AA")
)
expectTypeOf(query1).toEqualTypeOf<QueryWhere<Union, AA>>()
const res = yield* repo.query(() => query1)
expectTypeOf(res).toEqualTypeOf<readonly AA[]>()
expect([]).toEqual([])
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise)
)
it(
"project",
() =>
Effect
.gen(function*() {
const schema = S.Struct({
id: S.String,
createdAt: S
.optional(S.Date)
.pipe(
S.withDefaults({ constructor: () => new Date(), decoding: () => new Date() })
)
})
const repo = yield* makeRepo(
"test",
schema,
{}
)
const outputSchema = S.Struct({
id: S.Literal("123"),
createdAt: S
.optional(S.Date)
.pipe(
S.withDefaults({ constructor: () => new Date(), decoding: () => new Date() })
)
})
const result = yield* repo.query(where("id", "123"), project(outputSchema))
expect(result).toEqual([])
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise)
)
it(
"doesn't mess when refining fields",
() =>
Effect
.gen(function*() {
const schema = S.Struct({
id: S.String,
literals: S.Literal("a", "b", "c")
})
type Schema = typeof schema.Type
const repo = yield* makeRepo(
"test",
schema,
{}
)
const result = yield* repo.query(
where("id", "123"),
and("literals", "a")
)
expectTypeOf(result).toEqualTypeOf<readonly Schema[]>()
expect(result).toEqual([])
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise)
)
it(
"remove null 1",
() =>
Effect
.gen(function*() {
const schema = S.Struct({
id: S.String,
literals: S.Union(S.Literal("a", "b", "c"), S.Null)
})
type Schema = typeof schema.Type
const repo = yield* makeRepo(
"test",
schema,
{}
)
const expected = make<Schema>().pipe(
where("literals", "neq", null)
)
expectTypeOf(expected).toEqualTypeOf<
QueryWhere<Schema, {
readonly id: string
readonly literals: "a" | "b" | "c"
}>
>()
const result = yield* repo.query(
where("literals", "neq", null)
)
expectTypeOf(result).toEqualTypeOf<
readonly {
readonly id: string
readonly literals: "a" | "b" | "c"
}[]
>()
expect(result).toEqual([])
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise)
)
it(
"remove null 2",
() =>
Effect
.gen(function*() {
const schema = S.Struct({
id: S.String,
literals: S.Union(S.String, S.Null)
})
type Schema = typeof schema.Type
const repo = yield* makeRepo(
"test",
schema,
{}
)
const expected = make<Schema>().pipe(
where("literals", "ciao")
)
expectTypeOf(expected).toEqualTypeOf<
QueryWhere<Schema, {
readonly id: string
readonly literals: string
}>
>()
const result = yield* repo.query(
where("literals", "neq", null)
)
expectTypeOf(result).toEqualTypeOf<
readonly {
readonly id: string
readonly literals: string
}[]
>()
expect(result).toEqual([])
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise)
)
it("remove null from one constituent of a tagged union", () =>
Effect
.gen(function*() {
class AA extends S.Class<AA>()({
id: S.Literal("AA"),
a: S.String
}) {}
class BB extends S.Class<BB>()({
id: S.Literal("BB"),
b: S.NullOr(S.Number)
}) {}
type Union = AA | BB
const repo = yield* makeRepo("test", S.Union(AA, BB), {})
const query1 = make<Union>().pipe(
where("id", "AA"),
or(
where("b", "neq", null)
)
)
expectTypeOf(query1).toEqualTypeOf<
QueryWhere<
Union,
AA | {
readonly id: "BB"
readonly b: number
}
>
>()
const resQuer1 = yield* repo.query(() => query1)
expectTypeOf(resQuer1).toEqualTypeOf<
readonly ({
readonly id: "AA"
readonly a: string
} | {
readonly id: "BB"
readonly b: number
})[]
>()
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise))
it("refine 3", () =>
Effect
.gen(function*() {
class AA extends S.Class<AA>()({
id: S.Literal("AA"),
a: S.Unknown
}) {}
class BB extends S.Class<BB>()({
id: S.Literal("BB"),
b: S.Unknown
}) {}
class CC extends S.Class<CC>()({
id: S.Literal("CC"),
c: S.Unknown
}) {}
class DD extends S.Class<DD>()({
id: S.Literal("DD"),
d: S.Unknown
}) {}
type Union = AA | BB | CC | DD
const repo = yield* makeRepo("test", S.Union(AA, BB, CC, DD), {})
const query1 = make<Union>().pipe(
where("id", "AA")
)
expectTypeOf(query1).toEqualTypeOf<QueryWhere<Union, AA>>()
const resQuer1 = yield* repo.query(where("id", "AA"))
expectTypeOf(resQuer1).toEqualTypeOf<readonly AA[]>()
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise))
it("my test", () =>
Effect
.gen(function*() {
class AA extends S.Class<AA>()({
id: S.String,
as: S.Array(S.String)
}) {}
const repo = yield* makeRepo("test", AA, {})
const resQuer1 = yield* repo.query(
where("id", "in", ["id1", "id2"]),
and(`as.-1`, "startsWith", "a")
)
expectTypeOf(resQuer1).toEqualTypeOf<readonly AA[]>()
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise))
it("refine inner without imposing a projection", () =>
Effect
.gen(function*() {
class AA extends S.TaggedClass<AA>()("AA", {
a: S.Unknown
}) {}
class BB extends S.TaggedClass<BB>()("BB", {
b: S.Unknown
}) {}
class Data extends S.Class<Data>()({
id: S.String,
union: S.Union(AA, BB)
}) {}
const repo = yield* makeRepo("data", Data, {})
const query1 = make<Data>().pipe(
where("union._tag", "AA"),
// I can refine the overall output by providing a proper projection
// that mimics the internal refinement of the encoding type
project(S.Struct({ union: AA }))
)
expectTypeOf(query1).toEqualTypeOf<
QueryProjection<
{
readonly id: string
readonly union: AA
},
{
readonly union: AA
},
never,
"many"
>
>()
const query2 = make<Data>().pipe(
where("union._tag", "AA"),
// But if I wanna the whole Data as output ignoring the inner refinement
// I wanna be able to do so
project(S.Struct(Data.pick("union")))
)
expectTypeOf(query2).toEqualTypeOf<
QueryProjection<
{
readonly id: string
readonly union: AA
},
{
readonly union: AA | BB
},
never,
"many"
>
>()
const resQuer1 = yield* repo.query(() => query1)
expectTypeOf(resQuer1).toEqualTypeOf<
readonly {
readonly union: AA
}[]
>()
const resQuer2 = yield* repo.query(() => query2)
expectTypeOf(resQuer2).toEqualTypeOf<
readonly {
readonly union: AA | BB
}[]
>()
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise))
it("does not allow string queries on arrays", () =>
Effect
.gen(function*() {
type Some = {
readonly id: string
readonly id2: `${string}:${string}`
readonly items: string[]
}
const base = make<Some>()
// @ts-expect-error cannot query with contains on arrays
const bad1 = base.pipe(where("items", "contains", "a"))
// @ts-expect-error cannot query with startsWith on arrays
const bad2 = base.pipe(where("items", "startsWith", "a"))
// @ts-expect-error cannot query with endsWith on arrays
const bad3 = base.pipe(where("items", "endsWith", "a"))
// @ts-expect-error cannot query with notContains on arrays
const bad4 = base.pipe(where("items", "notContains", "a"))
// @ts-expect-error cannot query with notStartsWith on arrays
const bad5 = base.pipe(where("items", "notStartsWith", "a"))
// @ts-expect-error cannot query with notEndsWith on arrays
const bad6 = base.pipe(where("items", "notEndsWith", "a"))
const good1 = base.pipe(where("items", "includes", "a"))
const good2 = base.pipe(where("items", "includes-any", ["a"]))
const good3 = base.pipe(where("id", "startsWith", "a"))
const good4 = base.pipe(where("id2", "startsWith", "a"))
expectTypeOf(good1).toEqualTypeOf<QueryWhere<Some, Some>>()
expectTypeOf(good2).toEqualTypeOf<QueryWhere<Some, Some>>()
expectTypeOf(good3).toEqualTypeOf<QueryWhere<Some, Some>>()
expectTypeOf(good4).toEqualTypeOf<QueryWhere<Some, Some>>()
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise))
it("test array.length", () =>
Effect
.gen(function*() {
type Something = {
readonly id: string
readonly items: string[]
readonly tuple: [string, string]
}
const base = make<Something>()
const query1 = base.pipe(
where("items.length", 0)
)
const query2 = base.pipe(
where("items.length", "gt", 2)
)
const query3 = base.pipe(
where("tuple.length", 2)
)
base.pipe(
// @ts-expect-error tuple.length is not valid
where("tuple.length", 3)
)
expectTypeOf(query1).toEqualTypeOf<
QueryWhere<Something, Something>
>()
expectTypeOf(query2).toEqualTypeOf<
QueryWhere<Something, Something>
>()
expectTypeOf(query3).toEqualTypeOf<
QueryWhere<Something, Something>
>()
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise))
it("distribution over union", () =>
Effect
.gen(function*() {
const repo = yield* makeRepo("test", TestUnion, {})
const res = yield* repo.query(
where("_tag", Math.random() > 0.5 ? "animal" : "person")
)
expectTypeOf(res).toEqualTypeOf<
| readonly ({
readonly id: string
readonly surname: string
readonly _tag: "person"
})[]
| readonly ({
readonly id: string
readonly surname: string
readonly _tag: "animal"
})[]
>()
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise))
it("refine nested union", () =>
Effect
.gen(function*() {
class TestNested extends S.Class<TestNested>()({ id: S.String, nested: TestUnion }) {}
const repo = yield* makeRepo("test", TestNested, {})
const base = make<TestNested>()
const res_query = base.pipe(
where("nested._tag", Math.random() > 0.5 ? "animal" : "person")
)
expectTypeOf(res_query).toEqualTypeOf<
QueryWhere<TestNested, {
readonly id: string
readonly nested: {
readonly _tag: "person"
readonly id: string
readonly surname: string
} | {
readonly _tag: "animal"
readonly id: string
readonly surname: string
}
}, false>
>()
const res = yield* repo.query(
() => res_query
)
expectTypeOf(res).toEqualTypeOf<
readonly {
readonly id: string
readonly nested: Person | Animal
}[]
>()
})
.pipe(Effect.provide(MemoryStoreLive), setupRequestContextFromCurrent(), Effect.runPromise))
it("refine union with nested union", () =>
Effect
.gen(function*() {
class A extends S.TaggedClass<A>()("A", {
a: S.String
}) {}
class B extends S.TaggedClass<B>()("B", {
b: S.String
}) {}
class C extends S.TaggedClass<C>()("C", {
c: S.String
}) {}
class D extends S.TaggedClass<D>()("D", {
d: S.String
}) {}