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,417 @@
1
+ import * as Schema from "effect/Schema"
2
+
3
+ import * as Expression from "./expression.js"
4
+ import {
5
+ LocalDateStringSchema,
6
+ DecimalStringSchema,
7
+ LocalDateTimeStringSchema,
8
+ type LocalDateString,
9
+ type DecimalString,
10
+ type LocalDateTimeString
11
+ } from "./runtime-value.js"
12
+ import {
13
+ type AnyBoundColumn,
14
+ type AnyColumnDefinition,
15
+ type BaseSelectType,
16
+ type BoundColumn,
17
+ ColumnTypeId,
18
+ type ColumnDefinition,
19
+ type ColumnReference,
20
+ type HasDefault,
21
+ type InsertType,
22
+ type IsGenerated,
23
+ type IsNullable,
24
+ type IsPrimaryKey,
25
+ makeColumnDefinition,
26
+ type ReferencesOf,
27
+ remapColumnDefinition,
28
+ type SelectType,
29
+ type UpdateType
30
+ } from "./column-state.js"
31
+
32
+ type CompatibleReference<
33
+ Self extends AnyColumnDefinition,
34
+ Target extends AnyBoundColumn
35
+ > = [BaseSelectType<Self>] extends [BaseSelectType<Target>]
36
+ ? [BaseSelectType<Target>] extends [BaseSelectType<Self>]
37
+ ? Self
38
+ : never
39
+ : never
40
+
41
+ type NullableSelect<Select> = Select | null
42
+
43
+ type NullableColumn<Column extends AnyColumnDefinition> = ColumnDefinition<
44
+ NullableSelect<SelectType<Column>>,
45
+ NullableSelect<InsertType<Column>>,
46
+ NullableSelect<UpdateType<Column>>,
47
+ Column[typeof ColumnTypeId]["dbType"],
48
+ true,
49
+ HasDefault<Column>,
50
+ IsGenerated<Column>,
51
+ IsPrimaryKey<Column>,
52
+ Column[typeof ColumnTypeId]["unique"],
53
+ ReferencesOf<Column>
54
+ >
55
+
56
+ type PrimaryKeyColumn<Column extends AnyColumnDefinition> = ColumnDefinition<
57
+ SelectType<Column>,
58
+ InsertType<Column>,
59
+ UpdateType<Column>,
60
+ Column[typeof ColumnTypeId]["dbType"],
61
+ false,
62
+ HasDefault<Column>,
63
+ IsGenerated<Column>,
64
+ true,
65
+ true,
66
+ ReferencesOf<Column>
67
+ >
68
+
69
+ type UniqueColumn<Column extends AnyColumnDefinition> = ColumnDefinition<
70
+ SelectType<Column>,
71
+ InsertType<Column>,
72
+ UpdateType<Column>,
73
+ Column[typeof ColumnTypeId]["dbType"],
74
+ IsNullable<Column>,
75
+ HasDefault<Column>,
76
+ IsGenerated<Column>,
77
+ IsPrimaryKey<Column>,
78
+ true,
79
+ ReferencesOf<Column>
80
+ >
81
+
82
+ type HasDefaultColumn<Column extends AnyColumnDefinition> = ColumnDefinition<
83
+ SelectType<Column>,
84
+ InsertType<Column>,
85
+ UpdateType<Column>,
86
+ Column[typeof ColumnTypeId]["dbType"],
87
+ IsNullable<Column>,
88
+ true,
89
+ IsGenerated<Column>,
90
+ IsPrimaryKey<Column>,
91
+ Column[typeof ColumnTypeId]["unique"],
92
+ ReferencesOf<Column>
93
+ >
94
+
95
+ type GeneratedColumn<Column extends AnyColumnDefinition> = ColumnDefinition<
96
+ SelectType<Column>,
97
+ InsertType<Column>,
98
+ UpdateType<Column>,
99
+ Column[typeof ColumnTypeId]["dbType"],
100
+ IsNullable<Column>,
101
+ false,
102
+ true,
103
+ IsPrimaryKey<Column>,
104
+ Column[typeof ColumnTypeId]["unique"],
105
+ ReferencesOf<Column>
106
+ >
107
+
108
+ type ReferencingColumn<
109
+ Column extends AnyColumnDefinition,
110
+ Target extends AnyBoundColumn
111
+ > = ColumnDefinition<
112
+ SelectType<Column>,
113
+ InsertType<Column>,
114
+ UpdateType<Column>,
115
+ Column[typeof ColumnTypeId]["dbType"],
116
+ IsNullable<Column>,
117
+ HasDefault<Column>,
118
+ IsGenerated<Column>,
119
+ IsPrimaryKey<Column>,
120
+ Column[typeof ColumnTypeId]["unique"],
121
+ ColumnReference<Target>
122
+ >
123
+
124
+ type SchemaCompatibleColumn<
125
+ Column extends AnyColumnDefinition,
126
+ SchemaType extends Schema.Schema.Any
127
+ > = [BaseSelectType<Column>] extends [Schema.Schema.Encoded<SchemaType>]
128
+ ? Column
129
+ : never
130
+
131
+ type ColumnSchemaOutput<
132
+ Column extends AnyColumnDefinition,
133
+ SchemaType extends Schema.Schema.Any
134
+ > = IsNullable<Column> extends true
135
+ ? Schema.Schema.Type<SchemaType> | null
136
+ : Schema.Schema.Type<SchemaType>
137
+
138
+ type ColumnWithSchema<
139
+ Column extends AnyColumnDefinition,
140
+ SchemaType extends Schema.Schema.Any
141
+ > = ColumnDefinition<
142
+ ColumnSchemaOutput<Column, SchemaType>,
143
+ ColumnSchemaOutput<Column, SchemaType>,
144
+ ColumnSchemaOutput<Column, SchemaType>,
145
+ Column[typeof ColumnTypeId]["dbType"],
146
+ IsNullable<Column>,
147
+ HasDefault<Column>,
148
+ IsGenerated<Column>,
149
+ IsPrimaryKey<Column>,
150
+ Column[typeof ColumnTypeId]["unique"],
151
+ ReferencesOf<Column>,
152
+ Column[typeof ColumnTypeId]["source"],
153
+ Column[typeof ColumnTypeId]["dependencies"]
154
+ >
155
+
156
+ const mapColumn = <
157
+ Column extends AnyColumnDefinition,
158
+ Next extends AnyColumnDefinition
159
+ >(
160
+ column: Column,
161
+ metadata: Next["metadata"]
162
+ ): Next => remapColumnDefinition(column as any, {
163
+ metadata
164
+ }) as Next
165
+
166
+ const primitive = <Type, Db extends Expression.DbType.Any>(
167
+ schema: Schema.Schema<Type, any, any>,
168
+ dbType: Db
169
+ ): ColumnDefinition<Type, Type, Type, Db, false, false, false, false, false, undefined> =>
170
+ makeColumnDefinition(schema as Schema.Schema<NonNullable<Type>>, {
171
+ dbType,
172
+ nullable: false,
173
+ hasDefault: false,
174
+ generated: false,
175
+ primaryKey: false,
176
+ unique: false,
177
+ references: undefined
178
+ })
179
+
180
+ type ColumnModule<
181
+ Dialect extends string,
182
+ UuidKind extends string,
183
+ TextKind extends string,
184
+ IntKind extends string,
185
+ NumberKind extends string,
186
+ BooleanKind extends string,
187
+ DateKind extends string,
188
+ TimestampKind extends string,
189
+ JsonKind extends string
190
+ > = {
191
+ readonly custom: <
192
+ SchemaType extends Schema.Schema.Any,
193
+ Db extends Expression.DbType.Any
194
+ >(
195
+ schema: SchemaType,
196
+ dbType: Db
197
+ ) => ColumnDefinition<
198
+ Schema.Schema.Type<SchemaType>,
199
+ Schema.Schema.Type<SchemaType>,
200
+ Schema.Schema.Type<SchemaType>,
201
+ Db,
202
+ false,
203
+ false,
204
+ false,
205
+ false,
206
+ false,
207
+ undefined
208
+ >
209
+ readonly uuid: () => ColumnDefinition<string, string, string, Expression.DbType.Base<Dialect, UuidKind>, false, false, false, false, false, undefined>
210
+ readonly text: () => ColumnDefinition<string, string, string, Expression.DbType.Base<Dialect, TextKind>, false, false, false, false, false, undefined>
211
+ readonly int: () => ColumnDefinition<number, number, number, Expression.DbType.Base<Dialect, IntKind>, false, false, false, false, false, undefined>
212
+ readonly number: () => ColumnDefinition<DecimalString, DecimalString, DecimalString, Expression.DbType.Base<Dialect, NumberKind>, false, false, false, false, false, undefined>
213
+ readonly boolean: () => ColumnDefinition<boolean, boolean, boolean, Expression.DbType.Base<Dialect, BooleanKind>, false, false, false, false, false, undefined>
214
+ readonly date: () => ColumnDefinition<LocalDateString, LocalDateString, LocalDateString, Expression.DbType.Base<Dialect, DateKind>, false, false, false, false, false, undefined>
215
+ readonly timestamp: () => ColumnDefinition<LocalDateTimeString, LocalDateTimeString, LocalDateTimeString, Expression.DbType.Base<Dialect, TimestampKind>, false, false, false, false, false, undefined>
216
+ readonly json: <SchemaType extends Schema.Schema.Any>(
217
+ schema: SchemaType
218
+ ) => ColumnDefinition<
219
+ Schema.Schema.Type<SchemaType>,
220
+ Schema.Schema.Type<SchemaType>,
221
+ Schema.Schema.Type<SchemaType>,
222
+ Expression.DbType.Json<Dialect, JsonKind>,
223
+ false,
224
+ false,
225
+ false,
226
+ false,
227
+ false,
228
+ undefined
229
+ >
230
+ }
231
+
232
+ const typeFactory = <Dialect extends string>(dialect: Dialect) =>
233
+ <Kind extends string>(kind: Kind): Expression.DbType.Base<Dialect, Kind> => ({
234
+ dialect,
235
+ kind
236
+ })
237
+
238
+ const makeColumnModule = <
239
+ Dialect extends string,
240
+ UuidKind extends string,
241
+ TextKind extends string,
242
+ IntKind extends string,
243
+ NumberKind extends string,
244
+ BooleanKind extends string,
245
+ DateKind extends string,
246
+ TimestampKind extends string,
247
+ JsonKind extends string
248
+ >(
249
+ dialect: Dialect,
250
+ kinds: {
251
+ readonly uuid: UuidKind
252
+ readonly text: TextKind
253
+ readonly int: IntKind
254
+ readonly number: NumberKind
255
+ readonly boolean: BooleanKind
256
+ readonly date: DateKind
257
+ readonly timestamp: TimestampKind
258
+ readonly json: JsonKind
259
+ }
260
+ ): ColumnModule<Dialect, UuidKind, TextKind, IntKind, NumberKind, BooleanKind, DateKind, TimestampKind, JsonKind> => {
261
+ const dialectType = typeFactory(dialect)
262
+ return {
263
+ custom: <SchemaType extends Schema.Schema.Any, Db extends Expression.DbType.Any>(
264
+ schema: SchemaType,
265
+ dbType: Db
266
+ ) =>
267
+ makeColumnDefinition(schema as unknown as Schema.Schema<NonNullable<Schema.Schema.Type<SchemaType>>, any, any>, {
268
+ dbType,
269
+ nullable: false,
270
+ hasDefault: false,
271
+ generated: false,
272
+ primaryKey: false,
273
+ unique: false,
274
+ references: undefined
275
+ }),
276
+ uuid: () => primitive(Schema.UUID, dialectType(kinds.uuid)),
277
+ text: () => primitive(Schema.String, dialectType(kinds.text)),
278
+ int: () => primitive(Schema.Int, dialectType(kinds.int)),
279
+ number: () => primitive(DecimalStringSchema, dialectType(kinds.number)),
280
+ boolean: () => primitive(Schema.Boolean, dialectType(kinds.boolean)),
281
+ date: () => primitive(LocalDateStringSchema, dialectType(kinds.date)),
282
+ timestamp: () => primitive(LocalDateTimeStringSchema, dialectType(kinds.timestamp)),
283
+ json: <SchemaType extends Schema.Schema.Any>(schema: SchemaType) =>
284
+ makeColumnDefinition(schema as unknown as Schema.Schema<NonNullable<Schema.Schema.Type<SchemaType>>, any, any>, {
285
+ dbType: {
286
+ ...dialectType(kinds.json),
287
+ variant: "json"
288
+ } as Expression.DbType.Json<Dialect, JsonKind>,
289
+ nullable: false,
290
+ hasDefault: false,
291
+ generated: false,
292
+ primaryKey: false,
293
+ unique: false,
294
+ references: undefined
295
+ })
296
+ }
297
+ }
298
+
299
+ /** Postgres-specialized column constructors. */
300
+ export const postgres = makeColumnModule("postgres", {
301
+ uuid: "uuid",
302
+ text: "text",
303
+ int: "int4",
304
+ number: "numeric",
305
+ boolean: "bool",
306
+ date: "date",
307
+ timestamp: "timestamp",
308
+ json: "json"
309
+ })
310
+
311
+ /** MySQL-specialized column constructors. */
312
+ export const mysql = makeColumnModule("mysql", {
313
+ uuid: "uuid",
314
+ text: "text",
315
+ int: "int",
316
+ number: "decimal",
317
+ boolean: "boolean",
318
+ date: "date",
319
+ timestamp: "timestamp",
320
+ json: "json"
321
+ })
322
+
323
+ /** Creates a Postgres `uuid` column. */
324
+ export const uuid = postgres.uuid
325
+ /** Creates a Postgres `text` column. */
326
+ export const text = postgres.text
327
+ /** Creates a Postgres `int4` column. */
328
+ export const int = postgres.int
329
+ /** Creates a Postgres `numeric` column decoded as `DecimalString`. */
330
+ export const number = postgres.number
331
+ /** Creates a Postgres `bool` column. */
332
+ export const boolean = postgres.boolean
333
+ /** Creates a Postgres `date` column decoded as `LocalDateString`. */
334
+ export const date = postgres.date
335
+ /** Creates a Postgres `timestamp` column decoded as `LocalDateTimeString`. */
336
+ export const timestamp = postgres.timestamp
337
+
338
+ /** Creates a Postgres `json` column backed by an arbitrary Effect schema. */
339
+ export const json = postgres.json
340
+ /** Creates a Postgres column backed by an arbitrary SQL type and Effect schema. */
341
+ export const custom = postgres.custom
342
+
343
+ /** Replaces a column's runtime schema while preserving its SQL type metadata. */
344
+ export const schema = <SchemaType extends Schema.Schema.Any>(nextSchema: SchemaType) =>
345
+ <Column extends AnyColumnDefinition>(
346
+ column: SchemaCompatibleColumn<Column, SchemaType>
347
+ ): ColumnWithSchema<Column, SchemaType> =>
348
+ remapColumnDefinition(column as AnyColumnDefinition, {
349
+ schema: nextSchema
350
+ }) as ColumnWithSchema<Column, SchemaType>
351
+
352
+ /** Marks a column as nullable. Nullable columns decode as `T | null`. */
353
+ export const nullable = <Column extends AnyColumnDefinition>(
354
+ column: Column[typeof ColumnTypeId]["primaryKey"] extends true ? never : Column
355
+ ): NullableColumn<Column> =>
356
+ mapColumn(column, {
357
+ ...column.metadata,
358
+ nullable: true
359
+ })
360
+
361
+ /** Marks a column as a primary key. Primary keys are always unique and non-null. */
362
+ export const primaryKey = <Column extends AnyColumnDefinition>(
363
+ column: Column[typeof ColumnTypeId]["nullable"] extends true ? never : Column
364
+ ): PrimaryKeyColumn<Column> =>
365
+ mapColumn(column, {
366
+ ...column.metadata,
367
+ nullable: false,
368
+ primaryKey: true,
369
+ unique: true
370
+ })
371
+
372
+ /** Marks a column as unique. */
373
+ export const unique = <Column extends AnyColumnDefinition>(
374
+ column: Column
375
+ ): UniqueColumn<Column> =>
376
+ mapColumn(column, {
377
+ ...column.metadata,
378
+ unique: true
379
+ })
380
+
381
+ /** Marks a column as having a server-side default and therefore optional on insert. */
382
+ export const hasDefault = <Column extends AnyColumnDefinition>(
383
+ column: Column[typeof ColumnTypeId]["generated"] extends true ? never : Column
384
+ ): HasDefaultColumn<Column> =>
385
+ mapColumn(column, {
386
+ ...column.metadata,
387
+ hasDefault: true
388
+ })
389
+
390
+ /** Marks a column as generated by the database and omitted from insert/update. */
391
+ export const generated = <Column extends AnyColumnDefinition>(
392
+ column: Column[typeof ColumnTypeId]["hasDefault"] extends true ? never : Column
393
+ ): GeneratedColumn<Column> =>
394
+ mapColumn(column, {
395
+ ...column.metadata,
396
+ generated: true,
397
+ hasDefault: false
398
+ })
399
+
400
+ /**
401
+ * Attaches a lazy foreign-key reference to another bound column.
402
+ *
403
+ * The base, non-null select types must match.
404
+ */
405
+ export const references = <Target extends AnyBoundColumn>(target: () => Target) =>
406
+ <Column extends AnyColumnDefinition>(
407
+ column: CompatibleReference<Column, Target>
408
+ ): ReferencingColumn<Column, Target> =>
409
+ mapColumn(column, {
410
+ ...column.metadata,
411
+ references: { target }
412
+ })
413
+
414
+ /** Convenience alias for any column definition. */
415
+ export type Any = AnyColumnDefinition
416
+ /** Convenience alias for any bound column. */
417
+ export type AnyBound = BoundColumn<any, any, any, any, any, any, any, any, any, any, any, any>
@@ -0,0 +1,44 @@
1
+ import type * as Expression from ".././expression.js"
2
+ import type { DatatypeKindSpec } from "./shape.js"
3
+
4
+ export type DatatypeModule<
5
+ Dialect extends string,
6
+ Kinds extends Record<string, DatatypeKindSpec>,
7
+ Aliases extends Record<string, string> = Record<never, never>
8
+ > = {
9
+ readonly custom: <Kind extends string>(kind: Kind) => Expression.DbType.Base<Dialect, Kind>
10
+ } & {
11
+ readonly [Kind in keyof Kinds]: () => Expression.DbType.Base<Dialect, Kind & string>
12
+ } & {
13
+ readonly [Alias in keyof Aliases]: () => Expression.DbType.Base<Dialect, Aliases[Alias] & string>
14
+ }
15
+
16
+ export const makeDatatypeModule = <
17
+ Dialect extends string,
18
+ Kinds extends Record<string, DatatypeKindSpec>,
19
+ Aliases extends Record<string, string> = Record<never, never>
20
+ >(
21
+ dialect: Dialect,
22
+ kinds: Kinds,
23
+ aliases?: Aliases
24
+ ): DatatypeModule<Dialect, Kinds, Aliases> => {
25
+ const module: Record<string, (...args: readonly any[]) => Expression.DbType.Base<Dialect, string>> = {
26
+ custom: (kind: string) => ({
27
+ dialect,
28
+ kind
29
+ })
30
+ }
31
+ for (const kind of Object.keys(kinds)) {
32
+ module[kind] = () => ({
33
+ dialect,
34
+ kind
35
+ })
36
+ }
37
+ for (const [alias, kind] of Object.entries(aliases ?? {})) {
38
+ module[alias] = () => ({
39
+ dialect,
40
+ kind
41
+ })
42
+ }
43
+ return module as DatatypeModule<Dialect, Kinds, Aliases>
44
+ }