effect-qb 0.14.0 → 0.16.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 (146) hide show
  1. package/dist/mysql.js +61555 -4252
  2. package/dist/postgres/metadata.js +728 -104
  3. package/dist/postgres.js +6906 -4023
  4. package/package.json +15 -2
  5. package/src/internal/aggregation-validation.ts +3 -3
  6. package/src/internal/case-analysis.d.ts +18 -0
  7. package/src/internal/case-analysis.ts +4 -4
  8. package/src/internal/coercion/analysis.d.ts +7 -0
  9. package/src/internal/{coercion-analysis.ts → coercion/analysis.ts} +3 -3
  10. package/src/internal/coercion/errors.d.ts +17 -0
  11. package/src/internal/{coercion-errors.ts → coercion/errors.ts} +1 -1
  12. package/src/internal/coercion/kind.d.ts +4 -0
  13. package/src/internal/{coercion-kind.ts → coercion/kind.ts} +2 -2
  14. package/src/internal/{coercion-normalize.ts → coercion/normalize.ts} +1 -1
  15. package/src/internal/coercion/rules.d.ts +6 -0
  16. package/src/internal/{coercion-rules.ts → coercion/rules.ts} +2 -2
  17. package/src/internal/column-state.d.ts +190 -0
  18. package/src/internal/column-state.ts +43 -47
  19. package/src/internal/column.ts +43 -305
  20. package/src/internal/datatypes/define.d.ts +17 -0
  21. package/src/internal/datatypes/define.ts +18 -4
  22. package/src/internal/datatypes/lookup.d.ts +44 -0
  23. package/src/internal/datatypes/lookup.ts +61 -152
  24. package/src/internal/datatypes/shape.d.ts +16 -0
  25. package/src/internal/datatypes/shape.ts +1 -1
  26. package/src/internal/derived-table.d.ts +4 -0
  27. package/src/internal/derived-table.ts +21 -16
  28. package/src/internal/dialect.ts +12 -1
  29. package/src/internal/dsl-mutation-runtime.ts +378 -0
  30. package/src/internal/dsl-plan-runtime.ts +387 -0
  31. package/src/internal/dsl-query-runtime.ts +160 -0
  32. package/src/internal/dsl-transaction-ddl-runtime.ts +263 -0
  33. package/src/internal/executor.ts +146 -34
  34. package/src/internal/expression-ast.ts +15 -5
  35. package/src/internal/grouping-key.d.ts +3 -0
  36. package/src/internal/grouping-key.ts +1 -1
  37. package/src/internal/implication-runtime.d.ts +15 -0
  38. package/src/internal/implication-runtime.ts +4 -4
  39. package/src/internal/json/ast.d.ts +30 -0
  40. package/src/internal/json/ast.ts +1 -1
  41. package/src/internal/json/errors.d.ts +8 -0
  42. package/src/internal/json/path.d.ts +75 -0
  43. package/src/internal/json/path.ts +1 -1
  44. package/src/internal/json/types.d.ts +62 -0
  45. package/src/internal/predicate/analysis.d.ts +20 -0
  46. package/src/internal/predicate/analysis.ts +183 -0
  47. package/src/internal/predicate/atom.d.ts +28 -0
  48. package/src/internal/{predicate-atom.ts → predicate/atom.ts} +7 -0
  49. package/src/internal/{predicate-branches.ts → predicate/branches.ts} +2 -2
  50. package/src/internal/predicate/context.d.ts +67 -0
  51. package/src/internal/{predicate-context.ts → predicate/context.ts} +163 -20
  52. package/src/internal/predicate/formula.d.ts +35 -0
  53. package/src/internal/{predicate-formula.ts → predicate/formula.ts} +1 -1
  54. package/src/internal/predicate/key.d.ts +11 -0
  55. package/src/internal/predicate/key.ts +73 -0
  56. package/src/internal/{predicate-nnf.ts → predicate/nnf.ts} +2 -2
  57. package/src/internal/predicate/normalize.d.ts +53 -0
  58. package/src/internal/{predicate-normalize.ts → predicate/normalize.ts} +130 -49
  59. package/src/internal/predicate/runtime.d.ts +31 -0
  60. package/src/internal/{predicate-runtime.ts → predicate/runtime.ts} +127 -17
  61. package/src/internal/projection-alias.d.ts +13 -0
  62. package/src/internal/projections.d.ts +31 -0
  63. package/src/internal/projections.ts +1 -1
  64. package/src/internal/query-ast.d.ts +217 -0
  65. package/src/internal/query-ast.ts +1 -1
  66. package/src/internal/query-requirements.d.ts +20 -0
  67. package/src/internal/query.d.ts +775 -0
  68. package/src/internal/query.ts +683 -369
  69. package/src/internal/renderer.ts +11 -21
  70. package/src/internal/row-set.d.ts +53 -0
  71. package/src/internal/{plan.ts → row-set.ts} +11 -9
  72. package/src/internal/runtime/driver-value-mapping.ts +186 -0
  73. package/src/internal/{runtime-normalize.ts → runtime/normalize.ts} +9 -31
  74. package/src/internal/{runtime-schema.ts → runtime/schema.ts} +13 -38
  75. package/src/internal/runtime/value.d.ts +22 -0
  76. package/src/internal/{runtime-value.ts → runtime/value.ts} +2 -2
  77. package/src/internal/scalar.d.ts +107 -0
  78. package/src/internal/scalar.ts +202 -0
  79. package/src/internal/schema-derivation.d.ts +105 -0
  80. package/src/internal/schema-expression.d.ts +18 -0
  81. package/src/internal/schema-expression.ts +38 -7
  82. package/src/internal/table-options.d.ts +94 -0
  83. package/src/internal/table-options.ts +8 -2
  84. package/src/internal/table.d.ts +173 -0
  85. package/src/internal/table.ts +32 -14
  86. package/src/mysql/column.ts +95 -18
  87. package/src/mysql/datatypes/index.ts +47 -7
  88. package/src/mysql/errors/generated.ts +57336 -0
  89. package/src/mysql/errors/index.ts +1 -0
  90. package/src/mysql/errors/normalize.ts +55 -53
  91. package/src/mysql/errors/types.ts +74 -0
  92. package/src/mysql/executor.ts +88 -11
  93. package/src/mysql/function/aggregate.ts +1 -5
  94. package/src/mysql/function/core.ts +1 -4
  95. package/src/mysql/function/index.ts +0 -1
  96. package/src/mysql/function/string.ts +1 -5
  97. package/src/mysql/function/temporal.ts +12 -15
  98. package/src/mysql/function/window.ts +1 -6
  99. package/src/{internal/mysql-dialect.ts → mysql/internal/dialect.ts} +12 -6
  100. package/src/{internal/mysql-query.ts → mysql/internal/dsl.ts} +1299 -2143
  101. package/src/mysql/internal/renderer.ts +46 -0
  102. package/src/mysql/internal/sql-expression-renderer.ts +1501 -0
  103. package/src/mysql/json.ts +2 -0
  104. package/src/mysql/query.ts +111 -91
  105. package/src/mysql/renderer.ts +8 -3
  106. package/src/mysql/table.ts +1 -1
  107. package/src/mysql.ts +6 -4
  108. package/src/postgres/cast.ts +30 -16
  109. package/src/postgres/column.ts +179 -46
  110. package/src/postgres/datatypes/index.d.ts +515 -0
  111. package/src/postgres/datatypes/index.ts +22 -13
  112. package/src/postgres/datatypes/spec.d.ts +412 -0
  113. package/src/postgres/errors/generated.ts +2636 -0
  114. package/src/postgres/errors/index.ts +1 -0
  115. package/src/postgres/errors/normalize.ts +47 -62
  116. package/src/postgres/errors/types.ts +92 -34
  117. package/src/postgres/executor.ts +54 -7
  118. package/src/postgres/function/aggregate.ts +1 -5
  119. package/src/postgres/function/core.ts +12 -6
  120. package/src/postgres/function/index.ts +0 -1
  121. package/src/postgres/function/string.ts +1 -5
  122. package/src/postgres/function/temporal.ts +12 -15
  123. package/src/postgres/function/window.ts +1 -6
  124. package/src/{internal/postgres-dialect.ts → postgres/internal/dialect.ts} +12 -6
  125. package/src/{internal/postgres-query.ts → postgres/internal/dsl.ts} +1356 -2133
  126. package/src/{internal/postgres-renderer.ts → postgres/internal/renderer.ts} +17 -8
  127. package/src/postgres/internal/schema-ddl.ts +108 -0
  128. package/src/{internal/postgres-schema-model.ts → postgres/internal/schema-model.ts} +12 -6
  129. package/src/{internal → postgres/internal}/sql-expression-renderer.ts +79 -25
  130. package/src/postgres/{function/json.ts → json.ts} +77 -85
  131. package/src/postgres/metadata.ts +2 -2
  132. package/src/postgres/query.ts +113 -89
  133. package/src/postgres/renderer.ts +8 -13
  134. package/src/postgres/schema-expression.ts +2 -1
  135. package/src/postgres/schema-management.ts +1 -1
  136. package/src/postgres/table.ts +12 -4
  137. package/src/postgres/type.ts +33 -2
  138. package/src/postgres.ts +6 -4
  139. package/src/internal/expression.ts +0 -327
  140. package/src/internal/mysql-renderer.ts +0 -37
  141. package/src/internal/predicate-analysis.ts +0 -81
  142. package/src/internal/predicate-key.ts +0 -28
  143. package/src/internal/schema-ddl.ts +0 -55
  144. package/src/mysql/function/json.ts +0 -4
  145. package/src/mysql/private/query.ts +0 -1
  146. package/src/postgres/private/query.ts +0 -1
@@ -2,3 +2,4 @@ export * from "./catalog.js"
2
2
  export * from "./fields.js"
3
3
  export * from "./normalize.js"
4
4
  export * from "./requirements.js"
5
+ export * from "./types.js"
@@ -8,14 +8,35 @@ import {
8
8
  type PostgresErrorTag,
9
9
  type PostgresSqlStateCode
10
10
  } from "./catalog.js"
11
+ import {
12
+ postgresKnownErrorClassesByCode,
13
+ type KnownPostgresErrorByCode as ExactKnownPostgresErrorByCode
14
+ } from "./generated.js"
11
15
  import type {
12
16
  PostgresErrorFields,
13
17
  PostgresQueryContext
14
18
  } from "./fields.js"
19
+ import type {
20
+ PostgresErrorLike,
21
+ PostgresKnownErrorBase
22
+ } from "./types.js"
15
23
 
16
24
  const isRecord = (value: unknown): value is Record<string, unknown> =>
17
25
  typeof value === "object" && value !== null
18
26
 
27
+ const unwrapPostgresDriverCause = (cause: unknown): unknown => {
28
+ let current = cause
29
+ while (
30
+ isRecord(current) &&
31
+ "_tag" in current &&
32
+ current._tag === "SqlError" &&
33
+ "cause" in current
34
+ ) {
35
+ current = current.cause
36
+ }
37
+ return current
38
+ }
39
+
19
40
  const asString = (value: unknown): string | undefined =>
20
41
  typeof value === "string" ? value : undefined
21
42
 
@@ -51,49 +72,14 @@ const normalizeFields = (error: Record<string, unknown>): PostgresErrorFields =>
51
72
 
52
73
  const sqlStatePattern = /^[0-9A-Z]{5}$/
53
74
 
54
- /** Raw Postgres-like error object as commonly exposed by client libraries. */
55
- export interface PostgresErrorLike {
56
- readonly code?: string
57
- readonly message?: string
58
- readonly messagePrimary?: string
59
- readonly schema?: string
60
- readonly table?: string
61
- readonly column?: string
62
- readonly dataType?: string
63
- readonly constraint?: string
64
- readonly severity?: string
65
- readonly severityNonLocalized?: string
66
- readonly detail?: string
67
- readonly hint?: string
68
- readonly position?: string | number
69
- readonly internalPosition?: string | number
70
- readonly internalQuery?: string
71
- readonly where?: string
72
- readonly file?: string
73
- readonly line?: string | number
74
- readonly routine?: string
75
- }
75
+ export type { PostgresErrorLike } from "./types.js"
76
76
 
77
77
  /** Structured known Postgres SQLSTATE error derived from the catalog. */
78
- export type KnownPostgresError<Code extends PostgresSqlStateCode = PostgresSqlStateCode> = {
79
- readonly [Current in Code]: Readonly<{
80
- readonly _tag: PostgresErrorTag<Current>
81
- readonly code: Current
82
- readonly condition: PostgresErrorDescriptor<Current>["condition"]
83
- readonly classCode: PostgresErrorDescriptor<Current>["classCode"]
84
- readonly className: PostgresErrorDescriptor<Current>["className"]
85
- readonly message: string
86
- readonly primaryFields: PostgresErrorDescriptor<Current>["primaryFields"]
87
- readonly query?: PostgresQueryContext
88
- readonly raw: PostgresErrorLike
89
- } & PostgresErrorFields>
90
- }[Code]
78
+ export type KnownPostgresError<Code extends PostgresSqlStateCode = PostgresSqlStateCode> =
79
+ ExactKnownPostgresErrorByCode<Code>
91
80
 
92
81
  /** Extracts the known Postgres error variant for a specific SQLSTATE code. */
93
- export type KnownPostgresErrorByCode<Code extends PostgresSqlStateCode> = Extract<
94
- KnownPostgresError,
95
- { readonly code: Code }
96
- >
82
+ export type KnownPostgresErrorByCode<Code extends PostgresSqlStateCode> = ExactKnownPostgresErrorByCode<Code>
97
83
 
98
84
  /** Postgres-like error whose SQLSTATE is well-formed but not in the current catalog. */
99
85
  export type UnknownPostgresSqlStateError = Readonly<{
@@ -133,24 +119,19 @@ export const isPostgresErrorLike = (value: unknown): value is PostgresErrorLike
133
119
  const errorMessageOf = (error: PostgresErrorLike): string =>
134
120
  error.message ?? error.messagePrimary ?? "Postgres driver error"
135
121
 
136
- const makeKnownPostgresError = <Code extends PostgresSqlStateCode>(
137
- code: Code,
122
+ const makeKnownPostgresError = (
123
+ code: PostgresSqlStateCode,
138
124
  raw: PostgresErrorLike,
139
125
  query?: PostgresQueryContext
140
- ): KnownPostgresError<Code> => {
126
+ ): KnownPostgresError => {
141
127
  const descriptor = getPostgresErrorDescriptor(code)
142
- return {
143
- _tag: descriptor.tag,
144
- code,
145
- condition: descriptor.condition,
146
- classCode: descriptor.classCode,
147
- className: descriptor.className,
128
+ const ErrorClass = postgresKnownErrorClassesByCode[code]
129
+ return new ErrorClass({
148
130
  message: errorMessageOf(raw),
149
- primaryFields: descriptor.primaryFields,
150
131
  query,
151
132
  raw,
152
133
  ...normalizeFields(raw as Record<string, unknown>)
153
- } as KnownPostgresError<Code>
134
+ }) as KnownPostgresError
154
135
  }
155
136
 
156
137
  /** Normalizes an unknown failure into a structured Postgres driver error. */
@@ -158,44 +139,45 @@ export const normalizePostgresDriverError = (
158
139
  cause: unknown,
159
140
  query?: PostgresQueryContext | Renderer.RenderedQuery<any, "postgres">
160
141
  ): PostgresDriverError => {
142
+ const normalizedCause = unwrapPostgresDriverCause(cause)
161
143
  const context = query === undefined
162
144
  ? undefined
163
145
  : "sql" in query
164
146
  ? { sql: query.sql, params: query.params }
165
147
  : query
166
148
 
167
- if (!isPostgresErrorLike(cause)) {
149
+ if (!isPostgresErrorLike(normalizedCause)) {
168
150
  return {
169
151
  _tag: "@postgres/unknown/driver",
170
- message: cause instanceof Error ? cause.message : "Unknown Postgres driver failure",
152
+ message: normalizedCause instanceof Error ? normalizedCause.message : "Unknown Postgres driver failure",
171
153
  query: context,
172
154
  cause
173
155
  } as UnknownPostgresDriverError
174
156
  }
175
157
 
176
- if (cause.code && isPostgresSqlStateCode(cause.code)) {
177
- return makeKnownPostgresError(cause.code, cause, context)
158
+ if (normalizedCause.code && isPostgresSqlStateCode(normalizedCause.code)) {
159
+ return makeKnownPostgresError(normalizedCause.code, normalizedCause, context)
178
160
  }
179
161
 
180
- if (typeof cause.code === "string" && sqlStatePattern.test(cause.code)) {
181
- const classCode = cause.code.slice(0, 2)
162
+ if (typeof normalizedCause.code === "string" && sqlStatePattern.test(normalizedCause.code)) {
163
+ const classCode = normalizedCause.code.slice(0, 2)
182
164
  return {
183
165
  _tag: "@postgres/unknown/sqlstate",
184
- code: cause.code,
166
+ code: normalizedCause.code,
185
167
  classCode,
186
168
  className: classCode in postgresErrorClasses
187
169
  ? postgresErrorClasses[classCode as PostgresErrorClassCode]
188
170
  : undefined,
189
- message: errorMessageOf(cause),
171
+ message: errorMessageOf(normalizedCause),
190
172
  query: context,
191
- raw: cause,
192
- ...normalizeFields(cause as Record<string, unknown>)
173
+ raw: normalizedCause,
174
+ ...normalizeFields(normalizedCause as Record<string, unknown>)
193
175
  } as UnknownPostgresSqlStateError
194
176
  }
195
177
 
196
178
  return {
197
179
  _tag: "@postgres/unknown/driver",
198
- message: errorMessageOf(cause),
180
+ message: errorMessageOf(normalizedCause),
199
181
  query: context,
200
182
  cause
201
183
  } as UnknownPostgresDriverError
@@ -206,4 +188,7 @@ export const hasSqlState = <Code extends PostgresSqlStateCode>(
206
188
  error: PostgresDriverError | { readonly code?: string },
207
189
  code: Code
208
190
  ): error is KnownPostgresErrorByCode<Code> =>
209
- "code" in error && error.code === code
191
+ (typeof error === "object" &&
192
+ error !== null &&
193
+ error instanceof postgresKnownErrorClassesByCode[code]) ||
194
+ ("code" in error && error.code === code)
@@ -1,38 +1,96 @@
1
- /** Structured Postgres error-report fields commonly exposed by drivers. */
2
- export type PostgresErrorSemanticField =
3
- | "schema"
4
- | "table"
5
- | "column"
6
- | "constraint"
7
- | "dataType"
8
- | "position"
9
- | "internalPosition"
10
- | "internalQuery"
11
- | "file"
12
- | "line"
13
- | "routine"
1
+ import type { PostgresErrorTag, PostgresSqlStateCode } from "./catalog.js"
2
+ import type { PostgresErrorFields, PostgresQueryContext } from "./fields.js"
14
3
 
15
- /** Class-level metadata for a family of SQLSTATE errors. */
16
- export interface PostgresSqlStateClassMetadata<
17
- ClassCode extends string = string,
18
- ClassName extends string = string
19
- > {
20
- readonly classCode: ClassCode
21
- readonly className: ClassName
22
- readonly tag: string
4
+ /** Raw Postgres-like error object as commonly exposed by client libraries. */
5
+ export interface PostgresErrorLike {
6
+ readonly code?: string
7
+ readonly message?: string
8
+ readonly messagePrimary?: string
9
+ readonly schema?: string
10
+ readonly table?: string
11
+ readonly column?: string
12
+ readonly dataType?: string
13
+ readonly constraint?: string
14
+ readonly severity?: string
15
+ readonly severityNonLocalized?: string
16
+ readonly detail?: string
17
+ readonly hint?: string
18
+ readonly position?: string | number
19
+ readonly internalPosition?: string | number
20
+ readonly internalQuery?: string
21
+ readonly where?: string
22
+ readonly file?: string
23
+ readonly line?: string | number
24
+ readonly routine?: string
23
25
  }
24
26
 
25
- /** Metadata for a single Postgres SQLSTATE condition. */
26
- export interface PostgresSqlStateMetadata<
27
- Code extends string = string,
28
- Condition extends string = string,
29
- ClassCode extends string = string,
30
- ClassName extends string = string
31
- > {
32
- readonly code: Code
33
- readonly condition: Condition
34
- readonly classCode: ClassCode
35
- readonly className: ClassName
36
- readonly tag: string
37
- readonly semanticFields: readonly PostgresErrorSemanticField[]
27
+ /** Broad known-Postgres error surface used by the normalizer return type. */
28
+ export interface PostgresKnownErrorBase extends Error, PostgresErrorFields {
29
+ readonly _tag: PostgresErrorTag<PostgresSqlStateCode>
30
+ readonly code: PostgresSqlStateCode
31
+ readonly condition: string
32
+ readonly classCode: string
33
+ readonly className: string
34
+ readonly message: string
35
+ readonly primaryFields: readonly string[]
36
+ readonly query?: PostgresQueryContext
37
+ readonly raw: PostgresErrorLike
38
+ }
39
+
40
+ /** Shared constructor payload for generated Postgres error classes. */
41
+ export type PostgresKnownErrorArgs = Readonly<{
42
+ readonly message: string
43
+ readonly query?: PostgresQueryContext
44
+ readonly raw: PostgresErrorLike
45
+ } & PostgresErrorFields>
46
+
47
+ /** Runtime base class shared by generated Postgres error classes. */
48
+ export abstract class PostgresKnownErrorClass extends Error implements PostgresKnownErrorBase {
49
+ abstract readonly _tag: PostgresErrorTag<PostgresSqlStateCode>
50
+ abstract readonly code: PostgresSqlStateCode
51
+ abstract readonly condition: string
52
+ abstract readonly classCode: string
53
+ abstract readonly className: string
54
+ abstract readonly primaryFields: readonly string[]
55
+ readonly severity?
56
+ readonly severityNonLocalized?
57
+ readonly detail?
58
+ readonly hint?
59
+ readonly position?
60
+ readonly internalPosition?
61
+ readonly internalQuery?
62
+ readonly where?
63
+ readonly schemaName?
64
+ readonly tableName?
65
+ readonly columnName?
66
+ readonly dataTypeName?
67
+ readonly constraintName?
68
+ readonly file?
69
+ readonly line?
70
+ readonly routine?
71
+ readonly query?
72
+ readonly raw
73
+
74
+ constructor(args: PostgresKnownErrorArgs) {
75
+ super(args.message)
76
+ this.name = new.target.name
77
+ this.severity = args.severity
78
+ this.severityNonLocalized = args.severityNonLocalized
79
+ this.detail = args.detail
80
+ this.hint = args.hint
81
+ this.position = args.position
82
+ this.internalPosition = args.internalPosition
83
+ this.internalQuery = args.internalQuery
84
+ this.where = args.where
85
+ this.schemaName = args.schemaName
86
+ this.tableName = args.tableName
87
+ this.columnName = args.columnName
88
+ this.dataTypeName = args.dataTypeName
89
+ this.constraintName = args.constraintName
90
+ this.file = args.file
91
+ this.line = args.line
92
+ this.routine = args.routine
93
+ this.query = args.query
94
+ this.raw = args.raw
95
+ }
38
96
  }
@@ -1,9 +1,12 @@
1
1
  import * as Effect from "effect/Effect"
2
2
  import * as SqlClient from "@effect/sql/SqlClient"
3
+ import * as Stream from "effect/Stream"
3
4
 
4
5
  import * as CoreExecutor from "../internal/executor.js"
5
6
  import * as CoreQuery from "../internal/query.js"
6
7
  import * as CoreRenderer from "../internal/renderer.js"
8
+ import type * as Expression from "../internal/scalar.js"
9
+ import { renderPostgresPlan } from "./internal/renderer.js"
7
10
  import {
8
11
  narrowPostgresDriverErrorForReadQuery,
9
12
  normalizePostgresDriverError,
@@ -26,6 +29,7 @@ export interface MakeOptions<Error = never, Context = never> {
26
29
  readonly renderer?: Renderer
27
30
  readonly driver?: Driver<Error, Context>
28
31
  readonly driverMode?: CoreExecutor.DriverMode
32
+ readonly valueMappings?: Expression.DriverValueMappings
29
33
  }
30
34
  /** Standard composed error shape for Postgres executors. */
31
35
  export type PostgresExecutorError = PostgresDriverError | RowDecodeError
@@ -44,6 +48,11 @@ export interface QueryExecutor<Context = never> {
44
48
  execute<PlanValue extends CoreQuery.QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
45
49
  plan: CoreQuery.DialectCompatiblePlan<PlanValue, "postgres">
46
50
  ): Effect.Effect<CoreQuery.ResultRows<PlanValue>, PostgresQueryError<PlanValue>, Context>
51
+ stream<PlanValue extends CoreQuery.QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
52
+ plan: Exclude<CoreQuery.CapabilitiesOfPlan<PlanValue>, "read" | "locking"> extends never
53
+ ? CoreQuery.DialectCompatiblePlan<PlanValue, "postgres">
54
+ : never
55
+ ): Stream.Stream<CoreQuery.ResultRow<PlanValue>, PostgresQueryError<PlanValue>, Context>
47
56
  }
48
57
 
49
58
  /** Constructs a Postgres-specialized SQL driver. */
@@ -60,7 +69,8 @@ const fromDriver = <
60
69
  >(
61
70
  renderer: Renderer,
62
71
  sqlDriver: Driver<Error, Context>,
63
- driverMode: CoreExecutor.DriverMode = "raw"
72
+ driverMode: CoreExecutor.DriverMode = "raw",
73
+ valueMappings?: Expression.DriverValueMappings
64
74
  ): QueryExecutor<Context> => ({
65
75
  dialect: "postgres",
66
76
  execute(plan) {
@@ -69,7 +79,7 @@ const fromDriver = <
69
79
  Effect.flatMap(
70
80
  sqlDriver.execute(rendered),
71
81
  (rows) => Effect.try({
72
- try: () => CoreExecutor.decodeRows(rendered, plan, rows, { driverMode }),
82
+ try: () => CoreExecutor.decodeRows(rendered, plan, rows, { driverMode, valueMappings }),
73
83
  catch: (error) => error as RowDecodeError
74
84
  })
75
85
  ),
@@ -83,13 +93,38 @@ const fromDriver = <
83
93
  : narrowPostgresDriverErrorForReadQuery(normalized)
84
94
  }
85
95
  ) as Effect.Effect<any, any, Context>
96
+ },
97
+ stream(plan) {
98
+ const rendered = renderer.render(plan)
99
+ return Stream.mapError(
100
+ Stream.mapChunksEffect(
101
+ sqlDriver.stream(rendered),
102
+ (rows) => Effect.try({
103
+ try: () => CoreExecutor.decodeChunk(rendered, plan, rows, { driverMode, valueMappings }),
104
+ catch: (error) => error as RowDecodeError
105
+ })
106
+ ),
107
+ (error) => {
108
+ if (typeof error === "object" && error !== null && "_tag" in error && error._tag === "RowDecodeError") {
109
+ return error as RowDecodeError
110
+ }
111
+ const normalized = normalizePostgresDriverError(error, rendered)
112
+ return CoreExecutor.hasWriteCapability(plan)
113
+ ? normalized
114
+ : narrowPostgresDriverErrorForReadQuery(normalized)
115
+ }
116
+ ) as Stream.Stream<any, any, Context>
86
117
  }
87
118
  })
88
119
 
89
120
  const sqlClientDriver = (): Driver<any, SqlClient.SqlClient> =>
90
- driver((query: CoreRenderer.RenderedQuery<any, "postgres">) =>
91
- Effect.flatMap(SqlClient.SqlClient, (sql) =>
92
- sql.unsafe<FlatRow>(query.sql, [...query.params])))
121
+ driver({
122
+ execute: (query: CoreRenderer.RenderedQuery<any, "postgres">) =>
123
+ Effect.flatMap(SqlClient.SqlClient, (sql) =>
124
+ sql.unsafe<FlatRow>(query.sql, [...query.params])),
125
+ stream: (query: CoreRenderer.RenderedQuery<any, "postgres">) =>
126
+ CoreExecutor.streamFromSqlClient(query)
127
+ })
93
128
 
94
129
  /**
95
130
  * Creates the standard Postgres executor pipeline.
@@ -103,6 +138,7 @@ export function make(
103
138
  options: {
104
139
  readonly renderer?: Renderer
105
140
  readonly driverMode?: CoreExecutor.DriverMode
141
+ readonly valueMappings?: Expression.DriverValueMappings
106
142
  }
107
143
  ): QueryExecutor<SqlClient.SqlClient>
108
144
  export function make<Error = never, Context = never>(
@@ -110,15 +146,26 @@ export function make<Error = never, Context = never>(
110
146
  readonly renderer?: Renderer
111
147
  readonly driver: Driver<Error, Context>
112
148
  readonly driverMode?: CoreExecutor.DriverMode
149
+ readonly valueMappings?: Expression.DriverValueMappings
113
150
  }
114
151
  ): QueryExecutor<Context>
115
152
  export function make<Error = never, Context = never>(
116
153
  options: MakeOptions<Error, Context> = {}
117
154
  ): QueryExecutor<any> {
118
155
  if (options.driver) {
119
- return fromDriver(options.renderer ?? CoreRenderer.make("postgres"), options.driver, options.driverMode)
156
+ return fromDriver(
157
+ options.renderer ?? CoreRenderer.make("postgres", (plan) => renderPostgresPlan(plan, { valueMappings: options.valueMappings })),
158
+ options.driver,
159
+ options.driverMode,
160
+ options.valueMappings
161
+ )
120
162
  }
121
- return fromDriver(options.renderer ?? CoreRenderer.make("postgres"), sqlClientDriver(), options.driverMode)
163
+ return fromDriver(
164
+ options.renderer ?? CoreRenderer.make("postgres", (plan) => renderPostgresPlan(plan, { valueMappings: options.valueMappings })),
165
+ sqlClientDriver(),
166
+ options.driverMode,
167
+ options.valueMappings
168
+ )
122
169
  }
123
170
 
124
171
  /** Creates a Postgres-specialized executor from a typed implementation callback. */
@@ -1,6 +1,2 @@
1
- import { postgresQuery } from "../private/query.js"
2
-
3
1
  /** Postgres aggregate functions. */
4
- export const count = postgresQuery.count
5
- export const max = postgresQuery.max
6
- export const min = postgresQuery.min
2
+ export { count, max, min } from "../internal/dsl.js"
@@ -1,16 +1,22 @@
1
1
  import type { ExpressionInput } from "../query.js"
2
- import { postgresQuery } from "../private/query.js"
2
+ import {
3
+ call,
4
+ cast,
5
+ coalesce,
6
+ literal,
7
+ nextVal as nextValInternal,
8
+ type as postgresType,
9
+ uuidGenerateV4
10
+ } from "../internal/dsl.js"
3
11
  import { isSequenceDefinition, type SequenceDefinition } from "../schema-management.js"
4
12
 
5
13
  /** Postgres scalar core functions. */
6
- export const coalesce = postgresQuery.coalesce
7
- export const call = postgresQuery.call
8
- export const uuidGenerateV4 = postgresQuery.uuidGenerateV4
14
+ export { coalesce, call, uuidGenerateV4 }
9
15
  export const nextVal = (
10
16
  value: ExpressionInput | SequenceDefinition<string, string | undefined>
11
17
  ) =>
12
- postgresQuery.nextVal(
18
+ nextValInternal(
13
19
  isSequenceDefinition(value)
14
- ? postgresQuery.cast(postgresQuery.literal(value.qualifiedName()), postgresQuery.type.regclass())
20
+ ? cast(literal(value.qualifiedName()), postgresType.regclass())
15
21
  : value
16
22
  )
@@ -2,7 +2,6 @@ export * as core from "./core.js"
2
2
  export * as string from "./string.js"
3
3
  export * as aggregate from "./aggregate.js"
4
4
  export * as window from "./window.js"
5
- export { json, jsonb } from "./json.js"
6
5
  export * as temporal from "./temporal.js"
7
6
 
8
7
  export { coalesce } from "./core.js"
@@ -1,6 +1,2 @@
1
- import { postgresQuery } from "../private/query.js"
2
-
3
1
  /** Postgres string functions. */
4
- export const lower = postgresQuery.lower
5
- export const upper = postgresQuery.upper
6
- export const concat = postgresQuery.concat
2
+ export { lower, upper, concat } from "../internal/dsl.js"
@@ -1,8 +1,9 @@
1
1
  import type * as Schema from "effect/Schema"
2
2
 
3
- import type * as Expression from "../../internal/expression.js"
3
+ import type * as Expression from "../../internal/scalar.js"
4
4
  import type * as ExpressionAst from "../../internal/expression-ast.js"
5
5
  import { makeExpression } from "../../internal/query.js"
6
+ import { postgresDatatypes } from "../datatypes/index.js"
6
7
  import {
7
8
  InstantStringSchema,
8
9
  LocalDateStringSchema,
@@ -14,21 +15,19 @@ import {
14
15
  type LocalDateTimeString,
15
16
  type LocalTimeString,
16
17
  type OffsetTimeString
17
- } from "../../internal/runtime-value.js"
18
+ } from "../../internal/runtime/value.js"
18
19
 
19
20
  type TemporalExpression<
20
21
  Runtime,
21
22
  Db extends Expression.DbType.Any,
22
23
  Name extends string
23
- > = Expression.Expression<
24
+ > = Expression.Scalar<
24
25
  Runtime,
25
26
  Db,
26
27
  "never",
27
28
  "postgres",
28
29
  "scalar",
29
- never,
30
- {},
31
- "resolved"
30
+ never
32
31
  > & {
33
32
  readonly [ExpressionAst.TypeId]: ExpressionAst.FunctionCallNode<Name, readonly []>
34
33
  }
@@ -48,10 +47,8 @@ const makeTemporal = <
48
47
  runtimeSchema,
49
48
  nullability: "never",
50
49
  dialect: "postgres",
51
- aggregation: "scalar",
52
- source: undefined as never,
50
+ kind: "scalar",
53
51
  dependencies: {},
54
- sourceNullability: "resolved"
55
52
  }, {
56
53
  kind: "function",
57
54
  name,
@@ -62,7 +59,7 @@ const makeTemporal = <
62
59
  export const now = () =>
63
60
  makeTemporal(
64
61
  "now",
65
- { dialect: "postgres", kind: "timestamptz" } as Expression.DbType.PgTimestamptz,
62
+ postgresDatatypes.timestamptz(),
66
63
  InstantStringSchema
67
64
  )
68
65
 
@@ -70,7 +67,7 @@ export const now = () =>
70
67
  export const currentDate = () =>
71
68
  makeTemporal(
72
69
  "current_date",
73
- { dialect: "postgres", kind: "date" } as Expression.DbType.PgDate,
70
+ postgresDatatypes.date(),
74
71
  LocalDateStringSchema
75
72
  )
76
73
 
@@ -78,7 +75,7 @@ export const currentDate = () =>
78
75
  export const currentTime = () =>
79
76
  makeTemporal(
80
77
  "current_time",
81
- { dialect: "postgres", kind: "timetz" } as Expression.DbType.PgTimetz,
78
+ postgresDatatypes.timetz(),
82
79
  OffsetTimeStringSchema
83
80
  )
84
81
 
@@ -86,7 +83,7 @@ export const currentTime = () =>
86
83
  export const currentTimestamp = () =>
87
84
  makeTemporal(
88
85
  "current_timestamp",
89
- { dialect: "postgres", kind: "timestamptz" } as Expression.DbType.PgTimestamptz,
86
+ postgresDatatypes.timestamptz(),
90
87
  InstantStringSchema
91
88
  )
92
89
 
@@ -94,7 +91,7 @@ export const currentTimestamp = () =>
94
91
  export const localTime = () =>
95
92
  makeTemporal(
96
93
  "localtime",
97
- { dialect: "postgres", kind: "time" } as Expression.DbType.PgTime,
94
+ postgresDatatypes.time(),
98
95
  LocalTimeStringSchema
99
96
  )
100
97
 
@@ -102,6 +99,6 @@ export const localTime = () =>
102
99
  export const localTimestamp = () =>
103
100
  makeTemporal(
104
101
  "localtimestamp",
105
- { dialect: "postgres", kind: "timestamp" } as Expression.DbType.PgTimestamp,
102
+ postgresDatatypes.timestamp(),
106
103
  LocalDateTimeStringSchema
107
104
  )
@@ -1,7 +1,2 @@
1
- import { postgresQuery } from "../private/query.js"
2
-
3
1
  /** Postgres window functions. */
4
- export const over = postgresQuery.over
5
- export const rowNumber = postgresQuery.rowNumber
6
- export const rank = postgresQuery.rank
7
- export const denseRank = postgresQuery.denseRank
2
+ export { over, rowNumber, rank, denseRank } from "../internal/dsl.js"
@@ -1,15 +1,21 @@
1
- import type { RenderState, SqlDialect } from "./dialect.js"
1
+ import type { RenderState, RenderValueContext, SqlDialect } from "../../internal/dialect.js"
2
+ import { toDriverValue } from "../../internal/runtime/driver-value-mapping.js"
2
3
 
3
4
  const quoteIdentifier = (value: string): string => `"${value.replaceAll("\"", "\"\"")}"`
4
5
 
5
- const renderLiteral = (value: unknown, state: RenderState): string => {
6
- if (value === null) {
6
+ const renderLiteral = (value: unknown, state: RenderState, context: RenderValueContext = {}): string => {
7
+ const driverValue = toDriverValue(value, {
8
+ dialect: "postgres",
9
+ valueMappings: state.valueMappings,
10
+ ...context
11
+ })
12
+ if (driverValue === null) {
7
13
  return "null"
8
14
  }
9
- if (typeof value === "boolean") {
10
- return value ? "true" : "false"
15
+ if (typeof driverValue === "boolean") {
16
+ return driverValue ? "true" : "false"
11
17
  }
12
- state.params.push(value)
18
+ state.params.push(driverValue)
13
19
  return `$${state.params.length}`
14
20
  }
15
21