effect-qb 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/dist/mysql.js +1661 -591
  2. package/dist/postgres/metadata.js +1930 -135
  3. package/dist/postgres.js +7808 -6718
  4. package/dist/sqlite.js +8360 -0
  5. package/package.json +6 -1
  6. package/src/internal/derived-table.ts +29 -3
  7. package/src/internal/dialect.ts +2 -0
  8. package/src/internal/dsl-mutation-runtime.ts +173 -4
  9. package/src/internal/dsl-plan-runtime.ts +165 -20
  10. package/src/internal/dsl-query-runtime.ts +60 -6
  11. package/src/internal/dsl-transaction-ddl-runtime.ts +72 -2
  12. package/src/internal/executor.ts +47 -9
  13. package/src/internal/expression-ast.ts +3 -2
  14. package/src/internal/grouping-key.ts +141 -1
  15. package/src/internal/implication-runtime.ts +2 -1
  16. package/src/internal/json/types.ts +155 -40
  17. package/src/internal/predicate/context.ts +14 -1
  18. package/src/internal/predicate/key.ts +19 -2
  19. package/src/internal/predicate/runtime.ts +27 -3
  20. package/src/internal/query.ts +252 -30
  21. package/src/internal/renderer.ts +35 -2
  22. package/src/internal/runtime/driver-value-mapping.ts +58 -0
  23. package/src/internal/runtime/normalize.ts +62 -38
  24. package/src/internal/runtime/schema.ts +5 -3
  25. package/src/internal/runtime/value.ts +153 -30
  26. package/src/internal/table-options.ts +108 -1
  27. package/src/internal/table.ts +87 -29
  28. package/src/mysql/column.ts +18 -2
  29. package/src/mysql/datatypes/index.ts +21 -0
  30. package/src/mysql/errors/catalog.ts +5 -5
  31. package/src/mysql/errors/normalize.ts +2 -2
  32. package/src/mysql/internal/dsl.ts +736 -218
  33. package/src/mysql/internal/renderer.ts +2 -1
  34. package/src/mysql/internal/sql-expression-renderer.ts +486 -130
  35. package/src/mysql/query.ts +9 -2
  36. package/src/mysql/table.ts +38 -12
  37. package/src/postgres/column.ts +4 -2
  38. package/src/postgres/errors/normalize.ts +2 -2
  39. package/src/postgres/executor.ts +48 -5
  40. package/src/postgres/function/core.ts +19 -1
  41. package/src/postgres/internal/dsl.ts +683 -240
  42. package/src/postgres/internal/renderer.ts +2 -1
  43. package/src/postgres/internal/schema-ddl.ts +2 -1
  44. package/src/postgres/internal/schema-model.ts +6 -3
  45. package/src/postgres/internal/sql-expression-renderer.ts +420 -91
  46. package/src/postgres/json.ts +57 -17
  47. package/src/postgres/query.ts +9 -2
  48. package/src/postgres/schema-management.ts +91 -4
  49. package/src/postgres/schema.ts +1 -1
  50. package/src/postgres/table.ts +189 -53
  51. package/src/sqlite/column.ts +128 -0
  52. package/src/sqlite/datatypes/index.ts +79 -0
  53. package/src/sqlite/datatypes/spec.ts +98 -0
  54. package/src/sqlite/errors/catalog.ts +103 -0
  55. package/src/sqlite/errors/fields.ts +19 -0
  56. package/src/sqlite/errors/index.ts +19 -0
  57. package/src/sqlite/errors/normalize.ts +229 -0
  58. package/src/sqlite/errors/requirements.ts +71 -0
  59. package/src/sqlite/errors/types.ts +29 -0
  60. package/src/sqlite/executor.ts +227 -0
  61. package/src/sqlite/function/aggregate.ts +2 -0
  62. package/src/sqlite/function/core.ts +2 -0
  63. package/src/sqlite/function/index.ts +19 -0
  64. package/src/sqlite/function/string.ts +2 -0
  65. package/src/sqlite/function/temporal.ts +100 -0
  66. package/src/sqlite/function/window.ts +2 -0
  67. package/src/sqlite/internal/dialect.ts +37 -0
  68. package/src/sqlite/internal/dsl.ts +6926 -0
  69. package/src/sqlite/internal/renderer.ts +47 -0
  70. package/src/sqlite/internal/sql-expression-renderer.ts +1821 -0
  71. package/src/sqlite/json.ts +2 -0
  72. package/src/sqlite/query.ts +196 -0
  73. package/src/sqlite/renderer.ts +24 -0
  74. package/src/sqlite/table.ts +183 -0
  75. package/src/sqlite.ts +22 -0
@@ -63,6 +63,23 @@ type TupleIndex<
63
63
  : TupleIndex<Tail, Index, [...Count, unknown]>
64
64
  : never
65
65
 
66
+ type TupleIndexFromEnd<
67
+ Values extends readonly unknown[],
68
+ Distance extends number,
69
+ Count extends readonly unknown[] = [unknown]
70
+ > = Values extends readonly [...infer Rest, infer Last]
71
+ ? Count["length"] extends Distance
72
+ ? Last
73
+ : TupleIndexFromEnd<Rest, Distance, [...Count, unknown]>
74
+ : never
75
+
76
+ type TupleIndexAt<
77
+ Values extends readonly unknown[],
78
+ Index extends number
79
+ > = `${Index}` extends `-${infer Distance extends number}`
80
+ ? TupleIndexFromEnd<Values, Distance>
81
+ : TupleIndex<Values, Index>
82
+
66
83
  type DropTupleIndex<
67
84
  Values extends readonly unknown[],
68
85
  Index extends number,
@@ -73,6 +90,23 @@ type DropTupleIndex<
73
90
  : readonly [Head, ...DropTupleIndex<Tail, Index, [...Count, unknown]>]
74
91
  : readonly []
75
92
 
93
+ type DropTupleIndexFromEnd<
94
+ Values extends readonly unknown[],
95
+ Distance extends number,
96
+ Count extends readonly unknown[] = [unknown]
97
+ > = Values extends readonly [...infer Rest, infer Last]
98
+ ? Count["length"] extends Distance
99
+ ? readonly [...Rest]
100
+ : readonly [...DropTupleIndexFromEnd<Rest, Distance, [...Count, unknown]>, Last]
101
+ : readonly []
102
+
103
+ type DropTupleIndexAt<
104
+ Values extends readonly unknown[],
105
+ Index extends number
106
+ > = `${Index}` extends `-${infer Distance extends number}`
107
+ ? DropTupleIndexFromEnd<Values, Distance>
108
+ : DropTupleIndex<Values, Index>
109
+
76
110
  type SetTupleIndex<
77
111
  Values extends readonly unknown[],
78
112
  Index extends number,
@@ -84,6 +118,25 @@ type SetTupleIndex<
84
118
  : readonly [Head, ...SetTupleIndex<Tail, Index, Next, [...Count, unknown]>]
85
119
  : readonly NormalizeJsonLiteral<Next>[]
86
120
 
121
+ type SetTupleIndexFromEnd<
122
+ Values extends readonly unknown[],
123
+ Distance extends number,
124
+ Next,
125
+ Count extends readonly unknown[] = [unknown]
126
+ > = Values extends readonly [...infer Rest, infer Last]
127
+ ? Count["length"] extends Distance
128
+ ? readonly [...Rest, NormalizeJsonLiteral<Next>]
129
+ : readonly [...SetTupleIndexFromEnd<Rest, Distance, Next, [...Count, unknown]>, Last]
130
+ : readonly NormalizeJsonLiteral<Next>[]
131
+
132
+ type SetTupleIndexAt<
133
+ Values extends readonly unknown[],
134
+ Index extends number,
135
+ Next
136
+ > = `${Index}` extends `-${infer Distance extends number}`
137
+ ? SetTupleIndexFromEnd<Values, Distance, Next>
138
+ : SetTupleIndex<Values, Index, Next>
139
+
87
140
  type InsertTupleIndex<
88
141
  Values extends readonly unknown[],
89
142
  Index extends number,
@@ -98,6 +151,29 @@ type InsertTupleIndex<
98
151
  : readonly [Head, ...InsertTupleIndex<Tail, Index, Next, After, [...Count, unknown]>]
99
152
  : readonly NormalizeJsonLiteral<Next>[]
100
153
 
154
+ type InsertTupleIndexFromEnd<
155
+ Values extends readonly unknown[],
156
+ Distance extends number,
157
+ Next,
158
+ After extends boolean,
159
+ Count extends readonly unknown[] = [unknown]
160
+ > = Values extends readonly [...infer Rest, infer Last]
161
+ ? Count["length"] extends Distance
162
+ ? After extends true
163
+ ? readonly [...Rest, Last, NormalizeJsonLiteral<Next>]
164
+ : readonly [...Rest, NormalizeJsonLiteral<Next>, Last]
165
+ : readonly [...InsertTupleIndexFromEnd<Rest, Distance, Next, After, [...Count, unknown]>, Last]
166
+ : readonly NormalizeJsonLiteral<Next>[]
167
+
168
+ type InsertTupleIndexAt<
169
+ Values extends readonly unknown[],
170
+ Index extends number,
171
+ Next,
172
+ After extends boolean
173
+ > = `${Index}` extends `-${infer Distance extends number}`
174
+ ? InsertTupleIndexFromEnd<Values, Distance, Next, After>
175
+ : InsertTupleIndex<Values, Index, Next, After>
176
+
101
177
  type IndexStep<
102
178
  Root,
103
179
  Index extends number,
@@ -105,7 +181,7 @@ type IndexStep<
105
181
  > = Root extends readonly unknown[]
106
182
  ? number extends Root["length"]
107
183
  ? NormalizeJsonLiteral<Root[number]> | null
108
- : NormalizeJsonLiteral<TupleIndex<Root, Index>> | (TupleIndex<Root, Index> extends never ? null : never)
184
+ : NormalizeJsonLiteral<TupleIndexAt<Root, Index>> | (TupleIndexAt<Root, Index> extends never ? null : never)
109
185
  : JsonPathUsageError<Operation, Root, JsonPath.IndexSegment<Index>, "index segments require an array-like json value">
110
186
 
111
187
  type NonExactStep<
@@ -146,9 +222,9 @@ type StepSet<
146
222
  ? Root extends readonly unknown[]
147
223
  ? number extends Root["length"]
148
224
  ? readonly (NormalizeJsonLiteral<Root[number]> | NormalizeJsonLiteral<Next>)[]
149
- : SetTupleIndex<Root, Index, Next>
225
+ : SetTupleIndexAt<Root, Index, Next>
150
226
  : JsonPathUsageError<Operation, Root, Segment, "index segments require an array-like json value">
151
- : NormalizeJsonLiteral<Root>
227
+ : JsonPathUsageError<Operation, Root, Segment, "mutation paths require exact key/index segments">
152
228
 
153
229
  type StepDelete<
154
230
  Root,
@@ -164,9 +240,9 @@ type StepDelete<
164
240
  ? Root extends readonly unknown[]
165
241
  ? number extends Root["length"]
166
242
  ? Root
167
- : DropTupleIndex<Root, Index>
243
+ : DropTupleIndexAt<Root, Index>
168
244
  : JsonPathUsageError<Operation, Root, Segment, "index segments require an array-like json value">
169
- : NormalizeJsonLiteral<Root>
245
+ : JsonPathUsageError<Operation, Root, Segment, "mutation paths require exact key/index segments">
170
246
 
171
247
  type StepInsert<
172
248
  Root,
@@ -178,7 +254,7 @@ type StepInsert<
178
254
  ? Root extends readonly unknown[]
179
255
  ? number extends Root["length"]
180
256
  ? readonly (NormalizeJsonLiteral<Root[number]> | NormalizeJsonLiteral<Next>)[]
181
- : InsertTupleIndex<Root, Index, Next, After>
257
+ : InsertTupleIndexAt<Root, Index, Next, After>
182
258
  : JsonPathUsageError<Operation, Root, Segment, "index segments require an array-like json value">
183
259
  : Segment extends JsonPath.KeySegment<infer Key extends string>
184
260
  ? Root extends readonly unknown[]
@@ -190,7 +266,7 @@ type StepInsert<
190
266
  readonly [K in Key]: NormalizeJsonLiteral<Next>
191
267
  }
192
268
  : JsonPathUsageError<Operation, Root, Segment, "key segments require an object-like json value">
193
- : NormalizeJsonLiteral<Root>
269
+ : JsonPathUsageError<Operation, Root, Segment, "mutation paths require exact key/index segments">
194
270
 
195
271
  type RecurseValue<
196
272
  Current,
@@ -219,12 +295,16 @@ type RecurseSet<
219
295
  : StepValue<Current, Head, Operation> extends infer Child
220
296
  ? Child extends JsonPathUsageError<any, any, any, any>
221
297
  ? Child
222
- : StepSet<
223
- Current,
224
- Head,
225
- RecurseSet<StripNull<Child>, Tail, Next, Operation>,
226
- Operation
227
- >
298
+ : RecurseSet<StripNull<Child>, Tail, Next, Operation> extends infer UpdatedChild
299
+ ? UpdatedChild extends JsonPathUsageError<any, any, any, any>
300
+ ? UpdatedChild
301
+ : StepSet<
302
+ Current,
303
+ Head,
304
+ UpdatedChild,
305
+ Operation
306
+ >
307
+ : never
228
308
  : never
229
309
  : NormalizeJsonLiteral<Next>
230
310
 
@@ -238,12 +318,16 @@ type RecurseDelete<
238
318
  : StepValue<Current, Head, Operation> extends infer Child
239
319
  ? Child extends JsonPathUsageError<any, any, any, any>
240
320
  ? Child
241
- : StepSet<
242
- Current,
243
- Head,
244
- RecurseDelete<StripNull<Child>, Tail, Operation>,
245
- Operation
246
- >
321
+ : RecurseDelete<StripNull<Child>, Tail, Operation> extends infer UpdatedChild
322
+ ? UpdatedChild extends JsonPathUsageError<any, any, any, any>
323
+ ? UpdatedChild
324
+ : StepSet<
325
+ Current,
326
+ Head,
327
+ UpdatedChild,
328
+ Operation
329
+ >
330
+ : never
247
331
  : never
248
332
  : NormalizeJsonLiteral<Current>
249
333
 
@@ -259,12 +343,16 @@ type RecurseInsert<
259
343
  : StepValue<Current, Head, Operation> extends infer Child
260
344
  ? Child extends JsonPathUsageError<any, any, any, any>
261
345
  ? Child
262
- : StepSet<
263
- Current,
264
- Head,
265
- RecurseInsert<StripNull<Child>, Tail, Next, After, Operation>,
266
- Operation
267
- >
346
+ : RecurseInsert<StripNull<Child>, Tail, Next, After, Operation> extends infer UpdatedChild
347
+ ? UpdatedChild extends JsonPathUsageError<any, any, any, any>
348
+ ? UpdatedChild
349
+ : StepSet<
350
+ Current,
351
+ Head,
352
+ UpdatedChild,
353
+ Operation
354
+ >
355
+ : never
268
356
  : never
269
357
  : NormalizeJsonLiteral<Current>
270
358
 
@@ -364,23 +452,50 @@ export type JsonBuildArray<
364
452
 
365
453
  export type JsonTextResult<Value> = Value extends JsonPrimitive ? `${Value}` : string
366
454
 
367
- export type JsonTypeName<Value> =
368
- NormalizeJsonLiteral<Value> extends null ? "null"
369
- : NormalizeJsonLiteral<Value> extends string ? "string"
370
- : NormalizeJsonLiteral<Value> extends number ? "number"
371
- : NormalizeJsonLiteral<Value> extends boolean ? "boolean"
372
- : NormalizeJsonLiteral<Value> extends readonly unknown[] ? "array"
373
- : NormalizeJsonLiteral<Value> extends object ? "object"
455
+ type JsonTypeNameOfNormalized<Value> =
456
+ Value extends null ? "null"
457
+ : Value extends string ? "string"
458
+ : Value extends number ? "number"
459
+ : Value extends boolean ? "boolean"
460
+ : Value extends readonly unknown[] ? "array"
461
+ : Value extends object ? "object"
374
462
  : "unknown"
375
463
 
376
- export type JsonLengthResult<Value> =
377
- NormalizeJsonLiteral<Value> extends readonly unknown[] ? number :
378
- NormalizeJsonLiteral<Value> extends object ? number :
464
+ export type JsonTypeName<Value> =
465
+ [NormalizeJsonLiteral<Value>] extends [never]
466
+ ? "unknown"
467
+ : NormalizeJsonLiteral<Value> extends infer Normalized
468
+ ? Normalized extends unknown
469
+ ? JsonTypeNameOfNormalized<Normalized>
470
+ : never
471
+ : never
472
+
473
+ type JsonLengthResultOfNormalized<Value> =
474
+ Value extends readonly unknown[] ? number :
475
+ Value extends object ? number :
379
476
  null
380
477
 
478
+ export type JsonLengthResult<Value> =
479
+ [NormalizeJsonLiteral<Value>] extends [never]
480
+ ? null
481
+ : NormalizeJsonLiteral<Value> extends infer Normalized
482
+ ? Normalized extends unknown
483
+ ? JsonLengthResultOfNormalized<Normalized>
484
+ : never
485
+ : never
486
+
487
+ type JsonKeysResultOfNormalized<Value> =
488
+ Value extends readonly unknown[]
489
+ ? null
490
+ : Value extends object
491
+ ? readonly Extract<keyof Value, string>[]
492
+ : null
493
+
381
494
  export type JsonKeysResult<Value> =
382
- NormalizeJsonLiteral<Value> extends object
383
- ? NormalizeJsonLiteral<Value> extends readonly unknown[]
384
- ? readonly []
385
- : readonly Extract<keyof NormalizeJsonLiteral<Value>, string>[]
386
- : null
495
+ [NormalizeJsonLiteral<Value>] extends [never]
496
+ ? null
497
+ : NormalizeJsonLiteral<Value> extends infer Normalized
498
+ ? Normalized extends unknown
499
+ ? JsonKeysResultOfNormalized<Normalized>
500
+ : never
501
+ : never
@@ -46,7 +46,20 @@ type Frame<
46
46
  readonly polarity: Direction
47
47
  }
48
48
 
49
- type SourceNameOfKey<Key extends string> = Key extends `${infer SourceName}.${string}` ? SourceName : never
49
+ type SourceNameOfKey<
50
+ Key extends string,
51
+ Current extends string = ""
52
+ > = string extends Key
53
+ ? string
54
+ : Key extends `\\.${infer Rest}`
55
+ ? SourceNameOfKey<Rest, `${Current}.`>
56
+ : Key extends `\\\\${infer Rest}`
57
+ ? SourceNameOfKey<Rest, `${Current}\\`>
58
+ : Key extends `.${string}`
59
+ ? Current
60
+ : Key extends `${infer Character}${infer Rest}`
61
+ ? SourceNameOfKey<Rest, `${Current}${Character}`>
62
+ : never
50
63
 
51
64
  type EqLiteralValueOf<
52
65
  EqLiterals,
@@ -2,10 +2,27 @@ import type * as Expression from "../scalar.js"
2
2
  import type * as ExpressionAst from "../expression-ast.js"
3
3
  import type * as JsonPath from "../json/path.js"
4
4
 
5
+ type EscapePredicateBackslashes<Value extends string> = string extends Value
6
+ ? string
7
+ : Value extends `${infer Head}\\${infer Tail}`
8
+ ? `${Head}\\\\${EscapePredicateBackslashes<Tail>}`
9
+ : Value
10
+
11
+ type EscapePredicateDots<Value extends string> = string extends Value
12
+ ? string
13
+ : Value extends `${infer Head}.${infer Tail}`
14
+ ? `${Head}\\.${EscapePredicateDots<Tail>}`
15
+ : Value
16
+
17
+ type EscapePredicateSegment<Value extends string> =
18
+ EscapePredicateDots<EscapePredicateBackslashes<Value>>
19
+
20
+ type EscapeJsonPathSegment<Value extends string> = EscapePredicateSegment<Value>
21
+
5
22
  export type ColumnKey<
6
23
  TableName extends string,
7
24
  ColumnName extends string
8
- > = `${TableName}.${ColumnName}`
25
+ > = `${EscapePredicateSegment<TableName>}.${EscapePredicateSegment<ColumnName>}`
9
26
 
10
27
  export type ColumnKeyOfAst<Ast extends ExpressionAst.Any> =
11
28
  [Ast] extends [ExpressionAst.ColumnNode<infer TableName extends string, infer ColumnName extends string>]
@@ -25,7 +42,7 @@ type JsonPathKey<
25
42
  ? Segment extends JsonPath.KeySegment<infer Key extends string>
26
43
  ? JsonPathKey<
27
44
  Tail,
28
- [Current] extends [never] ? Key : `${Current}.${Key}`,
45
+ [Current] extends [never] ? EscapeJsonPathSegment<Key> : `${Current}.${EscapeJsonPathSegment<Key>}`,
29
46
  readonly [...Seen, unknown]
30
47
  >
31
48
  : never
@@ -98,7 +98,26 @@ const cloneContext = (context: MutableContext): MutableContext => ({
98
98
 
99
99
  const freezeContext = (context: MutableContext): RuntimeContext => context
100
100
 
101
- const sourceNameOfKey = (key: string): string => key.split(".", 1)[0] ?? key
101
+ export const columnPredicateKey = (tableName: string, columnName: string): string =>
102
+ JSON.stringify([tableName, columnName])
103
+
104
+ const columnPredicateKeyParts = (key: string): readonly [string, string] | undefined => {
105
+ const jsonSeparator = key.indexOf("#json:")
106
+ const columnKey = jsonSeparator === -1 ? key : key.slice(0, jsonSeparator)
107
+ try {
108
+ const parsed = JSON.parse(columnKey) as unknown
109
+ return Array.isArray(parsed) &&
110
+ parsed.length === 2 &&
111
+ typeof parsed[0] === "string" &&
112
+ typeof parsed[1] === "string"
113
+ ? [parsed[0], parsed[1]]
114
+ : undefined
115
+ } catch {
116
+ return undefined
117
+ }
118
+ }
119
+
120
+ const sourceNameOfKey = (key: string): string => columnPredicateKeyParts(key)?.[0] ?? key.split(".", 1)[0] ?? key
102
121
 
103
122
  const addSourceName = (context: MutableContext, key: string): void => {
104
123
  context.sourceNames.add(sourceNameOfKey(key))
@@ -386,12 +405,15 @@ const astOf = (value: Expression.Any): ExpressionAst.Any =>
386
405
 
387
406
  const columnKeyOfExpression = (value: Expression.Any): string | undefined => {
388
407
  const ast = astOf(value)
389
- return ast.kind === "column" ? `${ast.tableName}.${ast.columnName}` : undefined
408
+ return ast.kind === "column" ? columnPredicateKey(ast.tableName, ast.columnName) : undefined
390
409
  }
391
410
 
392
411
  const sameDbType = (left: Expression.DbType.Any, right: Expression.DbType.Any): boolean =>
393
412
  left.dialect === right.dialect && left.kind === right.kind
394
413
 
414
+ const escapeJsonPathPredicateKeySegment = (value: string): string =>
415
+ value.replaceAll("\\", "\\\\").replaceAll(".", "\\.")
416
+
395
417
  const jsonPathPredicateKeyOfExpression = (value: Expression.Any): string | undefined => {
396
418
  const ast = astOf(value)
397
419
  switch (ast.kind) {
@@ -415,7 +437,9 @@ const jsonPathPredicateKeyOfExpression = (value: Expression.Any): string | undef
415
437
  return undefined
416
438
  }
417
439
  const baseKey = columnKeyOfExpression(jsonAst.base)
418
- return baseKey === undefined ? undefined : `${baseKey}#json:${path.join(".")}`
440
+ return baseKey === undefined
441
+ ? undefined
442
+ : `${baseKey}#json:${path.map(escapeJsonPathPredicateKeySegment).join(".")}`
419
443
  }
420
444
  default:
421
445
  return undefined