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
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as Query from "../../internal/query.js"
|
|
2
|
+
import type * as Expression from "../../internal/scalar.js"
|
|
3
|
+
import type * as Casing from "../../internal/casing.js"
|
|
4
|
+
import { type RenderState } from "../../internal/dialect.js"
|
|
5
|
+
import { type Projection } from "../../internal/projections.js"
|
|
6
|
+
import { renderQueryAst } from "../../internal/sql-expression-renderer.js"
|
|
7
|
+
import { standardDialect } from "../dialect.js"
|
|
8
|
+
|
|
9
|
+
export interface StandardRenderResult {
|
|
10
|
+
readonly sql: string
|
|
11
|
+
readonly params: readonly unknown[]
|
|
12
|
+
readonly projections: readonly Projection[]
|
|
13
|
+
readonly valueMappings?: Expression.DriverValueMappings
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface StandardRenderOptions {
|
|
17
|
+
readonly valueMappings?: Expression.DriverValueMappings
|
|
18
|
+
readonly casing?: Casing.Options
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const renderStandardPlan = <PlanValue extends Query.Plan.Any>(
|
|
22
|
+
plan: Query.DialectCompatiblePlan<PlanValue, "standard">,
|
|
23
|
+
options: StandardRenderOptions = {}
|
|
24
|
+
): StandardRenderResult => {
|
|
25
|
+
const state: RenderState = {
|
|
26
|
+
params: [],
|
|
27
|
+
valueMappings: options.valueMappings,
|
|
28
|
+
casing: options.casing,
|
|
29
|
+
ctes: [],
|
|
30
|
+
cteNames: new Set<string>(),
|
|
31
|
+
cteSources: new Map<string, unknown>(),
|
|
32
|
+
sourceNames: new Map()
|
|
33
|
+
}
|
|
34
|
+
const rendered = renderQueryAst(
|
|
35
|
+
Query.getAst(plan as Query.Plan.Any) as any,
|
|
36
|
+
state,
|
|
37
|
+
standardDialect
|
|
38
|
+
)
|
|
39
|
+
return {
|
|
40
|
+
sql: rendered.sql,
|
|
41
|
+
params: state.params,
|
|
42
|
+
projections: rendered.projections,
|
|
43
|
+
valueMappings: state.valueMappings
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type as standardType
|
|
3
|
+
} from "../internal/standard-dsl.js"
|
|
4
|
+
import type * as Expression from "../internal/scalar.js"
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
literal,
|
|
8
|
+
column,
|
|
9
|
+
cast,
|
|
10
|
+
eq,
|
|
11
|
+
neq,
|
|
12
|
+
lt,
|
|
13
|
+
lte,
|
|
14
|
+
gt,
|
|
15
|
+
gte,
|
|
16
|
+
isNull,
|
|
17
|
+
isNotNull,
|
|
18
|
+
like,
|
|
19
|
+
ilike,
|
|
20
|
+
collate,
|
|
21
|
+
regexMatch,
|
|
22
|
+
regexIMatch,
|
|
23
|
+
regexNotMatch,
|
|
24
|
+
regexNotIMatch,
|
|
25
|
+
and,
|
|
26
|
+
or,
|
|
27
|
+
not,
|
|
28
|
+
all,
|
|
29
|
+
any,
|
|
30
|
+
case_ as case,
|
|
31
|
+
match,
|
|
32
|
+
in_ as in,
|
|
33
|
+
notIn,
|
|
34
|
+
between,
|
|
35
|
+
contains,
|
|
36
|
+
containedBy,
|
|
37
|
+
overlaps,
|
|
38
|
+
exists,
|
|
39
|
+
isDistinctFrom,
|
|
40
|
+
isNotDistinctFrom,
|
|
41
|
+
excluded,
|
|
42
|
+
as,
|
|
43
|
+
with_ as with,
|
|
44
|
+
withRecursive,
|
|
45
|
+
lateral,
|
|
46
|
+
scalar,
|
|
47
|
+
inSubquery,
|
|
48
|
+
compareAny,
|
|
49
|
+
compareAll,
|
|
50
|
+
values,
|
|
51
|
+
unnest,
|
|
52
|
+
select,
|
|
53
|
+
returning,
|
|
54
|
+
onConflict,
|
|
55
|
+
insert,
|
|
56
|
+
update,
|
|
57
|
+
upsert,
|
|
58
|
+
delete_ as delete,
|
|
59
|
+
truncate,
|
|
60
|
+
merge,
|
|
61
|
+
transaction,
|
|
62
|
+
commit,
|
|
63
|
+
rollback,
|
|
64
|
+
savepoint,
|
|
65
|
+
rollbackTo,
|
|
66
|
+
releaseSavepoint,
|
|
67
|
+
createTable,
|
|
68
|
+
dropTable,
|
|
69
|
+
createIndex,
|
|
70
|
+
dropIndex,
|
|
71
|
+
union,
|
|
72
|
+
unionAll,
|
|
73
|
+
intersect,
|
|
74
|
+
intersectAll,
|
|
75
|
+
except,
|
|
76
|
+
exceptAll,
|
|
77
|
+
where,
|
|
78
|
+
having,
|
|
79
|
+
from,
|
|
80
|
+
innerJoin,
|
|
81
|
+
leftJoin,
|
|
82
|
+
rightJoin,
|
|
83
|
+
fullJoin,
|
|
84
|
+
crossJoin,
|
|
85
|
+
distinct,
|
|
86
|
+
limit,
|
|
87
|
+
offset,
|
|
88
|
+
lock,
|
|
89
|
+
orderBy,
|
|
90
|
+
groupBy,
|
|
91
|
+
lower,
|
|
92
|
+
upper,
|
|
93
|
+
concat,
|
|
94
|
+
coalesce,
|
|
95
|
+
call,
|
|
96
|
+
count,
|
|
97
|
+
max,
|
|
98
|
+
min,
|
|
99
|
+
over,
|
|
100
|
+
rowNumber,
|
|
101
|
+
rank,
|
|
102
|
+
denseRank
|
|
103
|
+
} from "../internal/standard-dsl.js"
|
|
104
|
+
|
|
105
|
+
export { standardType as type }
|
|
106
|
+
export { union_query_capabilities } from "../internal/query.js"
|
|
107
|
+
|
|
108
|
+
export type MutationInputOf<Shape> = {
|
|
109
|
+
readonly [K in keyof Shape]:
|
|
110
|
+
| Shape[K]
|
|
111
|
+
| Expression.Scalar<Shape[K], Expression.DbType.Any, Expression.Nullability, "standard", Expression.ScalarKind, Expression.BindingId>
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export type {
|
|
115
|
+
AnyTableFunctionSource,
|
|
116
|
+
AnyUnnestSource,
|
|
117
|
+
AnyValuesSource,
|
|
118
|
+
CapabilitiesOfPlan,
|
|
119
|
+
CompletePlan,
|
|
120
|
+
CteSource,
|
|
121
|
+
DialectCompatiblePlan,
|
|
122
|
+
DerivedSourceRequiredError,
|
|
123
|
+
EffectiveNullability,
|
|
124
|
+
ExpressionInput,
|
|
125
|
+
ExpressionOutput,
|
|
126
|
+
GroupByInput,
|
|
127
|
+
HavingPredicateInput,
|
|
128
|
+
MergeCapabilities,
|
|
129
|
+
MergeCapabilityTuple,
|
|
130
|
+
MutationTargetLike,
|
|
131
|
+
NumericExpressionInput,
|
|
132
|
+
OrderDirection,
|
|
133
|
+
OutputOfSelection,
|
|
134
|
+
PredicateInput,
|
|
135
|
+
QueryCapability,
|
|
136
|
+
QueryPlan,
|
|
137
|
+
QueryRequirement,
|
|
138
|
+
QueryStatement,
|
|
139
|
+
ResultRow,
|
|
140
|
+
ResultRows,
|
|
141
|
+
RuntimeResultRow,
|
|
142
|
+
RuntimeResultRows,
|
|
143
|
+
SchemaTableLike,
|
|
144
|
+
SetCompatiblePlan,
|
|
145
|
+
SetCompatibleRightPlan,
|
|
146
|
+
SetOperator,
|
|
147
|
+
SourceCapabilitiesOf,
|
|
148
|
+
SourceRequiredOf,
|
|
149
|
+
SourceRequirementError,
|
|
150
|
+
StatementOfPlan,
|
|
151
|
+
StringExpressionInput
|
|
152
|
+
} from "../internal/query.js"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as CoreRenderer from "../internal/renderer.js"
|
|
2
|
+
import type * as Casing from "../internal/casing.js"
|
|
3
|
+
import type * as Expression from "../internal/scalar.js"
|
|
4
|
+
import type { StandardDatatypeFamily, StandardDatatypeKind } from "./datatypes/spec.js"
|
|
5
|
+
import { renderStandardPlan } from "./internal/renderer.js"
|
|
6
|
+
|
|
7
|
+
export type RenderedQuery<Row> = CoreRenderer.RenderedQuery<Row, "standard">
|
|
8
|
+
|
|
9
|
+
export type RowOf<Value extends RenderedQuery<any>> = CoreRenderer.RowOf<Value>
|
|
10
|
+
|
|
11
|
+
export type Renderer = CoreRenderer.Renderer<"standard">
|
|
12
|
+
|
|
13
|
+
export type ValueMappings = Expression.DriverValueMappingsFor<StandardDatatypeKind | "uuid", StandardDatatypeFamily | "uuid">
|
|
14
|
+
|
|
15
|
+
export interface MakeOptions {
|
|
16
|
+
readonly valueMappings?: ValueMappings
|
|
17
|
+
readonly casing?: Casing.Options
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const make = (options: MakeOptions = {}): Renderer =>
|
|
21
|
+
CoreRenderer.makeTrusted("standard", (plan) => renderStandardPlan(plan, options))
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { AnyColumnDefinition } from "../internal/column-state.js"
|
|
2
|
+
import * as BaseTable from "../internal/table.js"
|
|
3
|
+
|
|
4
|
+
type Dialect = string
|
|
5
|
+
|
|
6
|
+
type DialectColumn = AnyColumnDefinition
|
|
7
|
+
|
|
8
|
+
type DialectFieldMap = Record<string, DialectColumn>
|
|
9
|
+
|
|
10
|
+
type InlinePrimaryKeyKeys<Fields extends DialectFieldMap> = Extract<{
|
|
11
|
+
[K in keyof Fields]: Fields[K]["metadata"]["primaryKey"] extends true ? K : never
|
|
12
|
+
}[keyof Fields], string>
|
|
13
|
+
|
|
14
|
+
export type TableDefinition<
|
|
15
|
+
Name extends string,
|
|
16
|
+
Fields extends DialectFieldMap,
|
|
17
|
+
PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>,
|
|
18
|
+
Kind extends "schema" | "alias" = "schema",
|
|
19
|
+
SchemaName extends string = "public"
|
|
20
|
+
> = BaseTable.TableDefinition<Name, Fields, PrimaryKeyColumns, Kind, SchemaName>
|
|
21
|
+
|
|
22
|
+
export type TableClassStatic<
|
|
23
|
+
Name extends string,
|
|
24
|
+
Fields extends DialectFieldMap,
|
|
25
|
+
PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>,
|
|
26
|
+
SchemaName extends string = "public"
|
|
27
|
+
> = BaseTable.TableClassStatic<Name, Fields, PrimaryKeyColumns, SchemaName>
|
|
28
|
+
|
|
29
|
+
export type AnyTable = BaseTable.AnyTable<Dialect>
|
|
30
|
+
|
|
31
|
+
type FieldsOfTable<Table extends BaseTable.AnyTable> = Table[typeof BaseTable.TypeId]["fields"] extends infer Fields extends DialectFieldMap
|
|
32
|
+
? Fields
|
|
33
|
+
: never
|
|
34
|
+
|
|
35
|
+
type PrimaryKeyOfTable<Table extends BaseTable.AnyTable> = Table[typeof BaseTable.TypeId]["primaryKey"][number]
|
|
36
|
+
|
|
37
|
+
type SchemaNameOfTable<Table extends BaseTable.AnyTable> =
|
|
38
|
+
Table[typeof BaseTable.TypeId]["schemaName"] extends infer SchemaName extends string ? SchemaName : "public"
|
|
39
|
+
|
|
40
|
+
export type TableOption = BaseTable.TableOption
|
|
41
|
+
|
|
42
|
+
export const TypeId = BaseTable.TypeId
|
|
43
|
+
export const OptionsSymbol = BaseTable.OptionsSymbol
|
|
44
|
+
export const options = BaseTable.options
|
|
45
|
+
export const option = BaseTable.option
|
|
46
|
+
|
|
47
|
+
export function make<
|
|
48
|
+
Name extends string,
|
|
49
|
+
Fields extends DialectFieldMap
|
|
50
|
+
>(
|
|
51
|
+
name: BaseTable.NonEmptyStringInput<Name>,
|
|
52
|
+
fields: Fields & BaseTable.NonEmptyFieldMap<Fields>
|
|
53
|
+
): TableDefinition<Name, Fields, InlinePrimaryKeyKeys<Fields>, "schema", "public">
|
|
54
|
+
export function make<
|
|
55
|
+
Name extends string,
|
|
56
|
+
Fields extends DialectFieldMap,
|
|
57
|
+
const SchemaName extends string
|
|
58
|
+
>(
|
|
59
|
+
name: BaseTable.NonEmptyStringInput<Name>,
|
|
60
|
+
fields: Fields & BaseTable.NonEmptyFieldMap<Fields>,
|
|
61
|
+
schemaName: BaseTable.NonEmptySchemaNameInput<SchemaName>
|
|
62
|
+
): TableDefinition<Name, Fields, InlinePrimaryKeyKeys<Fields>, "schema", SchemaName>
|
|
63
|
+
export function make(
|
|
64
|
+
name: string,
|
|
65
|
+
fields: DialectFieldMap,
|
|
66
|
+
schemaName?: string
|
|
67
|
+
): TableDefinition<string, DialectFieldMap, string, "schema", string> {
|
|
68
|
+
return arguments.length >= 3
|
|
69
|
+
? BaseTable.make(name, fields, schemaName as string) as TableDefinition<string, DialectFieldMap, string, "schema", string>
|
|
70
|
+
: BaseTable.make(name, fields) as TableDefinition<string, DialectFieldMap, string, "schema", string>
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const alias = <
|
|
74
|
+
Table extends AnyTable,
|
|
75
|
+
AliasName extends string
|
|
76
|
+
>(
|
|
77
|
+
table: Table,
|
|
78
|
+
aliasName: BaseTable.LiteralStringInput<AliasName>
|
|
79
|
+
): TableDefinition<
|
|
80
|
+
AliasName,
|
|
81
|
+
FieldsOfTable<Table>,
|
|
82
|
+
PrimaryKeyOfTable<Table>,
|
|
83
|
+
"alias",
|
|
84
|
+
SchemaNameOfTable<Table>
|
|
85
|
+
> =>
|
|
86
|
+
BaseTable.alias(table as any, aliasName) as TableDefinition<
|
|
87
|
+
AliasName,
|
|
88
|
+
FieldsOfTable<Table>,
|
|
89
|
+
PrimaryKeyOfTable<Table>,
|
|
90
|
+
"alias",
|
|
91
|
+
SchemaNameOfTable<Table>
|
|
92
|
+
>
|
|
93
|
+
|
|
94
|
+
type ClassApi = {
|
|
95
|
+
<Self = never>(
|
|
96
|
+
name: "",
|
|
97
|
+
schemaName?: string | undefined
|
|
98
|
+
): never
|
|
99
|
+
<Self = never>(
|
|
100
|
+
name: string,
|
|
101
|
+
schemaName: ""
|
|
102
|
+
): never
|
|
103
|
+
<Self = never, const SchemaName extends string = "public", const Name extends string = string>(
|
|
104
|
+
name: BaseTable.NonEmptyStringInput<Name>,
|
|
105
|
+
schemaName?: BaseTable.NonEmptySchemaNameInput<SchemaName>
|
|
106
|
+
): <
|
|
107
|
+
Fields extends DialectFieldMap
|
|
108
|
+
>(fields: Fields & BaseTable.NonEmptyFieldMap<Fields>) => [Self] extends [never]
|
|
109
|
+
? BaseTable.MissingSelfGeneric
|
|
110
|
+
: TableClassStatic<Name, Fields, InlinePrimaryKeyKeys<Fields>, SchemaName>
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export const Class: ClassApi = ((
|
|
114
|
+
name: string,
|
|
115
|
+
schemaName: string | undefined = undefined
|
|
116
|
+
) => {
|
|
117
|
+
const base = BaseTable.Class(name as never, schemaName)
|
|
118
|
+
return base
|
|
119
|
+
}) as ClassApi
|
|
120
|
+
|
|
121
|
+
export const primaryKey = BaseTable.primaryKey
|
|
122
|
+
export const unique = BaseTable.unique
|
|
123
|
+
export const index = BaseTable.index
|
|
124
|
+
export const foreignKey = <
|
|
125
|
+
LocalColumns extends string | readonly string[],
|
|
126
|
+
TargetTable extends AnyTable,
|
|
127
|
+
TargetColumns extends string | readonly string[]
|
|
128
|
+
>(
|
|
129
|
+
columns: LocalColumns & BaseTable.NonEmptyColumnInput<LocalColumns>,
|
|
130
|
+
target: () => TargetTable,
|
|
131
|
+
referencedColumns: TargetColumns & BaseTable.NonEmptyColumnInput<TargetColumns> & BaseTable.MatchingColumnArityInput<LocalColumns, TargetColumns>
|
|
132
|
+
) =>
|
|
133
|
+
BaseTable.foreignKey<LocalColumns, TargetTable, TargetColumns>(
|
|
134
|
+
columns as LocalColumns & BaseTable.NonEmptyColumnInput<LocalColumns>,
|
|
135
|
+
target,
|
|
136
|
+
referencedColumns as TargetColumns & BaseTable.NonEmptyColumnInput<TargetColumns> & BaseTable.MatchingColumnArityInput<LocalColumns, TargetColumns>
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
export const check = BaseTable.check
|
|
140
|
+
|
|
141
|
+
export const selectSchema = BaseTable.selectSchema
|
|
142
|
+
export const insertSchema = BaseTable.insertSchema
|
|
143
|
+
export const updateSchema = BaseTable.updateSchema
|
|
144
|
+
|
|
145
|
+
export type SelectOf<Table extends AnyTable> = BaseTable.SelectOf<Table>
|
|
146
|
+
export type InsertOf<Table extends AnyTable> = BaseTable.InsertOf<Table>
|
|
147
|
+
export type UpdateOf<Table extends AnyTable> = BaseTable.UpdateOf<Table>
|
package/src/standard.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Standard SQL column-definition DSL. */
|
|
2
|
+
export * as Column from "./standard/column.js"
|
|
3
|
+
/** Standard SQL datatype witnesses and coercion families. */
|
|
4
|
+
export * as Datatypes from "./standard/datatypes/index.js"
|
|
5
|
+
/** Shared scalar SQL interfaces and DB-type descriptors. */
|
|
6
|
+
export * as Scalar from "./internal/scalar.js"
|
|
7
|
+
/** Standard SQL function expressions. */
|
|
8
|
+
export * as Function from "./standard/function/index.js"
|
|
9
|
+
/** Standard SQL typed query execution contracts. */
|
|
10
|
+
export * as Executor from "./internal/executor.js"
|
|
11
|
+
/** Shared logical row-set interfaces. */
|
|
12
|
+
export * as RowSet from "./internal/row-set.js"
|
|
13
|
+
/** Standard SQL query-construction DSL. */
|
|
14
|
+
export * as Query from "./standard/query.js"
|
|
15
|
+
/** Standard SQL table-definition DSL. */
|
|
16
|
+
export * as Table from "./standard/table.js"
|
|
17
|
+
/** Standard SQL built-in renderer entrypoint. */
|
|
18
|
+
export * as Renderer from "./standard/renderer.js"
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import * as Expression from "./scalar.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].kind === "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].kind
|
|
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
|
-
}
|
package/src/mysql/table.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import type * as Schema from "effect/Schema"
|
|
2
|
-
|
|
3
|
-
import type * as Expression from "../internal/scalar.js"
|
|
4
|
-
import { ColumnTypeId, type AnyColumnDefinition } from "../internal/column-state.js"
|
|
5
|
-
import * as BaseTable from "../internal/table.js"
|
|
6
|
-
|
|
7
|
-
type Dialect = "mysql"
|
|
8
|
-
|
|
9
|
-
type DialectColumn = AnyColumnDefinition & {
|
|
10
|
-
readonly [ColumnTypeId]: {
|
|
11
|
-
readonly dbType: Expression.DbType.Any & { readonly dialect: Dialect }
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
type DialectFieldMap = Record<string, DialectColumn>
|
|
16
|
-
|
|
17
|
-
type InlinePrimaryKeyKeys<Fields extends DialectFieldMap> = Extract<{
|
|
18
|
-
[K in keyof Fields]: Fields[K]["metadata"]["primaryKey"] extends true ? K : never
|
|
19
|
-
}[keyof Fields], string>
|
|
20
|
-
|
|
21
|
-
export type TableDefinition<
|
|
22
|
-
Name extends string,
|
|
23
|
-
Fields extends DialectFieldMap,
|
|
24
|
-
PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>,
|
|
25
|
-
Kind extends "schema" | "alias" = "schema",
|
|
26
|
-
SchemaName extends string | undefined = undefined
|
|
27
|
-
> = BaseTable.TableDefinition<Name, Fields, PrimaryKeyColumns, Kind, SchemaName>
|
|
28
|
-
|
|
29
|
-
export type TableClassStatic<
|
|
30
|
-
Name extends string,
|
|
31
|
-
Fields extends DialectFieldMap,
|
|
32
|
-
PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>,
|
|
33
|
-
SchemaName extends string | undefined = undefined
|
|
34
|
-
> = BaseTable.TableClassStatic<Name, Fields, PrimaryKeyColumns, SchemaName>
|
|
35
|
-
|
|
36
|
-
export type AnyTable = BaseTable.AnyTable
|
|
37
|
-
|
|
38
|
-
type FieldsOfTable<Table> = Table extends BaseTable.TableDefinition<any, infer Fields extends DialectFieldMap, any, any, any>
|
|
39
|
-
? Fields
|
|
40
|
-
: Table extends BaseTable.TableClassStatic<any, infer Fields extends DialectFieldMap, any, any>
|
|
41
|
-
? Fields
|
|
42
|
-
: never
|
|
43
|
-
|
|
44
|
-
type PrimaryKeyOfTable<Table> = Table extends BaseTable.TableDefinition<any, any, infer PrimaryKeyColumns extends string, any, any>
|
|
45
|
-
? PrimaryKeyColumns
|
|
46
|
-
: Table extends BaseTable.TableClassStatic<any, any, infer PrimaryKeyColumns extends string, any>
|
|
47
|
-
? PrimaryKeyColumns
|
|
48
|
-
: never
|
|
49
|
-
|
|
50
|
-
type SchemaNameOfTable<Table> = Table extends BaseTable.TableDefinition<any, any, any, any, infer SchemaName>
|
|
51
|
-
? SchemaName
|
|
52
|
-
: Table extends BaseTable.TableClassStatic<any, any, any, infer SchemaName>
|
|
53
|
-
? SchemaName
|
|
54
|
-
: never
|
|
55
|
-
|
|
56
|
-
type ApplySchemaTableOptions<
|
|
57
|
-
Name extends string,
|
|
58
|
-
Fields extends DialectFieldMap,
|
|
59
|
-
PrimaryKeyColumns extends keyof Fields & string,
|
|
60
|
-
SchemaName extends string,
|
|
61
|
-
Options extends BaseTable.DeclaredTableOptions
|
|
62
|
-
> = BaseTable.ApplyDeclaredOptions<
|
|
63
|
-
BaseTable.TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>,
|
|
64
|
-
Options
|
|
65
|
-
> extends BaseTable.TableDefinition<any, any, infer AppliedPrimaryKeyColumns extends keyof Fields & string, "schema", any>
|
|
66
|
-
? TableDefinition<Name, Fields, AppliedPrimaryKeyColumns, "schema", SchemaName>
|
|
67
|
-
: TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>
|
|
68
|
-
|
|
69
|
-
export type TableSchemaNamespace<SchemaName extends string> = {
|
|
70
|
-
readonly schemaName: SchemaName
|
|
71
|
-
readonly table: <
|
|
72
|
-
Name extends string,
|
|
73
|
-
Fields extends DialectFieldMap,
|
|
74
|
-
const Options extends BaseTable.DeclaredTableOptions,
|
|
75
|
-
PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>
|
|
76
|
-
>(
|
|
77
|
-
name: Name,
|
|
78
|
-
fields: Fields,
|
|
79
|
-
...options: Options & BaseTable.ValidateDeclaredOptions<BaseTable.TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>, Options>
|
|
80
|
-
) => ApplySchemaTableOptions<Name, Fields, PrimaryKeyColumns, SchemaName, Options>
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export type TableOption = BaseTable.TableOption
|
|
84
|
-
|
|
85
|
-
export const TypeId = BaseTable.TypeId
|
|
86
|
-
export const OptionsSymbol = BaseTable.OptionsSymbol
|
|
87
|
-
export const options = BaseTable.options
|
|
88
|
-
|
|
89
|
-
export const make = <
|
|
90
|
-
Name extends string,
|
|
91
|
-
Fields extends DialectFieldMap,
|
|
92
|
-
SchemaName extends string | undefined = undefined
|
|
93
|
-
>(
|
|
94
|
-
name: Name,
|
|
95
|
-
fields: Fields,
|
|
96
|
-
schemaName: SchemaName = undefined as SchemaName
|
|
97
|
-
): TableDefinition<Name, Fields> =>
|
|
98
|
-
BaseTable.make(name, fields, schemaName) as TableDefinition<Name, Fields>
|
|
99
|
-
|
|
100
|
-
export const schema = <SchemaName extends string>(
|
|
101
|
-
schemaName: SchemaName
|
|
102
|
-
): TableSchemaNamespace<SchemaName> => {
|
|
103
|
-
const table = <
|
|
104
|
-
Name extends string,
|
|
105
|
-
Fields extends DialectFieldMap,
|
|
106
|
-
const Options extends BaseTable.DeclaredTableOptions,
|
|
107
|
-
PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>
|
|
108
|
-
>(
|
|
109
|
-
name: Name,
|
|
110
|
-
fields: Fields,
|
|
111
|
-
...declaredOptions: Options & BaseTable.ValidateDeclaredOptions<BaseTable.TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>, Options>
|
|
112
|
-
) =>
|
|
113
|
-
(BaseTable.schema(schemaName).table as (
|
|
114
|
-
name: Name,
|
|
115
|
-
fields: Fields,
|
|
116
|
-
...options: BaseTable.DeclaredTableOptions
|
|
117
|
-
) => BaseTable.TableDefinition<any, any, any, "schema", any>)(
|
|
118
|
-
name,
|
|
119
|
-
fields,
|
|
120
|
-
...declaredOptions
|
|
121
|
-
) as ApplySchemaTableOptions<Name, Fields, PrimaryKeyColumns, SchemaName, Options>
|
|
122
|
-
return {
|
|
123
|
-
schemaName,
|
|
124
|
-
table
|
|
125
|
-
} as unknown as TableSchemaNamespace<SchemaName>
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export const alias = <
|
|
129
|
-
Table extends AnyTable,
|
|
130
|
-
AliasName extends string
|
|
131
|
-
>(
|
|
132
|
-
table: Table,
|
|
133
|
-
aliasName: AliasName
|
|
134
|
-
): TableDefinition<
|
|
135
|
-
AliasName,
|
|
136
|
-
FieldsOfTable<Table>,
|
|
137
|
-
PrimaryKeyOfTable<Table>,
|
|
138
|
-
"alias",
|
|
139
|
-
SchemaNameOfTable<Table>
|
|
140
|
-
> =>
|
|
141
|
-
BaseTable.alias(table as any, aliasName) as TableDefinition<
|
|
142
|
-
AliasName,
|
|
143
|
-
FieldsOfTable<Table>,
|
|
144
|
-
PrimaryKeyOfTable<Table>,
|
|
145
|
-
"alias",
|
|
146
|
-
SchemaNameOfTable<Table>
|
|
147
|
-
>
|
|
148
|
-
|
|
149
|
-
export const Class = <Self = never, SchemaName extends string | undefined = undefined>(
|
|
150
|
-
name: string,
|
|
151
|
-
schemaName: SchemaName = undefined as SchemaName
|
|
152
|
-
) => {
|
|
153
|
-
const base = BaseTable.Class<Self, SchemaName>(name, schemaName)
|
|
154
|
-
return base as unknown as <
|
|
155
|
-
Fields extends DialectFieldMap
|
|
156
|
-
>(fields: Fields) => [Self] extends [never]
|
|
157
|
-
? BaseTable.MissingSelfGeneric
|
|
158
|
-
: TableClassStatic<typeof name, Fields, InlinePrimaryKeyKeys<Fields>, SchemaName>
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export const primaryKey = BaseTable.primaryKey
|
|
162
|
-
export const unique = BaseTable.unique
|
|
163
|
-
export const index = BaseTable.index
|
|
164
|
-
export const foreignKey = <
|
|
165
|
-
LocalColumns extends string | readonly string[],
|
|
166
|
-
TargetTable extends AnyTable,
|
|
167
|
-
TargetColumns extends string | readonly string[]
|
|
168
|
-
>(
|
|
169
|
-
columns: LocalColumns & BaseTable.NonEmptyColumnInput<LocalColumns>,
|
|
170
|
-
target: () => TargetTable,
|
|
171
|
-
referencedColumns: TargetColumns & BaseTable.NonEmptyColumnInput<TargetColumns> & BaseTable.MatchingColumnArityInput<LocalColumns, TargetColumns>
|
|
172
|
-
): BaseTable.TableOption =>
|
|
173
|
-
BaseTable.foreignKey<LocalColumns, BaseTable.AnyTable, TargetColumns>(
|
|
174
|
-
columns as LocalColumns & BaseTable.NonEmptyColumnInput<LocalColumns>,
|
|
175
|
-
target as () => BaseTable.AnyTable,
|
|
176
|
-
referencedColumns as TargetColumns & BaseTable.NonEmptyColumnInput<TargetColumns> & BaseTable.MatchingColumnArityInput<LocalColumns, TargetColumns>
|
|
177
|
-
)
|
|
178
|
-
|
|
179
|
-
export const check = BaseTable.check
|
|
180
|
-
|
|
181
|
-
export type SelectOf<Table extends { readonly schemas: { readonly select: Schema.Schema<any> } }> = BaseTable.SelectOf<Table>
|
|
182
|
-
export type InsertOf<Table extends { readonly schemas: { readonly insert: Schema.Schema<any> } }> = BaseTable.InsertOf<Table>
|
|
183
|
-
export type UpdateOf<Table extends { readonly schemas: { readonly update: Schema.Schema<any> } }> = BaseTable.UpdateOf<Table>
|