effect-qb 0.15.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 (88) hide show
  1. package/dist/mysql.js +1957 -595
  2. package/dist/postgres/metadata.js +2507 -182
  3. package/dist/postgres.js +9587 -8201
  4. package/dist/sqlite.js +8360 -0
  5. package/package.json +7 -2
  6. package/src/internal/column-state.ts +7 -0
  7. package/src/internal/column.ts +22 -0
  8. package/src/internal/derived-table.ts +29 -3
  9. package/src/internal/dialect.ts +14 -1
  10. package/src/internal/dsl-mutation-runtime.ts +173 -4
  11. package/src/internal/dsl-plan-runtime.ts +165 -20
  12. package/src/internal/dsl-query-runtime.ts +60 -6
  13. package/src/internal/dsl-transaction-ddl-runtime.ts +72 -2
  14. package/src/internal/executor.ts +62 -13
  15. package/src/internal/expression-ast.ts +3 -2
  16. package/src/internal/grouping-key.ts +141 -1
  17. package/src/internal/implication-runtime.ts +2 -1
  18. package/src/internal/json/types.ts +155 -40
  19. package/src/internal/predicate/analysis.ts +103 -1
  20. package/src/internal/predicate/atom.ts +7 -0
  21. package/src/internal/predicate/context.ts +170 -17
  22. package/src/internal/predicate/key.ts +64 -2
  23. package/src/internal/predicate/normalize.ts +115 -34
  24. package/src/internal/predicate/runtime.ts +144 -13
  25. package/src/internal/query.ts +563 -103
  26. package/src/internal/renderer.ts +39 -2
  27. package/src/internal/runtime/driver-value-mapping.ts +244 -0
  28. package/src/internal/runtime/normalize.ts +62 -38
  29. package/src/internal/runtime/schema.ts +5 -3
  30. package/src/internal/runtime/value.ts +153 -30
  31. package/src/internal/scalar.ts +11 -0
  32. package/src/internal/table-options.ts +108 -1
  33. package/src/internal/table.ts +87 -29
  34. package/src/mysql/column.ts +19 -2
  35. package/src/mysql/datatypes/index.ts +21 -0
  36. package/src/mysql/errors/catalog.ts +5 -5
  37. package/src/mysql/errors/normalize.ts +2 -2
  38. package/src/mysql/executor.ts +20 -5
  39. package/src/mysql/internal/dialect.ts +12 -6
  40. package/src/mysql/internal/dsl.ts +995 -263
  41. package/src/mysql/internal/renderer.ts +13 -3
  42. package/src/mysql/internal/sql-expression-renderer.ts +530 -128
  43. package/src/mysql/query.ts +9 -2
  44. package/src/mysql/renderer.ts +7 -2
  45. package/src/mysql/table.ts +38 -12
  46. package/src/postgres/cast.ts +22 -7
  47. package/src/postgres/column.ts +5 -2
  48. package/src/postgres/errors/normalize.ts +2 -2
  49. package/src/postgres/executor.ts +68 -10
  50. package/src/postgres/function/core.ts +19 -1
  51. package/src/postgres/internal/dialect.ts +12 -6
  52. package/src/postgres/internal/dsl.ts +958 -288
  53. package/src/postgres/internal/renderer.ts +13 -3
  54. package/src/postgres/internal/schema-ddl.ts +2 -1
  55. package/src/postgres/internal/schema-model.ts +6 -3
  56. package/src/postgres/internal/sql-expression-renderer.ts +477 -96
  57. package/src/postgres/json.ts +57 -17
  58. package/src/postgres/query.ts +9 -2
  59. package/src/postgres/renderer.ts +7 -2
  60. package/src/postgres/schema-management.ts +91 -4
  61. package/src/postgres/schema.ts +1 -1
  62. package/src/postgres/table.ts +189 -53
  63. package/src/postgres/type.ts +4 -0
  64. package/src/sqlite/column.ts +128 -0
  65. package/src/sqlite/datatypes/index.ts +79 -0
  66. package/src/sqlite/datatypes/spec.ts +98 -0
  67. package/src/sqlite/errors/catalog.ts +103 -0
  68. package/src/sqlite/errors/fields.ts +19 -0
  69. package/src/sqlite/errors/index.ts +19 -0
  70. package/src/sqlite/errors/normalize.ts +229 -0
  71. package/src/sqlite/errors/requirements.ts +71 -0
  72. package/src/sqlite/errors/types.ts +29 -0
  73. package/src/sqlite/executor.ts +227 -0
  74. package/src/sqlite/function/aggregate.ts +2 -0
  75. package/src/sqlite/function/core.ts +2 -0
  76. package/src/sqlite/function/index.ts +19 -0
  77. package/src/sqlite/function/string.ts +2 -0
  78. package/src/sqlite/function/temporal.ts +100 -0
  79. package/src/sqlite/function/window.ts +2 -0
  80. package/src/sqlite/internal/dialect.ts +37 -0
  81. package/src/sqlite/internal/dsl.ts +6926 -0
  82. package/src/sqlite/internal/renderer.ts +47 -0
  83. package/src/sqlite/internal/sql-expression-renderer.ts +1821 -0
  84. package/src/sqlite/json.ts +2 -0
  85. package/src/sqlite/query.ts +196 -0
  86. package/src/sqlite/renderer.ts +24 -0
  87. package/src/sqlite/table.ts +183 -0
  88. package/src/sqlite.ts +22 -0
@@ -1,10 +1,12 @@
1
1
  import * as Expression from "../internal/scalar.js"
2
+ import type * as ExpressionAst from "../internal/expression-ast.js"
2
3
  import type { JsonPathUsageError } from "../internal/json/errors.js"
3
4
  import * as JsonPath from "../internal/json/path.js"
4
5
  import type {
5
6
  JsonDeleteAtPath,
6
7
  JsonInsertAtPath,
7
8
  JsonSetAtPath,
9
+ JsonTextResult,
8
10
  JsonValueAtPath
9
11
  } from "../internal/json/types.js"
10
12
  import { json as postgresJson, jsonb as postgresJsonb } from "./internal/dsl.js"
@@ -75,6 +77,16 @@ type JsonSetOutputOf<
75
77
  ? JsonSetAtPath<Root, JsonPath.Path<[Target]>, Next, Operation>
76
78
  : never
77
79
 
80
+ type JsonSetOutputWithCreateMissing<
81
+ Root,
82
+ Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment,
83
+ Next,
84
+ Operation extends string,
85
+ CreateMissing extends boolean
86
+ > = false extends CreateMissing
87
+ ? Root | JsonSetOutputOf<Root, Target, Next, Operation>
88
+ : JsonSetOutputOf<Root, Target, Next, Operation>
89
+
78
90
  type JsonInsertOutputOf<
79
91
  Root,
80
92
  Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment,
@@ -144,17 +156,37 @@ type JsonNullabilityOf<Output> =
144
156
  ? Exclude<Output, null> extends never ? "always" : "maybe"
145
157
  : "never"
146
158
 
159
+ type JsonPathSegmentsOf<Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment> =
160
+ Target extends JsonPath.Path<infer Segments extends readonly JsonPath.CanonicalSegment[]> ? Segments :
161
+ Target extends JsonPath.CanonicalSegment ? readonly [Target] :
162
+ readonly []
163
+
164
+ type JsonGetAccessKind<Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment> =
165
+ Target extends JsonPath.Path<any>
166
+ ? JsonPath.IsExactPath<Target> extends true ? "jsonPath" : "jsonTraverse"
167
+ : Target extends JsonPath.ExactSegment ? "jsonGet" : "jsonAccess"
168
+
169
+ type JsonTextAccessKind<Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment> =
170
+ Target extends JsonPath.Path<any>
171
+ ? JsonPath.IsExactPath<Target> extends true ? "jsonPathText" : "jsonTraverseText"
172
+ : Target extends JsonPath.ExactSegment ? "jsonGetText" : "jsonAccessText"
173
+
147
174
  type JsonResultExpression<
148
175
  Runtime,
149
- Db extends Expression.DbType.Json<any, any>
176
+ Db extends Expression.DbType.Json<any, any>,
177
+ Kind extends Expression.ScalarKind = Expression.ScalarKind,
178
+ Dependencies extends Expression.BindingId = Expression.BindingId,
179
+ Ast extends ExpressionAst.Any = never
150
180
  > = Expression.Scalar<
151
181
  Runtime,
152
182
  Db,
153
183
  JsonNullabilityOf<Runtime>,
154
184
  string,
155
- Expression.ScalarKind,
156
- Expression.BindingId
157
- >
185
+ Kind,
186
+ Dependencies
187
+ > & ([Ast] extends [never] ? unknown : {
188
+ readonly [ExpressionAst.TypeId]: Ast
189
+ })
158
190
 
159
191
  type JsonDbOf<Base extends PostgresJsonExpression<any>> =
160
192
  Expression.DbTypeOf<Base> extends Expression.DbType.Json<"postgres", infer Variant>
@@ -167,14 +199,17 @@ type JsonGetResultExpression<
167
199
  Operation extends string
168
200
  > = JsonResultExpression<
169
201
  JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, Operation>,
170
- JsonDbOf<Base>
202
+ JsonDbOf<Base>,
203
+ Expression.KindOf<Base>,
204
+ Expression.DependenciesOf<Base>,
205
+ ExpressionAst.JsonAccessNode<JsonGetAccessKind<Target>, Base, JsonPathSegmentsOf<Target>>
171
206
  >
172
207
 
173
208
  type JsonTextRuntime<
174
209
  Base extends PostgresJsonExpression<any>,
175
210
  Target extends JsonPath.Path<any> | JsonPath.CanonicalSegment
176
211
  > =
177
- Extract<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.text">, string> |
212
+ JsonTextResult<Exclude<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.text">, JsonPathUsageError<any, any, any, any> | null>> |
178
213
  (null extends JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.text"> ? null : never)
179
214
 
180
215
  type JsonTextResultExpression<
@@ -185,9 +220,11 @@ type JsonTextResultExpression<
185
220
  Expression.DbType.Base<"postgres", "text">,
186
221
  JsonNullabilityOf<JsonTextRuntime<Base, Target>>,
187
222
  string,
188
- Expression.ScalarKind,
189
- Expression.BindingId
190
- >
223
+ Expression.KindOf<Base>,
224
+ Expression.DependenciesOf<Base>
225
+ > & {
226
+ readonly [ExpressionAst.TypeId]: ExpressionAst.JsonAccessNode<JsonTextAccessKind<Target>, Base, JsonPathSegmentsOf<Target>>
227
+ }
191
228
 
192
229
  const exactPath = <Segments extends readonly JsonPath.CanonicalSegment[]>(
193
230
  ...segments: Segments & ExactJsonPathSegmentsGuard<Segments>
@@ -222,7 +259,7 @@ const json = {
222
259
  typeof jsonGetDirect & {
223
260
  <Target extends ExactJsonPathInput>(
224
261
  target: Target & ExactJsonPathGuard<Target>
225
- ): <Base extends PostgresJsonExpression<any>>(base: Base) => ReturnType<typeof jsonGetDirect>
262
+ ): <Base extends PostgresJsonExpression<any>>(base: Base) => JsonGetResultExpression<Base, Target, "json.get">
226
263
  },
227
264
  access: <
228
265
  Base extends PostgresJsonExpression<any>,
@@ -245,7 +282,7 @@ const json = {
245
282
  typeof jsonTextDirect & {
246
283
  <Target extends ExactJsonPathInput>(
247
284
  target: Target & ExactJsonPathGuard<Target>
248
- ): <Base extends PostgresJsonExpression<any>>(base: Base) => ReturnType<typeof jsonTextDirect>
285
+ ): <Base extends PostgresJsonExpression<any>>(base: Base) => JsonTextResultExpression<Base, Target>
249
286
  },
250
287
  accessText: <
251
288
  Base extends PostgresJsonExpression<any>,
@@ -416,17 +453,20 @@ const jsonb = {
416
453
  set: <
417
454
  Base extends PostgresJsonExpression<any>,
418
455
  Target extends JsonPath.CanonicalSegment | JsonPath.Path<any>,
419
- Next extends Parameters<typeof postgresJsonb.set>[2]
456
+ Next extends Parameters<typeof postgresJsonb.set>[2],
457
+ CreateMissing extends boolean = true
420
458
  >(
421
459
  base: Base & JsonbBaseGuard<Base, "jsonb.set">,
422
- target: Target & JsonSetPathGuard<Expression.RuntimeOf<Base>, Target, Next, "json.set">,
460
+ target: Target & JsonSetPathGuard<Expression.RuntimeOf<Base>, Target, NoInfer<Next>, "json.set">,
423
461
  next: Next,
424
- options?: Parameters<typeof postgresJsonb.set>[3]
462
+ options?: {
463
+ readonly createMissing?: CreateMissing
464
+ }
425
465
  ): JsonResultExpression<
426
- JsonSetOutputOf<Expression.RuntimeOf<Base>, Target, Next, "json.set">,
466
+ JsonSetOutputWithCreateMissing<Expression.RuntimeOf<Base>, Target, Next, "json.set", CreateMissing>,
427
467
  Expression.DbTypeOf<Base>
428
468
  > => postgresJsonb.set(base as any, target as any, next, options) as unknown as JsonResultExpression<
429
- JsonSetOutputOf<Expression.RuntimeOf<Base>, Target, Next, "json.set">,
469
+ JsonSetOutputWithCreateMissing<Expression.RuntimeOf<Base>, Target, Next, "json.set", CreateMissing>,
430
470
  Expression.DbTypeOf<Base>
431
471
  >,
432
472
  insert: <
@@ -436,7 +476,7 @@ const jsonb = {
436
476
  InsertAfter extends boolean = false
437
477
  >(
438
478
  base: Base & JsonbBaseGuard<Base, "jsonb.insert">,
439
- target: Target & JsonInsertPathGuard<Expression.RuntimeOf<Base>, Target, Next, InsertAfter, "json.insert">,
479
+ target: Target & JsonInsertPathGuard<Expression.RuntimeOf<Base>, Target, NoInfer<Next>, NoInfer<InsertAfter>, "json.insert">,
440
480
  next: Next,
441
481
  options?: {
442
482
  readonly insertAfter?: InsertAfter
@@ -16,7 +16,6 @@ import {
16
16
  type HavingPredicateInput,
17
17
  type OrderDirection,
18
18
  type OutputOfSelection,
19
- type MutationInputOf,
20
19
  type MutationTargetLike,
21
20
  type NumericExpressionInput,
22
21
  type PredicateInput,
@@ -137,9 +136,18 @@ export {
137
136
  orderBy,
138
137
  groupBy
139
138
  } from "./internal/dsl.js"
139
+ import type * as Expression from "../internal/scalar.js"
140
140
  export { postgresType as type }
141
141
  export const generateSeries: PublicGenerateSeriesApi = dslGenerateSeries as PublicGenerateSeriesApi
142
142
 
143
+ type PostgresMutationValueInput<Value> =
144
+ | Value
145
+ | Expression.Scalar<Value, Expression.DbType.Any, Expression.Nullability, "postgres", Expression.ScalarKind, Expression.BindingId>
146
+
147
+ export type MutationInputOf<Shape> = {
148
+ readonly [K in keyof Shape]: PostgresMutationValueInput<Shape[K]>
149
+ }
150
+
143
151
  type StructuredSource = AnyValuesSource | AnyUnnestSource | AnyTableFunctionSource
144
152
 
145
153
  type StructuredFromApi = <CurrentSource extends StructuredSource>(
@@ -165,7 +173,6 @@ export type {
165
173
  HavingPredicateInput,
166
174
  OrderDirection,
167
175
  OutputOfSelection,
168
- MutationInputOf,
169
176
  MutationTargetLike,
170
177
  NumericExpressionInput,
171
178
  PredicateInput,
@@ -1,4 +1,5 @@
1
1
  import * as CoreRenderer from "../internal/renderer.js"
2
+ import type * as Expression from "../internal/scalar.js"
2
3
  import { renderPostgresPlan } from "./internal/renderer.js"
3
4
 
4
5
  /** Postgres-specialized rendered query shape. */
@@ -8,12 +9,16 @@ export type RowOf<Value extends RenderedQuery<any>> = CoreRenderer.RowOf<Value>
8
9
  /** Postgres-specialized renderer contract. */
9
10
  export type Renderer = CoreRenderer.Renderer<"postgres">
10
11
 
12
+ export interface MakeOptions {
13
+ readonly valueMappings?: Expression.DriverValueMappings
14
+ }
15
+
11
16
  export { TypeId } from "../internal/renderer.js"
12
17
  export type { Projection } from "../internal/renderer.js"
13
18
 
14
19
  /** Creates the built-in Postgres renderer. */
15
- export const make = (): Renderer =>
16
- CoreRenderer.make("postgres", renderPostgresPlan)
20
+ export const make = (options: MakeOptions = {}): Renderer =>
21
+ CoreRenderer.make("postgres", (plan) => renderPostgresPlan(plan, options))
17
22
 
18
23
  /** Shared built-in Postgres renderer instance. */
19
24
  export const postgres = make()
@@ -16,6 +16,75 @@ type QualifiedName<
16
16
  : `${SchemaName}.${Name}`
17
17
  : Name
18
18
 
19
+ type LowerAlpha =
20
+ | "a"
21
+ | "b"
22
+ | "c"
23
+ | "d"
24
+ | "e"
25
+ | "f"
26
+ | "g"
27
+ | "h"
28
+ | "i"
29
+ | "j"
30
+ | "k"
31
+ | "l"
32
+ | "m"
33
+ | "n"
34
+ | "o"
35
+ | "p"
36
+ | "q"
37
+ | "r"
38
+ | "s"
39
+ | "t"
40
+ | "u"
41
+ | "v"
42
+ | "w"
43
+ | "x"
44
+ | "y"
45
+ | "z"
46
+
47
+ type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
48
+ type SafeIdentifierStart = LowerAlpha | "_"
49
+ type SafeIdentifierRest = SafeIdentifierStart | Digit | "$"
50
+
51
+ type EscapeIdentifier<Value extends string> =
52
+ Value extends `${infer Head}"${infer Tail}`
53
+ ? `${Head}""${EscapeIdentifier<Tail>}`
54
+ : Value
55
+
56
+ type IsSafeIdentifierRest<Value extends string> =
57
+ Value extends ""
58
+ ? true
59
+ : Value extends `${infer Head}${infer Tail}`
60
+ ? Head extends SafeIdentifierRest
61
+ ? IsSafeIdentifierRest<Tail>
62
+ : false
63
+ : false
64
+
65
+ type IsSafeIdentifier<Value extends string> =
66
+ Value extends `${infer Head}${infer Tail}`
67
+ ? Head extends SafeIdentifierStart
68
+ ? IsSafeIdentifierRest<Tail>
69
+ : false
70
+ : false
71
+
72
+ type RenderIdentifier<Value extends string> =
73
+ string extends Value
74
+ ? string
75
+ : IsSafeIdentifier<Value> extends true
76
+ ? Value
77
+ : `"${EscapeIdentifier<Value>}"`
78
+
79
+ type RenderQualifiedTypeName<
80
+ Name extends string,
81
+ SchemaName extends string | undefined
82
+ > = SchemaName extends string
83
+ ? SchemaName extends "public"
84
+ ? RenderIdentifier<Name>
85
+ : `${RenderIdentifier<SchemaName>}.${RenderIdentifier<Name>}`
86
+ : RenderIdentifier<Name>
87
+
19
88
  type EnumColumn<
20
89
  Name extends string,
21
90
  Values extends readonly [string, ...string[]],
@@ -24,7 +93,7 @@ type EnumColumn<
24
93
  Values[number],
25
94
  Values[number],
26
95
  Values[number],
27
- Expression.DbType.Enum<"postgres", QualifiedName<Name, SchemaName>>,
96
+ Expression.DbType.Enum<"postgres", RenderQualifiedTypeName<Name, SchemaName>>,
28
97
  false,
29
98
  false,
30
99
  false,
@@ -33,6 +102,24 @@ type EnumColumn<
33
102
  undefined
34
103
  >
35
104
 
105
+ const safeUnquotedIdentifier = /^[a-z_][a-z0-9_$]*$/
106
+
107
+ const quoteIdentifier = (value: string): string =>
108
+ `"${value.replaceAll("\"", "\"\"")}"`
109
+
110
+ const renderIdentifier = (value: string): string =>
111
+ safeUnquotedIdentifier.test(value)
112
+ ? value
113
+ : quoteIdentifier(value)
114
+
115
+ const renderQualifiedTypeName = (
116
+ name: string,
117
+ schemaName: string | undefined
118
+ ): string =>
119
+ schemaName === undefined || schemaName === "public"
120
+ ? renderIdentifier(name)
121
+ : `${renderIdentifier(schemaName)}.${renderIdentifier(name)}`
122
+
36
123
  const EnumProto = {
37
124
  pipe(this: unknown) {
38
125
  return pipeArguments(this, arguments)
@@ -45,7 +132,7 @@ const EnumProto = {
45
132
  type(this: EnumDefinition) {
46
133
  return {
47
134
  dialect: "postgres",
48
- kind: this.qualifiedName(),
135
+ kind: renderQualifiedTypeName(this.name, this.schemaName),
49
136
  variant: "enum"
50
137
  }
51
138
  },
@@ -61,7 +148,7 @@ const EnumProto = {
61
148
  primaryKey: false,
62
149
  unique: false,
63
150
  references: undefined,
64
- ddlType: this.qualifiedName(),
151
+ ddlType: renderQualifiedTypeName(this.name, this.schemaName),
65
152
  identity: undefined,
66
153
  enum: {
67
154
  name: this.name,
@@ -99,7 +186,7 @@ export interface EnumDefinition<
99
186
  readonly schemaName: SchemaName
100
187
  }
101
188
  readonly qualifiedName: () => QualifiedName<Name, SchemaName>
102
- readonly type: () => Expression.DbType.Enum<"postgres", QualifiedName<Name, SchemaName>>
189
+ readonly type: () => Expression.DbType.Enum<"postgres", RenderQualifiedTypeName<Name, SchemaName>>
103
190
  readonly column: () => EnumColumn<Name, Values, SchemaName>
104
191
  }
105
192
 
@@ -1,4 +1,4 @@
1
- import { schema as makeTableSchemaNamespace, type TableSchemaNamespace } from "../internal/table.js"
1
+ import { schema as makeTableSchemaNamespace, type TableSchemaNamespace } from "./table.js"
2
2
  import { enumType, sequence, type EnumDefinition, type SequenceDefinition } from "./schema-management.js"
3
3
 
4
4
  export type SchemaNamespace<SchemaName extends string> = TableSchemaNamespace<SchemaName> & {