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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "effect-qb",
3
- "version": "0.12.3",
3
+ "version": "0.14.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -12,31 +12,36 @@
12
12
  "types": "./src/postgres.ts",
13
13
  "import": "./dist/postgres.js"
14
14
  },
15
+ "./postgres/metadata": {
16
+ "types": "./src/postgres/metadata.ts",
17
+ "import": "./dist/postgres/metadata.js"
18
+ },
15
19
  "./mysql": {
16
20
  "types": "./src/mysql.ts",
17
21
  "import": "./dist/mysql.js"
18
22
  },
19
23
  "./package.json": "./package.json"
20
24
  },
25
+ "imports": {
26
+ "#postgres": "./src/postgres.ts",
27
+ "#mysql": "./src/mysql.ts",
28
+ "#internal/*": "./src/internal/*"
29
+ },
21
30
  "publishConfig": {
22
31
  "access": "public"
23
32
  },
24
33
  "scripts": {
25
- "build": "bun scripts/build-package.ts",
26
- "test": "bun test",
27
- "test:types": "bunx tsgo -p tsconfig.type-tests.json",
28
- "test:integration": "bun scripts/test-integration.ts",
34
+ "build": "bun scripts/build.ts",
29
35
  "prepack": "bun run build"
30
36
  },
31
37
  "devDependencies": {
32
- "@effect/sql-mysql2": "0.48.0",
33
- "@effect/sql-pg": "0.48.0",
34
38
  "@types/bun": "latest",
35
39
  "@typescript/native-preview": "latest"
36
40
  },
37
41
  "dependencies": {
38
42
  "@effect/experimental": "^0.57.0",
39
43
  "@effect/sql": "^0.48.0",
40
- "effect": "^3.19.3"
44
+ "effect": "^3.19.3",
45
+ "pgsql-ast-parser": "^12.0.2"
41
46
  }
42
47
  }
@@ -1,8 +1,10 @@
1
+ import type * as Brand from "effect/Brand"
1
2
  import { pipeArguments, type Pipeable } from "effect/Pipeable"
2
3
  import * as Schema from "effect/Schema"
3
4
 
4
5
  import * as Expression from "./expression.js"
5
6
  import * as ExpressionAst from "./expression-ast.js"
7
+ import type * as SchemaExpression from "./schema-expression.js"
6
8
 
7
9
  /** Symbol used to attach column-definition metadata. */
8
10
  export const ColumnTypeId: unique symbol = Symbol.for("effect-qb/Column")
@@ -12,9 +14,34 @@ export const BoundColumnTypeId: unique symbol = Symbol.for("effect-qb/BoundColum
12
14
  export type ColumnTypeId = typeof ColumnTypeId
13
15
  export type BoundColumnTypeId = typeof BoundColumnTypeId
14
16
 
17
+ export type DdlExpression = Expression.Any | SchemaExpression.Any
18
+
15
19
  /** Lazy reference to another bound column. */
16
20
  export interface ColumnReference<Target = unknown> {
17
21
  readonly target: () => Target
22
+ readonly name?: string
23
+ readonly onUpdate?: "noAction" | "restrict" | "cascade" | "setNull" | "setDefault"
24
+ readonly onDelete?: "noAction" | "restrict" | "cascade" | "setNull" | "setDefault"
25
+ readonly deferrable?: boolean
26
+ readonly initiallyDeferred?: boolean
27
+ }
28
+
29
+ /** Inline single-column index metadata. */
30
+ export interface ColumnIndexOptions {
31
+ readonly name?: string
32
+ readonly method?: string
33
+ readonly include?: readonly string[]
34
+ readonly predicate?: DdlExpression
35
+ readonly order?: "asc" | "desc"
36
+ readonly nulls?: "first" | "last"
37
+ }
38
+
39
+ /** Inline single-column unique-constraint metadata. */
40
+ export interface ColumnUniqueOptions {
41
+ readonly name?: string
42
+ readonly nullsNotDistinct?: boolean
43
+ readonly deferrable?: boolean
44
+ readonly initiallyDeferred?: boolean
18
45
  }
19
46
 
20
47
  /** Complete static state tracked for a column definition. */
@@ -42,6 +69,20 @@ export interface ColumnState<
42
69
  readonly primaryKey: PrimaryKey
43
70
  readonly unique: Unique
44
71
  readonly references: Ref
72
+ readonly brand?: true
73
+ readonly index?: ColumnIndexOptions
74
+ readonly uniqueConstraint?: ColumnUniqueOptions
75
+ readonly defaultValue?: DdlExpression
76
+ readonly generatedValue?: DdlExpression
77
+ readonly ddlType?: string
78
+ readonly identity?: {
79
+ readonly generation: "always" | "byDefault"
80
+ }
81
+ readonly enum?: {
82
+ readonly name: string
83
+ readonly schemaName?: string
84
+ readonly values: readonly [string, ...string[]]
85
+ }
45
86
  readonly source: Source
46
87
  readonly dependencies: Dependencies
47
88
  }
@@ -93,6 +134,20 @@ export interface ColumnDefinition<
93
134
  readonly primaryKey: PrimaryKey
94
135
  readonly unique: Unique
95
136
  readonly references: Ref
137
+ readonly brand?: true
138
+ readonly index?: ColumnIndexOptions
139
+ readonly uniqueConstraint?: ColumnUniqueOptions
140
+ readonly defaultValue?: DdlExpression
141
+ readonly generatedValue?: DdlExpression
142
+ readonly ddlType?: string
143
+ readonly identity?: {
144
+ readonly generation: "always" | "byDefault"
145
+ }
146
+ readonly enum?: {
147
+ readonly name: string
148
+ readonly schemaName?: string
149
+ readonly values: readonly [string, ...string[]]
150
+ }
96
151
  }
97
152
  }
98
153
 
@@ -249,6 +304,11 @@ export const makeColumnDefinition = <
249
304
  primaryKey: metadata.primaryKey,
250
305
  unique: metadata.unique,
251
306
  references: metadata.references,
307
+ defaultValue: metadata.defaultValue,
308
+ generatedValue: metadata.generatedValue,
309
+ ddlType: metadata.ddlType,
310
+ identity: metadata.identity,
311
+ enum: metadata.enum,
252
312
  source: undefined as Source,
253
313
  dependencies: {} as Dependencies
254
314
  }
@@ -338,7 +398,12 @@ export const remapColumnDefinition = <
338
398
  generated: metadata.generated,
339
399
  primaryKey: metadata.primaryKey,
340
400
  unique: metadata.unique,
341
- references: metadata.references
401
+ references: metadata.references,
402
+ defaultValue: metadata.defaultValue,
403
+ generatedValue: metadata.generatedValue,
404
+ ddlType: metadata.ddlType,
405
+ identity: metadata.identity,
406
+ enum: metadata.enum
342
407
  }
343
408
  if (ExpressionAst.TypeId in column) {
344
409
  next[ExpressionAst.TypeId] = (column as unknown as {
@@ -372,13 +437,17 @@ export const bindColumn = <
372
437
  baseTableName: BaseTableName,
373
438
  schemaName?: SchemaName
374
439
  ): BoundColumnFrom<Column, TableName, ColumnName, BaseTableName> => {
440
+ const brandName = `${tableName}.${columnName}`
441
+ const schema = column.metadata.brand === true
442
+ ? Schema.brand(brandName)(column.schema)
443
+ : column.schema
375
444
  const bound = Object.create(ColumnProto)
376
- bound.schema = column.schema
445
+ bound.schema = schema
377
446
  bound.metadata = column.metadata
378
447
  bound[Expression.TypeId] = {
379
448
  runtime: undefined as SelectType<Column>,
380
449
  dbType: column.metadata.dbType,
381
- runtimeSchema: column.schema,
450
+ runtimeSchema: schema,
382
451
  nullability: (column.metadata.nullable ? "maybe" : "never") as IsNullable<Column> extends true ? "maybe" : "never",
383
452
  dialect: column.metadata.dbType.dialect,
384
453
  aggregation: "scalar",
@@ -430,6 +499,13 @@ export type ReferencesOf<Column extends AnyColumnDefinition> = ColumnStateOf<Col
430
499
  /** Extracts the non-null select type of a column. */
431
500
  export type BaseSelectType<Column extends AnyColumnDefinition> = NonNullable<SelectType<Column>>
432
501
 
502
+ type BrandedValue<
503
+ Value,
504
+ BrandName extends string
505
+ > = [Extract<Value, null | undefined>] extends [never]
506
+ ? Value & Brand.Brand<BrandName>
507
+ : Exclude<Value, null | undefined> & Brand.Brand<BrandName> | Extract<Value, null | undefined>
508
+
433
509
  /** Rebinds a generic column definition to a specific table and key. */
434
510
  export type BoundColumnFrom<
435
511
  Column extends AnyColumnDefinition,
@@ -437,9 +513,15 @@ export type BoundColumnFrom<
437
513
  ColumnName extends string,
438
514
  BaseTableName extends string = TableName
439
515
  > = BoundColumn<
440
- SelectType<Column>,
441
- InsertType<Column>,
442
- UpdateType<Column>,
516
+ Column["metadata"]["brand"] extends true
517
+ ? BrandedValue<SelectType<Column>, `${TableName}.${ColumnName}`>
518
+ : SelectType<Column>,
519
+ Column["metadata"]["brand"] extends true
520
+ ? BrandedValue<InsertType<Column>, `${TableName}.${ColumnName}`>
521
+ : InsertType<Column>,
522
+ Column["metadata"]["brand"] extends true
523
+ ? BrandedValue<UpdateType<Column>, `${TableName}.${ColumnName}`>
524
+ : UpdateType<Column>,
443
525
  ColumnStateOf<Column>["dbType"],
444
526
  IsNullable<Column>,
445
527
  HasDefault<Column>,