drizzle-orm 1.0.0-beta.1-03aea0b → 1.0.0-beta.1-7946562
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/gel-core/columns/common.cjs +0 -8
- package/gel-core/columns/common.cjs.map +1 -1
- package/gel-core/columns/common.d.cts +0 -2
- package/gel-core/columns/common.d.ts +0 -2
- package/gel-core/columns/common.js +0 -8
- package/gel-core/columns/common.js.map +1 -1
- package/gel-core/columns/custom.cjs +0 -12
- package/gel-core/columns/custom.cjs.map +1 -1
- package/gel-core/columns/custom.d.cts +4 -116
- package/gel-core/columns/custom.d.ts +4 -116
- package/gel-core/columns/custom.js +0 -12
- package/gel-core/columns/custom.js.map +1 -1
- package/gel-core/dialect.cjs +2 -21
- package/gel-core/dialect.cjs.map +1 -1
- package/gel-core/dialect.js +3 -22
- package/gel-core/dialect.js.map +1 -1
- package/mysql-core/columns/custom.cjs +0 -28
- package/mysql-core/columns/custom.cjs.map +1 -1
- package/mysql-core/columns/custom.d.cts +4 -119
- package/mysql-core/columns/custom.d.ts +4 -119
- package/mysql-core/columns/custom.js +0 -28
- package/mysql-core/columns/custom.js.map +1 -1
- package/mysql-core/dialect.cjs +0 -3
- package/mysql-core/dialect.cjs.map +1 -1
- package/mysql-core/dialect.js +0 -3
- package/mysql-core/dialect.js.map +1 -1
- package/package.json +145 -145
- package/pg-core/columns/common.cjs +0 -8
- package/pg-core/columns/common.cjs.map +1 -1
- package/pg-core/columns/common.d.cts +0 -1
- package/pg-core/columns/common.d.ts +0 -1
- package/pg-core/columns/common.js +0 -8
- package/pg-core/columns/common.js.map +1 -1
- package/pg-core/columns/custom.cjs +0 -27
- package/pg-core/columns/custom.cjs.map +1 -1
- package/pg-core/columns/custom.d.cts +4 -118
- package/pg-core/columns/custom.d.ts +4 -118
- package/pg-core/columns/custom.js +0 -27
- package/pg-core/columns/custom.js.map +1 -1
- package/pg-core/dialect.cjs +3 -7
- package/pg-core/dialect.cjs.map +1 -1
- package/pg-core/dialect.js +3 -7
- package/pg-core/dialect.js.map +1 -1
- package/relations.cjs +1 -1
- package/relations.cjs.map +1 -1
- package/relations.js +1 -1
- package/relations.js.map +1 -1
- package/singlestore-core/columns/custom.cjs +0 -28
- package/singlestore-core/columns/custom.cjs.map +1 -1
- package/singlestore-core/columns/custom.d.cts +4 -119
- package/singlestore-core/columns/custom.d.ts +4 -119
- package/singlestore-core/columns/custom.js +0 -28
- package/singlestore-core/columns/custom.js.map +1 -1
- package/sql/sql.cjs.map +1 -1
- package/sql/sql.d.cts +0 -1
- package/sql/sql.d.ts +0 -1
- package/sql/sql.js.map +1 -1
- package/sqlite-core/columns/custom.cjs +0 -27
- package/sqlite-core/columns/custom.cjs.map +1 -1
- package/sqlite-core/columns/custom.d.cts +4 -119
- package/sqlite-core/columns/custom.d.ts +4 -119
- package/sqlite-core/columns/custom.js +0 -27
- package/sqlite-core/columns/custom.js.map +1 -1
- package/sqlite-core/dialect.cjs +0 -3
- package/sqlite-core/dialect.cjs.map +1 -1
- package/sqlite-core/dialect.js +0 -3
- package/sqlite-core/dialect.js.map +1 -1
- package/version.cjs +1 -1
- package/version.d.cts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
|
@@ -2,7 +2,7 @@ import type { ColumnBuilderBaseConfig } from "../../column-builder.js";
|
|
|
2
2
|
import type { ColumnBaseConfig } from "../../column.js";
|
|
3
3
|
import { entityKind } from "../../entity.js";
|
|
4
4
|
import type { AnyMySqlTable } from "../table.js";
|
|
5
|
-
import type { SQL
|
|
5
|
+
import type { SQL } from "../../sql/sql.js";
|
|
6
6
|
import { type Equal } from "../../utils.js";
|
|
7
7
|
import { MySqlColumn, MySqlColumnBuilder } from "./common.js";
|
|
8
8
|
export type ConvertCustomConfig<TName extends string, T extends Partial<CustomTypeValues>> = {
|
|
@@ -34,15 +34,11 @@ export declare class MySqlCustomColumn<T extends ColumnBaseConfig<'custom', 'MyS
|
|
|
34
34
|
private sqlName;
|
|
35
35
|
private mapTo?;
|
|
36
36
|
private mapFrom?;
|
|
37
|
-
private mapJson?;
|
|
38
|
-
private forJsonSelect?;
|
|
39
37
|
constructor(table: AnyMySqlTable<{
|
|
40
38
|
name: T['tableName'];
|
|
41
39
|
}>, config: MySqlCustomColumnBuilder<T>['config']);
|
|
42
40
|
getSQLType(): string;
|
|
43
41
|
mapFromDriverValue(value: T['driverParam']): T['data'];
|
|
44
|
-
mapFromJsonValue(value: unknown): T['data'];
|
|
45
|
-
jsonSelectIdentifier(identifier: SQL, sql: SQLGenerator): SQL;
|
|
46
42
|
mapToDriverValue(value: T['data']): T['driverParam'];
|
|
47
43
|
}
|
|
48
44
|
export type CustomTypeValues = {
|
|
@@ -56,23 +52,10 @@ export type CustomTypeValues = {
|
|
|
56
52
|
* If you want your column to be `number` type after selecting/or on inserting - use `data: number`. Like `integer`
|
|
57
53
|
*/
|
|
58
54
|
data: unknown;
|
|
59
|
-
/**
|
|
60
|
-
* Type helper, that represents what type database driver is returning for specific database data type
|
|
61
|
-
*
|
|
62
|
-
* Needed only in case driver's output and input for type differ
|
|
63
|
-
*
|
|
64
|
-
* @default
|
|
65
|
-
* Defaults to `driverData`
|
|
66
|
-
*/
|
|
67
|
-
driverOutput?: unknown;
|
|
68
55
|
/**
|
|
69
56
|
* Type helper, that represents what type database driver is accepting for specific database data type
|
|
70
57
|
*/
|
|
71
58
|
driverData?: unknown;
|
|
72
|
-
/**
|
|
73
|
-
* Type helper, that represents what type field returns after being aggregated to JSON
|
|
74
|
-
*/
|
|
75
|
-
jsonData?: unknown;
|
|
76
59
|
/**
|
|
77
60
|
* What config type should be used for {@link CustomTypeParams} `dataType` generation
|
|
78
61
|
*/
|
|
@@ -137,7 +120,7 @@ export interface CustomTypeParams<T extends CustomTypeValues> {
|
|
|
137
120
|
*/
|
|
138
121
|
dataType: (config: T['config'] | (Equal<T['configRequired'], true> extends true ? never : undefined)) => string;
|
|
139
122
|
/**
|
|
140
|
-
* Optional mapping function,
|
|
123
|
+
* Optional mapping function, between user input and driver
|
|
141
124
|
* @example
|
|
142
125
|
* For example, when using jsonb we need to map JS/TS object to string before writing to database
|
|
143
126
|
* ```
|
|
@@ -148,114 +131,16 @@ export interface CustomTypeParams<T extends CustomTypeValues> {
|
|
|
148
131
|
*/
|
|
149
132
|
toDriver?: (value: T['data']) => T['driverData'] | SQL;
|
|
150
133
|
/**
|
|
151
|
-
* Optional mapping function, that is
|
|
134
|
+
* Optional mapping function, that is responsible for data mapping from database to JS/TS code
|
|
152
135
|
* @example
|
|
153
136
|
* For example, when using timestamp we need to map string Date representation to JS Date
|
|
154
137
|
* ```
|
|
155
138
|
* fromDriver(value: string): Date {
|
|
156
139
|
* return new Date(value);
|
|
157
|
-
* }
|
|
158
|
-
* ```
|
|
159
|
-
*
|
|
160
|
-
* It'll cause the returned data to change from:
|
|
161
|
-
* ```
|
|
162
|
-
* {
|
|
163
|
-
* customField: "2025-04-07 03:25:16.635";
|
|
164
|
-
* }
|
|
165
|
-
* ```
|
|
166
|
-
* to:
|
|
167
|
-
* ```
|
|
168
|
-
* {
|
|
169
|
-
* customField: new Date("2025-04-07 03:25:16.635");
|
|
170
|
-
* }
|
|
171
|
-
* ```
|
|
172
|
-
*/
|
|
173
|
-
fromDriver?: (value: 'driverOutput' extends keyof T ? T['driverOutput'] : T['driverData']) => T['data'];
|
|
174
|
-
/**
|
|
175
|
-
* Optional mapping function, that is used for transforming data returned by transofmed to JSON in database data to desired format
|
|
176
|
-
*
|
|
177
|
-
* Used by relational queries
|
|
178
|
-
* @example
|
|
179
|
-
* For example, when querying bigint column via RQB or JSON funcitons, the result field will be returned as it's string representation, as opposed to bigint from regular query
|
|
180
|
-
* To handle that, we need a separate function to handle such field's mapping:
|
|
181
|
-
* ```
|
|
182
|
-
* fromJson(value: string): bigint {
|
|
183
|
-
* return BigInt(value);
|
|
184
|
-
* },
|
|
185
|
-
* ```
|
|
186
|
-
*
|
|
187
|
-
* It'll cause the returned data to change from:
|
|
188
|
-
* ```
|
|
189
|
-
* {
|
|
190
|
-
* customField: "5044565289845416380";
|
|
191
|
-
* }
|
|
192
|
-
* ```
|
|
193
|
-
* to:
|
|
194
|
-
* ```
|
|
195
|
-
* {
|
|
196
|
-
* customField: 5044565289845416380n;
|
|
197
|
-
* }
|
|
198
|
-
* ```
|
|
199
|
-
* @default
|
|
200
|
-
* Defaults to {@link fromDriver} function
|
|
201
|
-
*/
|
|
202
|
-
fromJson?: (value: T['jsonData']) => T['data'];
|
|
203
|
-
/**
|
|
204
|
-
* Optional selection modifier function, that is used for modifying selection of column inside JSON functions
|
|
205
|
-
*
|
|
206
|
-
* Additional mapping that could be required for such scenarios can be handled using {@link fromJson} function
|
|
207
|
-
*
|
|
208
|
-
* Used by relational queries
|
|
209
|
-
* @example
|
|
210
|
-
* For example, when using bigint we need to cast field to text to preserve data integrity
|
|
211
|
-
* ```
|
|
212
|
-
* forJsonSelect(identifier: SQL, sql: SQLGenerator): SQL {
|
|
213
|
-
* return sql`cast(${identifier} as char)`
|
|
214
140
|
* },
|
|
215
141
|
* ```
|
|
216
|
-
*
|
|
217
|
-
* This will change query from:
|
|
218
|
-
* ```
|
|
219
|
-
* SELECT
|
|
220
|
-
* json_build_object('bigint', `t`.`bigint`)
|
|
221
|
-
* FROM
|
|
222
|
-
* (
|
|
223
|
-
* SELECT
|
|
224
|
-
* `table`.`custom_bigint` AS `bigint`
|
|
225
|
-
* FROM
|
|
226
|
-
* `table`
|
|
227
|
-
* ) AS `t`
|
|
228
|
-
* ```
|
|
229
|
-
* to:
|
|
230
|
-
* ```
|
|
231
|
-
* SELECT
|
|
232
|
-
* json_build_object('bigint', `t`.`bigint`)
|
|
233
|
-
* FROM
|
|
234
|
-
* (
|
|
235
|
-
* SELECT
|
|
236
|
-
* cast(`table`.`custom_bigint` as char) AS `bigint`
|
|
237
|
-
* FROM
|
|
238
|
-
* `table`
|
|
239
|
-
* ) AS `t`
|
|
240
|
-
* ```
|
|
241
|
-
*
|
|
242
|
-
* Returned by query object will change from:
|
|
243
|
-
* ```
|
|
244
|
-
* {
|
|
245
|
-
* bigint: 5044565289845416000; // Partial data loss due to direct conversion to JSON format
|
|
246
|
-
* }
|
|
247
|
-
* ```
|
|
248
|
-
* to:
|
|
249
|
-
* ```
|
|
250
|
-
* {
|
|
251
|
-
* bigint: "5044565289845416380"; // Data is preserved due to conversion of field to text before JSON-ification
|
|
252
|
-
* }
|
|
253
|
-
* ```
|
|
254
|
-
*
|
|
255
|
-
* @default
|
|
256
|
-
* Following types are being casted to text by default: `binary`, `varbinary`, `time`, `datetime`, `decimal`, `float`, 'bigint'
|
|
257
142
|
*/
|
|
258
|
-
|
|
143
|
+
fromDriver?: (value: T['driverData']) => T['data'];
|
|
259
144
|
}
|
|
260
145
|
/**
|
|
261
146
|
* Custom mysql database data type generator
|
|
@@ -21,15 +21,11 @@ class MySqlCustomColumn extends MySqlColumn {
|
|
|
21
21
|
sqlName;
|
|
22
22
|
mapTo;
|
|
23
23
|
mapFrom;
|
|
24
|
-
mapJson;
|
|
25
|
-
forJsonSelect;
|
|
26
24
|
constructor(table, config) {
|
|
27
25
|
super(table, config);
|
|
28
26
|
this.sqlName = config.customTypeParams.dataType(config.fieldConfig);
|
|
29
27
|
this.mapTo = config.customTypeParams.toDriver;
|
|
30
28
|
this.mapFrom = config.customTypeParams.fromDriver;
|
|
31
|
-
this.mapJson = config.customTypeParams.fromJson;
|
|
32
|
-
this.forJsonSelect = config.customTypeParams.forJsonSelect;
|
|
33
29
|
}
|
|
34
30
|
getSQLType() {
|
|
35
31
|
return this.sqlName;
|
|
@@ -37,30 +33,6 @@ class MySqlCustomColumn extends MySqlColumn {
|
|
|
37
33
|
mapFromDriverValue(value) {
|
|
38
34
|
return typeof this.mapFrom === "function" ? this.mapFrom(value) : value;
|
|
39
35
|
}
|
|
40
|
-
mapFromJsonValue(value) {
|
|
41
|
-
return typeof this.mapJson === "function" ? this.mapJson(value) : this.mapFromDriverValue(value);
|
|
42
|
-
}
|
|
43
|
-
jsonSelectIdentifier(identifier, sql) {
|
|
44
|
-
if (typeof this.forJsonSelect === "function")
|
|
45
|
-
return this.forJsonSelect(identifier, sql);
|
|
46
|
-
const rawType = this.getSQLType().toLowerCase();
|
|
47
|
-
const parenPos = rawType.indexOf("(");
|
|
48
|
-
const type = parenPos + 1 ? rawType.slice(0, parenPos) : rawType;
|
|
49
|
-
switch (type) {
|
|
50
|
-
case "binary":
|
|
51
|
-
case "varbinary":
|
|
52
|
-
case "time":
|
|
53
|
-
case "datetime":
|
|
54
|
-
case "decimal":
|
|
55
|
-
case "float":
|
|
56
|
-
case "bigint": {
|
|
57
|
-
return sql`cast(${identifier} as char)`;
|
|
58
|
-
}
|
|
59
|
-
default: {
|
|
60
|
-
return identifier;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
36
|
mapToDriverValue(value) {
|
|
65
37
|
return typeof this.mapTo === "function" ? this.mapTo(value) : value;
|
|
66
38
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/mysql-core/columns/custom.ts"],"sourcesContent":["import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';\nimport type { ColumnBaseConfig } from '~/column.ts';\nimport { entityKind } from '~/entity.ts';\nimport type { AnyMySqlTable } from '~/mysql-core/table.ts';\nimport type { SQL, SQLGenerator } from '~/sql/sql.ts';\nimport { type Equal, getColumnNameAndConfig } from '~/utils.ts';\nimport { MySqlColumn, MySqlColumnBuilder } from './common.ts';\n\nexport type ConvertCustomConfig<TName extends string, T extends Partial<CustomTypeValues>> =\n\t& {\n\t\tname: TName;\n\t\tdataType: 'custom';\n\t\tcolumnType: 'MySqlCustomColumn';\n\t\tdata: T['data'];\n\t\tdriverParam: T['driverData'];\n\t\tenumValues: undefined;\n\t}\n\t& (T['notNull'] extends true ? { notNull: true } : {})\n\t& (T['default'] extends true ? { hasDefault: true } : {});\n\nexport interface MySqlCustomColumnInnerConfig {\n\tcustomTypeValues: CustomTypeValues;\n}\n\nexport class MySqlCustomColumnBuilder<T extends ColumnBuilderBaseConfig<'custom', 'MySqlCustomColumn'>>\n\textends MySqlColumnBuilder<\n\t\tT,\n\t\t{\n\t\t\tfieldConfig: CustomTypeValues['config'];\n\t\t\tcustomTypeParams: CustomTypeParams<any>;\n\t\t},\n\t\t{\n\t\t\tmysqlColumnBuilderBrand: 'MySqlCustomColumnBuilderBrand';\n\t\t}\n\t>\n{\n\tstatic override readonly [entityKind]: string = 'MySqlCustomColumnBuilder';\n\n\tconstructor(\n\t\tname: T['name'],\n\t\tfieldConfig: CustomTypeValues['config'],\n\t\tcustomTypeParams: CustomTypeParams<any>,\n\t) {\n\t\tsuper(name, 'custom', 'MySqlCustomColumn');\n\t\tthis.config.fieldConfig = fieldConfig;\n\t\tthis.config.customTypeParams = customTypeParams;\n\t}\n\n\t/** @internal */\n\tbuild<TTableName extends string>(\n\t\ttable: AnyMySqlTable<{ name: TTableName }>,\n\t): MySqlCustomColumn<MakeColumnConfig<T, TTableName>> {\n\t\treturn new MySqlCustomColumn<MakeColumnConfig<T, TTableName>>(\n\t\t\ttable,\n\t\t\tthis.config as ColumnBuilderRuntimeConfig<any, any>,\n\t\t);\n\t}\n}\n\nexport class MySqlCustomColumn<T extends ColumnBaseConfig<'custom', 'MySqlCustomColumn'>> extends MySqlColumn<T> {\n\tstatic override readonly [entityKind]: string = 'MySqlCustomColumn';\n\n\tprivate sqlName: string;\n\tprivate mapTo?: (value: T['data']) => T['driverParam'];\n\tprivate mapFrom?: (value: T['driverParam']) => T['data'];\n\tprivate mapJson?: (value: unknown) => T['data'];\n\tprivate forJsonSelect?: (name: SQL, sql: SQLGenerator) => SQL;\n\n\tconstructor(\n\t\ttable: AnyMySqlTable<{ name: T['tableName'] }>,\n\t\tconfig: MySqlCustomColumnBuilder<T>['config'],\n\t) {\n\t\tsuper(table, config);\n\t\tthis.sqlName = config.customTypeParams.dataType(config.fieldConfig);\n\t\tthis.mapTo = config.customTypeParams.toDriver;\n\t\tthis.mapFrom = config.customTypeParams.fromDriver;\n\t\tthis.mapJson = config.customTypeParams.fromJson;\n\t\tthis.forJsonSelect = config.customTypeParams.forJsonSelect;\n\t}\n\n\tgetSQLType(): string {\n\t\treturn this.sqlName;\n\t}\n\n\toverride mapFromDriverValue(value: T['driverParam']): T['data'] {\n\t\treturn typeof this.mapFrom === 'function' ? this.mapFrom(value) : value as T['data'];\n\t}\n\n\tmapFromJsonValue(value: unknown): T['data'] {\n\t\treturn typeof this.mapJson === 'function' ? this.mapJson(value) : this.mapFromDriverValue(value) as T['data'];\n\t}\n\n\tjsonSelectIdentifier(identifier: SQL, sql: SQLGenerator): SQL {\n\t\tif (typeof this.forJsonSelect === 'function') return this.forJsonSelect(identifier, sql);\n\n\t\tconst rawType = this.getSQLType().toLowerCase();\n\t\tconst parenPos = rawType.indexOf('(');\n\t\tconst type = (parenPos + 1) ? rawType.slice(0, parenPos) : rawType;\n\n\t\tswitch (type) {\n\t\t\tcase 'binary':\n\t\t\tcase 'varbinary':\n\t\t\tcase 'time':\n\t\t\tcase 'datetime':\n\t\t\tcase 'decimal':\n\t\t\tcase 'float':\n\t\t\tcase 'bigint': {\n\t\t\t\treturn sql`cast(${identifier} as char)`;\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn identifier;\n\t\t\t}\n\t\t}\n\t}\n\n\toverride mapToDriverValue(value: T['data']): T['driverParam'] {\n\t\treturn typeof this.mapTo === 'function' ? this.mapTo(value) : value as T['data'];\n\t}\n}\n\nexport type CustomTypeValues = {\n\t/**\n\t * Required type for custom column, that will infer proper type model\n\t *\n\t * Examples:\n\t *\n\t * If you want your column to be `string` type after selecting/or on inserting - use `data: string`. Like `text`, `varchar`\n\t *\n\t * If you want your column to be `number` type after selecting/or on inserting - use `data: number`. Like `integer`\n\t */\n\tdata: unknown;\n\n\t/**\n\t * Type helper, that represents what type database driver is returning for specific database data type\n\t *\n\t * Needed only in case driver's output and input for type differ\n\t *\n\t * @default\n\t * Defaults to `driverData`\n\t */\n\tdriverOutput?: unknown;\n\n\t/**\n\t * Type helper, that represents what type database driver is accepting for specific database data type\n\t */\n\tdriverData?: unknown;\n\n\t/**\n\t * Type helper, that represents what type field returns after being aggregated to JSON\n\t */\n\tjsonData?: unknown;\n\n\t/**\n\t * What config type should be used for {@link CustomTypeParams} `dataType` generation\n\t */\n\tconfig?: Record<string, any>;\n\n\t/**\n\t * Whether the config argument should be required or not\n\t * @default false\n\t */\n\tconfigRequired?: boolean;\n\n\t/**\n\t * If your custom data type should be notNull by default you can use `notNull: true`\n\t *\n\t * @example\n\t * const customSerial = customType<{ data: number, notNull: true, default: true }>({\n\t * \t dataType() {\n\t * \t return 'serial';\n\t * },\n\t * });\n\t */\n\tnotNull?: boolean;\n\n\t/**\n\t * If your custom data type has default you can use `default: true`\n\t *\n\t * @example\n\t * const customSerial = customType<{ data: number, notNull: true, default: true }>({\n\t * \t dataType() {\n\t * \t return 'serial';\n\t * },\n\t * });\n\t */\n\tdefault?: boolean;\n};\n\nexport interface CustomTypeParams<T extends CustomTypeValues> {\n\t/**\n\t * Database data type string representation, that is used for migrations\n\t * @example\n\t * ```\n\t * `jsonb`, `text`\n\t * ```\n\t *\n\t * If database data type needs additional params you can use them from `config` param\n\t * @example\n\t * ```\n\t * `varchar(256)`, `numeric(2,3)`\n\t * ```\n\t *\n\t * To make `config` be of specific type please use config generic in {@link CustomTypeValues}\n\t *\n\t * @example\n\t * Usage example\n\t * ```\n\t * dataType() {\n\t * return 'boolean';\n\t * },\n\t * ```\n\t * Or\n\t * ```\n\t * dataType(config) {\n\t * \t return typeof config.length !== 'undefined' ? `varchar(${config.length})` : `varchar`;\n\t * \t }\n\t * ```\n\t */\n\tdataType: (config: T['config'] | (Equal<T['configRequired'], true> extends true ? never : undefined)) => string;\n\n\t/**\n\t * Optional mapping function, that is used to transform inputs from desired to be used in code format to one suitable for driver\n\t * @example\n\t * For example, when using jsonb we need to map JS/TS object to string before writing to database\n\t * ```\n\t * toDriver(value: TData): string {\n\t * \t return JSON.stringify(value);\n\t * }\n\t * ```\n\t */\n\ttoDriver?: (value: T['data']) => T['driverData'] | SQL;\n\n\t/**\n\t * Optional mapping function, that is used for transforming data returned by driver to desired column's output format\n\t * @example\n\t * For example, when using timestamp we need to map string Date representation to JS Date\n\t * ```\n\t * fromDriver(value: string): Date {\n\t * \treturn new Date(value);\n\t * }\n\t * ```\n\t *\n\t * It'll cause the returned data to change from:\n\t * ```\n\t * {\n\t * \tcustomField: \"2025-04-07 03:25:16.635\";\n\t * }\n\t * ```\n\t * to:\n\t * ```\n\t * {\n\t * \tcustomField: new Date(\"2025-04-07 03:25:16.635\");\n\t * }\n\t * ```\n\t */\n\tfromDriver?: (value: 'driverOutput' extends keyof T ? T['driverOutput'] : T['driverData']) => T['data'];\n\n\t/**\n\t * Optional mapping function, that is used for transforming data returned by transofmed to JSON in database data to desired format\n\t *\n\t * Used by relational queries\n\t * @example\n\t * For example, when querying bigint column via RQB or JSON funcitons, the result field will be returned as it's string representation, as opposed to bigint from regular query\n\t * To handle that, we need a separate function to handle such field's mapping:\n\t * ```\n\t * fromJson(value: string): bigint {\n\t * \treturn BigInt(value);\n\t * },\n\t * ```\n\t *\n\t * It'll cause the returned data to change from:\n\t * ```\n\t * {\n\t * \tcustomField: \"5044565289845416380\";\n\t * }\n\t * ```\n\t * to:\n\t * ```\n\t * {\n\t * \tcustomField: 5044565289845416380n;\n\t * }\n\t * ```\n\t * @default\n\t * Defaults to {@link fromDriver} function\n\t */\n\tfromJson?: (value: T['jsonData']) => T['data'];\n\n\t/**\n\t * Optional selection modifier function, that is used for modifying selection of column inside JSON functions\n\t *\n\t * Additional mapping that could be required for such scenarios can be handled using {@link fromJson} function\n\t *\n\t * Used by relational queries\n\t * @example\n\t * For example, when using bigint we need to cast field to text to preserve data integrity\n\t * ```\n\t * forJsonSelect(identifier: SQL, sql: SQLGenerator): SQL {\n\t * \treturn sql`cast(${identifier} as char)`\n\t * },\n\t * ```\n\t *\n\t * This will change query from:\n\t * ```\n\t * SELECT\n\t * \tjson_build_object('bigint', `t`.`bigint`)\n\t * \tFROM\n\t * \t(\n\t * \t\tSELECT\n\t * \t\t`table`.`custom_bigint` AS `bigint`\n\t * \t\tFROM\n\t * \t\t`table`\n\t * \t) AS `t`\n\t * ```\n\t * to:\n\t * ```\n\t * SELECT\n\t * \tjson_build_object('bigint', `t`.`bigint`)\n\t * \tFROM\n\t * \t(\n\t * \t\tSELECT\n\t * \t\tcast(`table`.`custom_bigint` as char) AS `bigint`\n\t * \t\tFROM\n\t * \t\t`table`\n\t * \t) AS `t`\n\t * ```\n\t *\n\t * Returned by query object will change from:\n\t * ```\n\t * {\n\t * \tbigint: 5044565289845416000; // Partial data loss due to direct conversion to JSON format\n\t * }\n\t * ```\n\t * to:\n\t * ```\n\t * {\n\t * \tbigint: \"5044565289845416380\"; // Data is preserved due to conversion of field to text before JSON-ification\n\t * }\n\t * ```\n\t *\n\t * @default\n\t * Following types are being casted to text by default: `binary`, `varbinary`, `time`, `datetime`, `decimal`, `float`, 'bigint'\n\t */\n\tforJsonSelect?: (identifier: SQL, sql: SQLGenerator) => SQL;\n}\n\n/**\n * Custom mysql database data type generator\n */\nexport function customType<T extends CustomTypeValues = CustomTypeValues>(\n\tcustomTypeParams: CustomTypeParams<T>,\n): Equal<T['configRequired'], true> extends true ? {\n\t\t<TConfig extends Record<string, any> & T['config']>(\n\t\t\tfieldConfig: TConfig,\n\t\t): MySqlCustomColumnBuilder<ConvertCustomConfig<'', T>>;\n\t\t<TName extends string>(\n\t\t\tdbName: TName,\n\t\t\tfieldConfig: T['config'],\n\t\t): MySqlCustomColumnBuilder<ConvertCustomConfig<TName, T>>;\n\t}\n\t: {\n\t\t(): MySqlCustomColumnBuilder<ConvertCustomConfig<'', T>>;\n\t\t<TConfig extends Record<string, any> & T['config']>(\n\t\t\tfieldConfig?: TConfig,\n\t\t): MySqlCustomColumnBuilder<ConvertCustomConfig<'', T>>;\n\t\t<TName extends string>(\n\t\t\tdbName: TName,\n\t\t\tfieldConfig?: T['config'],\n\t\t): MySqlCustomColumnBuilder<ConvertCustomConfig<TName, T>>;\n\t}\n{\n\treturn <TName extends string>(\n\t\ta?: TName | T['config'],\n\t\tb?: T['config'],\n\t): MySqlCustomColumnBuilder<ConvertCustomConfig<TName, T>> => {\n\t\tconst { name, config } = getColumnNameAndConfig<T['config']>(a, b);\n\t\treturn new MySqlCustomColumnBuilder(name as ConvertCustomConfig<TName, T>['name'], config, customTypeParams);\n\t};\n}\n"],"mappings":"AAEA,SAAS,kBAAkB;AAG3B,SAAqB,8BAA8B;AACnD,SAAS,aAAa,0BAA0B;AAkBzC,MAAM,iCACJ,mBAUT;AAAA,EACC,QAA0B,UAAU,IAAY;AAAA,EAEhD,YACC,MACA,aACA,kBACC;AACD,UAAM,MAAM,UAAU,mBAAmB;AACzC,SAAK,OAAO,cAAc;AAC1B,SAAK,OAAO,mBAAmB;AAAA,EAChC;AAAA;AAAA,EAGA,MACC,OACqD;AACrD,WAAO,IAAI;AAAA,MACV;AAAA,MACA,KAAK;AAAA,IACN;AAAA,EACD;AACD;AAEO,MAAM,0BAAqF,YAAe;AAAA,EAChH,QAA0B,UAAU,IAAY;AAAA,EAExC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAER,YACC,OACA,QACC;AACD,UAAM,OAAO,MAAM;AACnB,SAAK,UAAU,OAAO,iBAAiB,SAAS,OAAO,WAAW;AAClE,SAAK,QAAQ,OAAO,iBAAiB;AACrC,SAAK,UAAU,OAAO,iBAAiB;AACvC,SAAK,UAAU,OAAO,iBAAiB;AACvC,SAAK,gBAAgB,OAAO,iBAAiB;AAAA,EAC9C;AAAA,EAEA,aAAqB;AACpB,WAAO,KAAK;AAAA,EACb;AAAA,EAES,mBAAmB,OAAoC;AAC/D,WAAO,OAAO,KAAK,YAAY,aAAa,KAAK,QAAQ,KAAK,IAAI;AAAA,EACnE;AAAA,EAEA,iBAAiB,OAA2B;AAC3C,WAAO,OAAO,KAAK,YAAY,aAAa,KAAK,QAAQ,KAAK,IAAI,KAAK,mBAAmB,KAAK;AAAA,EAChG;AAAA,EAEA,qBAAqB,YAAiB,KAAwB;AAC7D,QAAI,OAAO,KAAK,kBAAkB;AAAY,aAAO,KAAK,cAAc,YAAY,GAAG;AAEvF,UAAM,UAAU,KAAK,WAAW,EAAE,YAAY;AAC9C,UAAM,WAAW,QAAQ,QAAQ,GAAG;AACpC,UAAM,OAAQ,WAAW,IAAK,QAAQ,MAAM,GAAG,QAAQ,IAAI;AAE3D,YAAQ,MAAM;AAAA,MACb,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK,UAAU;AACd,eAAO,WAAW,UAAU;AAAA,MAC7B;AAAA,MACA,SAAS;AACR,eAAO;AAAA,MACR;AAAA,IACD;AAAA,EACD;AAAA,EAES,iBAAiB,OAAoC;AAC7D,WAAO,OAAO,KAAK,UAAU,aAAa,KAAK,MAAM,KAAK,IAAI;AAAA,EAC/D;AACD;AAsOO,SAAS,WACf,kBAoBD;AACC,SAAO,CACN,GACA,MAC6D;AAC7D,UAAM,EAAE,MAAM,OAAO,IAAI,uBAAoC,GAAG,CAAC;AACjE,WAAO,IAAI,yBAAyB,MAA+C,QAAQ,gBAAgB;AAAA,EAC5G;AACD;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/mysql-core/columns/custom.ts"],"sourcesContent":["import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';\nimport type { ColumnBaseConfig } from '~/column.ts';\nimport { entityKind } from '~/entity.ts';\nimport type { AnyMySqlTable } from '~/mysql-core/table.ts';\nimport type { SQL } from '~/sql/sql.ts';\nimport { type Equal, getColumnNameAndConfig } from '~/utils.ts';\nimport { MySqlColumn, MySqlColumnBuilder } from './common.ts';\n\nexport type ConvertCustomConfig<TName extends string, T extends Partial<CustomTypeValues>> =\n\t& {\n\t\tname: TName;\n\t\tdataType: 'custom';\n\t\tcolumnType: 'MySqlCustomColumn';\n\t\tdata: T['data'];\n\t\tdriverParam: T['driverData'];\n\t\tenumValues: undefined;\n\t}\n\t& (T['notNull'] extends true ? { notNull: true } : {})\n\t& (T['default'] extends true ? { hasDefault: true } : {});\n\nexport interface MySqlCustomColumnInnerConfig {\n\tcustomTypeValues: CustomTypeValues;\n}\n\nexport class MySqlCustomColumnBuilder<T extends ColumnBuilderBaseConfig<'custom', 'MySqlCustomColumn'>>\n\textends MySqlColumnBuilder<\n\t\tT,\n\t\t{\n\t\t\tfieldConfig: CustomTypeValues['config'];\n\t\t\tcustomTypeParams: CustomTypeParams<any>;\n\t\t},\n\t\t{\n\t\t\tmysqlColumnBuilderBrand: 'MySqlCustomColumnBuilderBrand';\n\t\t}\n\t>\n{\n\tstatic override readonly [entityKind]: string = 'MySqlCustomColumnBuilder';\n\n\tconstructor(\n\t\tname: T['name'],\n\t\tfieldConfig: CustomTypeValues['config'],\n\t\tcustomTypeParams: CustomTypeParams<any>,\n\t) {\n\t\tsuper(name, 'custom', 'MySqlCustomColumn');\n\t\tthis.config.fieldConfig = fieldConfig;\n\t\tthis.config.customTypeParams = customTypeParams;\n\t}\n\n\t/** @internal */\n\tbuild<TTableName extends string>(\n\t\ttable: AnyMySqlTable<{ name: TTableName }>,\n\t): MySqlCustomColumn<MakeColumnConfig<T, TTableName>> {\n\t\treturn new MySqlCustomColumn<MakeColumnConfig<T, TTableName>>(\n\t\t\ttable,\n\t\t\tthis.config as ColumnBuilderRuntimeConfig<any, any>,\n\t\t);\n\t}\n}\n\nexport class MySqlCustomColumn<T extends ColumnBaseConfig<'custom', 'MySqlCustomColumn'>> extends MySqlColumn<T> {\n\tstatic override readonly [entityKind]: string = 'MySqlCustomColumn';\n\n\tprivate sqlName: string;\n\tprivate mapTo?: (value: T['data']) => T['driverParam'];\n\tprivate mapFrom?: (value: T['driverParam']) => T['data'];\n\n\tconstructor(\n\t\ttable: AnyMySqlTable<{ name: T['tableName'] }>,\n\t\tconfig: MySqlCustomColumnBuilder<T>['config'],\n\t) {\n\t\tsuper(table, config);\n\t\tthis.sqlName = config.customTypeParams.dataType(config.fieldConfig);\n\t\tthis.mapTo = config.customTypeParams.toDriver;\n\t\tthis.mapFrom = config.customTypeParams.fromDriver;\n\t}\n\n\tgetSQLType(): string {\n\t\treturn this.sqlName;\n\t}\n\n\toverride mapFromDriverValue(value: T['driverParam']): T['data'] {\n\t\treturn typeof this.mapFrom === 'function' ? this.mapFrom(value) : value as T['data'];\n\t}\n\n\toverride mapToDriverValue(value: T['data']): T['driverParam'] {\n\t\treturn typeof this.mapTo === 'function' ? this.mapTo(value) : value as T['data'];\n\t}\n}\n\nexport type CustomTypeValues = {\n\t/**\n\t * Required type for custom column, that will infer proper type model\n\t *\n\t * Examples:\n\t *\n\t * If you want your column to be `string` type after selecting/or on inserting - use `data: string`. Like `text`, `varchar`\n\t *\n\t * If you want your column to be `number` type after selecting/or on inserting - use `data: number`. Like `integer`\n\t */\n\tdata: unknown;\n\n\t/**\n\t * Type helper, that represents what type database driver is accepting for specific database data type\n\t */\n\tdriverData?: unknown;\n\n\t/**\n\t * What config type should be used for {@link CustomTypeParams} `dataType` generation\n\t */\n\tconfig?: Record<string, any>;\n\n\t/**\n\t * Whether the config argument should be required or not\n\t * @default false\n\t */\n\tconfigRequired?: boolean;\n\n\t/**\n\t * If your custom data type should be notNull by default you can use `notNull: true`\n\t *\n\t * @example\n\t * const customSerial = customType<{ data: number, notNull: true, default: true }>({\n\t * \t dataType() {\n\t * \t return 'serial';\n\t * },\n\t * });\n\t */\n\tnotNull?: boolean;\n\n\t/**\n\t * If your custom data type has default you can use `default: true`\n\t *\n\t * @example\n\t * const customSerial = customType<{ data: number, notNull: true, default: true }>({\n\t * \t dataType() {\n\t * \t return 'serial';\n\t * },\n\t * });\n\t */\n\tdefault?: boolean;\n};\n\nexport interface CustomTypeParams<T extends CustomTypeValues> {\n\t/**\n\t * Database data type string representation, that is used for migrations\n\t * @example\n\t * ```\n\t * `jsonb`, `text`\n\t * ```\n\t *\n\t * If database data type needs additional params you can use them from `config` param\n\t * @example\n\t * ```\n\t * `varchar(256)`, `numeric(2,3)`\n\t * ```\n\t *\n\t * To make `config` be of specific type please use config generic in {@link CustomTypeValues}\n\t *\n\t * @example\n\t * Usage example\n\t * ```\n\t * dataType() {\n\t * return 'boolean';\n\t * },\n\t * ```\n\t * Or\n\t * ```\n\t * dataType(config) {\n\t * \t return typeof config.length !== 'undefined' ? `varchar(${config.length})` : `varchar`;\n\t * \t }\n\t * ```\n\t */\n\tdataType: (config: T['config'] | (Equal<T['configRequired'], true> extends true ? never : undefined)) => string;\n\n\t/**\n\t * Optional mapping function, between user input and driver\n\t * @example\n\t * For example, when using jsonb we need to map JS/TS object to string before writing to database\n\t * ```\n\t * toDriver(value: TData): string {\n\t * \t return JSON.stringify(value);\n\t * }\n\t * ```\n\t */\n\ttoDriver?: (value: T['data']) => T['driverData'] | SQL;\n\n\t/**\n\t * Optional mapping function, that is responsible for data mapping from database to JS/TS code\n\t * @example\n\t * For example, when using timestamp we need to map string Date representation to JS Date\n\t * ```\n\t * fromDriver(value: string): Date {\n\t * \treturn new Date(value);\n\t * },\n\t * ```\n\t */\n\tfromDriver?: (value: T['driverData']) => T['data'];\n}\n\n/**\n * Custom mysql database data type generator\n */\nexport function customType<T extends CustomTypeValues = CustomTypeValues>(\n\tcustomTypeParams: CustomTypeParams<T>,\n): Equal<T['configRequired'], true> extends true ? {\n\t\t<TConfig extends Record<string, any> & T['config']>(\n\t\t\tfieldConfig: TConfig,\n\t\t): MySqlCustomColumnBuilder<ConvertCustomConfig<'', T>>;\n\t\t<TName extends string>(\n\t\t\tdbName: TName,\n\t\t\tfieldConfig: T['config'],\n\t\t): MySqlCustomColumnBuilder<ConvertCustomConfig<TName, T>>;\n\t}\n\t: {\n\t\t(): MySqlCustomColumnBuilder<ConvertCustomConfig<'', T>>;\n\t\t<TConfig extends Record<string, any> & T['config']>(\n\t\t\tfieldConfig?: TConfig,\n\t\t): MySqlCustomColumnBuilder<ConvertCustomConfig<'', T>>;\n\t\t<TName extends string>(\n\t\t\tdbName: TName,\n\t\t\tfieldConfig?: T['config'],\n\t\t): MySqlCustomColumnBuilder<ConvertCustomConfig<TName, T>>;\n\t}\n{\n\treturn <TName extends string>(\n\t\ta?: TName | T['config'],\n\t\tb?: T['config'],\n\t): MySqlCustomColumnBuilder<ConvertCustomConfig<TName, T>> => {\n\t\tconst { name, config } = getColumnNameAndConfig<T['config']>(a, b);\n\t\treturn new MySqlCustomColumnBuilder(name as ConvertCustomConfig<TName, T>['name'], config, customTypeParams);\n\t};\n}\n"],"mappings":"AAEA,SAAS,kBAAkB;AAG3B,SAAqB,8BAA8B;AACnD,SAAS,aAAa,0BAA0B;AAkBzC,MAAM,iCACJ,mBAUT;AAAA,EACC,QAA0B,UAAU,IAAY;AAAA,EAEhD,YACC,MACA,aACA,kBACC;AACD,UAAM,MAAM,UAAU,mBAAmB;AACzC,SAAK,OAAO,cAAc;AAC1B,SAAK,OAAO,mBAAmB;AAAA,EAChC;AAAA;AAAA,EAGA,MACC,OACqD;AACrD,WAAO,IAAI;AAAA,MACV;AAAA,MACA,KAAK;AAAA,IACN;AAAA,EACD;AACD;AAEO,MAAM,0BAAqF,YAAe;AAAA,EAChH,QAA0B,UAAU,IAAY;AAAA,EAExC;AAAA,EACA;AAAA,EACA;AAAA,EAER,YACC,OACA,QACC;AACD,UAAM,OAAO,MAAM;AACnB,SAAK,UAAU,OAAO,iBAAiB,SAAS,OAAO,WAAW;AAClE,SAAK,QAAQ,OAAO,iBAAiB;AACrC,SAAK,UAAU,OAAO,iBAAiB;AAAA,EACxC;AAAA,EAEA,aAAqB;AACpB,WAAO,KAAK;AAAA,EACb;AAAA,EAES,mBAAmB,OAAoC;AAC/D,WAAO,OAAO,KAAK,YAAY,aAAa,KAAK,QAAQ,KAAK,IAAI;AAAA,EACnE;AAAA,EAES,iBAAiB,OAAoC;AAC7D,WAAO,OAAO,KAAK,UAAU,aAAa,KAAK,MAAM,KAAK,IAAI;AAAA,EAC/D;AACD;AAmHO,SAAS,WACf,kBAoBD;AACC,SAAO,CACN,GACA,MAC6D;AAC7D,UAAM,EAAE,MAAM,OAAO,IAAI,uBAAoC,GAAG,CAAC;AACjE,WAAO,IAAI,yBAAyB,MAA+C,QAAQ,gBAAgB;AAAA,EAC5G;AACD;","names":[]}
|
package/mysql-core/dialect.cjs
CHANGED
|
@@ -881,9 +881,6 @@ class MySqlDialect {
|
|
|
881
881
|
case "MySqlBigInt64": {
|
|
882
882
|
return import_sql.sql`cast(${name} as char) as ${import_sql.sql.identifier(key)}`;
|
|
883
883
|
}
|
|
884
|
-
case "MySqlCustomColumn": {
|
|
885
|
-
return import_sql.sql`${column.jsonSelectIdentifier(name, import_sql.sql)} as ${import_sql.sql.identifier(key)}`;
|
|
886
|
-
}
|
|
887
884
|
default: {
|
|
888
885
|
return import_sql.sql`${name} as ${import_sql.sql.identifier(key)}`;
|
|
889
886
|
}
|