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
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "effect-qb",
3
+ "version": "0.12.3",
4
+ "type": "module",
5
+ "files": [
6
+ "dist",
7
+ "src",
8
+ "README.md"
9
+ ],
10
+ "exports": {
11
+ "./postgres": {
12
+ "types": "./src/postgres.ts",
13
+ "import": "./dist/postgres.js"
14
+ },
15
+ "./mysql": {
16
+ "types": "./src/mysql.ts",
17
+ "import": "./dist/mysql.js"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "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",
29
+ "prepack": "bun run build"
30
+ },
31
+ "devDependencies": {
32
+ "@effect/sql-mysql2": "0.48.0",
33
+ "@effect/sql-pg": "0.48.0",
34
+ "@types/bun": "latest",
35
+ "@typescript/native-preview": "latest"
36
+ },
37
+ "dependencies": {
38
+ "@effect/experimental": "^0.57.0",
39
+ "@effect/sql": "^0.48.0",
40
+ "effect": "^3.19.3"
41
+ }
42
+ }
@@ -0,0 +1,57 @@
1
+ import * as Expression from "./expression.js"
2
+ import { groupingKeyOfExpression } from "./grouping-key.js"
3
+
4
+ /** Recursive selection value accepted by aggregate-shape validation. */
5
+ export type SelectionValue =
6
+ | Expression.Any
7
+ | {
8
+ readonly [key: string]: SelectionValue
9
+ }
10
+
11
+ const isExpression = (value: unknown): value is Expression.Any =>
12
+ typeof value === "object" && value !== null && Expression.TypeId in value
13
+
14
+ const selectionHasAggregate = (selection: SelectionValue): boolean => {
15
+ if (isExpression(selection)) {
16
+ return selection[Expression.TypeId].aggregation === "aggregate"
17
+ }
18
+ return Object.values(selection).some((value) => selectionHasAggregate(value))
19
+ }
20
+
21
+ const isGroupedSelectionValid = (
22
+ selection: SelectionValue,
23
+ groupedExpressions: ReadonlySet<string>
24
+ ): boolean => {
25
+ if (isExpression(selection)) {
26
+ const aggregation = selection[Expression.TypeId].aggregation
27
+ if (aggregation === "aggregate") {
28
+ return true
29
+ }
30
+ if (aggregation === "window") {
31
+ return false
32
+ }
33
+ if (Object.keys(selection[Expression.TypeId].dependencies).length === 0) {
34
+ return true
35
+ }
36
+ return groupedExpressions.has(groupingKeyOfExpression(selection))
37
+ }
38
+ return Object.values(selection).every((value) => isGroupedSelectionValid(value, groupedExpressions))
39
+ }
40
+
41
+ /**
42
+ * Validates that grouped/scalar selection mixing is legal for the provided
43
+ * `groupBy(...)` expressions.
44
+ */
45
+ export const validateAggregationSelection = (
46
+ selection: SelectionValue,
47
+ grouped: readonly Expression.Any[]
48
+ ): void => {
49
+ const groupedExpressions = new Set(grouped.map(groupingKeyOfExpression))
50
+ const hasAggregate = selectionHasAggregate(selection)
51
+ const isValid = hasAggregate || grouped.length > 0
52
+ ? isGroupedSelectionValid(selection, groupedExpressions)
53
+ : true
54
+ if (!isValid) {
55
+ throw new Error("Invalid grouped selection: scalar expressions must be covered by groupBy(...) when aggregates are present")
56
+ }
57
+ }
@@ -0,0 +1,50 @@
1
+ import type * as Expression from "./expression.js"
2
+ import type * as ExpressionAst from "./expression-ast.js"
3
+ import type { PredicateFormula } from "./predicate-formula.js"
4
+ import type { AssumeFormulaFalse, AssumeFormulaTrue, Contradicts, Implies } from "./predicate-analysis.js"
5
+ import type { FormulaOfExpression } from "./predicate-normalize.js"
6
+
7
+ export interface CasePath<
8
+ Assumptions extends PredicateFormula,
9
+ Value extends Expression.Any
10
+ > {
11
+ readonly assumptions: Assumptions
12
+ readonly value: Value
13
+ }
14
+
15
+ type PredicateFormulaOf<Predicate extends Expression.Any> = FormulaOfExpression<Predicate>
16
+
17
+ export type CaseBranchAssumeTrue<
18
+ Assumptions extends PredicateFormula,
19
+ Predicate extends Expression.Any
20
+ > = AssumeFormulaTrue<Assumptions, PredicateFormulaOf<Predicate>>
21
+
22
+ export type CaseBranchAssumeFalse<
23
+ Assumptions extends PredicateFormula,
24
+ Predicate extends Expression.Any
25
+ > = AssumeFormulaFalse<Assumptions, PredicateFormulaOf<Predicate>>
26
+
27
+ export type CaseBranchDecision<
28
+ Assumptions extends PredicateFormula,
29
+ Predicate extends Expression.Any
30
+ > = Contradicts<Assumptions, Predicate> extends true
31
+ ? "skip"
32
+ : Implies<Assumptions, Predicate> extends true
33
+ ? "take"
34
+ : "branch"
35
+
36
+ export type ReachableCasePaths<
37
+ Assumptions extends PredicateFormula,
38
+ Branches extends readonly ExpressionAst.CaseBranchNode[],
39
+ Else extends Expression.Any
40
+ > = Branches extends readonly [
41
+ infer Head extends ExpressionAst.CaseBranchNode,
42
+ ...infer Tail extends readonly ExpressionAst.CaseBranchNode[]
43
+ ]
44
+ ? CaseBranchDecision<Assumptions, Head["when"]> extends "skip"
45
+ ? ReachableCasePaths<Assumptions, Tail, Else>
46
+ : CaseBranchDecision<Assumptions, Head["when"]> extends "take"
47
+ ? CasePath<CaseBranchAssumeTrue<Assumptions, Head["when"]>, Head["then"]>
48
+ : CasePath<CaseBranchAssumeTrue<Assumptions, Head["when"]>, Head["then"]> |
49
+ ReachableCasePaths<CaseBranchAssumeFalse<Assumptions, Head["when"]>, Tail, Else>
50
+ : CasePath<Assumptions, Else>
@@ -0,0 +1,30 @@
1
+ import type * as Expression from "./expression.js"
2
+ import type { CastTargetError, OperandCompatibilityError } from "./coercion-errors.js"
3
+ import type { CanCastDbType, CanCompareDbTypes, CanTextuallyCoerceDbType } from "./coercion-rules.js"
4
+
5
+ export type ComparableDbType<
6
+ Left extends Expression.DbType.Any,
7
+ Right extends Expression.DbType.Any,
8
+ Dialect extends string,
9
+ Operator extends string = "comparison"
10
+ > = CanCompareDbTypes<Left, Right, Dialect> extends true
11
+ ? Right
12
+ : OperandCompatibilityError<Operator, Left, Right, Dialect, "the same db type family">
13
+
14
+ export type TextCompatibleDbType<
15
+ Db extends Expression.DbType.Any,
16
+ Dialect extends string,
17
+ Operator extends string = "text operator"
18
+ > = CanTextuallyCoerceDbType<Db, Dialect> extends true
19
+ ? Db
20
+ : OperandCompatibilityError<Operator, Db, Db, Dialect, "a text-compatible db type">
21
+
22
+ export type CastableDbType<
23
+ Source extends Expression.DbType.Any,
24
+ Target extends Expression.DbType.Any,
25
+ Dialect extends string
26
+ > = CanCastDbType<Source, Target, Dialect> extends true
27
+ ? Target
28
+ : CastTargetError<Source, Target, Dialect>
29
+
30
+ export type RuntimeOfDbType<Db extends Expression.DbType.Any> = Expression.RuntimeOfDbType<Db>
@@ -0,0 +1,29 @@
1
+ import type * as Expression from "./expression.js"
2
+
3
+ export type OperandCompatibilityError<
4
+ Operator extends string,
5
+ Left extends Expression.DbType.Any,
6
+ Right extends Expression.DbType.Any,
7
+ Dialect extends string,
8
+ Expected extends string
9
+ > = {
10
+ readonly __effect_qb_error__: "effect-qb: incompatible operand types"
11
+ readonly __effect_qb_operator__: Operator
12
+ readonly __effect_qb_left_db_type__: Left
13
+ readonly __effect_qb_right_db_type__: Right
14
+ readonly __effect_qb_dialect__: Dialect
15
+ readonly __effect_qb_expected__: Expected
16
+ readonly __effect_qb_hint__: "Use cast(...) or pick values from the same db type family"
17
+ }
18
+
19
+ export type CastTargetError<
20
+ Source extends Expression.DbType.Any,
21
+ Target extends Expression.DbType.Any,
22
+ Dialect extends string
23
+ > = {
24
+ readonly __effect_qb_error__: "effect-qb: unsupported cast target"
25
+ readonly __effect_qb_source_db_type__: Source
26
+ readonly __effect_qb_target_db_type__: Target
27
+ readonly __effect_qb_dialect__: Dialect
28
+ readonly __effect_qb_hint__: "Use one of the supported Q.type.<kind>() witnesses"
29
+ }
@@ -0,0 +1,32 @@
1
+ import type * as Expression from "./expression.js"
2
+ import type { FamilyOfDbType } from "./datatypes/lookup.js"
3
+
4
+ export type CoercionKind =
5
+ | "text"
6
+ | "numeric"
7
+ | "boolean"
8
+ | "date"
9
+ | "time"
10
+ | "timestamp"
11
+ | "binary"
12
+ | "interval"
13
+ | "uuid"
14
+ | "json"
15
+ | "xml"
16
+ | "bit"
17
+ | "identifier"
18
+ | "network"
19
+ | "spatial"
20
+ | "textsearch"
21
+ | "range"
22
+ | "multirange"
23
+ | "array"
24
+ | "record"
25
+ | "domain"
26
+ | "enum"
27
+ | "set"
28
+ | "money"
29
+ | "null"
30
+ | `other:${string}`
31
+
32
+ export type CoercionKindOf<Db extends Expression.DbType.Any> = FamilyOfDbType<Db>
@@ -0,0 +1,7 @@
1
+ import type * as Expression from "./expression.js"
2
+
3
+ /** Extracts the database type carried by an expression. */
4
+ export type DbTypeOfExpression<Value extends Expression.Any> = Expression.DbTypeOf<Value>
5
+
6
+ /** Extracts the decoded runtime type carried by an expression. */
7
+ export type RuntimeOfExpression<Value extends Expression.Any> = Expression.RuntimeOf<Value>
@@ -0,0 +1,25 @@
1
+ import type * as Expression from "./expression.js"
2
+ import type { CanCastDbType as LookupCanCastDbType, CanCompareDbTypes as LookupCanCompareDbTypes, CanContainDbTypes as LookupCanContainDbTypes, CanTextuallyCoerceDbType as LookupCanTextuallyCoerceDbType } from "./datatypes/lookup.js"
3
+
4
+ export type CanCompareDbTypes<
5
+ Left extends Expression.DbType.Any,
6
+ Right extends Expression.DbType.Any,
7
+ Dialect extends string
8
+ > = LookupCanCompareDbTypes<Left, Right, Dialect>
9
+
10
+ export type CanContainDbTypes<
11
+ Left extends Expression.DbType.Any,
12
+ Right extends Expression.DbType.Any,
13
+ Dialect extends string
14
+ > = LookupCanContainDbTypes<Left, Right, Dialect>
15
+
16
+ export type CanTextuallyCoerceDbType<
17
+ Db extends Expression.DbType.Any,
18
+ Dialect extends string
19
+ > = LookupCanTextuallyCoerceDbType<Db, Dialect>
20
+
21
+ export type CanCastDbType<
22
+ Source extends Expression.DbType.Any,
23
+ Target extends Expression.DbType.Any,
24
+ Dialect extends string
25
+ > = LookupCanCastDbType<Source, Target, Dialect>