@effect-app/infra 2.6.1 → 2.7.1

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.
Files changed (34) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/_cjs/services/Repository/legacy.cjs.map +1 -1
  3. package/_cjs/services/RepositoryBase.cjs.map +1 -1
  4. package/_cjs/services/Store/service.cjs.map +1 -1
  5. package/_cjs/services/query/dsl.cjs +10 -11
  6. package/_cjs/services/query/dsl.cjs.map +1 -1
  7. package/_cjs/services/query/new-kid-interpreter.cjs +1 -0
  8. package/_cjs/services/query/new-kid-interpreter.cjs.map +1 -1
  9. package/dist/services/Repository/legacy.d.ts +7 -7
  10. package/dist/services/Repository/legacy.d.ts.map +1 -1
  11. package/dist/services/Repository/legacy.js +1 -1
  12. package/dist/services/Repository/service.d.ts +10 -4
  13. package/dist/services/Repository/service.d.ts.map +1 -1
  14. package/dist/services/RepositoryBase.d.ts.map +1 -1
  15. package/dist/services/RepositoryBase.js +1 -1
  16. package/dist/services/Store/service.d.ts +6 -5
  17. package/dist/services/Store/service.d.ts.map +1 -1
  18. package/dist/services/Store/service.js +1 -1
  19. package/dist/services/query/dsl.d.ts +116 -58
  20. package/dist/services/query/dsl.d.ts.map +1 -1
  21. package/dist/services/query/dsl.js +12 -13
  22. package/dist/services/query/new-kid-interpreter.d.ts +2 -1
  23. package/dist/services/query/new-kid-interpreter.d.ts.map +1 -1
  24. package/dist/services/query/new-kid-interpreter.js +2 -1
  25. package/examples/query.ts +37 -11
  26. package/package.json +1 -1
  27. package/src/services/Repository/legacy.ts +14 -12
  28. package/src/services/Repository/service.ts +20 -12
  29. package/src/services/RepositoryBase.ts +4 -2
  30. package/src/services/Store/service.ts +6 -3
  31. package/src/services/query/dsl.ts +742 -315
  32. package/src/services/query/new-kid-interpreter.ts +10 -3
  33. package/test/dist/query.test.d.ts.map +1 -1
  34. package/test/query.test.ts +245 -11
@@ -18,7 +18,12 @@ type Result<TFieldValues extends FieldValues, A = TFieldValues, R = never> = {
18
18
  mode: "collect" | "project" | "transform" | undefined
19
19
  }
20
20
 
21
- const interpret = <TFieldValues extends FieldValues, A = TFieldValues, R = never>(_: QAll<TFieldValues, A, R>) => {
21
+ const interpret = <
22
+ TFieldValues extends FieldValues,
23
+ TFieldValuesRefined extends TFieldValues = TFieldValues,
24
+ A = TFieldValues,
25
+ R = never
26
+ >(_: QAll<TFieldValues, TFieldValuesRefined, A, R>) => {
22
27
  const a = _ as Q<TFieldValues>
23
28
 
24
29
  const data: Result<TFieldValues, any, any> = {
@@ -133,9 +138,10 @@ const interpret = <TFieldValues extends FieldValues, A = TFieldValues, R = never
133
138
  export const toFilter = <
134
139
  TFieldValues extends FieldValues,
135
140
  A,
136
- R
141
+ R,
142
+ TFieldValuesRefined extends TFieldValues = TFieldValues
137
143
  >(
138
- q: QAll<TFieldValues, A, R>
144
+ q: QAll<TFieldValues, TFieldValuesRefined, A, R>
139
145
  ) => {
140
146
  // TODO: Native interpreter for each db adapter, instead of the intermediate "new-kid" format
141
147
  const a = interpret(q)
@@ -151,6 +157,7 @@ export const toFilter = <
151
157
  }
152
158
  }
153
159
  return dropUndefinedT({
160
+ t: null as unknown as TFieldValues,
154
161
  limit: a.limit,
155
162
  skip: a.skip,
156
163
  select: Option.getOrUndefined(toNonEmptyArray(select)),
@@ -1 +1 @@
1
- {"version":3,"file":"query.test.d.ts","sourceRoot":"","sources":["../query.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAQ,KAAK,EAAgB,CAAC,EAAU,MAAM,YAAY,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWlF,qBAAa,SAAU,SAAQ,cAK7B;CAAG;AACL,MAAM,CAAC,OAAO,WAAW,SAAS,CAAC;IACjC,UAAiB,OAAQ,SAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,SAAS,CAAC;KAAG;CACvE;;;;;;;;;;;;;;;;;;;;;;;;;AAGD,qBAAa,WAAY,SAAQ,gBAAgE;CAAG"}
1
+ {"version":3,"file":"query.test.d.ts","sourceRoot":"","sources":["../query.test.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAQ,KAAK,EAAgB,CAAC,EAAU,MAAM,YAAY,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYlF,qBAAa,SAAU,SAAQ,cAK7B;CAAG;AACL,MAAM,CAAC,OAAO,WAAW,SAAS,CAAC;IAEjC,UAAiB,OAAQ,SAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,SAAS,CAAC;KAAG;CACvE;;;;;;;;;;;;;;;;;;;;;;;;;AAGD,qBAAa,WAAY,SAAQ,gBAAgE;CAAG"}
@@ -1,8 +1,11 @@
1
+ /* eslint-disable @typescript-eslint/no-empty-object-type */
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
3
  import { Context, Effect, flow, Layer, Option, pipe, S, Struct } from "effect-app"
2
4
  import { inspect } from "util"
3
- import { expect, it } from "vitest"
4
- import { and, make, one, or, order, page, project, toFilter, where } from "../src/services/query.js"
5
- import { RepositoryDefaultImpl2 } from "../src/services/Repository/legacy.js"
5
+ import { expect, expectTypeOf, it } from "vitest"
6
+ import type { QueryEnd, QueryProjection, QueryWhere } from "../src/services/query.js"
7
+ import { and, count, make, one, or, order, page, project, toFilter, where } from "../src/services/query.js"
8
+ import { makeRepo } from "../src/services/RepositoryBase.js"
6
9
  import { memFilter, MemoryStoreLive } from "../src/services/Store/Memory.js"
7
10
 
8
11
  const str = S.Struct({ _tag: S.Literal("string"), value: S.String })
@@ -16,6 +19,7 @@ export class Something extends S.Class<Something>()({
16
19
  union: someUnion.pipe(S.withDefaultConstructor(() => ({ _tag: "string" as const, value: "hi" })))
17
20
  }) {}
18
21
  export declare namespace Something {
22
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
19
23
  export interface Encoded extends S.Schema.Encoded<typeof Something> {}
20
24
  }
21
25
 
@@ -84,14 +88,21 @@ it("works", () => {
84
88
  expect(processed).toEqual(items.slice(0, 2).toReversed().map(Struct.pick("id", "displayName")))
85
89
  })
86
90
 
87
- class SomethingRepo extends RepositoryDefaultImpl2<SomethingRepo>()(
88
- "test",
89
- Something,
90
- { idKey: "id" }
91
- ) {
92
- static readonly Test = SomethingRepo.DefaultWithoutDependencies.pipe(
93
- Layer.provide(MemoryStoreLive)
94
- )
91
+ class SomethingRepo extends Effect.Service<SomethingRepo>()("SomethingRepo", {
92
+ effect: Effect.gen(function*() {
93
+ return yield* makeRepo("Something", Something, {})
94
+ })
95
+ }) {
96
+ static readonly Test = Layer
97
+ .effect(
98
+ SomethingRepo,
99
+ Effect.gen(function*() {
100
+ return SomethingRepo.make(yield* makeRepo("Something", Something, { makeInitial: Effect.sync(() => items) }))
101
+ })
102
+ )
103
+ .pipe(
104
+ Layer.provide(MemoryStoreLive)
105
+ )
95
106
  }
96
107
 
97
108
  it("works with repo", () =>
@@ -174,3 +185,226 @@ it("collect", () =>
174
185
  .toEqual("hi")
175
186
  })
176
187
  .pipe(Effect.provide(Layer.mergeAll(SomethingRepo.Test, SomeService.toLayer())), Effect.runPromise))
188
+
189
+ class Person extends S.ExtendedTaggedClass<Person, Person.From>()("person", {
190
+ id: S.String,
191
+ surname: S.String
192
+ }) {}
193
+ class Animal extends S.ExtendedTaggedClass<Animal, Animal.From>()("animal", {
194
+ id: S.String,
195
+ surname: S.String
196
+ }) {}
197
+ class Test extends S.ExtendedTaggedClass<Test, Test.From>()("test", {
198
+ id: S.String
199
+ }) {}
200
+
201
+ namespace Person {
202
+ export interface From extends S.Struct.Encoded<typeof Person["fields"]> {}
203
+ }
204
+ namespace Animal {
205
+ export interface From extends S.Struct.Encoded<typeof Animal["fields"]> {}
206
+ }
207
+ namespace Test {
208
+ export interface From extends S.Struct.Encoded<typeof Test["fields"]> {}
209
+ }
210
+
211
+ const TestUnion = S.Union(Person, Animal, Test)
212
+ type TestUnion = typeof TestUnion.Type
213
+ namespace TestUnion {
214
+ export type From = typeof TestUnion.Encoded
215
+ }
216
+
217
+ it(
218
+ "refine",
219
+ () =>
220
+ Effect
221
+ .gen(function*() {
222
+ const repo = yield* makeRepo("test", TestUnion, {})
223
+ const result = (yield* repo.query(flow(where("id", "123"), and("_tag", "animal")))) satisfies readonly Animal[]
224
+ const result2 = (yield* repo.query(flow(where("_tag", "animal")))) satisfies readonly Animal[]
225
+
226
+ expect(result).toEqual([])
227
+ expect(result2).toEqual([])
228
+ })
229
+ .pipe(Effect.provide(MemoryStoreLive), Effect.runPromise)
230
+ )
231
+
232
+ it(
233
+ "refine2",
234
+ () =>
235
+ Effect
236
+ .gen(function*() {
237
+ class AA extends S.TaggedClass<AA>()("AA", {
238
+ id: S.String,
239
+ a: S.Unknown
240
+ }) {}
241
+
242
+ class BB extends S.TaggedClass<BB>()("BB", {
243
+ id: S.String,
244
+ b: S.Unknown
245
+ }) {}
246
+
247
+ class CC extends S.TaggedClass<CC>()("CC", {
248
+ id: S.String,
249
+ c: S.Unknown
250
+ }) {}
251
+
252
+ class DD extends S.TaggedClass<DD>()("DD", {
253
+ id: S.String,
254
+ d: S.Unknown
255
+ }) {}
256
+
257
+ type Union = AA | BB | CC | DD
258
+
259
+ const query1 = make<Union>().pipe(
260
+ where("id", "bla"),
261
+ and("_tag", "AA")
262
+ )
263
+ expectTypeOf(query1).toEqualTypeOf<QueryWhere<Union, AA>>()
264
+
265
+ const query2 = make<Union>().pipe(
266
+ where("_tag", "AA")
267
+ )
268
+ expectTypeOf(query2).toEqualTypeOf<QueryWhere<Union, AA>>()
269
+
270
+ const query3 = make<Union>().pipe(
271
+ where("_tag", "AA"),
272
+ or(
273
+ where("id", "test"),
274
+ and("_tag", "BB")
275
+ )
276
+ )
277
+ expectTypeOf(query3).toEqualTypeOf<QueryWhere<Union, AA | BB>>()
278
+
279
+ const query3b = make<Union>().pipe(
280
+ where("_tag", "AA"),
281
+ or(
282
+ where("_tag", "BB")
283
+ )
284
+ )
285
+ expectTypeOf(query3b).toEqualTypeOf<QueryWhere<Union, AA | BB>>()
286
+
287
+ const query4 = make<Union>().pipe(
288
+ where("_tag", "AA"),
289
+ project(S.Struct({ id: S.String, a: S.Unknown }))
290
+ )
291
+ expectTypeOf(query4).toEqualTypeOf<
292
+ QueryProjection<
293
+ AA,
294
+ {
295
+ readonly id: string
296
+ readonly a: unknown
297
+ },
298
+ never,
299
+ "many"
300
+ >
301
+ >()
302
+
303
+ // eslint-disable-next-line unused-imports/no-unused-vars
304
+ const query5 = make<Union>().pipe(
305
+ where("id", "bla"),
306
+ // @ts-expect-error cannot project over fields that are not in common between the union members (you must refine the union first)
307
+ project(S.Struct({ id: S.String, a: S.Unknown }))
308
+ )
309
+ console.log(query5)
310
+
311
+ const query6 = make<Union>().pipe(
312
+ where("_tag", "neq", "AA")
313
+ )
314
+ expectTypeOf(query6).toEqualTypeOf<QueryWhere<Union, BB | CC | DD>>()
315
+
316
+ const query7 = make<Union>().pipe(
317
+ where("_tag", "AA"),
318
+ or(
319
+ where("id", "test"),
320
+ and("_tag", "neq", "BB")
321
+ )
322
+ )
323
+ expectTypeOf(query7).toEqualTypeOf<QueryWhere<Union, AA | CC | DD>>()
324
+
325
+ const query8 = make<Union>().pipe(
326
+ where("_tag", "neq", "AA"),
327
+ and("_tag", "AA")
328
+ )
329
+ expectTypeOf(query8).toEqualTypeOf<QueryWhere<Union, never>>()
330
+
331
+ const query9 = make<Union>().pipe(
332
+ where("id", "AA"),
333
+ and("_tag", "AA"),
334
+ or(
335
+ where("_tag", "BB"),
336
+ or(
337
+ where("id", "test"),
338
+ and("_tag", "CC")
339
+ )
340
+ )
341
+ )
342
+ expectTypeOf(query9).toEqualTypeOf<QueryWhere<Union, AA | BB | CC>>()
343
+
344
+ const query10 = make<Union>().pipe(
345
+ where("id", "AA"),
346
+ and("_tag", "AA"),
347
+ or(
348
+ where("id", "test"),
349
+ and("_tag", "BB")
350
+ ),
351
+ order("id", "ASC"),
352
+ page({ take: 10 }),
353
+ count
354
+ )
355
+ expectTypeOf(query10).toEqualTypeOf<QueryProjection<AA | BB, S.NonNegativeInt, never, "count">>()
356
+
357
+ const query11 = make<Union>().pipe(
358
+ where("id", "AA"),
359
+ and("_tag", "AA"),
360
+ or(
361
+ where("id", "test"),
362
+ and("_tag", "BB")
363
+ ),
364
+ order("id", "ASC"),
365
+ page({ take: 10 }),
366
+ one
367
+ )
368
+ expectTypeOf(query11).toEqualTypeOf<QueryEnd<AA | BB, "one">>()
369
+
370
+ expect([]).toEqual([])
371
+ })
372
+ .pipe(Effect.runPromise)
373
+ )
374
+
375
+ it(
376
+ "refine2",
377
+ () =>
378
+ Effect
379
+ .gen(function*() {
380
+ class AA extends S.Class<AA>()({
381
+ id: S.Literal("AA"),
382
+ a: S.Unknown
383
+ }) {}
384
+
385
+ class BB extends S.Class<BB>()({
386
+ id: S.Literal("BB"),
387
+ b: S.Unknown
388
+ }) {}
389
+
390
+ class CC extends S.Class<CC>()({
391
+ id: S.Literal("CC"),
392
+ c: S.Unknown
393
+ }) {}
394
+
395
+ class DD extends S.Class<DD>()({
396
+ id: S.Literal("DD"),
397
+ d: S.Unknown
398
+ }) {}
399
+
400
+ type Union = AA | BB | CC | DD
401
+
402
+ const query1 = make<Union>().pipe(
403
+ where("id", "AA")
404
+ )
405
+ expectTypeOf(query1).toEqualTypeOf<QueryWhere<Union, AA>>()
406
+
407
+ expect([]).toEqual([])
408
+ })
409
+ .pipe(Effect.runPromise)
410
+ )