effect-qb 0.17.0 → 0.19.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.
- package/README.md +4 -0
- package/dist/index.js +8065 -0
- package/dist/mysql.js +3053 -2505
- package/dist/postgres/metadata.js +1366 -1250
- package/dist/postgres.js +2020 -2719
- package/dist/sqlite.js +3226 -2732
- package/dist/standard.js +8019 -0
- package/package.json +10 -3
- package/src/casing.ts +71 -0
- package/src/index.ts +2 -0
- package/src/internal/casing.ts +89 -0
- package/src/internal/column-state.ts +11 -6
- package/src/internal/column.ts +44 -7
- package/src/internal/datatypes/define.ts +2 -1
- package/src/internal/datatypes/enrich.ts +23 -0
- package/src/internal/datatypes/lookup.ts +14 -7
- package/src/internal/derived-table.ts +4 -36
- package/src/{mysql/internal/sql-expression-renderer.ts → internal/dialect-renderers/mysql.ts} +548 -359
- package/src/{postgres/internal/sql-expression-renderer.ts → internal/dialect-renderers/postgres.ts} +654 -399
- package/src/{sqlite/internal/sql-expression-renderer.ts → internal/dialect-renderers/sqlite.ts} +501 -345
- package/src/internal/dialect.ts +35 -0
- package/src/internal/dsl-mutation-runtime.ts +12 -162
- package/src/internal/dsl-plan-runtime.ts +10 -138
- package/src/internal/dsl-query-runtime.ts +5 -79
- package/src/internal/dsl-transaction-ddl-runtime.ts +41 -65
- package/src/internal/executor.ts +10 -6
- package/src/internal/grouping-key.ts +87 -20
- package/src/internal/implication-runtime.ts +1 -1
- package/src/internal/predicate/runtime.ts +3 -0
- package/src/internal/query.d.ts +38 -11
- package/src/internal/query.ts +64 -25
- package/src/internal/renderer.ts +26 -14
- package/src/internal/runtime/normalize.ts +12 -5
- package/src/internal/scalar.ts +6 -1
- package/src/internal/schema-derivation.d.ts +12 -61
- package/src/internal/schema-derivation.ts +90 -38
- package/src/internal/schema-expression.ts +2 -2
- package/src/internal/sql-expression-renderer.ts +19 -0
- package/src/internal/standard-dsl.ts +6885 -0
- package/src/internal/table-options.ts +126 -66
- package/src/internal/table.d.ts +33 -32
- package/src/internal/table.ts +406 -155
- package/src/mysql/column-extension.ts +3 -0
- package/src/mysql/column.ts +10 -11
- package/src/mysql/datatypes/index.ts +3 -2
- package/src/mysql/executor.ts +7 -5
- package/src/mysql/internal/dialect.ts +9 -4
- package/src/mysql/internal/dsl.ts +219 -155
- package/src/mysql/internal/renderer.ts +6 -2
- package/src/mysql/json.ts +37 -0
- package/src/mysql/query-extension.ts +16 -0
- package/src/mysql/renderer.ts +31 -4
- package/src/mysql.ts +4 -12
- package/src/postgres/column-extension.ts +28 -0
- package/src/postgres/column.ts +5 -11
- package/src/postgres/datatypes/index.d.ts +2 -1
- package/src/postgres/datatypes/index.ts +3 -2
- package/src/postgres/executor.ts +7 -5
- package/src/postgres/function/core.ts +1 -3
- package/src/postgres/function/index.ts +1 -17
- package/src/postgres/internal/dialect.ts +9 -4
- package/src/postgres/internal/dsl.ts +208 -160
- package/src/postgres/internal/renderer.ts +6 -2
- package/src/postgres/internal/schema-ddl.ts +22 -10
- package/src/postgres/internal/schema-model.ts +238 -7
- package/src/postgres/json.ts +43 -7
- package/src/postgres/jsonb.ts +38 -0
- package/src/postgres/query-extension.ts +2 -0
- package/src/postgres/renderer.ts +31 -4
- package/src/postgres/schema-management.ts +17 -12
- package/src/postgres/schema.ts +98 -15
- package/src/postgres/table.ts +193 -524
- package/src/postgres/type.ts +8 -7
- package/src/postgres.ts +9 -11
- package/src/sqlite/column-extension.ts +3 -0
- package/src/sqlite/column.ts +10 -11
- package/src/sqlite/datatypes/index.ts +3 -2
- package/src/sqlite/executor.ts +7 -5
- package/src/sqlite/internal/dialect.ts +9 -4
- package/src/sqlite/internal/dsl.ts +208 -155
- package/src/sqlite/internal/renderer.ts +6 -2
- package/src/sqlite/json.ts +37 -0
- package/src/sqlite/query-extension.ts +2 -0
- package/src/sqlite/renderer.ts +31 -4
- package/src/sqlite.ts +4 -12
- package/src/standard/column.ts +163 -0
- package/src/standard/datatypes/index.ts +83 -0
- package/src/standard/datatypes/spec.ts +98 -0
- package/src/standard/dialect.ts +40 -0
- package/src/standard/function/aggregate.ts +2 -0
- package/src/standard/function/core.ts +2 -0
- package/src/standard/function/index.ts +18 -0
- package/src/standard/function/string.ts +2 -0
- package/src/standard/function/temporal.ts +78 -0
- package/src/standard/function/window.ts +2 -0
- package/src/standard/internal/renderer.ts +45 -0
- package/src/standard/query.ts +152 -0
- package/src/standard/renderer.ts +21 -0
- package/src/standard/table.ts +147 -0
- package/src/standard.ts +18 -0
- package/src/internal/aggregation-validation.ts +0 -57
- package/src/mysql/table.ts +0 -183
- package/src/sqlite/table.ts +0 -183
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import * as Query from "../../internal/query.js"
|
|
2
2
|
import type * as Expression from "../../internal/scalar.js"
|
|
3
|
+
import type * as Casing from "../../internal/casing.js"
|
|
3
4
|
import { type RenderState } from "../../internal/dialect.js"
|
|
4
5
|
import { sqliteDialect } from "./dialect.js"
|
|
5
6
|
import { type Projection } from "../../internal/projections.js"
|
|
6
|
-
import { renderQueryAst } from "
|
|
7
|
+
import { renderQueryAst } from "../../internal/sql-expression-renderer.js"
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Internal rendered-query payload produced by the built-in SQLite renderer.
|
|
@@ -17,6 +18,7 @@ export interface SqliteRenderResult {
|
|
|
17
18
|
|
|
18
19
|
export interface SqliteRenderOptions {
|
|
19
20
|
readonly valueMappings?: Expression.DriverValueMappings
|
|
21
|
+
readonly casing?: Casing.Options
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
/**
|
|
@@ -29,9 +31,11 @@ export const renderSqlitePlan = <PlanValue extends Query.Plan.Any>(
|
|
|
29
31
|
const state: RenderState = {
|
|
30
32
|
params: [],
|
|
31
33
|
valueMappings: options.valueMappings,
|
|
34
|
+
casing: options.casing,
|
|
32
35
|
ctes: [],
|
|
33
36
|
cteNames: new Set<string>(),
|
|
34
|
-
cteSources: new Map<string, unknown>()
|
|
37
|
+
cteSources: new Map<string, unknown>(),
|
|
38
|
+
sourceNames: new Map()
|
|
35
39
|
}
|
|
36
40
|
const rendered = renderQueryAst(
|
|
37
41
|
Query.getAst(plan as Query.Plan.Any) as any,
|
package/src/sqlite/json.ts
CHANGED
|
@@ -1,2 +1,39 @@
|
|
|
1
1
|
/** SQLite JSON expression helpers. */
|
|
2
2
|
export { json } from "./internal/dsl.js"
|
|
3
|
+
import { json } from "./internal/dsl.js"
|
|
4
|
+
|
|
5
|
+
export const key = json.key
|
|
6
|
+
export const index = json.index
|
|
7
|
+
export const wildcard = json.wildcard
|
|
8
|
+
export const slice = json.slice
|
|
9
|
+
export const descend = json.descend
|
|
10
|
+
export const path = json.path
|
|
11
|
+
export const get = json.get
|
|
12
|
+
export const access = json.access
|
|
13
|
+
export const traverse = json.traverse
|
|
14
|
+
export const text = json.text
|
|
15
|
+
export const accessText = json.accessText
|
|
16
|
+
export const traverseText = json.traverseText
|
|
17
|
+
export const contains = json.contains
|
|
18
|
+
export const containedBy = json.containedBy
|
|
19
|
+
export const hasKey = json.hasKey
|
|
20
|
+
export const keyExists = json.keyExists
|
|
21
|
+
export const hasAnyKeys = json.hasAnyKeys
|
|
22
|
+
export const hasAllKeys = json.hasAllKeys
|
|
23
|
+
export const delete_ = json.delete
|
|
24
|
+
export { delete_ as delete }
|
|
25
|
+
export const remove = json.remove
|
|
26
|
+
export const set = json.set
|
|
27
|
+
export const insert = json.insert
|
|
28
|
+
export const concat = json.concat
|
|
29
|
+
export const merge = json.merge
|
|
30
|
+
export const buildObject = json.buildObject
|
|
31
|
+
export const buildArray = json.buildArray
|
|
32
|
+
export const toJson = json.toJson
|
|
33
|
+
export const toJsonb = json.toJsonb
|
|
34
|
+
export const typeOf = json.typeOf
|
|
35
|
+
export const length = json.length
|
|
36
|
+
export const keys = json.keys
|
|
37
|
+
export const stripNulls = json.stripNulls
|
|
38
|
+
export const pathExists = json.pathExists
|
|
39
|
+
export const pathMatch = json.pathMatch
|
package/src/sqlite/renderer.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { pipeArguments, type Pipeable } from "effect/Pipeable"
|
|
2
|
+
|
|
1
3
|
import * as CoreRenderer from "../internal/renderer.js"
|
|
4
|
+
import * as Casing from "../internal/casing.js"
|
|
2
5
|
import type * as Expression from "../internal/scalar.js"
|
|
6
|
+
import type { SqliteDatatypeFamily, SqliteDatatypeKind } from "./datatypes/spec.js"
|
|
3
7
|
import { renderSqlitePlan } from "./internal/renderer.js"
|
|
4
8
|
|
|
5
9
|
/** SQLite-specialized rendered query shape. */
|
|
@@ -7,18 +11,41 @@ export type RenderedQuery<Row> = CoreRenderer.RenderedQuery<Row, "sqlite">
|
|
|
7
11
|
/** Extracts the row type carried by a SQLite rendered query. */
|
|
8
12
|
export type RowOf<Value extends RenderedQuery<any>> = CoreRenderer.RowOf<Value>
|
|
9
13
|
/** SQLite-specialized renderer contract. */
|
|
10
|
-
export type Renderer = CoreRenderer.Renderer<"sqlite">
|
|
14
|
+
export type Renderer = CoreRenderer.Renderer<"sqlite"> & Pipeable & {
|
|
15
|
+
readonly [Casing.TypeId]: Casing.State
|
|
16
|
+
readonly withCasing: (options: Casing.Options) => Renderer
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type ValueMappings = Expression.DriverValueMappingsFor<SqliteDatatypeKind | "uuid", SqliteDatatypeFamily | "uuid">
|
|
11
20
|
|
|
12
21
|
export interface MakeOptions {
|
|
13
|
-
readonly valueMappings?:
|
|
22
|
+
readonly valueMappings?: ValueMappings
|
|
23
|
+
readonly casing?: Casing.Options
|
|
14
24
|
}
|
|
15
25
|
|
|
16
26
|
export { TypeId } from "../internal/renderer.js"
|
|
17
27
|
export type { Projection } from "../internal/renderer.js"
|
|
18
28
|
|
|
29
|
+
const RendererProto = {
|
|
30
|
+
pipe(this: Pipeable) {
|
|
31
|
+
return pipeArguments(this, arguments)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
19
35
|
/** Creates the built-in SQLite renderer. */
|
|
20
|
-
export const make = (options: MakeOptions = {}): Renderer =>
|
|
21
|
-
CoreRenderer.
|
|
36
|
+
export const make = (options: MakeOptions = {}): Renderer => {
|
|
37
|
+
const renderer = CoreRenderer.makeTrusted("sqlite", (plan) => renderSqlitePlan(plan, options))
|
|
38
|
+
return Object.assign(Object.create(RendererProto), renderer, {
|
|
39
|
+
[Casing.TypeId]: {
|
|
40
|
+
casing: options.casing
|
|
41
|
+
},
|
|
42
|
+
withCasing: (override: Casing.Options) =>
|
|
43
|
+
make({
|
|
44
|
+
...options,
|
|
45
|
+
casing: Casing.merge(options.casing, override)
|
|
46
|
+
})
|
|
47
|
+
}) as Renderer
|
|
48
|
+
}
|
|
22
49
|
|
|
23
50
|
/** Shared built-in SQLite renderer instance. */
|
|
24
51
|
export const sqlite = make()
|
package/src/sqlite.ts
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
|
-
/** SQLite-
|
|
2
|
-
export * as Column from "./sqlite/column.js"
|
|
1
|
+
/** SQLite-specific column extensions. Portable columns are exported from `effect-qb`. */
|
|
2
|
+
export * as Column from "./sqlite/column-extension.js"
|
|
3
3
|
/** SQLite datatype witnesses and coercion families. */
|
|
4
4
|
export * as Datatypes from "./sqlite/datatypes/index.js"
|
|
5
5
|
/** SQLite error catalog and error normalization helpers. */
|
|
6
6
|
export * as Errors from "./sqlite/errors/index.js"
|
|
7
|
-
/** Shared scalar SQL interfaces and DB-type descriptors. */
|
|
8
|
-
export * as Scalar from "./internal/scalar.js"
|
|
9
|
-
/** SQLite-specialized SQL function expressions. */
|
|
10
|
-
export * as Function from "./sqlite/function/index.js"
|
|
11
7
|
/** SQLite-specialized JSON expression helpers. */
|
|
12
8
|
export * as Json from "./sqlite/json.js"
|
|
13
9
|
/** SQLite-specialized typed query execution contracts. */
|
|
14
10
|
export * as Executor from "./sqlite/executor.js"
|
|
15
|
-
/**
|
|
16
|
-
export * as
|
|
17
|
-
/** SQLite-specialized query-construction DSL. */
|
|
18
|
-
export * as Query from "./sqlite/query.js"
|
|
19
|
-
/** SQLite-specialized table-definition DSL. */
|
|
20
|
-
export * as Table from "./sqlite/table.js"
|
|
11
|
+
/** SQLite-specific query helpers. Portable queries are exported from the root package. */
|
|
12
|
+
export * as Query from "./sqlite/query-extension.js"
|
|
21
13
|
/** SQLite-specialized built-in renderer entrypoint. */
|
|
22
14
|
export * as Renderer from "./sqlite/renderer.js"
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import * as Schema from "effect/Schema"
|
|
2
|
+
|
|
3
|
+
import * as BaseColumn from "../internal/column.js"
|
|
4
|
+
import { makeColumnDefinition, type AnyColumnDefinition, type ColumnDefinition } from "../internal/column-state.js"
|
|
5
|
+
import type * as Expression from "../internal/scalar.js"
|
|
6
|
+
import type { NonEmptyStringInput } from "../internal/table-options.js"
|
|
7
|
+
import { enrichDbType } from "../internal/datatypes/enrich.js"
|
|
8
|
+
import {
|
|
9
|
+
BigIntStringSchema,
|
|
10
|
+
DecimalStringSchema,
|
|
11
|
+
LocalDateStringSchema,
|
|
12
|
+
LocalDateTimeStringSchema,
|
|
13
|
+
LocalTimeStringSchema,
|
|
14
|
+
type BigIntString,
|
|
15
|
+
type DecimalString,
|
|
16
|
+
type LocalDateString,
|
|
17
|
+
type LocalDateTimeString,
|
|
18
|
+
type LocalTimeString
|
|
19
|
+
} from "../internal/runtime/value.js"
|
|
20
|
+
import { standardDatatypes } from "./datatypes/index.js"
|
|
21
|
+
|
|
22
|
+
const primitive = <Type, Db extends Expression.DbType.Any>(
|
|
23
|
+
schema: Schema.Schema<Type, any, any>,
|
|
24
|
+
dbType: Db
|
|
25
|
+
): ColumnDefinition<Type, Type, Type, Db, false, false, false, false, false, undefined> =>
|
|
26
|
+
makeColumnDefinition(schema as Schema.Schema<NonNullable<Type>>, {
|
|
27
|
+
dbType,
|
|
28
|
+
nullable: false,
|
|
29
|
+
hasDefault: false,
|
|
30
|
+
generated: false,
|
|
31
|
+
primaryKey: false,
|
|
32
|
+
unique: false,
|
|
33
|
+
references: undefined
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const renderNumericDdlType = (
|
|
37
|
+
kind: string,
|
|
38
|
+
options?: BaseColumn.NumericOptions
|
|
39
|
+
): string | undefined => {
|
|
40
|
+
if (options === undefined || options.precision === undefined) {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
return options.scale === undefined
|
|
44
|
+
? `${kind}(${options.precision})`
|
|
45
|
+
: `${kind}(${options.precision},${options.scale})`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const boundedString = (length?: number): Schema.Schema<string> =>
|
|
49
|
+
length === undefined
|
|
50
|
+
? Schema.String
|
|
51
|
+
: Schema.String.pipe(Schema.maxLength(length))
|
|
52
|
+
|
|
53
|
+
const finiteNumber = Schema.Number.pipe(Schema.finite())
|
|
54
|
+
|
|
55
|
+
export const custom = <SchemaType extends Schema.Schema.Any, Db extends Expression.DbType.Any>(
|
|
56
|
+
schema: SchemaType,
|
|
57
|
+
dbType: Db
|
|
58
|
+
) =>
|
|
59
|
+
makeColumnDefinition(schema as Schema.Schema<NonNullable<Schema.Schema.Type<SchemaType>>, any, any>, {
|
|
60
|
+
dbType: enrichDbType(standardDatatypes, dbType),
|
|
61
|
+
nullable: false,
|
|
62
|
+
hasDefault: false,
|
|
63
|
+
generated: false,
|
|
64
|
+
primaryKey: false,
|
|
65
|
+
unique: false,
|
|
66
|
+
references: undefined,
|
|
67
|
+
ddlType: undefined,
|
|
68
|
+
identity: undefined
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
export const uuid = () => primitive(Schema.UUID, standardDatatypes.uuid())
|
|
72
|
+
export const text = () => primitive(Schema.String, standardDatatypes.text())
|
|
73
|
+
export const varchar = (length?: number) =>
|
|
74
|
+
makeColumnDefinition(boundedString(length), {
|
|
75
|
+
dbType: standardDatatypes.varchar(),
|
|
76
|
+
nullable: false,
|
|
77
|
+
hasDefault: false,
|
|
78
|
+
generated: false,
|
|
79
|
+
primaryKey: false,
|
|
80
|
+
unique: false,
|
|
81
|
+
references: undefined,
|
|
82
|
+
ddlType: length === undefined ? "varchar" : `varchar(${length})`,
|
|
83
|
+
identity: undefined
|
|
84
|
+
})
|
|
85
|
+
export const char = (length = 1) =>
|
|
86
|
+
makeColumnDefinition(boundedString(length), {
|
|
87
|
+
dbType: standardDatatypes.char(),
|
|
88
|
+
nullable: false,
|
|
89
|
+
hasDefault: false,
|
|
90
|
+
generated: false,
|
|
91
|
+
primaryKey: false,
|
|
92
|
+
unique: false,
|
|
93
|
+
references: undefined,
|
|
94
|
+
ddlType: `char(${length})`,
|
|
95
|
+
identity: undefined
|
|
96
|
+
})
|
|
97
|
+
export const int = () => primitive(Schema.Int, standardDatatypes.int())
|
|
98
|
+
export const bigint = () => primitive(BigIntStringSchema, standardDatatypes.bigint())
|
|
99
|
+
export const number = (options?: BaseColumn.NumericOptions) =>
|
|
100
|
+
makeColumnDefinition(DecimalStringSchema, {
|
|
101
|
+
dbType: standardDatatypes.decimal(),
|
|
102
|
+
nullable: false,
|
|
103
|
+
hasDefault: false,
|
|
104
|
+
generated: false,
|
|
105
|
+
primaryKey: false,
|
|
106
|
+
unique: false,
|
|
107
|
+
references: undefined,
|
|
108
|
+
ddlType: renderNumericDdlType("decimal", options),
|
|
109
|
+
identity: undefined
|
|
110
|
+
})
|
|
111
|
+
export const real = () => primitive(finiteNumber, standardDatatypes.real())
|
|
112
|
+
export const boolean = () => primitive(Schema.Boolean, standardDatatypes.boolean())
|
|
113
|
+
export const date = () => primitive(LocalDateStringSchema, standardDatatypes.date())
|
|
114
|
+
export const time = () => primitive(LocalTimeStringSchema, standardDatatypes.time())
|
|
115
|
+
export const datetime = () => primitive(LocalDateTimeStringSchema, standardDatatypes.datetime())
|
|
116
|
+
export const timestamp = () => primitive(LocalDateTimeStringSchema, standardDatatypes.timestamp())
|
|
117
|
+
export const blob = () => primitive(Schema.Uint8ArrayFromSelf, standardDatatypes.blob())
|
|
118
|
+
export const json = <SchemaType extends Schema.Schema.Any>(schema: SchemaType) =>
|
|
119
|
+
makeColumnDefinition(schema as Schema.Schema<NonNullable<Schema.Schema.Type<SchemaType>>, any, any>, {
|
|
120
|
+
dbType: { ...standardDatatypes.json(), variant: "json" } as Expression.DbType.Json<"standard", "json">,
|
|
121
|
+
nullable: false,
|
|
122
|
+
hasDefault: false,
|
|
123
|
+
generated: false,
|
|
124
|
+
primaryKey: false,
|
|
125
|
+
unique: false,
|
|
126
|
+
references: undefined,
|
|
127
|
+
ddlType: undefined,
|
|
128
|
+
identity: undefined
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
export const nullable = BaseColumn.nullable
|
|
132
|
+
export const brand = BaseColumn.brand
|
|
133
|
+
export const primaryKey = BaseColumn.primaryKey
|
|
134
|
+
type UniqueColumn<Column extends AnyColumnDefinition> = ReturnType<typeof BaseColumn.unique<Column>>
|
|
135
|
+
|
|
136
|
+
type StandardUniqueOptions = {
|
|
137
|
+
readonly name?: string
|
|
138
|
+
readonly nullsNotDistinct?: never
|
|
139
|
+
readonly deferrable?: never
|
|
140
|
+
readonly initiallyDeferred?: never
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
type NonEmptyOptionNameInput<Options> = Options extends { readonly name: infer Name extends string }
|
|
144
|
+
? NonEmptyStringInput<Name> extends never ? never : unknown
|
|
145
|
+
: unknown
|
|
146
|
+
|
|
147
|
+
type UniqueModifier = {
|
|
148
|
+
<Column extends AnyColumnDefinition>(column: Column): UniqueColumn<Column>
|
|
149
|
+
readonly options: <const Options extends StandardUniqueOptions>(
|
|
150
|
+
options: Options & NonEmptyOptionNameInput<Options>
|
|
151
|
+
) => <Column extends AnyColumnDefinition>(column: Column) => UniqueColumn<Column>
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export const unique = BaseColumn.unique as UniqueModifier
|
|
155
|
+
const default_ = BaseColumn.default_
|
|
156
|
+
export const generated = BaseColumn.generated
|
|
157
|
+
export const driverValueMapping = BaseColumn.driverValueMapping
|
|
158
|
+
export const references = BaseColumn.references
|
|
159
|
+
export const schema = BaseColumn.schema
|
|
160
|
+
export { default_ as default }
|
|
161
|
+
|
|
162
|
+
export type Any = BaseColumn.Any
|
|
163
|
+
export type AnyBound = BaseColumn.AnyBound
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { DatatypeModule } from "../../internal/datatypes/define.js"
|
|
2
|
+
import type * as Expression from "../../internal/scalar.js"
|
|
3
|
+
import type { NonEmptyStringInput } from "../../internal/table-options.js"
|
|
4
|
+
import { standardDatatypeFamilies, standardDatatypeKinds } from "./spec.js"
|
|
5
|
+
|
|
6
|
+
const withMetadata = <Kind extends keyof typeof standardDatatypeKinds & string>(
|
|
7
|
+
kind: Kind
|
|
8
|
+
): Expression.DbType.Base<"standard", Kind> => {
|
|
9
|
+
const kindSpec = standardDatatypeKinds[kind]
|
|
10
|
+
const familySpec = standardDatatypeFamilies[kindSpec.family as keyof typeof standardDatatypeFamilies]
|
|
11
|
+
return {
|
|
12
|
+
dialect: "standard",
|
|
13
|
+
kind,
|
|
14
|
+
family: kindSpec.family,
|
|
15
|
+
runtime: kindSpec.runtime,
|
|
16
|
+
compareGroup: familySpec?.compareGroup,
|
|
17
|
+
castTargets: familySpec?.castTargets,
|
|
18
|
+
traits: familySpec?.traits
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const standardDatatypeModule = {
|
|
23
|
+
custom: <Kind extends string>(kind: NonEmptyStringInput<Kind>) => ({
|
|
24
|
+
dialect: "standard",
|
|
25
|
+
kind: kind as Kind
|
|
26
|
+
}),
|
|
27
|
+
uuid: () => ({
|
|
28
|
+
dialect: "standard",
|
|
29
|
+
kind: "uuid",
|
|
30
|
+
family: "uuid",
|
|
31
|
+
runtime: "string",
|
|
32
|
+
compareGroup: "uuid",
|
|
33
|
+
castTargets: ["uuid", "char", "varchar", "text"],
|
|
34
|
+
traits: {
|
|
35
|
+
textual: true
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
} as Record<string, (...args: readonly any[]) => Expression.DbType.Base<"standard", string>>
|
|
39
|
+
|
|
40
|
+
for (const kind of Object.keys(standardDatatypeKinds)) {
|
|
41
|
+
standardDatatypeModule[kind] = () => withMetadata(kind as keyof typeof standardDatatypeKinds & string)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type StandardUuidWitness = Expression.DbType.Base<"standard", "uuid"> & {
|
|
45
|
+
readonly family: "uuid"
|
|
46
|
+
readonly runtime: "string"
|
|
47
|
+
readonly compareGroup: "uuid"
|
|
48
|
+
readonly castTargets: readonly ["uuid", "char", "varchar", "text"]
|
|
49
|
+
readonly traits: {
|
|
50
|
+
readonly textual: true
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
type StandardJsonWitness = Expression.DbType.Base<"standard", "json"> & {
|
|
55
|
+
readonly family: "json"
|
|
56
|
+
readonly runtime: "json"
|
|
57
|
+
readonly compareGroup: "json"
|
|
58
|
+
readonly castTargets: readonly ["json", "text"]
|
|
59
|
+
readonly driverValueMapping: {
|
|
60
|
+
readonly toDriver: (value: unknown) => unknown
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
standardDatatypeModule.json = () => ({
|
|
65
|
+
...withMetadata("json"),
|
|
66
|
+
driverValueMapping: {
|
|
67
|
+
toDriver: (value: unknown) => JSON.stringify(value)
|
|
68
|
+
}
|
|
69
|
+
}) as StandardJsonWitness
|
|
70
|
+
|
|
71
|
+
export const standardDatatypes = {
|
|
72
|
+
...(standardDatatypeModule as DatatypeModule<
|
|
73
|
+
"standard",
|
|
74
|
+
typeof standardDatatypeKinds,
|
|
75
|
+
typeof standardDatatypeFamilies
|
|
76
|
+
> & {
|
|
77
|
+
readonly uuid: () => StandardUuidWitness
|
|
78
|
+
readonly json: () => StandardJsonWitness
|
|
79
|
+
}),
|
|
80
|
+
float8: () => withMetadata("real")
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export type StandardDatatypeModule = typeof standardDatatypes
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { DatatypeFamilySpec, DatatypeKindSpec } from "../../internal/datatypes/shape.js"
|
|
2
|
+
|
|
3
|
+
export const standardDatatypeFamilies = {
|
|
4
|
+
text: {
|
|
5
|
+
compareGroup: "text",
|
|
6
|
+
castTargets: ["text", "numeric", "integer", "real", "boolean", "date", "time", "datetime", "json", "blob", "null"],
|
|
7
|
+
traits: {
|
|
8
|
+
textual: true,
|
|
9
|
+
ordered: true
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
numeric: {
|
|
13
|
+
compareGroup: "numeric",
|
|
14
|
+
castTargets: ["numeric", "integer", "real", "text", "boolean", "date", "time", "datetime"],
|
|
15
|
+
traits: {
|
|
16
|
+
ordered: true
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
integer: {
|
|
20
|
+
compareGroup: "numeric",
|
|
21
|
+
castTargets: ["integer", "numeric", "real", "text", "boolean", "date", "time", "datetime"],
|
|
22
|
+
traits: {
|
|
23
|
+
ordered: true
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
real: {
|
|
27
|
+
compareGroup: "numeric",
|
|
28
|
+
castTargets: ["real", "numeric", "integer", "text", "boolean"],
|
|
29
|
+
traits: {
|
|
30
|
+
ordered: true
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
boolean: {
|
|
34
|
+
compareGroup: "boolean",
|
|
35
|
+
castTargets: ["boolean", "integer", "numeric", "text"],
|
|
36
|
+
traits: {}
|
|
37
|
+
},
|
|
38
|
+
date: {
|
|
39
|
+
compareGroup: "date",
|
|
40
|
+
castTargets: ["date", "time", "datetime", "text", "numeric", "integer"],
|
|
41
|
+
traits: {
|
|
42
|
+
ordered: true
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
time: {
|
|
46
|
+
compareGroup: "time",
|
|
47
|
+
castTargets: ["time", "date", "datetime", "text", "numeric", "integer"],
|
|
48
|
+
traits: {
|
|
49
|
+
ordered: true
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
datetime: {
|
|
53
|
+
compareGroup: "datetime",
|
|
54
|
+
castTargets: ["datetime", "date", "time", "text", "numeric", "integer"],
|
|
55
|
+
traits: {
|
|
56
|
+
ordered: true
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
json: {
|
|
60
|
+
compareGroup: "json",
|
|
61
|
+
castTargets: ["json", "text"],
|
|
62
|
+
traits: {}
|
|
63
|
+
},
|
|
64
|
+
blob: {
|
|
65
|
+
compareGroup: "blob",
|
|
66
|
+
castTargets: ["blob", "text"],
|
|
67
|
+
traits: {}
|
|
68
|
+
},
|
|
69
|
+
null: {
|
|
70
|
+
compareGroup: "null",
|
|
71
|
+
castTargets: ["text", "numeric", "integer", "real", "boolean", "date", "time", "datetime", "json", "blob", "null"],
|
|
72
|
+
traits: {}
|
|
73
|
+
}
|
|
74
|
+
} as const satisfies Record<string, DatatypeFamilySpec>
|
|
75
|
+
|
|
76
|
+
export const standardDatatypeKinds = {
|
|
77
|
+
text: { family: "text", runtime: "string" },
|
|
78
|
+
varchar: { family: "text", runtime: "string" },
|
|
79
|
+
char: { family: "text", runtime: "string" },
|
|
80
|
+
clob: { family: "text", runtime: "string" },
|
|
81
|
+
int: { family: "integer", runtime: "number" },
|
|
82
|
+
integer: { family: "integer", runtime: "number" },
|
|
83
|
+
bigint: { family: "integer", runtime: "bigintString" },
|
|
84
|
+
numeric: { family: "numeric", runtime: "decimalString" },
|
|
85
|
+
decimal: { family: "numeric", runtime: "decimalString" },
|
|
86
|
+
real: { family: "real", runtime: "number" },
|
|
87
|
+
double: { family: "real", runtime: "number" },
|
|
88
|
+
boolean: { family: "boolean", runtime: "boolean" },
|
|
89
|
+
date: { family: "date", runtime: "localDate" },
|
|
90
|
+
time: { family: "time", runtime: "localTime" },
|
|
91
|
+
datetime: { family: "datetime", runtime: "localDateTime" },
|
|
92
|
+
timestamp: { family: "datetime", runtime: "localDateTime" },
|
|
93
|
+
json: { family: "json", runtime: "json" },
|
|
94
|
+
blob: { family: "blob", runtime: "bytes" }
|
|
95
|
+
} as const satisfies Record<string, DatatypeKindSpec>
|
|
96
|
+
|
|
97
|
+
export type StandardDatatypeFamily = keyof typeof standardDatatypeFamilies
|
|
98
|
+
export type StandardDatatypeKind = keyof typeof standardDatatypeKinds
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { quoteDoubleQuotedIdentifier, type RenderState, type RenderValueContext, type SqlDialect } from "../internal/dialect.js"
|
|
2
|
+
import { renderExpression, renderQueryAst } from "../internal/dialect-renderers/postgres.js"
|
|
3
|
+
import { toDriverValue } from "../internal/runtime/driver-value-mapping.js"
|
|
4
|
+
|
|
5
|
+
const quoteIdentifier = quoteDoubleQuotedIdentifier
|
|
6
|
+
|
|
7
|
+
const renderLiteral = (value: unknown, state: RenderState, context: RenderValueContext = {}): string => {
|
|
8
|
+
const driverValue = toDriverValue(value, {
|
|
9
|
+
dialect: "standard",
|
|
10
|
+
valueMappings: state.valueMappings,
|
|
11
|
+
...context
|
|
12
|
+
})
|
|
13
|
+
if (driverValue === null) {
|
|
14
|
+
return "null"
|
|
15
|
+
}
|
|
16
|
+
if (typeof driverValue === "boolean") {
|
|
17
|
+
return driverValue ? "true" : "false"
|
|
18
|
+
}
|
|
19
|
+
state.params.push(driverValue)
|
|
20
|
+
return "?"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const standardDialect: SqlDialect<"standard"> = {
|
|
24
|
+
name: "standard",
|
|
25
|
+
quoteIdentifier,
|
|
26
|
+
renderLiteral,
|
|
27
|
+
renderTableReference(tableName, baseTableName, schemaName) {
|
|
28
|
+
const renderedBase = schemaName && schemaName !== "public"
|
|
29
|
+
? `${quoteIdentifier(schemaName)}.${quoteIdentifier(baseTableName)}`
|
|
30
|
+
: quoteIdentifier(baseTableName)
|
|
31
|
+
return tableName === baseTableName
|
|
32
|
+
? renderedBase
|
|
33
|
+
: `${renderedBase} as ${quoteIdentifier(tableName)}`
|
|
34
|
+
},
|
|
35
|
+
renderConcat(values) {
|
|
36
|
+
return `(${values.join(" || ")})`
|
|
37
|
+
},
|
|
38
|
+
renderQueryAst,
|
|
39
|
+
renderExpression
|
|
40
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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 * as temporal from "./temporal.js"
|
|
6
|
+
|
|
7
|
+
export { coalesce, call } from "./core.js"
|
|
8
|
+
export { lower, upper, concat } from "./string.js"
|
|
9
|
+
export { count, max, min } from "./aggregate.js"
|
|
10
|
+
export { over, rowNumber, rank, denseRank } from "./window.js"
|
|
11
|
+
export {
|
|
12
|
+
currentDate,
|
|
13
|
+
currentTime,
|
|
14
|
+
currentTimestamp,
|
|
15
|
+
localTime,
|
|
16
|
+
localTimestamp,
|
|
17
|
+
now
|
|
18
|
+
} from "./temporal.js"
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type * as Schema from "effect/Schema"
|
|
2
|
+
|
|
3
|
+
import type * as Expression from "../../internal/scalar.js"
|
|
4
|
+
import type * as ExpressionAst from "../../internal/expression-ast.js"
|
|
5
|
+
import { makeExpression } from "../../internal/query.js"
|
|
6
|
+
import {
|
|
7
|
+
InstantStringSchema,
|
|
8
|
+
LocalDateStringSchema,
|
|
9
|
+
LocalDateTimeStringSchema,
|
|
10
|
+
LocalTimeStringSchema,
|
|
11
|
+
type InstantString,
|
|
12
|
+
type LocalDateString,
|
|
13
|
+
type LocalDateTimeString,
|
|
14
|
+
type LocalTimeString
|
|
15
|
+
} from "../../internal/runtime/value.js"
|
|
16
|
+
import { standardDatatypes } from "../datatypes/index.js"
|
|
17
|
+
|
|
18
|
+
type TemporalExpression<
|
|
19
|
+
Runtime,
|
|
20
|
+
Db extends Expression.DbType.Any,
|
|
21
|
+
Name extends string
|
|
22
|
+
> = Expression.Scalar<
|
|
23
|
+
Runtime,
|
|
24
|
+
Db,
|
|
25
|
+
"never",
|
|
26
|
+
"standard",
|
|
27
|
+
"scalar",
|
|
28
|
+
never
|
|
29
|
+
> & {
|
|
30
|
+
readonly [ExpressionAst.TypeId]: ExpressionAst.FunctionCallNode<Name, readonly []>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const makeTemporal = <
|
|
34
|
+
Runtime,
|
|
35
|
+
Db extends Expression.DbType.Any,
|
|
36
|
+
Name extends string
|
|
37
|
+
>(
|
|
38
|
+
name: Name,
|
|
39
|
+
dbType: Db,
|
|
40
|
+
runtimeSchema: Schema.Schema<Runtime, any, any>
|
|
41
|
+
): TemporalExpression<Runtime, Db, Name> =>
|
|
42
|
+
makeExpression({
|
|
43
|
+
runtime: undefined as unknown as Runtime,
|
|
44
|
+
dbType,
|
|
45
|
+
runtimeSchema,
|
|
46
|
+
nullability: "never",
|
|
47
|
+
dialect: "standard",
|
|
48
|
+
kind: "scalar",
|
|
49
|
+
dependencies: {}
|
|
50
|
+
}, {
|
|
51
|
+
kind: "function",
|
|
52
|
+
name,
|
|
53
|
+
args: []
|
|
54
|
+
}) as TemporalExpression<Runtime, Db, Name>
|
|
55
|
+
|
|
56
|
+
/** Standard current instant. */
|
|
57
|
+
export const now = (): TemporalExpression<InstantString, ReturnType<typeof standardDatatypes.timestamp>, "now"> =>
|
|
58
|
+
makeTemporal("now", standardDatatypes.timestamp(), InstantStringSchema)
|
|
59
|
+
|
|
60
|
+
/** Standard current date. */
|
|
61
|
+
export const currentDate = (): TemporalExpression<LocalDateString, ReturnType<typeof standardDatatypes.date>, "current_date"> =>
|
|
62
|
+
makeTemporal("current_date", standardDatatypes.date(), LocalDateStringSchema)
|
|
63
|
+
|
|
64
|
+
/** Standard current time. */
|
|
65
|
+
export const currentTime = (): TemporalExpression<LocalTimeString, ReturnType<typeof standardDatatypes.time>, "current_time"> =>
|
|
66
|
+
makeTemporal("current_time", standardDatatypes.time(), LocalTimeStringSchema)
|
|
67
|
+
|
|
68
|
+
/** Standard current timestamp. */
|
|
69
|
+
export const currentTimestamp = (): TemporalExpression<LocalDateTimeString, ReturnType<typeof standardDatatypes.timestamp>, "current_timestamp"> =>
|
|
70
|
+
makeTemporal("current_timestamp", standardDatatypes.timestamp(), LocalDateTimeStringSchema)
|
|
71
|
+
|
|
72
|
+
/** Standard local time. */
|
|
73
|
+
export const localTime = (): TemporalExpression<LocalTimeString, ReturnType<typeof standardDatatypes.time>, "localtime"> =>
|
|
74
|
+
makeTemporal("localtime", standardDatatypes.time(), LocalTimeStringSchema)
|
|
75
|
+
|
|
76
|
+
/** Standard local timestamp. */
|
|
77
|
+
export const localTimestamp = (): TemporalExpression<LocalDateTimeString, ReturnType<typeof standardDatatypes.timestamp>, "localtimestamp"> =>
|
|
78
|
+
makeTemporal("localtimestamp", standardDatatypes.timestamp(), LocalDateTimeStringSchema)
|