effect-qb 0.12.3 → 0.14.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 (65) hide show
  1. package/README.md +6 -1283
  2. package/dist/mysql.js +6376 -4978
  3. package/dist/postgres/metadata.js +2724 -0
  4. package/dist/postgres.js +5475 -3636
  5. package/package.json +13 -8
  6. package/src/internal/column-state.ts +88 -6
  7. package/src/internal/column.ts +569 -34
  8. package/src/internal/datatypes/define.ts +0 -30
  9. package/src/internal/executor.ts +45 -11
  10. package/src/internal/expression-ast.ts +15 -0
  11. package/src/internal/expression.ts +3 -1
  12. package/src/internal/implication-runtime.ts +171 -0
  13. package/src/internal/mysql-query.ts +7173 -0
  14. package/src/internal/mysql-renderer.ts +2 -2
  15. package/src/internal/plan.ts +14 -4
  16. package/src/internal/{query-factory.ts → postgres-query.ts} +669 -230
  17. package/src/internal/postgres-renderer.ts +2 -2
  18. package/src/internal/postgres-schema-model.ts +144 -0
  19. package/src/internal/predicate-analysis.ts +10 -0
  20. package/src/internal/predicate-context.ts +112 -36
  21. package/src/internal/predicate-formula.ts +31 -19
  22. package/src/internal/predicate-normalize.ts +177 -106
  23. package/src/internal/predicate-runtime.ts +676 -0
  24. package/src/internal/query.ts +471 -41
  25. package/src/internal/renderer.ts +2 -2
  26. package/src/internal/runtime-schema.ts +74 -20
  27. package/src/internal/schema-ddl.ts +55 -0
  28. package/src/internal/schema-derivation.ts +93 -21
  29. package/src/internal/schema-expression.ts +44 -0
  30. package/src/internal/sql-expression-renderer.ts +123 -35
  31. package/src/internal/table-options.ts +88 -7
  32. package/src/internal/table.ts +106 -42
  33. package/src/mysql/column.ts +3 -1
  34. package/src/mysql/datatypes/index.ts +17 -2
  35. package/src/mysql/executor.ts +20 -17
  36. package/src/mysql/function/aggregate.ts +6 -0
  37. package/src/mysql/function/core.ts +5 -0
  38. package/src/mysql/function/index.ts +20 -0
  39. package/src/mysql/function/json.ts +4 -0
  40. package/src/mysql/function/string.ts +6 -0
  41. package/src/mysql/function/temporal.ts +103 -0
  42. package/src/mysql/function/window.ts +7 -0
  43. package/src/mysql/private/query.ts +1 -0
  44. package/src/mysql/query.ts +6 -26
  45. package/src/mysql.ts +2 -0
  46. package/src/postgres/cast.ts +31 -0
  47. package/src/postgres/column.ts +27 -1
  48. package/src/postgres/datatypes/index.ts +40 -5
  49. package/src/postgres/executor.ts +19 -17
  50. package/src/postgres/function/aggregate.ts +6 -0
  51. package/src/postgres/function/core.ts +16 -0
  52. package/src/postgres/function/index.ts +20 -0
  53. package/src/postgres/function/json.ts +501 -0
  54. package/src/postgres/function/string.ts +6 -0
  55. package/src/postgres/function/temporal.ts +107 -0
  56. package/src/postgres/function/window.ts +7 -0
  57. package/src/postgres/metadata.ts +31 -0
  58. package/src/postgres/private/query.ts +1 -0
  59. package/src/postgres/query.ts +6 -28
  60. package/src/postgres/schema-expression.ts +16 -0
  61. package/src/postgres/schema-management.ts +204 -0
  62. package/src/postgres/schema.ts +35 -0
  63. package/src/postgres/table.ts +307 -41
  64. package/src/postgres/type.ts +4 -0
  65. package/src/postgres.ts +16 -0
@@ -53,20 +53,26 @@ type SchemaNameOfTable<Table> = Table extends BaseTable.TableDefinition<any, any
53
53
  ? SchemaName
54
54
  : never
55
55
 
56
- export type TableSchemaNamespace<SchemaName extends string> = {
57
- readonly schemaName: SchemaName
58
- readonly table: <
59
- Name extends string,
60
- Fields extends DialectFieldMap,
61
- PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>
62
- >(
63
- name: Name,
64
- fields: Fields,
65
- ...options: BaseTable.DeclaredTableOptions
66
- ) => TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>
56
+ export type TableOption = BaseTable.TableOption
57
+ export type DdlExpressionLike = BaseTable.DdlExpressionLike
58
+ export type IndexKey = BaseTable.IndexKeySpec
59
+ export type ReferentialAction = BaseTable.ReferentialAction
60
+
61
+ type SchemaTable = {
62
+ readonly columns: Record<string, unknown>
67
63
  }
68
64
 
69
- export type TableOption = BaseTable.TableOption
65
+ type TableExpressionFactory<Table extends SchemaTable> = (
66
+ columns: Table["columns"]
67
+ ) => DdlExpressionLike
68
+
69
+ type TableScopedOptionBuilder<
70
+ Table extends SchemaTable,
71
+ Spec extends import("../internal/table-options.js").TableOptionSpec = import("../internal/table-options.js").TableOptionSpec
72
+ > = {
73
+ (table: Table): Table
74
+ readonly option: Spec
75
+ }
70
76
 
71
77
  export const TypeId = BaseTable.TypeId
72
78
  export const OptionsSymbol = BaseTable.OptionsSymbol
@@ -83,26 +89,6 @@ export const make = <
83
89
  ): TableDefinition<Name, Fields> =>
84
90
  BaseTable.make(name, fields, schemaName) as TableDefinition<Name, Fields>
85
91
 
86
- export const schema = <SchemaName extends string>(
87
- schemaName: SchemaName
88
- ): TableSchemaNamespace<SchemaName> => ({
89
- schemaName,
90
- table: <
91
- Name extends string,
92
- Fields extends DialectFieldMap,
93
- PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>
94
- >(
95
- name: Name,
96
- fields: Fields,
97
- ...declaredOptions: BaseTable.DeclaredTableOptions
98
- ) =>
99
- BaseTable.schema(schemaName).table(
100
- name,
101
- fields,
102
- ...declaredOptions
103
- ) as TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>
104
- })
105
-
106
92
  export const alias = <
107
93
  Table extends AnyTable,
108
94
  AliasName extends string
@@ -136,21 +122,301 @@ export const Class = <Self = never, SchemaName extends string | undefined = "pub
136
122
  : TableClassStatic<typeof name, Fields, InlinePrimaryKeyKeys<Fields>, SchemaName>
137
123
  }
138
124
 
139
- export const primaryKey = BaseTable.primaryKey
140
- export const unique = BaseTable.unique
141
- export const index = BaseTable.index
142
- export const foreignKey = <
125
+ export const option = BaseTable.option
126
+
127
+ type RichPrimaryKeyInput<Columns extends string | readonly string[]> = {
128
+ readonly columns: Columns
129
+ readonly name?: string
130
+ readonly deferrable?: boolean
131
+ readonly initiallyDeferred?: boolean
132
+ }
133
+
134
+ type RichUniqueInput<Columns extends string | readonly string[]> = {
135
+ readonly columns: Columns
136
+ readonly name?: string
137
+ readonly nullsNotDistinct?: boolean
138
+ readonly deferrable?: boolean
139
+ readonly initiallyDeferred?: boolean
140
+ }
141
+
142
+ type RichIndexKeyInput =
143
+ | {
144
+ readonly column: string
145
+ readonly order?: "asc" | "desc"
146
+ readonly nulls?: "first" | "last"
147
+ }
148
+ | {
149
+ readonly expression: DdlExpressionLike
150
+ readonly order?: "asc" | "desc"
151
+ readonly nulls?: "first" | "last"
152
+ }
153
+
154
+ type RichIndexInput<Columns extends string | readonly string[] = string | readonly string[]> = {
155
+ readonly columns?: Columns
156
+ readonly keys?: readonly [RichIndexKeyInput, ...RichIndexKeyInput[]]
157
+ readonly name?: string
158
+ readonly unique?: boolean
159
+ readonly method?: string
160
+ readonly include?: readonly string[]
161
+ readonly predicate?: DdlExpressionLike
162
+ }
163
+
164
+ type RichForeignKeyInput<
143
165
  LocalColumns extends string | readonly string[],
144
166
  TargetTable extends AnyTable,
145
167
  TargetColumns extends string | readonly string[]
168
+ > = {
169
+ readonly columns: LocalColumns
170
+ readonly target: () => TargetTable
171
+ readonly referencedColumns: TargetColumns
172
+ readonly name?: string
173
+ readonly onUpdate?: ReferentialAction
174
+ readonly onDelete?: ReferentialAction
175
+ readonly deferrable?: boolean
176
+ readonly initiallyDeferred?: boolean
177
+ }
178
+
179
+ type RichCheckInput<Name extends string = string> = {
180
+ readonly name: Name
181
+ readonly predicate: DdlExpressionLike
182
+ readonly noInherit?: boolean
183
+ }
184
+
185
+ const isObject = (value: unknown): value is Record<string, unknown> =>
186
+ typeof value === "object" && value !== null && !Array.isArray(value)
187
+
188
+ const isTableExpressionFactory = (value: unknown): value is TableExpressionFactory<SchemaTable> =>
189
+ typeof value === "function"
190
+
191
+ const makeTableScopedOption = <
192
+ Table extends SchemaTable,
193
+ Spec extends import("../internal/table-options.js").TableOptionSpec
194
+ >(
195
+ placeholder: Spec,
196
+ resolve: (table: Table) => Spec
197
+ ): TableScopedOptionBuilder<Table, Spec> => {
198
+ const builder = ((table: Table) =>
199
+ BaseTable.option(resolve(table))(table as never)) as unknown as TableScopedOptionBuilder<Table, Spec>
200
+ ;(builder as { option: Spec }).option = placeholder
201
+ return builder
202
+ }
203
+
204
+ const normalizeColumns = (columns: string | readonly string[]): readonly [string, ...string[]] =>
205
+ (Array.isArray(columns) ? [...columns] : [columns]) as unknown as readonly [string, ...string[]]
206
+
207
+ const normalizeIndexKeys = (
208
+ keys: readonly [RichIndexKeyInput, ...RichIndexKeyInput[]]
209
+ ): readonly [BaseTable.IndexKeySpec, ...BaseTable.IndexKeySpec[]] =>
210
+ keys.map((key) => "expression" in key
211
+ ? {
212
+ kind: "expression",
213
+ expression: key.expression,
214
+ order: key.order,
215
+ nulls: key.nulls
216
+ }
217
+ : {
218
+ kind: "column",
219
+ column: key.column,
220
+ order: key.order,
221
+ nulls: key.nulls
222
+ }) as unknown as readonly [BaseTable.IndexKeySpec, ...BaseTable.IndexKeySpec[]]
223
+
224
+ export const primaryKey: {
225
+ <const Columns extends string | readonly string[]>(
226
+ columns: Columns
227
+ ): BaseTable.TableOption<{
228
+ readonly kind: "primaryKey"
229
+ readonly columns: BaseTable.NormalizeColumns<Columns>
230
+ }>
231
+ <const Columns extends string | readonly string[]>(
232
+ spec: RichPrimaryKeyInput<Columns>
233
+ ): BaseTable.TableOption
234
+ } = ((input: unknown) =>
235
+ isObject(input) && "columns" in input
236
+ ? BaseTable.option({
237
+ kind: "primaryKey",
238
+ columns: normalizeColumns((input as RichPrimaryKeyInput<string | readonly string[]>).columns),
239
+ name: (input as RichPrimaryKeyInput<string | readonly string[]>).name,
240
+ deferrable: (input as RichPrimaryKeyInput<string | readonly string[]>).deferrable,
241
+ initiallyDeferred: (input as RichPrimaryKeyInput<string | readonly string[]>).initiallyDeferred
242
+ })
243
+ : BaseTable.primaryKey(input as string | readonly string[])) as never
244
+
245
+ export const unique: {
246
+ <const Columns extends string | readonly string[]>(
247
+ columns: Columns
248
+ ): BaseTable.TableOption<{
249
+ readonly kind: "unique"
250
+ readonly columns: BaseTable.NormalizeColumns<Columns>
251
+ }>
252
+ <const Columns extends string | readonly string[]>(
253
+ spec: RichUniqueInput<Columns>
254
+ ): BaseTable.TableOption
255
+ } = ((input: unknown) =>
256
+ isObject(input) && "columns" in input && ("name" in input || "nullsNotDistinct" in input || "deferrable" in input || "initiallyDeferred" in input)
257
+ ? BaseTable.option({
258
+ kind: "unique",
259
+ columns: normalizeColumns((input as RichUniqueInput<string | readonly string[]>).columns),
260
+ name: (input as RichUniqueInput<string | readonly string[]>).name,
261
+ nullsNotDistinct: (input as RichUniqueInput<string | readonly string[]>).nullsNotDistinct,
262
+ deferrable: (input as RichUniqueInput<string | readonly string[]>).deferrable,
263
+ initiallyDeferred: (input as RichUniqueInput<string | readonly string[]>).initiallyDeferred
264
+ })
265
+ : BaseTable.unique(input as string | readonly string[])) as never
266
+
267
+ export const index: {
268
+ <const Columns extends string | readonly string[]>(
269
+ columns: Columns
270
+ ): BaseTable.TableOption<{
271
+ readonly kind: "index"
272
+ readonly columns: BaseTable.NormalizeColumns<Columns>
273
+ }>
274
+ <Table extends SchemaTable, const Columns extends string | readonly string[]>(
275
+ spec: Omit<RichIndexInput<Columns>, "predicate"> & {
276
+ readonly predicate: TableExpressionFactory<Table>
277
+ }
278
+ ): TableScopedOptionBuilder<Table, {
279
+ readonly kind: "index"
280
+ readonly columns?: BaseTable.NormalizeColumns<Columns>
281
+ readonly keys?: readonly [BaseTable.IndexKeySpec, ...BaseTable.IndexKeySpec[]]
282
+ readonly name?: string
283
+ readonly unique?: boolean
284
+ readonly method?: string
285
+ readonly include?: readonly string[]
286
+ readonly predicate?: DdlExpressionLike
287
+ }>
288
+ <const Columns extends string | readonly string[]>(
289
+ spec: RichIndexInput<Columns>
290
+ ): BaseTable.TableOption
291
+ } = ((input: unknown) =>
292
+ isObject(input) && ("keys" in input || "name" in input || "unique" in input || "method" in input || "include" in input || "predicate" in input)
293
+ ? (() => {
294
+ const spec = input as RichIndexInput<string | readonly string[]> & {
295
+ readonly predicate?: DdlExpressionLike | TableExpressionFactory<SchemaTable>
296
+ }
297
+ const predicate = spec.predicate
298
+ const placeholder = {
299
+ kind: "index" as const,
300
+ columns: spec.columns === undefined
301
+ ? undefined
302
+ : normalizeColumns(spec.columns),
303
+ keys: spec.keys === undefined
304
+ ? undefined
305
+ : normalizeIndexKeys(spec.keys),
306
+ name: spec.name,
307
+ unique: spec.unique,
308
+ method: spec.method,
309
+ include: spec.include,
310
+ predicate: predicate as unknown as DdlExpressionLike
311
+ }
312
+ return isTableExpressionFactory(predicate)
313
+ ? makeTableScopedOption(placeholder, (table) => ({
314
+ ...placeholder,
315
+ predicate: predicate(table.columns)
316
+ }))
317
+ : BaseTable.option({
318
+ ...placeholder,
319
+ predicate
320
+ })
321
+ })()
322
+ : BaseTable.index(input as string | readonly string[])) as never
323
+ export const foreignKey = <
324
+ const LocalColumns extends string | readonly string[],
325
+ TargetTable extends AnyTable,
326
+ const TargetColumns extends string | readonly string[]
146
327
  >(
147
- columns: LocalColumns,
148
- target: () => TargetTable,
149
- referencedColumns: TargetColumns
328
+ columnsOrSpec: LocalColumns | RichForeignKeyInput<LocalColumns, TargetTable, TargetColumns>,
329
+ target?: () => TargetTable,
330
+ referencedColumns?: TargetColumns
150
331
  ): BaseTable.TableOption =>
151
- BaseTable.foreignKey(columns, target as () => BaseTable.AnyTable, referencedColumns)
332
+ isObject(columnsOrSpec) && "columns" in columnsOrSpec && "target" in columnsOrSpec
333
+ ? (() => {
334
+ const spec = columnsOrSpec as RichForeignKeyInput<LocalColumns, TargetTable, TargetColumns>
335
+ const targetTable = spec.target() as BaseTable.AnyTable
336
+ const targetState = targetTable[BaseTable.TypeId]
337
+ return BaseTable.option({
338
+ kind: "foreignKey",
339
+ columns: normalizeColumns(spec.columns),
340
+ name: spec.name,
341
+ references: () => ({
342
+ tableName: targetState.baseName,
343
+ schemaName: targetState.schemaName,
344
+ columns: normalizeColumns(spec.referencedColumns),
345
+ knownColumns: Object.keys(targetState.fields)
346
+ }),
347
+ onUpdate: spec.onUpdate,
348
+ onDelete: spec.onDelete,
349
+ deferrable: spec.deferrable,
350
+ initiallyDeferred: spec.initiallyDeferred
351
+ })
352
+ })()
353
+ : BaseTable.foreignKey(
354
+ columnsOrSpec as LocalColumns,
355
+ target as () => BaseTable.AnyTable,
356
+ referencedColumns as TargetColumns
357
+ )
152
358
 
153
- export const check = BaseTable.check
359
+ export const check: {
360
+ <Name extends string>(name: Name, predicate: DdlExpressionLike): BaseTable.TableOption
361
+ <Name extends string, Table extends SchemaTable>(
362
+ name: Name,
363
+ predicate: TableExpressionFactory<Table>
364
+ ): TableScopedOptionBuilder<Table, {
365
+ readonly kind: "check"
366
+ readonly name: Name
367
+ readonly predicate: DdlExpressionLike
368
+ }>
369
+ <Name extends string, Table extends SchemaTable>(
370
+ spec: Omit<RichCheckInput<Name>, "predicate"> & {
371
+ readonly predicate: TableExpressionFactory<Table>
372
+ }
373
+ ): TableScopedOptionBuilder<Table, {
374
+ readonly kind: "check"
375
+ readonly name: Name
376
+ readonly predicate: DdlExpressionLike
377
+ readonly noInherit?: boolean
378
+ }>
379
+ <Name extends string>(spec: RichCheckInput<Name>): BaseTable.TableOption
380
+ } = ((nameOrSpec: string | RichCheckInput, predicate?: DdlExpressionLike) =>
381
+ isObject(nameOrSpec)
382
+ ? (() => {
383
+ const spec = nameOrSpec as RichCheckInput & {
384
+ readonly predicate: DdlExpressionLike | TableExpressionFactory<SchemaTable>
385
+ }
386
+ const specPredicate = spec.predicate
387
+ const placeholder = {
388
+ kind: "check" as const,
389
+ name: spec.name,
390
+ predicate: specPredicate as unknown as DdlExpressionLike,
391
+ noInherit: spec.noInherit
392
+ }
393
+ return isTableExpressionFactory(specPredicate)
394
+ ? makeTableScopedOption(placeholder, (table) => ({
395
+ ...placeholder,
396
+ predicate: specPredicate(table.columns)
397
+ }))
398
+ : BaseTable.option({
399
+ ...placeholder,
400
+ predicate: specPredicate
401
+ })
402
+ })()
403
+ : (() => {
404
+ const predicateOrFactory = predicate as DdlExpressionLike | TableExpressionFactory<SchemaTable>
405
+ const placeholder = {
406
+ kind: "check" as const,
407
+ name: nameOrSpec,
408
+ predicate: predicateOrFactory as unknown as DdlExpressionLike
409
+ }
410
+ return isTableExpressionFactory(predicateOrFactory)
411
+ ? makeTableScopedOption(placeholder, (table) => ({
412
+ ...placeholder,
413
+ predicate: predicateOrFactory(table.columns)
414
+ }))
415
+ : BaseTable.option({
416
+ ...placeholder,
417
+ predicate: predicateOrFactory
418
+ })
419
+ })()) as never
154
420
 
155
421
  export type SelectOf<Table extends { readonly schemas: { readonly select: Schema.Schema<any> } }> = BaseTable.SelectOf<Table>
156
422
  export type InsertOf<Table extends { readonly schemas: { readonly insert: Schema.Schema<any> } }> = BaseTable.InsertOf<Table>
@@ -0,0 +1,4 @@
1
+ import { postgresQuery } from "./private/query.js"
2
+
3
+ /** Postgres database-type constructors for casts and typed column references. */
4
+ export const type = postgresQuery.type
package/src/postgres.ts CHANGED
@@ -6,12 +6,28 @@ export * as Datatypes from "./postgres/datatypes/index.js"
6
6
  export * as Errors from "./postgres/errors/index.js"
7
7
  /** Shared scalar SQL expression interfaces and DB-type descriptors. */
8
8
  export * as Expression from "./internal/expression.js"
9
+ /** Postgres cast helpers. */
10
+ export { cast as Cast } from "./postgres/cast.js"
11
+ /** Postgres-specialized SQL function expressions. */
12
+ export * as Function from "./postgres/function/index.js"
9
13
  /** Postgres-specialized typed query execution contracts. */
10
14
  export * as Executor from "./postgres/executor.js"
11
15
  /** Shared logical query-plan interfaces. */
12
16
  export * as Plan from "./internal/plan.js"
13
17
  /** Postgres-specialized query-construction DSL. */
14
18
  export * as Query from "./postgres/query.js"
19
+ /** Postgres database-type constructors for casts and typed references. */
20
+ export { type as Type } from "./postgres/type.js"
21
+ /** Postgres normalized table/enum metadata helpers. */
22
+ export * as Metadata from "./postgres/metadata.js"
23
+ /** Postgres schema-expression helpers for DDL-only metadata. */
24
+ export * as SchemaExpression from "./postgres/schema-expression.js"
25
+ /** Postgres schema-scoped table and enum builder helpers. */
26
+ export { schema } from "./postgres/schema.js"
27
+ export type { SchemaNamespace } from "./postgres/schema.js"
28
+ /** Postgres enum and sequence definition helpers. */
29
+ export { enumType as enum, sequence } from "./postgres/schema-management.js"
30
+ export type { EnumDefinition, SequenceDefinition } from "./postgres/schema-management.js"
15
31
  /** Postgres-specialized table-definition DSL. */
16
32
  export * as Table from "./postgres/table.js"
17
33
  /** Postgres-specialized built-in renderer entrypoint. */