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.
- package/dist/mysql.js +61555 -4252
- package/dist/postgres/metadata.js +728 -104
- package/dist/postgres.js +6906 -4023
- package/package.json +15 -2
- package/src/internal/aggregation-validation.ts +3 -3
- package/src/internal/case-analysis.d.ts +18 -0
- package/src/internal/case-analysis.ts +4 -4
- package/src/internal/coercion/analysis.d.ts +7 -0
- package/src/internal/{coercion-analysis.ts → coercion/analysis.ts} +3 -3
- package/src/internal/coercion/errors.d.ts +17 -0
- package/src/internal/{coercion-errors.ts → coercion/errors.ts} +1 -1
- package/src/internal/coercion/kind.d.ts +4 -0
- package/src/internal/{coercion-kind.ts → coercion/kind.ts} +2 -2
- package/src/internal/{coercion-normalize.ts → coercion/normalize.ts} +1 -1
- package/src/internal/coercion/rules.d.ts +6 -0
- package/src/internal/{coercion-rules.ts → coercion/rules.ts} +2 -2
- package/src/internal/column-state.d.ts +190 -0
- package/src/internal/column-state.ts +43 -47
- package/src/internal/column.ts +43 -305
- package/src/internal/datatypes/define.d.ts +17 -0
- package/src/internal/datatypes/define.ts +18 -4
- package/src/internal/datatypes/lookup.d.ts +44 -0
- package/src/internal/datatypes/lookup.ts +61 -152
- package/src/internal/datatypes/shape.d.ts +16 -0
- package/src/internal/datatypes/shape.ts +1 -1
- package/src/internal/derived-table.d.ts +4 -0
- package/src/internal/derived-table.ts +21 -16
- package/src/internal/dialect.ts +12 -1
- package/src/internal/dsl-mutation-runtime.ts +378 -0
- package/src/internal/dsl-plan-runtime.ts +387 -0
- package/src/internal/dsl-query-runtime.ts +160 -0
- package/src/internal/dsl-transaction-ddl-runtime.ts +263 -0
- package/src/internal/executor.ts +146 -34
- package/src/internal/expression-ast.ts +15 -5
- package/src/internal/grouping-key.d.ts +3 -0
- package/src/internal/grouping-key.ts +1 -1
- package/src/internal/implication-runtime.d.ts +15 -0
- package/src/internal/implication-runtime.ts +4 -4
- package/src/internal/json/ast.d.ts +30 -0
- package/src/internal/json/ast.ts +1 -1
- package/src/internal/json/errors.d.ts +8 -0
- package/src/internal/json/path.d.ts +75 -0
- package/src/internal/json/path.ts +1 -1
- package/src/internal/json/types.d.ts +62 -0
- package/src/internal/predicate/analysis.d.ts +20 -0
- package/src/internal/predicate/analysis.ts +183 -0
- package/src/internal/predicate/atom.d.ts +28 -0
- package/src/internal/{predicate-atom.ts → predicate/atom.ts} +7 -0
- package/src/internal/{predicate-branches.ts → predicate/branches.ts} +2 -2
- package/src/internal/predicate/context.d.ts +67 -0
- package/src/internal/{predicate-context.ts → predicate/context.ts} +163 -20
- package/src/internal/predicate/formula.d.ts +35 -0
- package/src/internal/{predicate-formula.ts → predicate/formula.ts} +1 -1
- package/src/internal/predicate/key.d.ts +11 -0
- package/src/internal/predicate/key.ts +73 -0
- package/src/internal/{predicate-nnf.ts → predicate/nnf.ts} +2 -2
- package/src/internal/predicate/normalize.d.ts +53 -0
- package/src/internal/{predicate-normalize.ts → predicate/normalize.ts} +130 -49
- package/src/internal/predicate/runtime.d.ts +31 -0
- package/src/internal/{predicate-runtime.ts → predicate/runtime.ts} +127 -17
- package/src/internal/projection-alias.d.ts +13 -0
- package/src/internal/projections.d.ts +31 -0
- package/src/internal/projections.ts +1 -1
- package/src/internal/query-ast.d.ts +217 -0
- package/src/internal/query-ast.ts +1 -1
- package/src/internal/query-requirements.d.ts +20 -0
- package/src/internal/query.d.ts +775 -0
- package/src/internal/query.ts +683 -369
- package/src/internal/renderer.ts +11 -21
- package/src/internal/row-set.d.ts +53 -0
- package/src/internal/{plan.ts → row-set.ts} +11 -9
- package/src/internal/runtime/driver-value-mapping.ts +186 -0
- package/src/internal/{runtime-normalize.ts → runtime/normalize.ts} +9 -31
- package/src/internal/{runtime-schema.ts → runtime/schema.ts} +13 -38
- package/src/internal/runtime/value.d.ts +22 -0
- package/src/internal/{runtime-value.ts → runtime/value.ts} +2 -2
- package/src/internal/scalar.d.ts +107 -0
- package/src/internal/scalar.ts +202 -0
- package/src/internal/schema-derivation.d.ts +105 -0
- package/src/internal/schema-expression.d.ts +18 -0
- package/src/internal/schema-expression.ts +38 -7
- package/src/internal/table-options.d.ts +94 -0
- package/src/internal/table-options.ts +8 -2
- package/src/internal/table.d.ts +173 -0
- package/src/internal/table.ts +32 -14
- package/src/mysql/column.ts +95 -18
- package/src/mysql/datatypes/index.ts +47 -7
- package/src/mysql/errors/generated.ts +57336 -0
- package/src/mysql/errors/index.ts +1 -0
- package/src/mysql/errors/normalize.ts +55 -53
- package/src/mysql/errors/types.ts +74 -0
- package/src/mysql/executor.ts +88 -11
- package/src/mysql/function/aggregate.ts +1 -5
- package/src/mysql/function/core.ts +1 -4
- package/src/mysql/function/index.ts +0 -1
- package/src/mysql/function/string.ts +1 -5
- package/src/mysql/function/temporal.ts +12 -15
- package/src/mysql/function/window.ts +1 -6
- package/src/{internal/mysql-dialect.ts → mysql/internal/dialect.ts} +12 -6
- package/src/{internal/mysql-query.ts → mysql/internal/dsl.ts} +1299 -2143
- package/src/mysql/internal/renderer.ts +46 -0
- package/src/mysql/internal/sql-expression-renderer.ts +1501 -0
- package/src/mysql/json.ts +2 -0
- package/src/mysql/query.ts +111 -91
- package/src/mysql/renderer.ts +8 -3
- package/src/mysql/table.ts +1 -1
- package/src/mysql.ts +6 -4
- package/src/postgres/cast.ts +30 -16
- package/src/postgres/column.ts +179 -46
- package/src/postgres/datatypes/index.d.ts +515 -0
- package/src/postgres/datatypes/index.ts +22 -13
- package/src/postgres/datatypes/spec.d.ts +412 -0
- package/src/postgres/errors/generated.ts +2636 -0
- package/src/postgres/errors/index.ts +1 -0
- package/src/postgres/errors/normalize.ts +47 -62
- package/src/postgres/errors/types.ts +92 -34
- package/src/postgres/executor.ts +54 -7
- package/src/postgres/function/aggregate.ts +1 -5
- package/src/postgres/function/core.ts +12 -6
- package/src/postgres/function/index.ts +0 -1
- package/src/postgres/function/string.ts +1 -5
- package/src/postgres/function/temporal.ts +12 -15
- package/src/postgres/function/window.ts +1 -6
- package/src/{internal/postgres-dialect.ts → postgres/internal/dialect.ts} +12 -6
- package/src/{internal/postgres-query.ts → postgres/internal/dsl.ts} +1356 -2133
- package/src/{internal/postgres-renderer.ts → postgres/internal/renderer.ts} +17 -8
- package/src/postgres/internal/schema-ddl.ts +108 -0
- package/src/{internal/postgres-schema-model.ts → postgres/internal/schema-model.ts} +12 -6
- package/src/{internal → postgres/internal}/sql-expression-renderer.ts +79 -25
- package/src/postgres/{function/json.ts → json.ts} +77 -85
- package/src/postgres/metadata.ts +2 -2
- package/src/postgres/query.ts +113 -89
- package/src/postgres/renderer.ts +8 -13
- package/src/postgres/schema-expression.ts +2 -1
- package/src/postgres/schema-management.ts +1 -1
- package/src/postgres/table.ts +12 -4
- package/src/postgres/type.ts +33 -2
- package/src/postgres.ts +6 -4
- package/src/internal/expression.ts +0 -327
- package/src/internal/mysql-renderer.ts +0 -37
- package/src/internal/predicate-analysis.ts +0 -81
- package/src/internal/predicate-key.ts +0 -28
- package/src/internal/schema-ddl.ts +0 -55
- package/src/mysql/function/json.ts +0 -4
- package/src/mysql/private/query.ts +0 -1
- package/src/postgres/private/query.ts +0 -1
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { type Pipeable } from "effect/Pipeable";
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
import * as Plan from "./row-set.js";
|
|
4
|
+
import type { TrueFormula } from "./predicate/formula.js";
|
|
5
|
+
import type { BoundColumnFrom } from "./column-state.js";
|
|
6
|
+
import { type DdlExpressionLike, type NormalizeColumns, type TableOptionSpec, type ValidateKnownColumns, type ValidatePrimaryKeyColumns } from "./table-options.js";
|
|
7
|
+
import { type InsertRow, type SelectRow, type TableFieldMap, type UpdateRow } from "./schema-derivation.js";
|
|
8
|
+
/** Symbol used to attach table-definition metadata. */
|
|
9
|
+
export declare const TypeId: unique symbol;
|
|
10
|
+
/** Symbol for the normalized table option list. */
|
|
11
|
+
export declare const OptionsSymbol: unique symbol;
|
|
12
|
+
/** Symbol used by `Table.Class` to declare table-level options. */
|
|
13
|
+
export declare const options: unique symbol;
|
|
14
|
+
declare const DeclaredOptionsSymbol: unique symbol;
|
|
15
|
+
type InlinePrimaryKeyKeys<Fields extends TableFieldMap> = Extract<{
|
|
16
|
+
[K in keyof Fields]: Fields[K]["metadata"]["primaryKey"] extends true ? K : never;
|
|
17
|
+
}[keyof Fields], string>;
|
|
18
|
+
type TableDialect<Fields extends TableFieldMap> = Fields[keyof Fields][typeof import("./column-state.js").ColumnTypeId]["dbType"]["dialect"];
|
|
19
|
+
type TableKind = "schema" | "alias";
|
|
20
|
+
type DefaultSchemaName = "public";
|
|
21
|
+
type ClassOptionSpec = Exclude<TableOptionSpec, {
|
|
22
|
+
readonly kind: "primaryKey";
|
|
23
|
+
}>;
|
|
24
|
+
interface TableOptionBuilderLike<Spec extends TableOptionSpec = TableOptionSpec> {
|
|
25
|
+
(table: TableDefinition<any, any, any, "schema", any>): TableDefinition<any, any, any, "schema", any>;
|
|
26
|
+
readonly option: Spec;
|
|
27
|
+
}
|
|
28
|
+
type ClassTableOption = TableOptionBuilderLike<ClassOptionSpec>;
|
|
29
|
+
type ClassDeclaredTableOptions = readonly ClassTableOption[];
|
|
30
|
+
type BuildPrimaryKey<Table extends TableDefinition<any, any, any, "schema", any>, Spec extends TableOptionSpec> = Spec extends {
|
|
31
|
+
readonly kind: "primaryKey";
|
|
32
|
+
readonly columns: infer Columns extends readonly string[];
|
|
33
|
+
} ? Columns[number] & keyof Table[typeof TypeId]["fields"] & string : Table[typeof TypeId]["primaryKey"][number];
|
|
34
|
+
type OptionInputTable<Table extends TableDefinition<any, any, any, "schema", any>, Spec extends TableOptionSpec> = Spec extends {
|
|
35
|
+
readonly kind: "primaryKey";
|
|
36
|
+
readonly columns: infer Columns extends readonly string[];
|
|
37
|
+
} ? ValidatePrimaryKeyColumns<Table[typeof TypeId]["fields"], Columns> extends never ? never : Table : Spec extends {
|
|
38
|
+
readonly columns: infer Columns extends readonly string[];
|
|
39
|
+
} ? ValidateKnownColumns<Table[typeof TypeId]["fields"], Columns> extends never ? never : Table : Table;
|
|
40
|
+
type ApplyOption<Table extends TableDefinition<any, any, any, "schema", any>, Spec extends TableOptionSpec> = Spec extends {
|
|
41
|
+
readonly kind: "primaryKey";
|
|
42
|
+
} ? TableDefinition<Table[typeof TypeId]["name"], Table[typeof TypeId]["fields"], BuildPrimaryKey<Table, Spec>, "schema"> : TableDefinition<Table[typeof TypeId]["name"], Table[typeof TypeId]["fields"], Table[typeof TypeId]["primaryKey"][number], "schema">;
|
|
43
|
+
export type MissingSelfGeneric = "Missing `Self` generic - use `class Self extends Table.Class<Self>(...) {}`";
|
|
44
|
+
/** Bound columns keyed by field name for a particular table. */
|
|
45
|
+
export type BoundColumns<Name extends string, Fields extends TableFieldMap> = {
|
|
46
|
+
readonly [K in keyof Fields]: BoundColumnFrom<Fields[K], Name, Extract<K, string>>;
|
|
47
|
+
};
|
|
48
|
+
/** Derived runtime schemas exposed by a table definition. */
|
|
49
|
+
export interface TableSchemas<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string> {
|
|
50
|
+
readonly select: Schema.Schema<SelectRow<Name, Fields>>;
|
|
51
|
+
readonly insert: Schema.Schema<InsertRow<Name, Fields>>;
|
|
52
|
+
readonly update: Schema.Schema<UpdateRow<Name, Fields, PrimaryKeyColumns>>;
|
|
53
|
+
}
|
|
54
|
+
interface TableState<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string, Kind extends TableKind = "schema", SchemaName extends string | undefined = DefaultSchemaName> {
|
|
55
|
+
readonly name: Name;
|
|
56
|
+
readonly baseName: string;
|
|
57
|
+
readonly schemaName: SchemaName;
|
|
58
|
+
readonly fields: Fields;
|
|
59
|
+
readonly primaryKey: readonly PrimaryKeyColumns[];
|
|
60
|
+
readonly kind: Kind;
|
|
61
|
+
}
|
|
62
|
+
/** Namespace-scoped table builder. */
|
|
63
|
+
export interface TableSchemaNamespace<SchemaName extends string> {
|
|
64
|
+
readonly schemaName: SchemaName;
|
|
65
|
+
readonly table: <Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>>(name: Name, fields: Fields, ...options: DeclaredTableOptions) => TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>;
|
|
66
|
+
}
|
|
67
|
+
export type DeclaredTableOptions = readonly TableOptionBuilderLike[];
|
|
68
|
+
export type { DdlExpressionLike, IndexKeySpec, NormalizeColumns, ReferentialAction } from "./table-options.js";
|
|
69
|
+
export type TableDefinition<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>, Kind extends TableKind = "schema", SchemaName extends string | undefined = DefaultSchemaName> = Pipeable & {
|
|
70
|
+
readonly name: Name;
|
|
71
|
+
readonly columns: BoundColumns<Name, Fields>;
|
|
72
|
+
readonly schemas: TableSchemas<Name, Fields, PrimaryKeyColumns>;
|
|
73
|
+
readonly [TypeId]: TableState<Name, Fields, PrimaryKeyColumns, Kind, SchemaName>;
|
|
74
|
+
readonly [Plan.TypeId]: Plan.State<BoundColumns<Name, Fields>, never, Record<Name, Plan.Source<Name, "required", TrueFormula>>, TableDialect<Fields>>;
|
|
75
|
+
readonly [OptionsSymbol]: readonly TableOptionSpec[];
|
|
76
|
+
readonly [DeclaredOptionsSymbol]: readonly TableOptionSpec[];
|
|
77
|
+
} & BoundColumns<Name, Fields> & Plan.RowSet<BoundColumns<Name, Fields>, never, Record<Name, Plan.Source<Name, "required", TrueFormula>>, TableDialect<Fields>>;
|
|
78
|
+
/**
|
|
79
|
+
* Static class-based table definition.
|
|
80
|
+
*
|
|
81
|
+
* The class object itself acts as the table definition, exposing static bound
|
|
82
|
+
* columns, derived schemas, and plan metadata.
|
|
83
|
+
*/
|
|
84
|
+
export type TableClassStatic<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>, SchemaName extends string | undefined = DefaultSchemaName> = (abstract new (...args: any[]) => any) & Pipeable & {
|
|
85
|
+
readonly columns: BoundColumns<Name, Fields>;
|
|
86
|
+
readonly schemas: TableSchemas<Name, Fields, PrimaryKeyColumns>;
|
|
87
|
+
readonly [TypeId]: TableState<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>;
|
|
88
|
+
readonly [Plan.TypeId]: Plan.State<BoundColumns<Name, Fields>, never, Record<Name, Plan.Source<Name, "required", TrueFormula>>, TableDialect<Fields>>;
|
|
89
|
+
readonly [OptionsSymbol]: readonly TableOptionSpec[];
|
|
90
|
+
readonly [DeclaredOptionsSymbol]?: readonly TableOptionSpec[];
|
|
91
|
+
readonly [options]?: ClassDeclaredTableOptions;
|
|
92
|
+
readonly tableName: Name;
|
|
93
|
+
} & BoundColumns<Name, Fields> & Plan.RowSet<BoundColumns<Name, Fields>, never, Record<Name, Plan.Source<Name, "required", TrueFormula>>, TableDialect<Fields>>;
|
|
94
|
+
/** Minimal structural table-like contract used across helper APIs. */
|
|
95
|
+
export type AnyTable = TableDefinition<any, any, any, any, any> | TableClassStatic<any, any, any, any>;
|
|
96
|
+
/** Public table-option builder type used by `Table.index`, `Table.primaryKey`, and friends. */
|
|
97
|
+
export type TableOption<Spec extends TableOptionSpec = TableOptionSpec> = {
|
|
98
|
+
<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string>(table: OptionInputTable<TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", any>, Spec>): ApplyOption<TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", any>, Spec>;
|
|
99
|
+
readonly option: Spec;
|
|
100
|
+
};
|
|
101
|
+
export declare const option: <Spec extends TableOptionSpec>(spec: Spec) => TableOption<Spec>;
|
|
102
|
+
export declare const optionFromTable: <Spec extends TableOptionSpec>(spec: Spec, resolve: (table: TableDefinition<any, any, any, "schema", any>) => Spec) => TableOption<Spec>;
|
|
103
|
+
/** Creates a table definition from a name and field map. */
|
|
104
|
+
export declare function make<Name extends string, Fields extends TableFieldMap, SchemaName extends string | undefined = DefaultSchemaName>(name: Name, fields: Fields, schemaName?: SchemaName): TableDefinition<Name, Fields, InlinePrimaryKeyKeys<Fields>, "schema", SchemaName>;
|
|
105
|
+
/**
|
|
106
|
+
* Creates a namespace-scoped builder for a concrete SQL schema/database.
|
|
107
|
+
*/
|
|
108
|
+
export declare const schema: <SchemaName extends string>(schemaName: SchemaName) => TableSchemaNamespace<SchemaName>;
|
|
109
|
+
/**
|
|
110
|
+
* Creates an aliased source from an existing table definition.
|
|
111
|
+
*
|
|
112
|
+
* The alias becomes the logical source identity used by the query layer while
|
|
113
|
+
* the original physical table name is retained in bound-column provenance for
|
|
114
|
+
* downstream SQL rendering work.
|
|
115
|
+
*/
|
|
116
|
+
export declare const alias: <Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string, SchemaName extends string, AliasName extends string>(table: TableClassStatic<Name, Fields, PrimaryKeyColumns, SchemaName> | TableDefinition<Name, Fields, PrimaryKeyColumns, any, SchemaName>, aliasName: AliasName) => TableDefinition<AliasName, Fields, PrimaryKeyColumns, "alias", SchemaName>;
|
|
117
|
+
/**
|
|
118
|
+
* Class-based table constructor mirroring `Schema.Class`.
|
|
119
|
+
*
|
|
120
|
+
* The returned base class can be extended and configured with
|
|
121
|
+
* `static readonly [Table.options]`.
|
|
122
|
+
*/
|
|
123
|
+
export declare function Class<Self = never, SchemaName extends string | undefined = DefaultSchemaName>(name: string, schemaName?: SchemaName): <Fields extends TableFieldMap>(fields: Fields) => [Self] extends [never] ? "Missing `Self` generic - use `class Self extends Table.Class<Self>(...) {}`" : TableClassStatic<string, Fields, Extract<{ [K in keyof Fields]: Fields[K]["metadata"]["primaryKey"] extends true ? K : never; }[keyof Fields], string>, SchemaName>;
|
|
124
|
+
/** Declares a table-level primary key. */
|
|
125
|
+
export declare const primaryKey: <const Columns extends string | readonly string[]>(columns: Columns) => TableOption<{
|
|
126
|
+
readonly kind: "primaryKey";
|
|
127
|
+
readonly columns: NormalizeColumns<Columns>;
|
|
128
|
+
}>;
|
|
129
|
+
/** Declares a table-level unique constraint. */
|
|
130
|
+
export declare const unique: <const Columns extends string | readonly string[]>(columns: Columns) => TableOption<{
|
|
131
|
+
readonly kind: "unique";
|
|
132
|
+
readonly columns: NormalizeColumns<Columns>;
|
|
133
|
+
}>;
|
|
134
|
+
/** Declares a table-level index. */
|
|
135
|
+
export declare const index: <const Columns extends string | readonly string[]>(columns: Columns) => TableOption<{
|
|
136
|
+
readonly kind: "index";
|
|
137
|
+
readonly columns: NormalizeColumns<Columns>;
|
|
138
|
+
}>;
|
|
139
|
+
/** Declares a table-level foreign key. */
|
|
140
|
+
export declare const foreignKey: <const LocalColumns extends string | readonly string[], TargetTable extends AnyTable, const TargetColumns extends string | readonly string[]>(columns: LocalColumns, target: () => TargetTable, referencedColumns: TargetColumns) => TableOption<{
|
|
141
|
+
readonly kind: "foreignKey";
|
|
142
|
+
readonly columns: NormalizeColumns<LocalColumns>;
|
|
143
|
+
readonly references: () => {
|
|
144
|
+
readonly tableName: string;
|
|
145
|
+
readonly schemaName?: string | undefined;
|
|
146
|
+
readonly columns: NormalizeColumns<TargetColumns>;
|
|
147
|
+
readonly knownColumns: readonly string[];
|
|
148
|
+
};
|
|
149
|
+
}>;
|
|
150
|
+
/** Declares a check constraint expression. */
|
|
151
|
+
export declare const check: <Name extends string>(name: Name, predicate: DdlExpressionLike) => TableOption<{
|
|
152
|
+
readonly kind: "check";
|
|
153
|
+
readonly name: Name;
|
|
154
|
+
readonly predicate: DdlExpressionLike;
|
|
155
|
+
}>;
|
|
156
|
+
/** Extracts the row type of a table's select schema. */
|
|
157
|
+
export type SelectOf<Table extends {
|
|
158
|
+
readonly schemas: {
|
|
159
|
+
readonly select: Schema.Schema<any>;
|
|
160
|
+
};
|
|
161
|
+
}> = Schema.Schema.Type<Table["schemas"]["select"]>;
|
|
162
|
+
/** Extracts the payload type of a table's insert schema. */
|
|
163
|
+
export type InsertOf<Table extends {
|
|
164
|
+
readonly schemas: {
|
|
165
|
+
readonly insert: Schema.Schema<any>;
|
|
166
|
+
};
|
|
167
|
+
}> = Schema.Schema.Type<Table["schemas"]["insert"]>;
|
|
168
|
+
/** Extracts the payload type of a table's update schema. */
|
|
169
|
+
export type UpdateOf<Table extends {
|
|
170
|
+
readonly schemas: {
|
|
171
|
+
readonly update: Schema.Schema<any>;
|
|
172
|
+
};
|
|
173
|
+
}> = Schema.Schema.Type<Table["schemas"]["update"]>;
|
package/src/internal/table.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { pipeArguments, type Pipeable } from "effect/Pipeable"
|
|
2
2
|
import * as Schema from "effect/Schema"
|
|
3
3
|
|
|
4
|
-
import * as Plan from "./
|
|
5
|
-
import type { Any as AnyExpression } from "./
|
|
6
|
-
import type { TrueFormula } from "./predicate
|
|
4
|
+
import * as Plan from "./row-set.js"
|
|
5
|
+
import type { Any as AnyExpression } from "./scalar.js"
|
|
6
|
+
import type { TrueFormula } from "./predicate/formula.js"
|
|
7
7
|
import type { BoundColumnFrom } from "./column-state.js"
|
|
8
8
|
import { bindColumn, type AnyColumnDefinition } from "./column-state.js"
|
|
9
9
|
import {
|
|
@@ -76,12 +76,19 @@ type OptionInputTable<
|
|
|
76
76
|
type ApplyOption<
|
|
77
77
|
Table extends TableDefinition<any, any, any, "schema", any>,
|
|
78
78
|
Spec extends TableOptionSpec
|
|
79
|
-
> =
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
79
|
+
> = Spec extends { readonly kind: "primaryKey" }
|
|
80
|
+
? TableDefinition<
|
|
81
|
+
Table[typeof TypeId]["name"],
|
|
82
|
+
Table[typeof TypeId]["fields"],
|
|
83
|
+
BuildPrimaryKey<Table, Spec>,
|
|
84
|
+
"schema"
|
|
85
|
+
>
|
|
86
|
+
: TableDefinition<
|
|
87
|
+
Table[typeof TypeId]["name"],
|
|
88
|
+
Table[typeof TypeId]["fields"],
|
|
89
|
+
Table[typeof TypeId]["primaryKey"][number],
|
|
90
|
+
"schema"
|
|
91
|
+
>
|
|
85
92
|
|
|
86
93
|
export type MissingSelfGeneric = "Missing `Self` generic - use `class Self extends Table.Class<Self>(...) {}`"
|
|
87
94
|
|
|
@@ -155,7 +162,7 @@ export type TableDefinition<
|
|
|
155
162
|
>
|
|
156
163
|
readonly [OptionsSymbol]: readonly TableOptionSpec[]
|
|
157
164
|
readonly [DeclaredOptionsSymbol]: readonly TableOptionSpec[]
|
|
158
|
-
} & BoundColumns<Name, Fields> & Plan.
|
|
165
|
+
} & BoundColumns<Name, Fields> & Plan.RowSet<
|
|
159
166
|
BoundColumns<Name, Fields>,
|
|
160
167
|
never,
|
|
161
168
|
Record<Name, Plan.Source<Name, "required", TrueFormula>>,
|
|
@@ -187,7 +194,7 @@ export type TableClassStatic<
|
|
|
187
194
|
readonly [DeclaredOptionsSymbol]?: readonly TableOptionSpec[]
|
|
188
195
|
readonly [options]?: ClassDeclaredTableOptions
|
|
189
196
|
readonly tableName: Name
|
|
190
|
-
} & BoundColumns<Name, Fields> & Plan.
|
|
197
|
+
} & BoundColumns<Name, Fields> & Plan.RowSet<
|
|
191
198
|
BoundColumns<Name, Fields>,
|
|
192
199
|
never,
|
|
193
200
|
Record<Name, Plan.Source<Name, "required", TrueFormula>>,
|
|
@@ -217,6 +224,17 @@ const TableProto = {
|
|
|
217
224
|
}
|
|
218
225
|
}
|
|
219
226
|
|
|
227
|
+
const attachPipe = <Value extends object>(value: Value): Value => {
|
|
228
|
+
Object.defineProperty(value, "pipe", {
|
|
229
|
+
configurable: true,
|
|
230
|
+
writable: true,
|
|
231
|
+
value: function(this: unknown) {
|
|
232
|
+
return pipeArguments(value, arguments)
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
return value
|
|
236
|
+
}
|
|
237
|
+
|
|
220
238
|
type BuildArtifacts<
|
|
221
239
|
Name extends string,
|
|
222
240
|
Fields extends TableFieldMap,
|
|
@@ -274,7 +292,7 @@ const makeTable = <
|
|
|
274
292
|
: ("public" as SchemaName)
|
|
275
293
|
const artifacts = buildArtifacts(name, fields, declaredOptions, resolvedSchemaName)
|
|
276
294
|
const dialect = resolveFieldDialect(fields)
|
|
277
|
-
const table = Object.create(TableProto)
|
|
295
|
+
const table = attachPipe(Object.create(TableProto))
|
|
278
296
|
table.name = name
|
|
279
297
|
table.columns = artifacts.columns
|
|
280
298
|
table.schemas = artifacts.schemas
|
|
@@ -339,7 +357,7 @@ const validateClassOptions = (declaredOptions: readonly TableOptionSpec[]): void
|
|
|
339
357
|
const resolveFieldDialect = (fields: TableFieldMap): string => {
|
|
340
358
|
const dialects = [...new Set(Object.values(fields).map((field) => field.metadata.dbType.dialect))]
|
|
341
359
|
if (dialects.length === 0) {
|
|
342
|
-
|
|
360
|
+
throw new Error("Cannot infer table dialect from an empty field set")
|
|
343
361
|
}
|
|
344
362
|
if (dialects.length > 1) {
|
|
345
363
|
throw new Error(`Mixed table dialects are not supported: ${dialects.join(", ")}`)
|
|
@@ -525,7 +543,7 @@ export const alias = <
|
|
|
525
543
|
const columns = Object.fromEntries(
|
|
526
544
|
Object.entries(state.fields).map(([key, column]) => [key, bindColumn(aliasName, key, column as AnyColumnDefinition, state.baseName, state.schemaName)])
|
|
527
545
|
) as BoundColumns<AliasName, Fields>
|
|
528
|
-
const aliased = Object.create(TableProto)
|
|
546
|
+
const aliased = attachPipe(Object.create(TableProto))
|
|
529
547
|
aliased.name = aliasName
|
|
530
548
|
aliased.columns = columns
|
|
531
549
|
aliased.schemas = deriveSchemas(aliasName, state.fields, state.primaryKey)
|
package/src/mysql/column.ts
CHANGED
|
@@ -1,22 +1,98 @@
|
|
|
1
|
+
import * as Schema from "effect/Schema"
|
|
2
|
+
|
|
1
3
|
import * as BaseColumn from "../internal/column.js"
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
4
|
+
import { makeColumnDefinition, type ColumnDefinition } from "../internal/column-state.js"
|
|
5
|
+
import type * as Expression from "../internal/scalar.js"
|
|
6
|
+
import {
|
|
7
|
+
DecimalStringSchema,
|
|
8
|
+
LocalDateStringSchema,
|
|
9
|
+
LocalDateTimeStringSchema,
|
|
10
|
+
type DecimalString,
|
|
11
|
+
type LocalDateString,
|
|
12
|
+
type LocalDateTimeString
|
|
13
|
+
} from "../internal/runtime/value.js"
|
|
14
|
+
import { mysqlDatatypes } from "./datatypes/index.js"
|
|
15
|
+
|
|
16
|
+
const enrichDbType = <Db extends Expression.DbType.Any>(dbType: Db): Db => {
|
|
17
|
+
const candidate = (mysqlDatatypes as unknown as Record<string, (() => Expression.DbType.Any) | undefined>)[dbType.kind]
|
|
18
|
+
return typeof candidate === "function"
|
|
19
|
+
? { ...candidate(), ...dbType } as Db
|
|
20
|
+
: dbType
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const primitive = <Type, Db extends Expression.DbType.Any>(
|
|
24
|
+
schema: Schema.Schema<Type, any, any>,
|
|
25
|
+
dbType: Db
|
|
26
|
+
): ColumnDefinition<Type, Type, Type, Db, false, false, false, false, false, undefined> =>
|
|
27
|
+
makeColumnDefinition(schema as Schema.Schema<NonNullable<Type>>, {
|
|
28
|
+
dbType,
|
|
29
|
+
nullable: false,
|
|
30
|
+
hasDefault: false,
|
|
31
|
+
generated: false,
|
|
32
|
+
primaryKey: false,
|
|
33
|
+
unique: false,
|
|
34
|
+
references: undefined
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const renderNumericDdlType = (
|
|
38
|
+
kind: string,
|
|
39
|
+
options?: BaseColumn.NumericOptions
|
|
40
|
+
): string | undefined => {
|
|
41
|
+
if (options === undefined || options.precision === undefined) {
|
|
42
|
+
return undefined
|
|
43
|
+
}
|
|
44
|
+
return options.scale === undefined
|
|
45
|
+
? `${kind}(${options.precision})`
|
|
46
|
+
: `${kind}(${options.precision},${options.scale})`
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const custom = <SchemaType extends Schema.Schema.Any, Db extends Expression.DbType.Any>(
|
|
50
|
+
schema: SchemaType,
|
|
51
|
+
dbType: Db
|
|
52
|
+
) =>
|
|
53
|
+
makeColumnDefinition(schema as unknown as Schema.Schema<NonNullable<Schema.Schema.Type<SchemaType>>, any, any>, {
|
|
54
|
+
dbType: enrichDbType(dbType),
|
|
55
|
+
nullable: false,
|
|
56
|
+
hasDefault: false,
|
|
57
|
+
generated: false,
|
|
58
|
+
primaryKey: false,
|
|
59
|
+
unique: false,
|
|
60
|
+
references: undefined,
|
|
61
|
+
ddlType: undefined,
|
|
62
|
+
identity: undefined
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
export const uuid = () => primitive(Schema.UUID, mysqlDatatypes.uuid())
|
|
66
|
+
export const text = () => primitive(Schema.String, mysqlDatatypes.text())
|
|
67
|
+
export const int = () => primitive(Schema.Int, mysqlDatatypes.int())
|
|
68
|
+
export const number = (options?: BaseColumn.NumericOptions) =>
|
|
69
|
+
makeColumnDefinition(DecimalStringSchema, {
|
|
70
|
+
dbType: mysqlDatatypes.decimal(),
|
|
71
|
+
nullable: false,
|
|
72
|
+
hasDefault: false,
|
|
73
|
+
generated: false,
|
|
74
|
+
primaryKey: false,
|
|
75
|
+
unique: false,
|
|
76
|
+
references: undefined,
|
|
77
|
+
ddlType: renderNumericDdlType("decimal", options),
|
|
78
|
+
identity: undefined
|
|
79
|
+
})
|
|
80
|
+
export const boolean = () => primitive(Schema.Boolean, mysqlDatatypes.boolean())
|
|
81
|
+
export const date = () => primitive(LocalDateStringSchema, mysqlDatatypes.date())
|
|
82
|
+
export const datetime = () => primitive(LocalDateTimeStringSchema, mysqlDatatypes.datetime())
|
|
83
|
+
export const timestamp = () => primitive(LocalDateTimeStringSchema, mysqlDatatypes.timestamp())
|
|
84
|
+
export const json = <SchemaType extends Schema.Schema.Any>(schema: SchemaType) =>
|
|
85
|
+
makeColumnDefinition(schema as unknown as Schema.Schema<NonNullable<Schema.Schema.Type<SchemaType>>, any, any>, {
|
|
86
|
+
dbType: { ...mysqlDatatypes.json(), variant: "json" } as Expression.DbType.Json<"mysql", "json">,
|
|
87
|
+
nullable: false,
|
|
88
|
+
hasDefault: false,
|
|
89
|
+
generated: false,
|
|
90
|
+
primaryKey: false,
|
|
91
|
+
unique: false,
|
|
92
|
+
references: undefined,
|
|
93
|
+
ddlType: undefined,
|
|
94
|
+
identity: undefined
|
|
95
|
+
})
|
|
20
96
|
|
|
21
97
|
export const nullable = BaseColumn.nullable
|
|
22
98
|
export const brand = BaseColumn.brand
|
|
@@ -24,6 +100,7 @@ export const primaryKey = BaseColumn.primaryKey
|
|
|
24
100
|
export const unique = BaseColumn.unique
|
|
25
101
|
const default_ = BaseColumn.default_
|
|
26
102
|
export const generated = BaseColumn.generated
|
|
103
|
+
export const driverValueMapping = BaseColumn.driverValueMapping
|
|
27
104
|
export const references = BaseColumn.references
|
|
28
105
|
export const schema = BaseColumn.schema
|
|
29
106
|
export { default_ as default }
|
|
@@ -1,21 +1,61 @@
|
|
|
1
1
|
import type { DatatypeModule } from "../../internal/datatypes/define.js"
|
|
2
|
-
import type * as Expression from "../../internal/
|
|
3
|
-
import { mysqlDatatypeKinds } from "./spec.js"
|
|
2
|
+
import type * as Expression from "../../internal/scalar.js"
|
|
3
|
+
import { mysqlDatatypeFamilies, mysqlDatatypeKinds } from "./spec.js"
|
|
4
|
+
|
|
5
|
+
const withMetadata = <Kind extends keyof typeof mysqlDatatypeKinds & string>(
|
|
6
|
+
kind: Kind
|
|
7
|
+
): Expression.DbType.Base<"mysql", Kind> => {
|
|
8
|
+
const kindSpec = mysqlDatatypeKinds[kind]
|
|
9
|
+
const familySpec = mysqlDatatypeFamilies[kindSpec.family as keyof typeof mysqlDatatypeFamilies]
|
|
10
|
+
return {
|
|
11
|
+
dialect: "mysql",
|
|
12
|
+
kind,
|
|
13
|
+
family: kindSpec.family,
|
|
14
|
+
runtime: kindSpec.runtime,
|
|
15
|
+
compareGroup: familySpec?.compareGroup,
|
|
16
|
+
castTargets: familySpec?.castTargets,
|
|
17
|
+
traits: familySpec?.traits
|
|
18
|
+
}
|
|
19
|
+
}
|
|
4
20
|
|
|
5
21
|
const mysqlDatatypeModule = {
|
|
6
22
|
custom: (kind: string) => ({
|
|
7
23
|
dialect: "mysql",
|
|
8
24
|
kind
|
|
25
|
+
}),
|
|
26
|
+
uuid: () => ({
|
|
27
|
+
dialect: "mysql",
|
|
28
|
+
kind: "uuid",
|
|
29
|
+
family: "uuid",
|
|
30
|
+
runtime: "string",
|
|
31
|
+
compareGroup: "uuid",
|
|
32
|
+
castTargets: ["uuid", "char", "varchar", "text"],
|
|
33
|
+
traits: {
|
|
34
|
+
textual: true
|
|
35
|
+
}
|
|
9
36
|
})
|
|
10
37
|
} as Record<string, (...args: readonly any[]) => Expression.DbType.Base<"mysql", string>>
|
|
11
38
|
|
|
12
39
|
for (const kind of Object.keys(mysqlDatatypeKinds)) {
|
|
13
|
-
mysqlDatatypeModule[kind] = () => (
|
|
14
|
-
dialect: "mysql",
|
|
15
|
-
kind
|
|
16
|
-
})
|
|
40
|
+
mysqlDatatypeModule[kind] = () => withMetadata(kind as keyof typeof mysqlDatatypeKinds & string)
|
|
17
41
|
}
|
|
18
42
|
|
|
19
|
-
|
|
43
|
+
type MysqlUuidWitness = Expression.DbType.Base<"mysql", "uuid"> & {
|
|
44
|
+
readonly family: "uuid"
|
|
45
|
+
readonly runtime: "string"
|
|
46
|
+
readonly compareGroup: "uuid"
|
|
47
|
+
readonly castTargets: readonly ["uuid", "char", "varchar", "text"]
|
|
48
|
+
readonly traits: {
|
|
49
|
+
readonly textual: true
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const mysqlDatatypes = mysqlDatatypeModule as DatatypeModule<
|
|
54
|
+
"mysql",
|
|
55
|
+
typeof mysqlDatatypeKinds,
|
|
56
|
+
typeof mysqlDatatypeFamilies
|
|
57
|
+
> & {
|
|
58
|
+
readonly uuid: () => MysqlUuidWitness
|
|
59
|
+
}
|
|
20
60
|
|
|
21
61
|
export type MysqlDatatypeModule = typeof mysqlDatatypes
|