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
@@ -1,5 +1,3 @@
1
- import * as Expression from "../internal/expression.js"
2
- import { mysqlDatatypes } from "./datatypes/index.js"
3
1
  import {
4
2
  type CapabilitiesOfPlan,
5
3
  type CompletePlan,
@@ -37,22 +35,12 @@ import {
37
35
  type StatementOfPlan,
38
36
  type StringExpressionInput
39
37
  } from "../internal/query.js"
40
- import { makeDialectQuery } from "../internal/query-factory.js"
41
-
42
- const mysqlQuery = makeDialectQuery({
43
- dialect: "mysql",
44
- textDb: { dialect: "mysql", kind: "text" } as Expression.DbType.MySqlText,
45
- numericDb: { dialect: "mysql", kind: "double" } as Expression.DbType.MySqlDouble,
46
- boolDb: { dialect: "mysql", kind: "boolean" } as Expression.DbType.MySqlBool,
47
- timestampDb: { dialect: "mysql", kind: "timestamp" } as Expression.DbType.MySqlTimestamp,
48
- nullDb: { dialect: "mysql", kind: "null" } as Expression.DbType.Base<"mysql", "null">,
49
- type: mysqlDatatypes
50
- })
38
+ import { mysqlQuery } from "./private/query.js"
51
39
 
52
40
  export const literal = mysqlQuery.literal
41
+ export const column = mysqlQuery.column
53
42
  export const cast = mysqlQuery.cast
54
43
  export const type = mysqlQuery.type
55
- export const json = mysqlQuery.json
56
44
  export const eq = mysqlQuery.eq
57
45
  export const neq = mysqlQuery.neq
58
46
  export const lt = mysqlQuery.lt
@@ -61,10 +49,12 @@ export const gt = mysqlQuery.gt
61
49
  export const gte = mysqlQuery.gte
62
50
  export const isNull = mysqlQuery.isNull
63
51
  export const isNotNull = mysqlQuery.isNotNull
64
- export const upper = mysqlQuery.upper
65
- export const lower = mysqlQuery.lower
66
52
  export const like = mysqlQuery.like
67
53
  export const ilike = mysqlQuery.ilike
54
+ export const regexMatch = mysqlQuery.regexMatch
55
+ export const regexIMatch = mysqlQuery.regexIMatch
56
+ export const regexNotMatch = mysqlQuery.regexNotMatch
57
+ export const regexNotIMatch = mysqlQuery.regexNotIMatch
68
58
  export const and = mysqlQuery.and
69
59
  export const or = mysqlQuery.or
70
60
  export const not = mysqlQuery.not
@@ -72,22 +62,13 @@ export const all = mysqlQuery.all
72
62
  export const any = mysqlQuery.any
73
63
  const case_ = mysqlQuery.case
74
64
  export const match = mysqlQuery.match
75
- export const coalesce = mysqlQuery.coalesce
76
65
  export const in_ = mysqlQuery.in
77
66
  export const notIn = mysqlQuery.notIn
78
67
  export const between = mysqlQuery.between
79
68
  export const contains = mysqlQuery.contains
80
69
  export const containedBy = mysqlQuery.containedBy
81
70
  export const overlaps = mysqlQuery.overlaps
82
- export const concat = mysqlQuery.concat
83
71
  export const exists = mysqlQuery.exists
84
- export const over = mysqlQuery.over
85
- export const rowNumber = mysqlQuery.rowNumber
86
- export const rank = mysqlQuery.rank
87
- export const denseRank = mysqlQuery.denseRank
88
- export const count = mysqlQuery.count
89
- export const max = mysqlQuery.max
90
- export const min = mysqlQuery.min
91
72
  export const isDistinctFrom = mysqlQuery.isDistinctFrom
92
73
  export const isNotDistinctFrom = mysqlQuery.isNotDistinctFrom
93
74
  export const excluded = mysqlQuery.excluded
@@ -103,7 +84,6 @@ export const values = mysqlQuery.values
103
84
  export const unnest = mysqlQuery.unnest
104
85
  export const generateSeries = mysqlQuery.generateSeries
105
86
  export const returning = mysqlQuery.returning
106
- export const defaultValues = mysqlQuery.defaultValues
107
87
  export const onConflict = mysqlQuery.onConflict
108
88
  export const insert = mysqlQuery.insert
109
89
  export const update = mysqlQuery.update
package/src/mysql.ts CHANGED
@@ -6,6 +6,8 @@ export * as Datatypes from "./mysql/datatypes/index.js"
6
6
  export * as Errors from "./mysql/errors/index.js"
7
7
  /** Shared scalar SQL expression interfaces and DB-type descriptors. */
8
8
  export * as Expression from "./internal/expression.js"
9
+ /** MySQL-specialized SQL function expressions. */
10
+ export * as Function from "./mysql/function/index.js"
9
11
  /** MySQL-specialized typed query execution contracts. */
10
12
  export * as Executor from "./mysql/executor.js"
11
13
  /** Shared logical query-plan interfaces. */
@@ -0,0 +1,31 @@
1
+ import type * as Expression from "../internal/expression.js"
2
+ import { postgresQuery } from "./private/query.js"
3
+
4
+ type CastInput = Parameters<typeof postgresQuery.cast>[0]
5
+ type CastTarget = Parameters<typeof postgresQuery.cast>[1]
6
+ type CastExpression<Target extends CastTarget> = Expression.Expression<
7
+ Expression.RuntimeOfDbType<Target>,
8
+ Target,
9
+ Expression.Nullability,
10
+ string,
11
+ Expression.AggregationKind,
12
+ any,
13
+ Expression.SourceDependencies,
14
+ Expression.SourceNullabilityMode
15
+ >
16
+
17
+ const to: {
18
+ <Value extends CastInput, Target extends CastTarget>(
19
+ value: Value,
20
+ target: Target
21
+ ): CastExpression<Target>
22
+ <Target extends CastTarget>(
23
+ target: Target
24
+ ): <Value extends CastInput>(value: Value) => CastExpression<Target>
25
+ } = ((...args: [CastInput, CastTarget] | [CastTarget]) =>
26
+ args.length === 1
27
+ ? ((value: CastInput) => postgresQuery.cast(value as never, args[0] as never))
28
+ : postgresQuery.cast(args[0] as never, args[1] as never)) as unknown as typeof to
29
+
30
+ /** Postgres cast helpers. */
31
+ export const cast = { to }
@@ -1,11 +1,37 @@
1
1
  export {
2
+ brand,
3
+ ddlType,
4
+ int2,
2
5
  boolean,
3
6
  date,
7
+ default_ as default,
8
+ int8,
9
+ float4,
10
+ float8,
4
11
  generated,
5
- hasDefault,
12
+ array,
13
+ identityAlways,
14
+ identityByDefault,
15
+ foreignKey,
16
+ index,
6
17
  int,
18
+ char,
19
+ varchar,
7
20
  json,
21
+ jsonb,
8
22
  nullable,
23
+ time,
24
+ timetz,
25
+ timestamptz,
26
+ interval,
27
+ bytea,
28
+ name,
29
+ oid,
30
+ regclass,
31
+ bit,
32
+ varbit,
33
+ xml,
34
+ pg_lsn,
9
35
  number,
10
36
  primaryKey,
11
37
  references,
@@ -1,8 +1,43 @@
1
- import { makeDatatypeModule } from "../../internal/datatypes/define.js"
2
- import { postgresDatatypeKinds } from "./spec.js"
1
+ import type { DatatypeModule } from "../../internal/datatypes/define.js"
2
+ import type * as Expression from "../../internal/expression.js"
3
+ import { postgresDatatypeFamilies, postgresDatatypeKinds } from "./spec.js"
3
4
 
4
- export const postgresDatatypes = makeDatatypeModule("postgres", postgresDatatypeKinds, {
5
- boolean: "bool"
6
- })
5
+ const postgresDatatypeModule = {
6
+ custom: (kind: string) => ({
7
+ dialect: "postgres",
8
+ kind
9
+ }),
10
+ boolean: () => ({
11
+ dialect: "postgres",
12
+ kind: "bool"
13
+ })
14
+ } as Record<string, (...args: readonly any[]) => Expression.DbType.Base<"postgres", string>>
15
+
16
+ for (const kind of Object.keys(postgresDatatypeKinds)) {
17
+ postgresDatatypeModule[kind] = () => ({
18
+ dialect: "postgres",
19
+ kind
20
+ })
21
+ }
22
+
23
+ export const postgresDatatypes = {
24
+ ...(postgresDatatypeModule as DatatypeModule<
25
+ "postgres",
26
+ typeof postgresDatatypeKinds,
27
+ { readonly boolean: "bool" }
28
+ >),
29
+ json: (): Expression.DbType.Json<"postgres", "json"> => ({
30
+ dialect: "postgres",
31
+ kind: "json",
32
+ variant: "json"
33
+ }),
34
+ jsonb: (): Expression.DbType.Json<"postgres", "jsonb"> => ({
35
+ dialect: "postgres",
36
+ kind: "jsonb",
37
+ variant: "jsonb"
38
+ })
39
+ }
40
+
41
+ export { postgresDatatypeFamilies, postgresDatatypeKinds }
7
42
 
8
43
  export type PostgresDatatypeModule = typeof postgresDatatypes
@@ -2,8 +2,8 @@ import * as Effect from "effect/Effect"
2
2
  import * as SqlClient from "@effect/sql/SqlClient"
3
3
 
4
4
  import * as CoreExecutor from "../internal/executor.js"
5
- import * as Query from "./query.js"
6
- import * as Renderer from "./renderer.js"
5
+ import * as CoreQuery from "../internal/query.js"
6
+ import * as CoreRenderer from "../internal/renderer.js"
7
7
  import {
8
8
  narrowPostgresDriverErrorForReadQuery,
9
9
  normalizePostgresDriverError,
@@ -19,17 +19,19 @@ export type RowDecodeError = CoreExecutor.RowDecodeError
19
19
  export type Driver<Error = never, Context = never> = CoreExecutor.Driver<"postgres", Error, Context>
20
20
  /** Postgres-specialized executor contract. */
21
21
  export type Executor<Error = never, Context = never> = CoreExecutor.Executor<"postgres", Error, Context>
22
+ /** Postgres-specialized renderer contract. */
23
+ export type Renderer = CoreRenderer.Renderer<"postgres">
22
24
  /** Optional renderer / driver overrides for the standard Postgres executor pipeline. */
23
25
  export interface MakeOptions<Error = never, Context = never> {
24
- readonly renderer?: Renderer.Renderer
26
+ readonly renderer?: Renderer
25
27
  readonly driver?: Driver<Error, Context>
26
28
  readonly driverMode?: CoreExecutor.DriverMode
27
29
  }
28
30
  /** Standard composed error shape for Postgres executors. */
29
31
  export type PostgresExecutorError = PostgresDriverError | RowDecodeError
30
32
  /** Read-query error surface emitted by built-in Postgres executors. */
31
- export type PostgresQueryError<PlanValue extends Query.QueryPlan<any, any, any, any, any, any, any, any, any, any>> =
32
- Exclude<Query.CapabilitiesOfPlan<PlanValue>, "read"> extends never ? PostgresReadQueryError : PostgresExecutorError
33
+ export type PostgresQueryError<PlanValue extends CoreQuery.QueryPlan<any, any, any, any, any, any, any, any, any, any>> =
34
+ Exclude<CoreQuery.CapabilitiesOfPlan<PlanValue>, "read"> extends never ? PostgresReadQueryError : PostgresExecutorError
33
35
 
34
36
  /** Runs an effect within the ambient Postgres SQL transaction service. */
35
37
  export const withTransaction = CoreExecutor.withTransaction
@@ -39,9 +41,9 @@ export const withSavepoint = CoreExecutor.withSavepoint
39
41
  /** Postgres executor whose error channel narrows based on the query plan. */
40
42
  export interface QueryExecutor<Context = never> {
41
43
  readonly dialect: "postgres"
42
- execute<PlanValue extends Query.QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
43
- plan: Query.DialectCompatiblePlan<PlanValue, "postgres">
44
- ): Effect.Effect<Query.ResultRows<PlanValue>, PostgresQueryError<PlanValue>, Context>
44
+ execute<PlanValue extends CoreQuery.QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
45
+ plan: CoreQuery.DialectCompatiblePlan<PlanValue, "postgres">
46
+ ): Effect.Effect<CoreQuery.ResultRows<PlanValue>, PostgresQueryError<PlanValue>, Context>
45
47
  }
46
48
 
47
49
  /** Constructs a Postgres-specialized SQL driver. */
@@ -56,7 +58,7 @@ const fromDriver = <
56
58
  Error = never,
57
59
  Context = never
58
60
  >(
59
- renderer: Renderer.Renderer,
61
+ renderer: Renderer,
60
62
  sqlDriver: Driver<Error, Context>,
61
63
  driverMode: CoreExecutor.DriverMode = "raw"
62
64
  ): QueryExecutor<Context> => ({
@@ -85,7 +87,7 @@ const fromDriver = <
85
87
  })
86
88
 
87
89
  const sqlClientDriver = (): Driver<any, SqlClient.SqlClient> =>
88
- driver((query: Renderer.RenderedQuery<any>) =>
90
+ driver((query: CoreRenderer.RenderedQuery<any, "postgres">) =>
89
91
  Effect.flatMap(SqlClient.SqlClient, (sql) =>
90
92
  sql.unsafe<FlatRow>(query.sql, [...query.params])))
91
93
 
@@ -99,13 +101,13 @@ const sqlClientDriver = (): Driver<any, SqlClient.SqlClient> =>
99
101
  export function make(): QueryExecutor<SqlClient.SqlClient>
100
102
  export function make(
101
103
  options: {
102
- readonly renderer?: Renderer.Renderer
104
+ readonly renderer?: Renderer
103
105
  readonly driverMode?: CoreExecutor.DriverMode
104
106
  }
105
107
  ): QueryExecutor<SqlClient.SqlClient>
106
108
  export function make<Error = never, Context = never>(
107
109
  options: {
108
- readonly renderer?: Renderer.Renderer
110
+ readonly renderer?: Renderer
109
111
  readonly driver: Driver<Error, Context>
110
112
  readonly driverMode?: CoreExecutor.DriverMode
111
113
  }
@@ -114,9 +116,9 @@ export function make<Error = never, Context = never>(
114
116
  options: MakeOptions<Error, Context> = {}
115
117
  ): QueryExecutor<any> {
116
118
  if (options.driver) {
117
- return fromDriver(options.renderer ?? Renderer.make(), options.driver, options.driverMode)
119
+ return fromDriver(options.renderer ?? CoreRenderer.make("postgres"), options.driver, options.driverMode)
118
120
  }
119
- return fromDriver(options.renderer ?? Renderer.make(), sqlClientDriver(), options.driverMode)
121
+ return fromDriver(options.renderer ?? CoreRenderer.make("postgres"), sqlClientDriver(), options.driverMode)
120
122
  }
121
123
 
122
124
  /** Creates a Postgres-specialized executor from a typed implementation callback. */
@@ -124,8 +126,8 @@ export const custom = <
124
126
  Error = never,
125
127
  Context = never
126
128
  >(
127
- execute: <PlanValue extends Query.QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
128
- plan: Query.DialectCompatiblePlan<PlanValue, "postgres">
129
- ) => Effect.Effect<Query.ResultRows<PlanValue>, Error, Context>
129
+ execute: <PlanValue extends CoreQuery.QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
130
+ plan: CoreQuery.DialectCompatiblePlan<PlanValue, "postgres">
131
+ ) => Effect.Effect<CoreQuery.ResultRows<PlanValue>, Error, Context>
130
132
  ): Executor<Error, Context> =>
131
133
  CoreExecutor.make("postgres", execute as any) as Executor<Error, Context>
@@ -0,0 +1,6 @@
1
+ import { postgresQuery } from "../private/query.js"
2
+
3
+ /** Postgres aggregate functions. */
4
+ export const count = postgresQuery.count
5
+ export const max = postgresQuery.max
6
+ export const min = postgresQuery.min
@@ -0,0 +1,16 @@
1
+ import type { ExpressionInput } from "../query.js"
2
+ import { postgresQuery } from "../private/query.js"
3
+ import { isSequenceDefinition, type SequenceDefinition } from "../schema-management.js"
4
+
5
+ /** Postgres scalar core functions. */
6
+ export const coalesce = postgresQuery.coalesce
7
+ export const call = postgresQuery.call
8
+ export const uuidGenerateV4 = postgresQuery.uuidGenerateV4
9
+ export const nextVal = (
10
+ value: ExpressionInput | SequenceDefinition<string, string | undefined>
11
+ ) =>
12
+ postgresQuery.nextVal(
13
+ isSequenceDefinition(value)
14
+ ? postgresQuery.cast(postgresQuery.literal(value.qualifiedName()), postgresQuery.type.regclass())
15
+ : value
16
+ )
@@ -0,0 +1,20 @@
1
+ export * as core from "./core.js"
2
+ export * as string from "./string.js"
3
+ export * as aggregate from "./aggregate.js"
4
+ export * as window from "./window.js"
5
+ export { json, jsonb } from "./json.js"
6
+ export * as temporal from "./temporal.js"
7
+
8
+ export { coalesce } from "./core.js"
9
+ export { call, uuidGenerateV4, nextVal } from "./core.js"
10
+ export { lower, upper, concat } from "./string.js"
11
+ export { count, max, min } from "./aggregate.js"
12
+ export { over, rowNumber, rank, denseRank } from "./window.js"
13
+ export {
14
+ currentDate,
15
+ currentTime,
16
+ currentTimestamp,
17
+ localTime,
18
+ localTimestamp,
19
+ now
20
+ } from "./temporal.js"