effect-qb 0.19.0 → 4.0.0-beta.92

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 (89) hide show
  1. package/README.md +7 -1
  2. package/dist/index.js +1987 -679
  3. package/dist/mysql.js +1486 -616
  4. package/dist/postgres/metadata.js +1334 -263
  5. package/dist/postgres.js +3374 -2308
  6. package/dist/sqlite.js +1569 -627
  7. package/dist/standard.js +1982 -674
  8. package/package.json +2 -4
  9. package/src/internal/coercion/rules.ts +13 -1
  10. package/src/internal/column-state.d.ts +3 -3
  11. package/src/internal/column-state.ts +13 -12
  12. package/src/internal/column.ts +8 -8
  13. package/src/internal/datatypes/define.ts +5 -0
  14. package/src/internal/datatypes/lookup.ts +67 -18
  15. package/src/internal/datatypes/matrix.ts +903 -0
  16. package/src/internal/datatypes/shape.ts +2 -0
  17. package/src/internal/dialect-renderers/mysql.ts +6 -4
  18. package/src/internal/dialect-renderers/postgres.ts +6 -4
  19. package/src/internal/dialect-renderers/sqlite.ts +6 -4
  20. package/src/internal/dialect.ts +1 -1
  21. package/src/internal/executor.ts +56 -43
  22. package/src/internal/json/path-access.ts +351 -0
  23. package/src/internal/query.d.ts +1 -1
  24. package/src/internal/query.ts +1 -1
  25. package/src/internal/runtime/driver-value-mapping.ts +3 -3
  26. package/src/internal/runtime/schema.ts +28 -38
  27. package/src/internal/runtime/value.ts +20 -23
  28. package/src/internal/scalar.d.ts +1 -1
  29. package/src/internal/scalar.ts +2 -1
  30. package/src/internal/schema-derivation.d.ts +7 -7
  31. package/src/internal/schema-derivation.ts +11 -11
  32. package/src/internal/standard-dsl.ts +118 -28
  33. package/src/internal/table.ts +451 -120
  34. package/src/mysql/column.ts +6 -6
  35. package/src/mysql/datatypes/index.ts +1 -0
  36. package/src/mysql/datatypes/spec.ts +6 -176
  37. package/src/mysql/executor.ts +4 -6
  38. package/src/mysql/function/temporal.ts +1 -1
  39. package/src/mysql/internal/dsl.ts +113 -16
  40. package/src/mysql/json.ts +1 -33
  41. package/src/mysql/renderer.ts +13 -6
  42. package/src/mysql/type.ts +60 -0
  43. package/src/mysql.ts +3 -1
  44. package/src/postgres/check.ts +1 -0
  45. package/src/postgres/column.ts +11 -11
  46. package/src/postgres/datatypes/index.ts +1 -0
  47. package/src/postgres/datatypes/spec.ts +6 -260
  48. package/src/postgres/executor.ts +4 -6
  49. package/src/postgres/foreign-key.ts +24 -0
  50. package/src/postgres/function/temporal.ts +1 -1
  51. package/src/postgres/index.ts +1 -0
  52. package/src/postgres/internal/dsl.ts +119 -21
  53. package/src/postgres/json-extension.ts +7 -0
  54. package/src/postgres/json.ts +726 -173
  55. package/src/postgres/jsonb.ts +0 -1
  56. package/src/postgres/primary-key.ts +24 -0
  57. package/src/postgres/renderer.ts +13 -6
  58. package/src/postgres/schema-management.ts +1 -6
  59. package/src/postgres/schema.ts +16 -8
  60. package/src/postgres/table.ts +111 -113
  61. package/src/postgres/type.ts +86 -4
  62. package/src/postgres/unique.ts +32 -0
  63. package/src/postgres.ts +12 -6
  64. package/src/sqlite/column.ts +6 -6
  65. package/src/sqlite/datatypes/index.ts +1 -0
  66. package/src/sqlite/datatypes/spec.ts +6 -94
  67. package/src/sqlite/executor.ts +4 -6
  68. package/src/sqlite/function/temporal.ts +1 -1
  69. package/src/sqlite/internal/dsl.ts +97 -5
  70. package/src/sqlite/json.ts +1 -32
  71. package/src/sqlite/renderer.ts +13 -6
  72. package/src/sqlite/type.ts +40 -0
  73. package/src/sqlite.ts +3 -1
  74. package/src/standard/cast.ts +113 -0
  75. package/src/standard/check.ts +17 -0
  76. package/src/standard/column.ts +10 -10
  77. package/src/standard/datatypes/index.ts +2 -2
  78. package/src/standard/datatypes/spec.ts +10 -96
  79. package/src/standard/foreign-key.ts +37 -0
  80. package/src/standard/function/temporal.ts +1 -1
  81. package/src/standard/index.ts +17 -0
  82. package/src/standard/json.ts +883 -0
  83. package/src/standard/primary-key.ts +17 -0
  84. package/src/standard/renderer.ts +31 -3
  85. package/src/standard/table.ts +25 -21
  86. package/src/standard/unique.ts +17 -0
  87. package/src/standard.ts +14 -0
  88. package/src/internal/table.d.ts +0 -174
  89. package/src/postgres/cast.ts +0 -45
@@ -1,5 +1,5 @@
1
1
  import * as Expression from "../internal/scalar.js"
2
- import type * as ExpressionAst from "../internal/expression-ast.js"
2
+ import * as ExpressionAst from "../internal/expression-ast.js"
3
3
  import type { JsonPathUsageError } from "../internal/json/errors.js"
4
4
  import * as JsonPath from "../internal/json/path.js"
5
5
  import type {
@@ -10,6 +10,8 @@ import type {
10
10
  JsonValueAtPath
11
11
  } from "../internal/json/types.js"
12
12
  import type { LiteralStringInput } from "../internal/table-options.js"
13
+ import type { JsonObjectKeyOf, WithJsonPathAccess } from "../internal/json/path-access.js"
14
+ import type { postgresDatatypes } from "./datatypes/index.js"
13
15
  import { json as postgresJson, jsonb as postgresJsonb } from "./internal/dsl.js"
14
16
 
15
17
  type PostgresJsonExpression<Runtime = unknown> = Expression.Scalar<
@@ -30,6 +32,15 @@ type PostgresJsonbExpression<Runtime = unknown> = Expression.Scalar<
30
32
  Expression.BindingId
31
33
  >
32
34
 
35
+ type AnyJsonExpression<Runtime = unknown> = Expression.Scalar<
36
+ Runtime,
37
+ Expression.DbType.Json<any, any>,
38
+ Expression.Nullability,
39
+ string,
40
+ Expression.ScalarKind,
41
+ Expression.BindingId
42
+ >
43
+
33
44
  type ExactJsonPathInput = JsonPath.ExactSegment | JsonPath.Path<any>
34
45
 
35
46
  type JsonPathPredicateExpression = Expression.Scalar<
@@ -49,7 +60,7 @@ type JsonPathPredicateQueryInput<Query extends JsonPathPredicateQuery> =
49
60
  type ExactJsonPathUsageError<Target> = {
50
61
  readonly __effect_qb_error__: "effect-qb: postgres json helpers only accept exact key/index paths"
51
62
  readonly __effect_qb_json_path__: Target
52
- readonly __effect_qb_hint__: "Use Postgres.Jsonb.path(...) when you need wildcard(), slice(), or descend() segments"
63
+ readonly __effect_qb_hint__: "Use Postgres.Jsonb.key(), wildcard(), slice(), or descend() in a pipe when you need jsonb path traversal"
53
64
  }
54
65
 
55
66
  type ExactJsonPathGuard<Target> = Target extends JsonPath.Path<any>
@@ -58,9 +69,6 @@ type ExactJsonPathGuard<Target> = Target extends JsonPath.Path<any>
58
69
  ? unknown
59
70
  : ExactJsonPathUsageError<Target>
60
71
 
61
- type ExactJsonPathSegmentsGuard<Segments extends readonly JsonPath.CanonicalSegment[]> =
62
- JsonPath.IsExactPath<JsonPath.Path<Segments>> extends true ? unknown : ExactJsonPathUsageError<JsonPath.Path<Segments>>
63
-
64
72
  type JsonPathOutputOf<
65
73
  Root,
66
74
  Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment,
@@ -166,11 +174,6 @@ type JsonbBaseGuard<
166
174
  ? unknown
167
175
  : JsonbOnlyUsageError<Operation, Base>
168
176
 
169
- type JsonNullabilityOf<Output> =
170
- null extends Output
171
- ? Exclude<Output, null> extends never ? "always" : "maybe"
172
- : "never"
173
-
174
177
  type JsonPathSegmentsOf<Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment> =
175
178
  Target extends JsonPath.Path<infer Segments extends readonly JsonPath.CanonicalSegment[]> ? Segments :
176
179
  Target extends JsonPath.CanonicalSegment ? readonly [Target] :
@@ -186,17 +189,27 @@ type JsonTextAccessKind<Target extends JsonPath.Path<any> | JsonPath.CanonicalSe
186
189
  ? JsonPath.IsExactPath<Target> extends true ? "jsonPathText" : "jsonTraverseText"
187
190
  : Target extends JsonPath.ExactSegment ? "jsonGetText" : "jsonAccessText"
188
191
 
192
+ type PostgresTextDb = ReturnType<typeof postgresDatatypes.text>
193
+ type PostgresBoolDb = ReturnType<typeof postgresDatatypes.boolean>
194
+
195
+ type DialectOf<Value extends Expression.Any> = Value[typeof Expression.TypeId]["dialect"]
196
+ type ScalarKindOf<Value extends Expression.Any> =
197
+ Extract<Expression.KindOf<Value>, "scalar"> extends never
198
+ ? Expression.KindOf<Value>
199
+ : Extract<Expression.KindOf<Value>, "scalar">
200
+
189
201
  type JsonResultExpression<
190
202
  Runtime,
191
203
  Db extends Expression.DbType.Json<any, any>,
192
204
  Kind extends Expression.ScalarKind = Expression.ScalarKind,
193
205
  Dependencies extends Expression.BindingId = Expression.BindingId,
194
- Ast extends ExpressionAst.Any = never
206
+ Ast extends ExpressionAst.Any = never,
207
+ Dialect extends string = string
195
208
  > = Expression.Scalar<
196
209
  Runtime,
197
210
  Db,
198
- JsonNullabilityOf<Runtime>,
199
- string,
211
+ Expression.Nullability,
212
+ Dialect,
200
213
  Kind,
201
214
  Dependencies
202
215
  > & ([Ast] extends [never] ? unknown : {
@@ -212,38 +225,390 @@ type JsonGetResultExpression<
212
225
  Base extends PostgresJsonExpression<any>,
213
226
  Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment,
214
227
  Operation extends string
215
- > = JsonResultExpression<
228
+ > = WithJsonPathAccess<JsonResultExpression<
216
229
  JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, Operation>,
217
230
  JsonDbOf<Base>,
218
231
  Expression.KindOf<Base>,
219
232
  Expression.DependenciesOf<Base>,
220
- ExpressionAst.JsonAccessNode<JsonGetAccessKind<Target>, Base, JsonPathSegmentsOf<Target>>
221
- >
233
+ ExpressionAst.JsonAccessNode<JsonGetAccessKind<Target>, Base, JsonPathSegmentsOf<Target>>,
234
+ DialectOf<Base>
235
+ >>
222
236
 
223
237
  type JsonTextRuntime<
224
- Base extends PostgresJsonExpression<any>,
238
+ Base extends AnyJsonExpression<any>,
225
239
  Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment
226
240
  > =
227
241
  JsonTextResult<Exclude<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.text">, JsonPathUsageError<any, any, any, any> | null>> |
228
242
  (null extends JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.text"> ? null : never)
229
243
 
230
244
  type JsonTextResultExpression<
231
- Base extends PostgresJsonExpression<any>,
245
+ Base extends AnyJsonExpression<any>,
232
246
  Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment
233
247
  > = Expression.Scalar<
234
248
  JsonTextRuntime<Base, Target>,
235
- Expression.DbType.Base<"postgres", "text">,
236
- JsonNullabilityOf<JsonTextRuntime<Base, Target>>,
237
- string,
249
+ PostgresTextDb,
250
+ Expression.Nullability,
251
+ DialectOf<Base>,
238
252
  Expression.KindOf<Base>,
239
253
  Expression.DependenciesOf<Base>
240
254
  > & {
241
255
  readonly [ExpressionAst.TypeId]: ExpressionAst.JsonAccessNode<JsonTextAccessKind<Target>, Base, JsonPathSegmentsOf<Target>>
242
256
  }
243
257
 
244
- const exactPath = <Segments extends readonly JsonPath.CanonicalSegment[]>(
245
- ...segments: Segments & ExactJsonPathSegmentsGuard<Segments>
246
- ): JsonPath.Path<Segments> => JsonPath.path(...segments) as unknown as JsonPath.Path<Segments>
258
+ type SegmentTuple<Segments> = Segments extends readonly JsonPath.CanonicalSegment[]
259
+ ? Segments
260
+ : readonly JsonPath.CanonicalSegment[]
261
+
262
+ type JsonAccessAst<Value> = Value extends {
263
+ readonly [ExpressionAst.TypeId]: infer Ast
264
+ } ? Ast : never
265
+
266
+ type JsonAccessExpression = {
267
+ readonly [ExpressionAst.TypeId]: ExpressionAst.JsonAccessNode<any, any, any>
268
+ }
269
+
270
+ type JsonAccessParts<
271
+ Value,
272
+ Acc extends readonly JsonPath.CanonicalSegment[] = readonly [],
273
+ Depth extends readonly unknown[] = []
274
+ > =
275
+ Depth["length"] extends 8 ? readonly [Value, readonly JsonPath.CanonicalSegment[]] :
276
+ [JsonAccessAst<Value>] extends [never]
277
+ ? Acc extends readonly [] ? never : readonly [Value, Acc]
278
+ : JsonAccessAst<Value> extends ExpressionAst.JsonAccessNode<any, infer Base extends Expression.Any, infer Segments>
279
+ ? JsonAccessParts<Base, readonly [...SegmentTuple<Segments>, ...Acc], readonly [...Depth, unknown]>
280
+ : Acc extends readonly [] ? never : readonly [Value, Acc]
281
+
282
+ type JsonAccessRoot<Value> =
283
+ JsonAccessParts<Value> extends readonly [infer Base, readonly JsonPath.CanonicalSegment[]]
284
+ ? Base
285
+ : Value
286
+
287
+ type JsonAccessSegments<Value> =
288
+ JsonAccessParts<Value> extends readonly [any, infer Segments extends readonly JsonPath.CanonicalSegment[]]
289
+ ? Segments
290
+ : never
291
+
292
+ type JsonAccessPath<Value> = JsonPath.Path<JsonAccessSegments<Value>>
293
+
294
+ type JsonAccessBase<Value extends Expression.Any> =
295
+ [JsonAccessSegments<Value>] extends [never]
296
+ ? Value
297
+ : JsonAccessRoot<Value> extends Expression.Any
298
+ ? JsonAccessRoot<Value>
299
+ : Value
300
+
301
+ type JsonTextValueRuntime<Value extends Expression.Any> =
302
+ (Exclude<Expression.RuntimeOf<Value>, JsonPathUsageError<any, any, any, any> | null> extends infer Runtime
303
+ ? Runtime extends string ? Runtime : string
304
+ : string) |
305
+ (null extends Expression.RuntimeOf<Value> ? null : never)
306
+
307
+ type JsonTextValueNullability<Value extends Expression.Any> =
308
+ null extends JsonTextValueRuntime<Value> ? "maybe" : "never"
309
+
310
+ type JsonAccessTextResultExpression<Value extends AnyJsonExpression<any>> =
311
+ Value extends JsonAccessExpression
312
+ ? Expression.Scalar<
313
+ JsonTextValueRuntime<Value>,
314
+ PostgresTextDb,
315
+ JsonTextValueNullability<Value>,
316
+ DialectOf<Value>,
317
+ ScalarKindOf<Value>,
318
+ Expression.DependenciesOf<Value>
319
+ > & {
320
+ readonly [ExpressionAst.TypeId]: ExpressionAst.JsonAccessNode<
321
+ "jsonPathText",
322
+ JsonAccessBase<Value>,
323
+ JsonAccessSegments<Value>
324
+ >
325
+ }
326
+ : JsonTextTerminalExpression<Value>
327
+
328
+ type JsonAccessDeleteResultExpression<
329
+ Value extends PostgresJsonExpression<any>,
330
+ Operation extends string
331
+ > = Value extends JsonAccessExpression
332
+ ? JsonAccessRoot<Value> extends PostgresJsonExpression<any>
333
+ ? WithJsonPathAccess<JsonResultExpression<
334
+ JsonDeleteOutputOf<Expression.RuntimeOf<JsonAccessRoot<Value>>, JsonAccessPath<Value>, Operation>,
335
+ Expression.DbTypeOf<JsonAccessRoot<Value>>,
336
+ Expression.KindOf<JsonAccessRoot<Value>>,
337
+ Expression.DependenciesOf<JsonAccessRoot<Value>>,
338
+ never,
339
+ DialectOf<JsonAccessRoot<Value>>
340
+ >>
341
+ : never
342
+ : WithJsonPathAccess<JsonResultExpression<
343
+ unknown,
344
+ Expression.DbTypeOf<Value>,
345
+ Expression.KindOf<Value>,
346
+ Expression.DependenciesOf<Value>,
347
+ never,
348
+ DialectOf<Value>
349
+ >>
350
+
351
+ type JsonAccessSetResultExpression<
352
+ Value extends PostgresJsonExpression<any>,
353
+ Next,
354
+ CreateMissing extends boolean
355
+ > = Value extends JsonAccessExpression
356
+ ? JsonAccessRoot<Value> extends PostgresJsonExpression<any>
357
+ ? WithJsonPathAccess<JsonResultExpression<
358
+ JsonSetOutputWithCreateMissing<Expression.RuntimeOf<JsonAccessRoot<Value>>, JsonAccessPath<Value>, Next, "json.set", CreateMissing>,
359
+ Expression.DbTypeOf<JsonAccessRoot<Value>>,
360
+ Expression.KindOf<JsonAccessRoot<Value>>,
361
+ Expression.DependenciesOf<JsonAccessRoot<Value>>,
362
+ never,
363
+ DialectOf<JsonAccessRoot<Value>>
364
+ >>
365
+ : never
366
+ : WithJsonPathAccess<JsonResultExpression<
367
+ unknown,
368
+ Expression.DbTypeOf<Value>,
369
+ Expression.KindOf<Value>,
370
+ Expression.DependenciesOf<Value>,
371
+ never,
372
+ DialectOf<Value>
373
+ >>
374
+
375
+ type JsonAccessInsertResultExpression<
376
+ Value extends PostgresJsonExpression<any>,
377
+ Next,
378
+ InsertAfter extends boolean
379
+ > = Value extends JsonAccessExpression
380
+ ? JsonAccessRoot<Value> extends PostgresJsonExpression<any>
381
+ ? WithJsonPathAccess<JsonResultExpression<
382
+ JsonInsertOutputOf<Expression.RuntimeOf<JsonAccessRoot<Value>>, JsonAccessPath<Value>, Next, InsertAfter, "json.insert">,
383
+ Expression.DbTypeOf<JsonAccessRoot<Value>>,
384
+ Expression.KindOf<JsonAccessRoot<Value>>,
385
+ Expression.DependenciesOf<JsonAccessRoot<Value>>,
386
+ never,
387
+ DialectOf<JsonAccessRoot<Value>>
388
+ >>
389
+ : never
390
+ : WithJsonPathAccess<JsonResultExpression<
391
+ unknown,
392
+ Expression.DbTypeOf<Value>,
393
+ Expression.KindOf<Value>,
394
+ Expression.DependenciesOf<Value>,
395
+ never,
396
+ DialectOf<Value>
397
+ >>
398
+
399
+ type JsonTextTerminalExpression<
400
+ Base extends AnyJsonExpression<any>
401
+ > = Expression.Scalar<
402
+ string | null,
403
+ PostgresTextDb,
404
+ Expression.Nullability,
405
+ DialectOf<Base>,
406
+ Expression.KindOf<Base>,
407
+ Expression.DependenciesOf<Base>
408
+ >
409
+
410
+ type JsonPredicateExpression<
411
+ Base extends PostgresJsonExpression<any>
412
+ > = Expression.Scalar<
413
+ boolean,
414
+ PostgresBoolDb,
415
+ "never",
416
+ DialectOf<Base>,
417
+ Expression.KindOf<Base>,
418
+ Expression.DependenciesOf<Base>
419
+ >
420
+
421
+ type JsonNullablePredicateExpression<
422
+ Base extends PostgresJsonExpression<any>
423
+ > = Expression.Scalar<
424
+ boolean | null,
425
+ PostgresBoolDb,
426
+ "maybe",
427
+ DialectOf<Base>,
428
+ Expression.KindOf<Base>,
429
+ Expression.DependenciesOf<Base>
430
+ >
431
+
432
+ type JsonbDeleteCallResult<
433
+ First,
434
+ Second,
435
+ Operation extends "json.delete" | "json.remove"
436
+ > =
437
+ First extends JsonPath.CanonicalSegment | JsonPath.Path<any>
438
+ ? <Base extends PostgresJsonbExpression<any>>(
439
+ base: Base
440
+ ) => WithJsonPathAccess<JsonResultExpression<
441
+ JsonDeleteOutputOf<Expression.RuntimeOf<Base>, First, Operation>,
442
+ Expression.DbTypeOf<Base>,
443
+ Expression.KindOf<Base>,
444
+ Expression.DependenciesOf<Base>,
445
+ never,
446
+ DialectOf<Base>
447
+ >>
448
+ : First extends PostgresJsonbExpression<any>
449
+ ? [Second] extends [undefined]
450
+ ? JsonAccessDeleteResultExpression<First, Operation>
451
+ : Second extends JsonPath.CanonicalSegment | JsonPath.Path<any>
452
+ ? WithJsonPathAccess<JsonResultExpression<
453
+ JsonDeleteOutputOf<Expression.RuntimeOf<First>, Second, Operation>,
454
+ Expression.DbTypeOf<First>,
455
+ Expression.KindOf<First>,
456
+ Expression.DependenciesOf<First>,
457
+ never,
458
+ DialectOf<First>
459
+ >>
460
+ : never
461
+ : never
462
+
463
+ type JsonAccessDeleteGuard<
464
+ Value,
465
+ Operation extends "json.delete" | "json.remove"
466
+ > = Value extends PostgresJsonExpression<any>
467
+ ? Value extends JsonAccessExpression
468
+ ? JsonAccessRoot<Value> extends PostgresJsonExpression<any>
469
+ ? JsonDeletePathGuard<Expression.RuntimeOf<JsonAccessRoot<Value>>, JsonAccessPath<Value>, Operation>
470
+ : unknown
471
+ : unknown
472
+ : unknown
473
+
474
+ type JsonbDeleteFirstGuard<
475
+ First,
476
+ Operation extends "json.delete" | "json.remove"
477
+ > = First extends JsonPath.CanonicalSegment | JsonPath.Path<any>
478
+ ? ExactJsonPathGuard<First>
479
+ : JsonAccessDeleteGuard<First, Operation>
480
+
481
+ type JsonbDeleteSecondGuard<
482
+ First,
483
+ Second,
484
+ Operation extends "json.delete" | "json.remove"
485
+ > = First extends PostgresJsonExpression<any>
486
+ ? Second extends JsonPath.CanonicalSegment | JsonPath.Path<any>
487
+ ? JsonDeletePathGuard<Expression.RuntimeOf<First>, Second, Operation>
488
+ : unknown
489
+ : unknown
490
+
491
+ type JsonTextCallResult<
492
+ First,
493
+ Second,
494
+ AllowedTarget extends JsonPath.CanonicalSegment | JsonPath.Path<any>
495
+ > =
496
+ First extends AllowedTarget
497
+ ? <Base extends PostgresJsonExpression<any>>(base: Base) => JsonTextResultExpression<Base, First>
498
+ : First extends PostgresJsonExpression<any>
499
+ ? [Second] extends [undefined]
500
+ ? JsonAccessTextResultExpression<First>
501
+ : Second extends AllowedTarget
502
+ ? JsonTextResultExpression<First, Second>
503
+ : never
504
+ : never
505
+
506
+ interface JsonbBaseSegmentOperation<Segment extends JsonPath.CanonicalSegment> {
507
+ <Base extends PostgresJsonbExpression<any>>(
508
+ base: Base
509
+ ): JsonGetResultExpression<Base, Segment, "json.get">
510
+ }
511
+
512
+ interface JsonbKeySegmentOperation<Key extends string>
513
+ extends JsonbBaseSegmentOperation<JsonPath.KeySegment<Key>>, JsonPath.KeySegment<Key> {}
514
+
515
+ interface JsonbIndexSegmentOperation<Index extends number>
516
+ extends JsonbBaseSegmentOperation<JsonPath.IndexSegment<Index>>, JsonPath.IndexSegment<Index> {}
517
+
518
+ interface JsonbWildcardSegmentOperation
519
+ extends JsonbBaseSegmentOperation<JsonPath.WildcardSegment>, JsonPath.WildcardSegment {}
520
+
521
+ interface JsonbSliceSegmentOperation<
522
+ Start extends number | undefined,
523
+ End extends number | undefined
524
+ > extends JsonbBaseSegmentOperation<JsonPath.SliceSegment<Start, End>>, JsonPath.SliceSegment<Start, End> {}
525
+
526
+ interface JsonbDescendSegmentOperation
527
+ extends JsonbBaseSegmentOperation<JsonPath.DescendSegment>, JsonPath.DescendSegment {}
528
+
529
+ type JsonbSegmentOperation<Segment extends JsonPath.CanonicalSegment> =
530
+ Segment extends JsonPath.KeySegment<infer Key extends string> ? JsonbKeySegmentOperation<Key> :
531
+ Segment extends JsonPath.IndexSegment<infer Index extends number> ? JsonbIndexSegmentOperation<Index> :
532
+ Segment extends JsonPath.WildcardSegment ? JsonbWildcardSegmentOperation :
533
+ Segment extends JsonPath.SliceSegment<infer Start extends number | undefined, infer End extends number | undefined> ? JsonbSliceSegmentOperation<Start, End> :
534
+ Segment extends JsonPath.DescendSegment ? JsonbDescendSegmentOperation :
535
+ never
536
+
537
+ const emptyPath = JsonPath.path()
538
+
539
+ const isObjectLike = (value: unknown): value is object =>
540
+ (typeof value === "object" || typeof value === "function") && value !== null
541
+
542
+ const isExpression = (value: unknown): value is Expression.Any =>
543
+ isObjectLike(value) && Expression.TypeId in value
544
+
545
+ const isPath = (value: unknown): value is JsonPath.Path<any> =>
546
+ isObjectLike(value) && JsonPath.TypeId in value
547
+
548
+ const isSegment = (value: unknown): value is JsonPath.CanonicalSegment =>
549
+ isObjectLike(value) && JsonPath.SegmentTypeId in value
550
+
551
+ const isTarget = (value: unknown): value is JsonPath.Path<any> | JsonPath.CanonicalSegment =>
552
+ isPath(value) || isSegment(value)
553
+
554
+ const normalizeSegment = <Segment extends JsonPath.CanonicalSegment>(
555
+ segment: Segment
556
+ ): Segment => {
557
+ switch (segment.kind) {
558
+ case "key":
559
+ return JsonPath.key(segment.key) as Segment
560
+ case "index":
561
+ return JsonPath.index(segment.index) as Segment
562
+ case "wildcard":
563
+ return JsonPath.wildcard() as Segment
564
+ case "slice":
565
+ return JsonPath.slice(segment.start, segment.end) as Segment
566
+ case "descend":
567
+ return JsonPath.descend() as Segment
568
+ }
569
+ }
570
+
571
+ const normalizeTarget = <Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment>(
572
+ target: Target
573
+ ): Target =>
574
+ isPath(target)
575
+ ? JsonPath.path(...target.segments.map(normalizeSegment)) as Target
576
+ : normalizeSegment(target as JsonPath.CanonicalSegment) as unknown as Target
577
+
578
+ const accessKinds = new Set<string>([
579
+ "jsonGet",
580
+ "jsonPath",
581
+ "jsonAccess",
582
+ "jsonTraverse",
583
+ "jsonGetText",
584
+ "jsonPathText",
585
+ "jsonAccessText",
586
+ "jsonTraverseText"
587
+ ])
588
+
589
+ const accessPathOf = (value: Expression.Any): {
590
+ readonly base: Expression.Any
591
+ readonly path: JsonPath.Path<readonly JsonPath.CanonicalSegment[]>
592
+ } | undefined => {
593
+ const segments: JsonPath.CanonicalSegment[] = []
594
+ let base: Expression.Any = value
595
+ while (isExpression(base)) {
596
+ const ast = (base as Expression.Any & {
597
+ readonly [ExpressionAst.TypeId]: ExpressionAst.Any
598
+ })[ExpressionAst.TypeId] as { readonly kind?: unknown; readonly base?: unknown; readonly segments?: unknown }
599
+ if (typeof ast.kind !== "string" || !accessKinds.has(ast.kind) || !isExpression(ast.base) || !Array.isArray(ast.segments)) {
600
+ break
601
+ }
602
+ segments.unshift(...ast.segments.map(normalizeSegment))
603
+ base = ast.base
604
+ }
605
+ return segments.length === 0
606
+ ? undefined
607
+ : {
608
+ base,
609
+ path: JsonPath.path(...segments)
610
+ }
611
+ }
247
612
 
248
613
  const jsonGetDirect = <
249
614
  Base extends PostgresJsonExpression<any>,
@@ -252,7 +617,7 @@ const jsonGetDirect = <
252
617
  base: Base,
253
618
  target: Target & ExactJsonPathGuard<Target>
254
619
  ): JsonGetResultExpression<Base, Target, "json.get"> =>
255
- postgresJson.get(base as never, target as never) as unknown as JsonGetResultExpression<Base, Target, "json.get">
620
+ postgresJson.get(base as never, normalizeTarget(target) as never) as unknown as JsonGetResultExpression<Base, Target, "json.get">
256
621
 
257
622
  const jsonTextDirect = <
258
623
  Base extends PostgresJsonExpression<any>,
@@ -261,20 +626,53 @@ const jsonTextDirect = <
261
626
  base: Base,
262
627
  target: Target & ExactJsonPathGuard<Target>
263
628
  ): JsonTextResultExpression<Base, Target> =>
264
- postgresJson.text(base as never, target as never) as unknown as JsonTextResultExpression<Base, Target>
629
+ postgresJson.text(base as never, normalizeTarget(target) as never) as unknown as JsonTextResultExpression<Base, Target>
630
+
631
+ const jsonbGetDirect = <
632
+ Base extends PostgresJsonbExpression<any>,
633
+ Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
634
+ >(
635
+ base: Base,
636
+ target: Target & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.get">
637
+ ): JsonGetResultExpression<Base, Target, "json.get"> =>
638
+ postgresJsonb.get(base as Base, normalizeTarget(target)) as unknown as JsonGetResultExpression<Base, Target, "json.get">
639
+
640
+ const jsonbTextDirect = <
641
+ Base extends PostgresJsonbExpression<any>,
642
+ Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
643
+ >(
644
+ base: Base,
645
+ target: Target & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.text">
646
+ ): JsonTextResultExpression<Base, Target> =>
647
+ postgresJsonb.text(base as Base, normalizeTarget(target)) as unknown as JsonTextResultExpression<Base, Target>
648
+
649
+ const jsonbSegmentOperation = <Segment extends JsonPath.CanonicalSegment>(
650
+ segment: Segment
651
+ ): JsonbSegmentOperation<Segment> =>
652
+ Object.assign(
653
+ ((base: PostgresJsonbExpression<any>) => jsonbGetDirect(base as never, segment as never)) as unknown as JsonbSegmentOperation<Segment>,
654
+ segment
655
+ )
265
656
 
266
657
  const json = {
267
- key: postgresJson.key,
268
- index: postgresJson.index,
269
- path: exactPath,
270
- get: ((...args: [PostgresJsonExpression<any>, ExactJsonPathInput] | [ExactJsonPathInput]) =>
271
- args.length === 1
272
- ? ((base: PostgresJsonExpression<any>) => jsonGetDirect(base as never, args[0] as never))
273
- : jsonGetDirect(args[0] as never, args[1] as never)) as unknown as
274
- typeof jsonGetDirect & {
658
+ get: ((...args: readonly [unknown] | readonly [unknown, unknown]) => {
659
+ if (args.length === 1) {
660
+ const [first] = args
661
+ if (isTarget(first)) {
662
+ return (base: PostgresJsonExpression<any>) => jsonGetDirect(base as never, first as never)
663
+ }
664
+ return first
665
+ }
666
+ return jsonGetDirect(args[0] as never, args[1] as never)
667
+ }) as unknown as {
668
+ <Base extends PostgresJsonExpression<any>, Target extends ExactJsonPathInput>(
669
+ base: Base,
670
+ target: Target & ExactJsonPathGuard<Target>
671
+ ): JsonGetResultExpression<Base, Target, "json.get">
275
672
  <Target extends ExactJsonPathInput>(
276
673
  target: Target & ExactJsonPathGuard<Target>
277
674
  ): <Base extends PostgresJsonExpression<any>>(base: Base) => JsonGetResultExpression<Base, Target, "json.get">
675
+ <Base>(base: Base & PostgresJsonExpression<any>): Base & PostgresJsonExpression<any>
278
676
  },
279
677
  access: <
280
678
  Base extends PostgresJsonExpression<any>,
@@ -282,37 +680,41 @@ const json = {
282
680
  >(
283
681
  base: Base,
284
682
  target: Target & ExactJsonPathGuard<Target> & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.access">
285
- ) => postgresJson.access(base, target),
683
+ ) => postgresJson.access(base, normalizeTarget(target)),
286
684
  traverse: <
287
685
  Base extends PostgresJsonExpression<any>,
288
686
  Target extends ExactJsonPathInput
289
687
  >(
290
688
  base: Base,
291
689
  target: Target & ExactJsonPathGuard<Target> & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.traverse">
292
- ) => postgresJson.traverse(base, target),
293
- text: ((...args: [PostgresJsonExpression<any>, ExactJsonPathInput] | [ExactJsonPathInput]) =>
294
- args.length === 1
295
- ? ((base: PostgresJsonExpression<any>) => jsonTextDirect(base as never, args[0] as never))
296
- : jsonTextDirect(args[0] as never, args[1] as never)) as unknown as
297
- typeof jsonTextDirect & {
298
- <Target extends ExactJsonPathInput>(
299
- target: Target & ExactJsonPathGuard<Target>
300
- ): <Base extends PostgresJsonExpression<any>>(base: Base) => JsonTextResultExpression<Base, Target>
301
- },
690
+ ) => postgresJson.traverse(base, normalizeTarget(target)),
691
+ text: ((...args: readonly [unknown] | readonly [unknown, unknown]) => {
692
+ if (args.length === 1) {
693
+ const [first] = args
694
+ if (isTarget(first)) {
695
+ return (base: PostgresJsonExpression<any>) => jsonTextDirect(base as never, first as never)
696
+ }
697
+ const access = isExpression(first) ? accessPathOf(first) : undefined
698
+ return access === undefined
699
+ ? postgresJson.text(first as never, emptyPath as never)
700
+ : jsonTextDirect(access.base as never, access.path as never)
701
+ }
702
+ return jsonTextDirect(args[0] as never, args[1] as never)
703
+ }) as unknown as <Base extends PostgresJsonExpression<any>>(base: Base) => JsonAccessTextResultExpression<Base>,
302
704
  accessText: <
303
705
  Base extends PostgresJsonExpression<any>,
304
706
  Target extends ExactJsonPathInput
305
707
  >(
306
708
  base: Base,
307
709
  target: Target & ExactJsonPathGuard<Target> & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.accessText">
308
- ) => postgresJson.accessText(base, target),
710
+ ) => postgresJson.accessText(base, normalizeTarget(target)),
309
711
  traverseText: <
310
712
  Base extends PostgresJsonExpression<any>,
311
713
  Target extends ExactJsonPathInput
312
714
  >(
313
715
  base: Base,
314
716
  target: Target & ExactJsonPathGuard<Target> & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.traverseText">
315
- ) => postgresJson.traverseText(base, target),
717
+ ) => postgresJson.traverseText(base, normalizeTarget(target)),
316
718
  buildObject: postgresJson.buildObject,
317
719
  buildArray: postgresJson.buildArray,
318
720
  toJson: postgresJson.toJson,
@@ -320,6 +722,13 @@ const json = {
320
722
  length: postgresJson.length,
321
723
  keys: postgresJson.keys,
322
724
  stripNulls: postgresJson.stripNulls,
725
+ pathMatch: <
726
+ Base extends PostgresJsonExpression<any>,
727
+ Query extends JsonPathPredicateQuery
728
+ >(
729
+ base: Base,
730
+ query: JsonPathPredicateQueryInput<Query>
731
+ ) => postgresJson.pathMatch(base, query) as unknown as JsonNullablePredicateExpression<Base>,
323
732
  delete: <
324
733
  Base extends PostgresJsonExpression<any>,
325
734
  Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
@@ -329,7 +738,7 @@ const json = {
329
738
  ): JsonResultExpression<
330
739
  JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.delete">,
331
740
  Expression.DbTypeOf<Base>
332
- > => postgresJson.delete(base as any, target as any) as unknown as JsonResultExpression<
741
+ > => postgresJson.delete(base as any, normalizeTarget(target) as any) as unknown as JsonResultExpression<
333
742
  JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.delete">,
334
743
  Expression.DbTypeOf<Base>
335
744
  >,
@@ -342,61 +751,96 @@ const json = {
342
751
  ): JsonResultExpression<
343
752
  JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.remove">,
344
753
  Expression.DbTypeOf<Base>
345
- > => postgresJson.remove(base as any, target as any) as unknown as JsonResultExpression<
754
+ > => postgresJson.remove(base as any, normalizeTarget(target) as any) as unknown as JsonResultExpression<
346
755
  JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.remove">,
347
756
  Expression.DbTypeOf<Base>
348
757
  >
349
758
  }
350
759
 
351
760
  const jsonb = {
352
- key: postgresJsonb.key,
353
- index: postgresJsonb.index,
354
- wildcard: postgresJsonb.wildcard,
355
- slice: postgresJsonb.slice,
356
- descend: postgresJsonb.descend,
357
- path: postgresJsonb.path,
358
- get: <
359
- Base extends PostgresJsonExpression<any>,
360
- Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
761
+ key: <const Key extends string>(value: Key): JsonbSegmentOperation<JsonPath.KeySegment<Key>> =>
762
+ jsonbSegmentOperation(JsonPath.key(value)),
763
+ index: <const Index extends number>(value: Index): JsonbSegmentOperation<JsonPath.IndexSegment<Index>> =>
764
+ jsonbSegmentOperation(JsonPath.index(value)),
765
+ wildcard: (): JsonbSegmentOperation<JsonPath.WildcardSegment> =>
766
+ jsonbSegmentOperation(JsonPath.wildcard()),
767
+ slice: <
768
+ const Start extends number | undefined = undefined,
769
+ const End extends number | undefined = undefined
361
770
  >(
362
- base: Base & JsonbBaseGuard<Base, "jsonb.get">,
363
- target: Target & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.get">
364
- ) => postgresJsonb.get(base as Base, target),
771
+ start?: Start,
772
+ end?: End
773
+ ): JsonbSegmentOperation<JsonPath.SliceSegment<Start, End>> =>
774
+ jsonbSegmentOperation(JsonPath.slice(start, end)),
775
+ descend: (): JsonbSegmentOperation<JsonPath.DescendSegment> =>
776
+ jsonbSegmentOperation(JsonPath.descend()),
777
+ get: ((...args: readonly [unknown] | readonly [unknown, unknown]) => {
778
+ if (args.length === 1) {
779
+ const [first] = args
780
+ if (isTarget(first)) {
781
+ return (base: PostgresJsonbExpression<any>) => jsonbGetDirect(base as never, first as never)
782
+ }
783
+ const access = isExpression(first) ? accessPathOf(first) : undefined
784
+ return access === undefined
785
+ ? first
786
+ : jsonbGetDirect(access.base as never, access.path as never)
787
+ }
788
+ return jsonbGetDirect(args[0] as never, args[1] as never)
789
+ }) as unknown as {
790
+ <Base>(
791
+ base: Base & PostgresJsonbExpression<any>
792
+ ): Base & PostgresJsonbExpression<any>
793
+ <Base extends PostgresJsonbExpression<any>, Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>>(
794
+ base: Base,
795
+ target: Target & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.get">
796
+ ): JsonGetResultExpression<Base, Target, "json.get">
797
+ <Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>>(
798
+ target: Target & JsonValuePathGuard<any, Target, "json.get">
799
+ ): <Base extends PostgresJsonbExpression<any>>(
800
+ base: Base
801
+ ) => JsonGetResultExpression<Base, Target, "json.get">
802
+ },
365
803
  access: <
366
804
  Base extends PostgresJsonExpression<any>,
367
805
  Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
368
806
  >(
369
807
  base: Base & JsonbBaseGuard<Base, "jsonb.access">,
370
808
  target: Target & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.access">
371
- ) => postgresJsonb.access(base as Base, target),
809
+ ) => postgresJsonb.access(base as Base, normalizeTarget(target)),
372
810
  traverse: <
373
811
  Base extends PostgresJsonExpression<any>,
374
812
  Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
375
813
  >(
376
814
  base: Base & JsonbBaseGuard<Base, "jsonb.traverse">,
377
815
  target: Target & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.traverse">
378
- ) => postgresJsonb.traverse(base as Base, target),
379
- text: <
380
- Base extends PostgresJsonExpression<any>,
381
- Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
382
- >(
383
- base: Base & JsonbBaseGuard<Base, "jsonb.text">,
384
- target: Target & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.text">
385
- ) => postgresJsonb.text(base as Base, target),
816
+ ) => postgresJsonb.traverse(base as Base, normalizeTarget(target)),
817
+ text: ((...args: readonly [unknown] | readonly [unknown, unknown]) => {
818
+ if (args.length === 1) {
819
+ const [first] = args
820
+ if (isTarget(first)) {
821
+ return (base: PostgresJsonbExpression<any>) => jsonbTextDirect(base as never, first as never)
822
+ }
823
+ const access = isExpression(first) ? accessPathOf(first) : undefined
824
+ return access === undefined
825
+ ? postgresJsonb.text(first as never, emptyPath as never)
826
+ : jsonbTextDirect(access.base as never, access.path as never)
827
+ }
828
+ return jsonbTextDirect(args[0] as never, args[1] as never)
829
+ }) as unknown as <Base extends PostgresJsonbExpression<any>>(base: Base) => JsonAccessTextResultExpression<Base>,
386
830
  accessText: <
387
831
  Base extends PostgresJsonExpression<any>,
388
832
  Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
389
833
  >(
390
834
  base: Base & JsonbBaseGuard<Base, "jsonb.accessText">,
391
835
  target: Target & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.accessText">
392
- ) => postgresJsonb.accessText(base as Base, target),
836
+ ) => postgresJsonb.accessText(base as Base, normalizeTarget(target)),
393
837
  traverseText: <
394
838
  Base extends PostgresJsonExpression<any>,
395
839
  Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
396
840
  >(
397
841
  base: Base & JsonbBaseGuard<Base, "jsonb.traverseText">,
398
842
  target: Target & JsonValuePathGuard<Expression.RuntimeOf<Base>, Target, "json.traverseText">
399
- ) => postgresJsonb.traverseText(base as Base, target),
843
+ ) => postgresJsonb.traverseText(base as Base, normalizeTarget(target)),
400
844
  contains: <
401
845
  Left extends PostgresJsonExpression<any>,
402
846
  Right extends Parameters<typeof postgresJsonb.contains>[1]
@@ -411,98 +855,192 @@ const jsonb = {
411
855
  left: Left & JsonbBaseGuard<Left, "jsonb.containedBy">,
412
856
  right: Right
413
857
  ) => postgresJsonb.containedBy(left as Left, right),
414
- hasKey: <
415
- Base extends PostgresJsonExpression<any>,
416
- Key extends string
417
- >(
418
- base: Base & JsonbBaseGuard<Base, "jsonb.hasKey">,
419
- key: Key
420
- ) => postgresJsonb.hasKey(base as Base, key),
421
- keyExists: <
422
- Base extends PostgresJsonExpression<any>,
423
- Key extends string
424
- >(
425
- base: Base & JsonbBaseGuard<Base, "jsonb.keyExists">,
426
- key: Key
427
- ) => postgresJsonb.keyExists(base as Base, key),
428
- hasAnyKeys: <
429
- Base extends PostgresJsonExpression<any>,
430
- Keys extends readonly [string, ...string[]]
431
- >(
432
- base: Base & JsonbBaseGuard<Base, "jsonb.hasAnyKeys">,
433
- ...keys: Keys
434
- ) => postgresJsonb.hasAnyKeys(base as Base, ...keys),
435
- hasAllKeys: <
436
- Base extends PostgresJsonExpression<any>,
437
- Keys extends readonly [string, ...string[]]
438
- >(
439
- base: Base & JsonbBaseGuard<Base, "jsonb.hasAllKeys">,
440
- ...keys: Keys
441
- ) => postgresJsonb.hasAllKeys(base as Base, ...keys),
442
- delete: <
443
- Base extends PostgresJsonExpression<any>,
444
- Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
445
- >(
446
- base: Base & JsonbBaseGuard<Base, "jsonb.delete">,
447
- target: Target & JsonDeletePathGuard<Expression.RuntimeOf<Base>, Target, "json.delete">
448
- ): JsonResultExpression<
449
- JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.delete">,
450
- Expression.DbTypeOf<Base>
451
- > => postgresJsonb.delete(base as any, target as any) as unknown as JsonResultExpression<
452
- JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.delete">,
453
- Expression.DbTypeOf<Base>
454
- >,
455
- remove: <
456
- Base extends PostgresJsonExpression<any>,
457
- Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>
458
- >(
459
- base: Base & JsonbBaseGuard<Base, "jsonb.remove">,
460
- target: Target & JsonDeletePathGuard<Expression.RuntimeOf<Base>, Target, "json.remove">
461
- ): JsonResultExpression<
462
- JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.remove">,
463
- Expression.DbTypeOf<Base>
464
- > => postgresJsonb.remove(base as any, target as any) as unknown as JsonResultExpression<
465
- JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.remove">,
466
- Expression.DbTypeOf<Base>
467
- >,
468
- set: <
469
- Base extends PostgresJsonExpression<any>,
470
- Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>,
471
- Next extends Parameters<typeof postgresJsonb.set>[2],
472
- CreateMissing extends boolean = true
473
- >(
474
- base: Base & JsonbBaseGuard<Base, "jsonb.set">,
475
- target: Target & JsonSetPathGuard<Expression.RuntimeOf<Base>, Target, NoInfer<Next>, "json.set">,
476
- next: Next,
477
- options?: {
478
- readonly createMissing?: CreateMissing
858
+ hasKey: ((...args: readonly [unknown] | readonly [unknown, unknown]) => {
859
+ if (args.length === 1) {
860
+ const [key] = args
861
+ return (base: PostgresJsonExpression<any>) => postgresJsonb.hasKey(base as never, key as never)
479
862
  }
480
- ): JsonResultExpression<
481
- JsonSetOutputWithCreateMissing<Expression.RuntimeOf<Base>, Target, Next, "json.set", CreateMissing>,
482
- Expression.DbTypeOf<Base>
483
- > => postgresJsonb.set(base as any, target as any, next, options) as unknown as JsonResultExpression<
484
- JsonSetOutputWithCreateMissing<Expression.RuntimeOf<Base>, Target, Next, "json.set", CreateMissing>,
485
- Expression.DbTypeOf<Base>
486
- >,
487
- insert: <
488
- Base extends PostgresJsonExpression<any>,
489
- Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>,
490
- Next extends Parameters<typeof postgresJsonb.insert>[2],
491
- InsertAfter extends boolean = false
492
- >(
493
- base: Base & JsonbBaseGuard<Base, "jsonb.insert">,
494
- target: Target & JsonInsertPathGuard<Expression.RuntimeOf<Base>, Target, NoInfer<Next>, NoInfer<InsertAfter>, "json.insert">,
495
- next: Next,
496
- options?: {
497
- readonly insertAfter?: InsertAfter
863
+ const [base, key] = args
864
+ return postgresJsonb.hasKey(base as never, key as never)
865
+ }) as {
866
+ <Base extends PostgresJsonExpression<any>, Key extends JsonObjectKeyOf<Base>>(
867
+ base: Base & JsonbBaseGuard<Base, "jsonb.hasKey">,
868
+ key: Key
869
+ ): JsonPredicateExpression<Base>
870
+ <Key extends string>(
871
+ key: Key
872
+ ): <Base extends PostgresJsonExpression<any>>(
873
+ base: Base & JsonbBaseGuard<Base, "jsonb.hasKey"> & (Key extends JsonObjectKeyOf<Base> ? unknown : never)
874
+ ) => JsonPredicateExpression<Base>
875
+ },
876
+ keyExists: ((...args: readonly [unknown] | readonly [unknown, unknown]) => {
877
+ if (args.length === 1) {
878
+ const [key] = args
879
+ return (base: PostgresJsonExpression<any>) => postgresJsonb.keyExists(base as never, key as never)
498
880
  }
499
- ): JsonResultExpression<
500
- JsonInsertOutputOf<Expression.RuntimeOf<Base>, Target, Next, InsertAfter, "json.insert">,
501
- Expression.DbTypeOf<Base>
502
- > => postgresJsonb.insert(base as any, target as any, next, options) as unknown as JsonResultExpression<
503
- JsonInsertOutputOf<Expression.RuntimeOf<Base>, Target, Next, InsertAfter, "json.insert">,
504
- Expression.DbTypeOf<Base>
505
- >,
881
+ const [base, key] = args
882
+ return postgresJsonb.keyExists(base as never, key as never)
883
+ }) as {
884
+ <Base extends PostgresJsonExpression<any>, Key extends JsonObjectKeyOf<Base>>(
885
+ base: Base & JsonbBaseGuard<Base, "jsonb.keyExists">,
886
+ key: Key
887
+ ): JsonPredicateExpression<Base>
888
+ <Key extends string>(
889
+ key: Key
890
+ ): <Base extends PostgresJsonExpression<any>>(
891
+ base: Base & JsonbBaseGuard<Base, "jsonb.keyExists"> & (Key extends JsonObjectKeyOf<Base> ? unknown : never)
892
+ ) => JsonPredicateExpression<Base>
893
+ },
894
+ hasAnyKeys: ((base: PostgresJsonExpression<any>, ...keys: readonly string[]) =>
895
+ postgresJsonb.hasAnyKeys(base as never, ...(keys as [string, ...string[]]))) as {
896
+ <Base extends PostgresJsonExpression<any>, Keys extends readonly [JsonObjectKeyOf<Base>, ...JsonObjectKeyOf<Base>[]]>(
897
+ base: Base & JsonbBaseGuard<Base, "jsonb.hasAnyKeys">,
898
+ ...keys: Keys
899
+ ): JsonPredicateExpression<Base>
900
+ },
901
+ hasAllKeys: ((base: PostgresJsonExpression<any>, ...keys: readonly string[]) =>
902
+ postgresJsonb.hasAllKeys(base as never, ...(keys as [string, ...string[]]))) as {
903
+ <Base extends PostgresJsonExpression<any>, Keys extends readonly [JsonObjectKeyOf<Base>, ...JsonObjectKeyOf<Base>[]]>(
904
+ base: Base & JsonbBaseGuard<Base, "jsonb.hasAllKeys">,
905
+ ...keys: Keys
906
+ ): JsonPredicateExpression<Base>
907
+ },
908
+ delete: ((...args: readonly [unknown] | readonly [unknown, unknown]) => {
909
+ if (args.length === 1) {
910
+ const [first] = args
911
+ if (isTarget(first)) {
912
+ return (base: PostgresJsonbExpression<any>) => postgresJsonb.delete(base as any, normalizeTarget(first) as any)
913
+ }
914
+ const access = isExpression(first) ? accessPathOf(first) : undefined
915
+ if (access === undefined) {
916
+ throw new Error("Jsonb.delete requires a piped JSON path or an explicit target")
917
+ }
918
+ return postgresJsonb.delete(access.base as any, access.path as any)
919
+ }
920
+ return postgresJsonb.delete(args[0] as any, normalizeTarget(args[1] as JsonPath.CanonicalSegment | JsonPath.Path<any>) as any)
921
+ }) as unknown as <First extends PostgresJsonbExpression<any> | JsonPath.CanonicalSegment | JsonPath.Path<any>, Second extends JsonPath.CanonicalSegment | JsonPath.Path<any> | undefined = undefined>(
922
+ first: First & JsonbDeleteFirstGuard<First, "json.delete">,
923
+ second?: Second & JsonbDeleteSecondGuard<First, Second, "json.delete">
924
+ ) => JsonbDeleteCallResult<First, Second, "json.delete">,
925
+ remove: ((...args: readonly [unknown] | readonly [unknown, unknown]) => {
926
+ if (args.length === 1) {
927
+ const [first] = args
928
+ if (isTarget(first)) {
929
+ return (base: PostgresJsonbExpression<any>) => postgresJsonb.remove(base as any, normalizeTarget(first) as any)
930
+ }
931
+ const access = isExpression(first) ? accessPathOf(first) : undefined
932
+ if (access === undefined) {
933
+ throw new Error("Jsonb.remove requires a piped JSON path or an explicit target")
934
+ }
935
+ return postgresJsonb.remove(access.base as any, access.path as any)
936
+ }
937
+ return postgresJsonb.remove(args[0] as any, normalizeTarget(args[1] as JsonPath.CanonicalSegment | JsonPath.Path<any>) as any)
938
+ }) as unknown as <First extends PostgresJsonbExpression<any> | JsonPath.CanonicalSegment | JsonPath.Path<any>, Second extends JsonPath.CanonicalSegment | JsonPath.Path<any> | undefined = undefined>(
939
+ first: First & JsonbDeleteFirstGuard<First, "json.remove">,
940
+ second?: Second & JsonbDeleteSecondGuard<First, Second, "json.remove">
941
+ ) => JsonbDeleteCallResult<First, Second, "json.remove">,
942
+ set: ((...args: readonly unknown[]) => {
943
+ if (args.length === 1 || (args.length === 2 && !isExpression(args[0]))) {
944
+ const [next, options] = args
945
+ return (base: PostgresJsonbExpression<any>) => {
946
+ const access = accessPathOf(base)
947
+ if (access === undefined) {
948
+ throw new Error("Jsonb.set requires a piped JSON path or an explicit target")
949
+ }
950
+ return postgresJsonb.set(access.base as any, access.path as any, next as any, options as any)
951
+ }
952
+ }
953
+ if ((args.length === 2 || args.length === 3) && isExpression(args[0]) && !isTarget(args[1])) {
954
+ const [base, next, options] = args
955
+ const access = accessPathOf(base)
956
+ if (access === undefined) {
957
+ throw new Error("Jsonb.set requires a piped JSON path or an explicit target")
958
+ }
959
+ return postgresJsonb.set(access.base as any, access.path as any, next as any, options as any)
960
+ }
961
+ const [base, target, next, options] = args
962
+ return postgresJsonb.set(base as any, normalizeTarget(target as JsonPath.CanonicalSegment | JsonPath.Path<any>) as any, next as any, options as any)
963
+ }) as unknown as {
964
+ <Next extends Parameters<typeof postgresJsonb.set>[2], CreateMissing extends boolean = true>(
965
+ next: Next,
966
+ options?: {
967
+ readonly createMissing?: CreateMissing
968
+ }
969
+ ): <Base extends PostgresJsonbExpression<any>>(base: Base) => JsonAccessSetResultExpression<Base, Next, CreateMissing>
970
+ <Base extends PostgresJsonbExpression<any>, Next extends Parameters<typeof postgresJsonb.set>[2], CreateMissing extends boolean = true>(
971
+ base: Base,
972
+ next: Next,
973
+ options?: {
974
+ readonly createMissing?: CreateMissing
975
+ }
976
+ ): JsonAccessSetResultExpression<Base, Next, CreateMissing>
977
+ <Base extends PostgresJsonbExpression<any>, Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>, Next extends Parameters<typeof postgresJsonb.set>[2], CreateMissing extends boolean = true>(
978
+ base: Base,
979
+ target: Target & JsonSetPathGuard<Expression.RuntimeOf<Base>, Target, NoInfer<Next>, "json.set">,
980
+ next: Next,
981
+ options?: {
982
+ readonly createMissing?: CreateMissing
983
+ }
984
+ ): JsonResultExpression<
985
+ JsonSetOutputWithCreateMissing<Expression.RuntimeOf<Base>, Target, Next, "json.set", CreateMissing>,
986
+ Expression.DbTypeOf<Base>,
987
+ Expression.KindOf<Base>,
988
+ Expression.DependenciesOf<Base>,
989
+ never,
990
+ DialectOf<Base>
991
+ >
992
+ },
993
+ insert: ((...args: readonly unknown[]) => {
994
+ if (args.length === 1 || (args.length === 2 && !isExpression(args[0]))) {
995
+ const [next, options] = args
996
+ return (base: PostgresJsonbExpression<any>) => {
997
+ const access = accessPathOf(base)
998
+ if (access === undefined) {
999
+ throw new Error("Jsonb.insert requires a piped JSON path or an explicit target")
1000
+ }
1001
+ return postgresJsonb.insert(access.base as any, access.path as any, next as any, options as any)
1002
+ }
1003
+ }
1004
+ if ((args.length === 2 || args.length === 3) && isExpression(args[0]) && !isTarget(args[1])) {
1005
+ const [base, next, options] = args
1006
+ const access = accessPathOf(base)
1007
+ if (access === undefined) {
1008
+ throw new Error("Jsonb.insert requires a piped JSON path or an explicit target")
1009
+ }
1010
+ return postgresJsonb.insert(access.base as any, access.path as any, next as any, options as any)
1011
+ }
1012
+ const [base, target, next, options] = args
1013
+ return postgresJsonb.insert(base as any, normalizeTarget(target as JsonPath.CanonicalSegment | JsonPath.Path<any>) as any, next as any, options as any)
1014
+ }) as unknown as {
1015
+ <Next extends Parameters<typeof postgresJsonb.insert>[2], InsertAfter extends boolean = false>(
1016
+ next: Next,
1017
+ options?: {
1018
+ readonly insertAfter?: InsertAfter
1019
+ }
1020
+ ): <Base extends PostgresJsonbExpression<any>>(base: Base) => JsonAccessInsertResultExpression<Base, Next, InsertAfter>
1021
+ <Base extends PostgresJsonbExpression<any>, Next extends Parameters<typeof postgresJsonb.insert>[2], InsertAfter extends boolean = false>(
1022
+ base: Base,
1023
+ next: Next,
1024
+ options?: {
1025
+ readonly insertAfter?: InsertAfter
1026
+ }
1027
+ ): JsonAccessInsertResultExpression<Base, Next, InsertAfter>
1028
+ <Base extends PostgresJsonbExpression<any>, Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>, Next extends Parameters<typeof postgresJsonb.insert>[2], InsertAfter extends boolean = false>(
1029
+ base: Base,
1030
+ target: Target & JsonInsertPathGuard<Expression.RuntimeOf<Base>, Target, NoInfer<Next>, NoInfer<InsertAfter>, "json.insert">,
1031
+ next: Next,
1032
+ options?: {
1033
+ readonly insertAfter?: InsertAfter
1034
+ }
1035
+ ): JsonResultExpression<
1036
+ JsonInsertOutputOf<Expression.RuntimeOf<Base>, Target, Next, InsertAfter, "json.insert">,
1037
+ Expression.DbTypeOf<Base>,
1038
+ Expression.KindOf<Base>,
1039
+ Expression.DependenciesOf<Base>,
1040
+ never,
1041
+ DialectOf<Base>
1042
+ >
1043
+ },
506
1044
  concat: postgresJsonb.concat,
507
1045
  merge: postgresJsonb.merge,
508
1046
  buildObject: postgresJsonb.buildObject,
@@ -528,27 +1066,41 @@ const jsonb = {
528
1066
  >(
529
1067
  base: Base & JsonbBaseGuard<Base, "jsonb.stripNulls">
530
1068
  ) => postgresJsonb.stripNulls(base as Base),
531
- pathExists: <
532
- Base extends PostgresJsonExpression<any>,
533
- Query extends JsonPathPredicateQuery
534
- >(
535
- base: Base & JsonbBaseGuard<Base, "jsonb.pathExists">,
536
- query: JsonPathPredicateQueryInput<Query>
537
- ) => postgresJsonb.pathExists(base as Base, query),
1069
+ pathExists: ((...args: readonly [unknown] | readonly [unknown, unknown]) => {
1070
+ if (args.length === 1) {
1071
+ const [first] = args
1072
+ if (isTarget(first) || typeof first === "string") {
1073
+ return (base: PostgresJsonbExpression<any>) => postgresJsonb.pathExists(base as any, isTarget(first) ? normalizeTarget(first) as any : first as any)
1074
+ }
1075
+ const access = isExpression(first) ? accessPathOf(first) : undefined
1076
+ if (access === undefined) {
1077
+ throw new Error("Jsonb.pathExists requires a piped JSON path or an explicit query")
1078
+ }
1079
+ return postgresJsonb.pathExists(access.base as any, access.path as any)
1080
+ }
1081
+ const [base, query] = args
1082
+ return postgresJsonb.pathExists(base as any, isTarget(query) ? normalizeTarget(query) as any : query as any)
1083
+ }) as unknown as {
1084
+ <Base extends PostgresJsonbExpression<any>, Query extends JsonPathPredicateQuery>(
1085
+ base: Base,
1086
+ query: JsonPathPredicateQueryInput<Query>
1087
+ ): ReturnType<typeof postgresJsonb.pathExists<Base, Query>>
1088
+ <Query extends JsonPathPredicateQuery>(
1089
+ query: JsonPathPredicateQueryInput<Query>
1090
+ ): <Base extends PostgresJsonbExpression<any>>(base: Base) => ReturnType<typeof postgresJsonb.pathExists<Base, Query>>
1091
+ <Base extends PostgresJsonbExpression<any>>(base: Base): JsonPredicateExpression<Base>
1092
+ },
538
1093
  pathMatch: <
539
1094
  Base extends PostgresJsonExpression<any>,
540
1095
  Query extends JsonPathPredicateQuery
541
1096
  >(
542
1097
  base: Base & JsonbBaseGuard<Base, "jsonb.pathMatch">,
543
1098
  query: JsonPathPredicateQueryInput<Query>
544
- ) => postgresJsonb.pathMatch(base as Base, query)
1099
+ ) => postgresJsonb.pathMatch(base as Base, query) as unknown as JsonNullablePredicateExpression<Base>
545
1100
  }
546
1101
 
547
1102
  /** Postgres shared JSON helpers for exact paths and functions that work on both json and jsonb. */
548
1103
  export { json }
549
- export const key = json.key
550
- export const index = json.index
551
- export const path = json.path
552
1104
  export const get = json.get
553
1105
  export const access = json.access
554
1106
  export const traverse = json.traverse
@@ -562,6 +1114,7 @@ export const typeOf = json.typeOf
562
1114
  export const length = json.length
563
1115
  export const keys = json.keys
564
1116
  export const stripNulls = json.stripNulls
1117
+ export const pathMatch = json.pathMatch
565
1118
  export const delete_ = json.delete
566
1119
  export { delete_ as delete }
567
1120
  export const remove = json.remove