effect-qb 0.12.3

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 (81) hide show
  1. package/README.md +1294 -0
  2. package/dist/mysql.js +57575 -0
  3. package/dist/postgres.js +6303 -0
  4. package/package.json +42 -0
  5. package/src/internal/aggregation-validation.ts +57 -0
  6. package/src/internal/case-analysis.ts +50 -0
  7. package/src/internal/coercion-analysis.ts +30 -0
  8. package/src/internal/coercion-errors.ts +29 -0
  9. package/src/internal/coercion-kind.ts +32 -0
  10. package/src/internal/coercion-normalize.ts +7 -0
  11. package/src/internal/coercion-rules.ts +25 -0
  12. package/src/internal/column-state.ts +453 -0
  13. package/src/internal/column.ts +417 -0
  14. package/src/internal/datatypes/define.ts +44 -0
  15. package/src/internal/datatypes/lookup.ts +280 -0
  16. package/src/internal/datatypes/shape.ts +72 -0
  17. package/src/internal/derived-table.ts +149 -0
  18. package/src/internal/dialect.ts +30 -0
  19. package/src/internal/executor.ts +390 -0
  20. package/src/internal/expression-ast.ts +349 -0
  21. package/src/internal/expression.ts +325 -0
  22. package/src/internal/grouping-key.ts +82 -0
  23. package/src/internal/json/ast.ts +63 -0
  24. package/src/internal/json/errors.ts +13 -0
  25. package/src/internal/json/path.ts +227 -0
  26. package/src/internal/json/shape.ts +1 -0
  27. package/src/internal/json/types.ts +386 -0
  28. package/src/internal/mysql-dialect.ts +39 -0
  29. package/src/internal/mysql-renderer.ts +37 -0
  30. package/src/internal/plan.ts +64 -0
  31. package/src/internal/postgres-dialect.ts +34 -0
  32. package/src/internal/postgres-renderer.ts +40 -0
  33. package/src/internal/predicate-analysis.ts +71 -0
  34. package/src/internal/predicate-atom.ts +43 -0
  35. package/src/internal/predicate-branches.ts +40 -0
  36. package/src/internal/predicate-context.ts +279 -0
  37. package/src/internal/predicate-formula.ts +100 -0
  38. package/src/internal/predicate-key.ts +28 -0
  39. package/src/internal/predicate-nnf.ts +12 -0
  40. package/src/internal/predicate-normalize.ts +202 -0
  41. package/src/internal/projection-alias.ts +15 -0
  42. package/src/internal/projections.ts +101 -0
  43. package/src/internal/query-ast.ts +297 -0
  44. package/src/internal/query-factory.ts +6757 -0
  45. package/src/internal/query-requirements.ts +40 -0
  46. package/src/internal/query.ts +1590 -0
  47. package/src/internal/renderer.ts +102 -0
  48. package/src/internal/runtime-normalize.ts +344 -0
  49. package/src/internal/runtime-schema.ts +428 -0
  50. package/src/internal/runtime-value.ts +85 -0
  51. package/src/internal/schema-derivation.ts +131 -0
  52. package/src/internal/sql-expression-renderer.ts +1353 -0
  53. package/src/internal/table-options.ts +225 -0
  54. package/src/internal/table.ts +674 -0
  55. package/src/mysql/column.ts +30 -0
  56. package/src/mysql/datatypes/index.ts +6 -0
  57. package/src/mysql/datatypes/spec.ts +180 -0
  58. package/src/mysql/errors/catalog.ts +51662 -0
  59. package/src/mysql/errors/fields.ts +21 -0
  60. package/src/mysql/errors/index.ts +18 -0
  61. package/src/mysql/errors/normalize.ts +232 -0
  62. package/src/mysql/errors/requirements.ts +73 -0
  63. package/src/mysql/executor.ts +134 -0
  64. package/src/mysql/query.ts +189 -0
  65. package/src/mysql/renderer.ts +19 -0
  66. package/src/mysql/table.ts +157 -0
  67. package/src/mysql.ts +18 -0
  68. package/src/postgres/column.ts +20 -0
  69. package/src/postgres/datatypes/index.ts +8 -0
  70. package/src/postgres/datatypes/spec.ts +264 -0
  71. package/src/postgres/errors/catalog.ts +452 -0
  72. package/src/postgres/errors/fields.ts +48 -0
  73. package/src/postgres/errors/index.ts +4 -0
  74. package/src/postgres/errors/normalize.ts +209 -0
  75. package/src/postgres/errors/requirements.ts +65 -0
  76. package/src/postgres/errors/types.ts +38 -0
  77. package/src/postgres/executor.ts +131 -0
  78. package/src/postgres/query.ts +189 -0
  79. package/src/postgres/renderer.ts +29 -0
  80. package/src/postgres/table.ts +157 -0
  81. package/src/postgres.ts +18 -0
@@ -0,0 +1,225 @@
1
+ import {
2
+ BoundColumnTypeId,
3
+ type AnyBoundColumn,
4
+ type AnyColumnDefinition,
5
+ type IsNullable
6
+ } from "./column-state.js"
7
+ import type { TableFieldMap } from "./schema-derivation.js"
8
+
9
+ /** Non-empty list of column names. */
10
+ export type ColumnList = readonly [string, ...string[]]
11
+
12
+ /** Normalized table-level option record. */
13
+ export type TableOptionSpec =
14
+ | {
15
+ readonly kind: "index"
16
+ readonly columns: ColumnList
17
+ }
18
+ | {
19
+ readonly kind: "unique"
20
+ readonly columns: ColumnList
21
+ }
22
+ | {
23
+ readonly kind: "primaryKey"
24
+ readonly columns: ColumnList
25
+ }
26
+ | {
27
+ readonly kind: "foreignKey"
28
+ readonly columns: ColumnList
29
+ readonly references: () => {
30
+ readonly tableName: string
31
+ readonly schemaName?: string
32
+ readonly columns: ColumnList
33
+ readonly knownColumns?: readonly string[]
34
+ }
35
+ }
36
+ | {
37
+ readonly kind: "check"
38
+ readonly name: string
39
+ readonly predicate: unknown
40
+ }
41
+
42
+ /** Thin wrapper used by the public `Table.*` option builders. */
43
+ export interface TableOptionBuilder<Spec extends TableOptionSpec = TableOptionSpec> {
44
+ readonly option: Spec
45
+ }
46
+
47
+ /** Collection of declared table options. */
48
+ export type DeclaredTableOptions = readonly TableOptionBuilder[]
49
+
50
+ type ColumnNameUnion<Fields extends TableFieldMap> = Extract<keyof Fields, string>
51
+ type NullableColumnNames<Fields extends TableFieldMap> = {
52
+ [K in keyof Fields]: Fields[K] extends AnyColumnDefinition
53
+ ? IsNullable<Fields[K]> extends true ? K : never
54
+ : never
55
+ }[keyof Fields]
56
+
57
+ type TupleFromColumns<Columns> = Columns extends readonly [infer Head extends string, ...infer Tail extends string[]]
58
+ ? readonly [Head, ...Tail]
59
+ : Columns extends readonly string[]
60
+ ? Columns extends readonly [string, ...string[]]
61
+ ? Columns
62
+ : never
63
+ : Columns extends string
64
+ ? readonly [Columns]
65
+ : never
66
+
67
+ type AssertKnownColumns<Fields extends TableFieldMap, Columns extends readonly string[]> = Exclude<
68
+ Columns[number],
69
+ ColumnNameUnion<Fields>
70
+ > extends never
71
+ ? Columns
72
+ : never
73
+
74
+ type AssertPrimaryKeyColumns<
75
+ Fields extends TableFieldMap,
76
+ Columns extends readonly string[]
77
+ > = Extract<Columns[number], NullableColumnNames<Fields>> extends never
78
+ ? Columns
79
+ : never
80
+
81
+ type InlinePrimaryKeyKeys<Fields extends TableFieldMap> = Extract<{
82
+ [K in keyof Fields]: Fields[K] extends AnyColumnDefinition
83
+ ? Fields[K]["metadata"]["primaryKey"] extends true ? K : never
84
+ : never
85
+ }[keyof Fields], string>
86
+
87
+ /** Normalizes a string or tuple input into a non-empty column list. */
88
+ export const normalizeColumnList = (columns: string | readonly string[]): ColumnList => {
89
+ const normalized = Array.isArray(columns) ? [...columns] : [columns]
90
+ if (normalized.length === 0) {
91
+ throw new Error("Table options require at least one column")
92
+ }
93
+ return normalized as unknown as ColumnList
94
+ }
95
+
96
+ /** Converts inline column flags into normalized table option records. */
97
+ export const collectInlineOptions = <Fields extends TableFieldMap>(
98
+ fields: Fields
99
+ ): readonly TableOptionSpec[] => {
100
+ const options: TableOptionSpec[] = []
101
+ for (const [columnName, column] of Object.entries(fields)) {
102
+ if (column.metadata.primaryKey) {
103
+ options.push({
104
+ kind: "primaryKey",
105
+ columns: [columnName]
106
+ })
107
+ }
108
+ if (column.metadata.unique && !column.metadata.primaryKey) {
109
+ options.push({
110
+ kind: "unique",
111
+ columns: [columnName]
112
+ })
113
+ }
114
+ if (column.metadata.references) {
115
+ const local = [columnName] as ColumnList
116
+ options.push({
117
+ kind: "foreignKey",
118
+ columns: local,
119
+ references: () => {
120
+ const targetColumn = column.metadata.references.target() as AnyBoundColumn
121
+ const bound = targetColumn[BoundColumnTypeId]
122
+ return {
123
+ tableName: bound.baseTableName,
124
+ schemaName: bound.schemaName,
125
+ columns: [bound.columnName]
126
+ }
127
+ }
128
+ })
129
+ }
130
+ }
131
+ return options
132
+ }
133
+
134
+ /** Resolves the effective primary-key columns for a table. */
135
+ export const resolvePrimaryKeyColumns = <Fields extends TableFieldMap>(
136
+ fields: Fields,
137
+ declaredOptions: readonly TableOptionSpec[]
138
+ ): readonly (keyof Fields & string)[] => {
139
+ const inline = Object.entries(fields)
140
+ .filter(([, column]) => column.metadata.primaryKey)
141
+ .map(([key]) => key) as (keyof Fields & string)[]
142
+ const explicit = declaredOptions
143
+ .filter((option) => option.kind === "primaryKey")
144
+ .map((option) => option.columns)
145
+ if (explicit.length > 1) {
146
+ throw new Error("Only one primary key declaration is allowed")
147
+ }
148
+ if (explicit.length === 0) {
149
+ return inline
150
+ }
151
+ const tablePrimaryKey = [...explicit[0]!] as (keyof Fields & string)[]
152
+ if (inline.length > 0) {
153
+ const same =
154
+ inline.length === tablePrimaryKey.length &&
155
+ inline.every((column) => tablePrimaryKey.includes(column))
156
+ if (!same) {
157
+ throw new Error("Inline primary keys conflict with table-level primary key declaration")
158
+ }
159
+ }
160
+ return tablePrimaryKey
161
+ }
162
+
163
+ /** Validates that options reference known, legal columns for the table. */
164
+ export const validateOptions = <Fields extends TableFieldMap>(
165
+ tableName: string,
166
+ fields: Fields,
167
+ options: readonly TableOptionSpec[]
168
+ ): void => {
169
+ const knownColumns = new Set(Object.keys(fields))
170
+ for (const option of options) {
171
+ switch (option.kind) {
172
+ case "index":
173
+ case "primaryKey":
174
+ case "unique":
175
+ case "foreignKey": {
176
+ if (option.columns.length === 0) {
177
+ throw new Error(`Option '${option.kind}' on table '${tableName}' requires at least one column`)
178
+ }
179
+ for (const column of option.columns) {
180
+ if (!knownColumns.has(column)) {
181
+ throw new Error(`Unknown column '${column}' on table '${tableName}'`)
182
+ }
183
+ }
184
+ if (option.kind === "foreignKey") {
185
+ const reference = option.references()
186
+ if (reference.columns.length !== option.columns.length) {
187
+ throw new Error(`Foreign key on table '${tableName}' must reference the same number of columns`)
188
+ }
189
+ if (reference.knownColumns) {
190
+ const referenced = new Set(reference.knownColumns)
191
+ for (const column of reference.columns) {
192
+ if (!referenced.has(column)) {
193
+ throw new Error(`Unknown referenced column '${column}' on table '${reference.tableName}'`)
194
+ }
195
+ }
196
+ }
197
+ }
198
+ break
199
+ }
200
+ case "check": {
201
+ break
202
+ }
203
+ }
204
+ }
205
+ for (const column of resolvePrimaryKeyColumns(fields, options)) {
206
+ if (fields[column]!.metadata.nullable) {
207
+ throw new Error(`Primary key column '${String(column)}' cannot be nullable`)
208
+ }
209
+ }
210
+ }
211
+
212
+ /** Compile-time validation that option columns exist on the table. */
213
+ export type ValidateKnownColumns<
214
+ Fields extends TableFieldMap,
215
+ Columns extends readonly string[]
216
+ > = AssertKnownColumns<Fields, Columns>
217
+
218
+ /** Compile-time validation that primary-key columns are known and non-nullable. */
219
+ export type ValidatePrimaryKeyColumns<
220
+ Fields extends TableFieldMap,
221
+ Columns extends readonly string[]
222
+ > = AssertPrimaryKeyColumns<Fields, AssertKnownColumns<Fields, Columns>>
223
+
224
+ /** Normalizes a public column input into the internal tuple form. */
225
+ export type NormalizeColumns<Columns extends string | readonly string[]> = TupleFromColumns<Columns>