sequelize-typescript-generator 13.0.1 → 13.2.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 +274 -20
- package/build/bin/cli.js +3 -3
- package/build/bin/utils.d.ts +13 -0
- package/build/bin/utils.js +25 -0
- package/build/builders/ModelBuilder.d.ts +2 -1
- package/build/builders/ModelBuilder.js +155 -15
- package/build/builders/NativeModelRenderer.d.ts +4 -3
- package/build/builders/NativeModelRenderer.js +41 -13
- package/build/builders/enumSupport.d.ts +104 -0
- package/build/builders/enumSupport.js +323 -0
- package/build/builders/generatedFile.d.ts +2 -1
- package/build/builders/generatedFile.js +12 -1
- package/build/builders/jsonSupport.d.ts +69 -0
- package/build/builders/jsonSupport.js +89 -0
- package/build/builders/typeOverrides.d.ts +30 -0
- package/build/builders/typeOverrides.js +93 -0
- package/build/builders/typeOverridesParser.d.ts +48 -0
- package/build/builders/typeOverridesParser.js +305 -0
- package/build/builders/unmappedTypes.d.ts +50 -0
- package/build/builders/unmappedTypes.js +62 -0
- package/build/builders/utils.d.ts +20 -2
- package/build/builders/utils.js +42 -5
- package/build/config/IConfig.d.ts +22 -0
- package/build/dialects/Dialect.d.ts +57 -3
- package/build/dialects/DialectMSSQL.d.ts +2 -2
- package/build/dialects/DialectMSSQL.js +1 -6
- package/build/dialects/DialectMariaDB.d.ts +13 -2
- package/build/dialects/DialectMariaDB.js +46 -6
- package/build/dialects/DialectMySQL.d.ts +2 -2
- package/build/dialects/DialectMySQL.js +2 -6
- package/build/dialects/DialectPostgres.d.ts +36 -3
- package/build/dialects/DialectPostgres.js +111 -13
- package/build/dialects/DialectSQLite.d.ts +2 -2
- package/build/dialects/DialectSQLite.js +1 -6
- package/build/dialects/dataTypes.d.ts +26 -4
- package/build/dialects/dataTypes.js +50 -5
- package/build/dialects/mariadbJson.d.ts +39 -0
- package/build/dialects/mariadbJson.js +52 -0
- package/build/dialects/utils.d.ts +0 -6
- package/build/dialects/utils.js +0 -10
- package/build/index.d.ts +2 -2
- package/package.json +3 -3
|
@@ -4,6 +4,7 @@ import { IConfig } from '../config/index.js';
|
|
|
4
4
|
import { IAssociationMetadata } from './AssociationsParser.js';
|
|
5
5
|
import type { ReferentialAction } from './foreignKeys.js';
|
|
6
6
|
import type { ISequelizeDataType } from './dataTypes.js';
|
|
7
|
+
import type { TypeNode } from 'typescript';
|
|
7
8
|
export interface ITablesMetadata {
|
|
8
9
|
[tableName: string]: ITableMetadata;
|
|
9
10
|
}
|
|
@@ -42,6 +43,48 @@ export interface IForeignKeyConstraintMetadata {
|
|
|
42
43
|
onUpdate: ReferentialAction;
|
|
43
44
|
isSourceColumnUnique: boolean;
|
|
44
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Type override applied to a column. Each side is set only when a matching type
|
|
48
|
+
* override entry sets it; an unset side keeps the type the generator derives.
|
|
49
|
+
*/
|
|
50
|
+
export interface IColumnTypeOverride {
|
|
51
|
+
/**
|
|
52
|
+
* TypeScript type replacing the generated one, before nullability wrapping.
|
|
53
|
+
*/
|
|
54
|
+
tsType?: TypeNode;
|
|
55
|
+
/**
|
|
56
|
+
* Sequelize data type emitted in place of `sequelizeType`/`dataType`, which
|
|
57
|
+
* keep the generated data type the TypeScript type is derived from.
|
|
58
|
+
*/
|
|
59
|
+
dataType?: ISequelizeDataType;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Database enum type a column is typed with: a Postgres user-defined enum, for a
|
|
63
|
+
* scalar column or an array column.
|
|
64
|
+
*/
|
|
65
|
+
export interface IColumnEnumType {
|
|
66
|
+
/**
|
|
67
|
+
* Schema the enum type is defined in, which may differ from the table schema.
|
|
68
|
+
*/
|
|
69
|
+
schema: string;
|
|
70
|
+
/**
|
|
71
|
+
* Database name of the enum type, e.g. `plan_tier`.
|
|
72
|
+
*/
|
|
73
|
+
name: string;
|
|
74
|
+
/**
|
|
75
|
+
* Enum labels in database sort order.
|
|
76
|
+
*/
|
|
77
|
+
labels: string[];
|
|
78
|
+
/**
|
|
79
|
+
* Set when the column holds an array of the enum.
|
|
80
|
+
*/
|
|
81
|
+
isArray: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Identifier of the shared enum type the column is typed with, assigned by
|
|
84
|
+
* the model builder once the shared type names of the run are resolved.
|
|
85
|
+
*/
|
|
86
|
+
sharedTypeName?: string;
|
|
87
|
+
}
|
|
45
88
|
export interface IColumnMetadata {
|
|
46
89
|
name: string;
|
|
47
90
|
originName: string;
|
|
@@ -49,6 +92,7 @@ export interface IColumnMetadata {
|
|
|
49
92
|
typeExt: string;
|
|
50
93
|
dataType?: string;
|
|
51
94
|
sequelizeType?: ISequelizeDataType;
|
|
95
|
+
isJson?: boolean;
|
|
52
96
|
primaryKey: boolean;
|
|
53
97
|
foreignKey?: IColumnForeignKeyMetadata;
|
|
54
98
|
allowNull: boolean;
|
|
@@ -56,6 +100,15 @@ export interface IColumnMetadata {
|
|
|
56
100
|
indices?: IIndexMetadata[];
|
|
57
101
|
comment?: string;
|
|
58
102
|
defaultValue?: any;
|
|
103
|
+
/**
|
|
104
|
+
* Set when the column is typed with a database enum type; drives the shared
|
|
105
|
+
* enum type the column is typed with.
|
|
106
|
+
*/
|
|
107
|
+
enumType?: IColumnEnumType;
|
|
108
|
+
/**
|
|
109
|
+
* Set when at least one type override entry matches the column.
|
|
110
|
+
*/
|
|
111
|
+
typeOverride?: IColumnTypeOverride;
|
|
59
112
|
}
|
|
60
113
|
export interface IIndexMetadata {
|
|
61
114
|
name: string;
|
|
@@ -95,11 +148,12 @@ export declare abstract class Dialect {
|
|
|
95
148
|
*/
|
|
96
149
|
abstract mapDbTypeToSequelize(dbType: string): AbstractDataTypeConstructor;
|
|
97
150
|
/**
|
|
98
|
-
* Map database data type to javascript data type
|
|
151
|
+
* Map database data type to javascript data type, or undefined when the
|
|
152
|
+
* dialect has no mapping for it (an unmapped type)
|
|
99
153
|
* @param {string} dbType
|
|
100
|
-
* @returns {string
|
|
154
|
+
* @returns {string | undefined}
|
|
101
155
|
*/
|
|
102
|
-
abstract mapDbTypeToJs(dbType: string): string;
|
|
156
|
+
abstract mapDbTypeToJs(dbType: string): string | undefined;
|
|
103
157
|
/**
|
|
104
158
|
* Map database default values to Sequelize type (e.g. uuid() => DataType.UUIDV4).
|
|
105
159
|
* @param {string} v
|
|
@@ -17,9 +17,9 @@ export declare class DialectMSSQL extends Dialect {
|
|
|
17
17
|
/**
|
|
18
18
|
* Map database data type to javascript data type
|
|
19
19
|
* @param {string} dbType
|
|
20
|
-
* @returns {string
|
|
20
|
+
* @returns {string | undefined}
|
|
21
21
|
*/
|
|
22
|
-
mapDbTypeToJs(dbType: string): string;
|
|
22
|
+
mapDbTypeToJs(dbType: string): string | undefined;
|
|
23
23
|
/**
|
|
24
24
|
* Map database default values to Sequelize type (e.g. uuid() => DataType.UUIDV4).
|
|
25
25
|
* @param {string} v
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { QueryTypes } from 'sequelize';
|
|
2
2
|
import { DataTypes } from 'sequelize';
|
|
3
3
|
import { Dialect } from './Dialect.js';
|
|
4
|
-
import { warnUnknownMappingForDataType } from './utils.js';
|
|
5
4
|
import { buildSequelizeDataType, renderDataTypeExpression, DATA_TYPE_NAMESPACES, } from './dataTypes.js';
|
|
6
5
|
import { groupForeignKeyRows, mapForeignKeyQueryRow, } from './foreignKeys.js';
|
|
7
6
|
const jsDataTypesMap = {
|
|
@@ -85,7 +84,7 @@ export class DialectMSSQL extends Dialect {
|
|
|
85
84
|
/**
|
|
86
85
|
* Map database data type to javascript data type
|
|
87
86
|
* @param {string} dbType
|
|
88
|
-
* @returns {string
|
|
87
|
+
* @returns {string | undefined}
|
|
89
88
|
*/
|
|
90
89
|
mapDbTypeToJs(dbType) {
|
|
91
90
|
return jsDataTypesMap[dbType];
|
|
@@ -169,10 +168,6 @@ export class DialectMSSQL extends Dialect {
|
|
|
169
168
|
raw: true,
|
|
170
169
|
});
|
|
171
170
|
for (const column of columns) {
|
|
172
|
-
// Unknown data type
|
|
173
|
-
if (!this.mapDbTypeToSequelize(column.DATA_TYPE)) {
|
|
174
|
-
warnUnknownMappingForDataType(column.DATA_TYPE);
|
|
175
|
-
}
|
|
176
171
|
const sequelizeConstructor = this.mapDbTypeToSequelize(column.DATA_TYPE);
|
|
177
172
|
// Data type arguments (precision or length)
|
|
178
173
|
let dataTypeArgs = [];
|
|
@@ -17,9 +17,9 @@ export declare class DialectMariaDB extends Dialect {
|
|
|
17
17
|
/**
|
|
18
18
|
* Map database data type to javascript data type
|
|
19
19
|
* @param {string} dbType
|
|
20
|
-
* @returns {string
|
|
20
|
+
* @returns {string | undefined}
|
|
21
21
|
*/
|
|
22
|
-
mapDbTypeToJs(dbType: string): string;
|
|
22
|
+
mapDbTypeToJs(dbType: string): string | undefined;
|
|
23
23
|
/**
|
|
24
24
|
* Map database default values to Sequelize type (e.g. uuid() => DataType.UUIDV4).
|
|
25
25
|
* @param {string} v
|
|
@@ -41,6 +41,17 @@ export declare class DialectMariaDB extends Dialect {
|
|
|
41
41
|
* @returns {Promise<IColumnMetadata[]>}
|
|
42
42
|
*/
|
|
43
43
|
protected fetchColumnsMetadata(connection: Sequelize, config: IConfig, table: string): Promise<IColumnMetadata[]>;
|
|
44
|
+
/**
|
|
45
|
+
* Fetch the database column names of a table backed by longtext with a
|
|
46
|
+
* `json_valid(...)` CHECK constraint. Detection reads information_schema.
|
|
47
|
+
* CHECK_CONSTRAINTS and falls back to parsing `SHOW CREATE TABLE` when that
|
|
48
|
+
* view is unavailable (MariaDB before 10.5).
|
|
49
|
+
* @param {Sequelize} connection
|
|
50
|
+
* @param {IConfig} config
|
|
51
|
+
* @param {string} table
|
|
52
|
+
* @returns {Promise<Set<string>>}
|
|
53
|
+
*/
|
|
54
|
+
private fetchJsonColumnNames;
|
|
44
55
|
/**
|
|
45
56
|
* Fetch index metadata for the provided table and column
|
|
46
57
|
* @param {Sequelize} connection
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { QueryTypes } from 'sequelize';
|
|
2
2
|
import { DataTypes } from 'sequelize';
|
|
3
3
|
import { Dialect } from './Dialect.js';
|
|
4
|
-
import { warnUnknownMappingForDataType } from './utils.js';
|
|
5
4
|
import { buildSequelizeDataType, renderDataTypeExpression, parseEnumValues, DATA_TYPE_NAMESPACES, } from './dataTypes.js';
|
|
6
5
|
import { groupForeignKeyRows, buildInformationSchemaForeignKeysQuery, mapForeignKeyQueryRow, } from './foreignKeys.js';
|
|
6
|
+
import { extractJsonColumnsFromCheckConstraints, extractJsonColumnsFromCreateTable, getCreateTableStatement, } from './mariadbJson.js';
|
|
7
7
|
const sequelizeDataTypesMap = {
|
|
8
8
|
bigint: DataTypes.BIGINT,
|
|
9
9
|
int: DataTypes.INTEGER,
|
|
@@ -103,7 +103,7 @@ export class DialectMariaDB extends Dialect {
|
|
|
103
103
|
/**
|
|
104
104
|
* Map database data type to javascript data type
|
|
105
105
|
* @param {string} dbType
|
|
106
|
-
* @returns {string
|
|
106
|
+
* @returns {string | undefined}
|
|
107
107
|
*/
|
|
108
108
|
mapDbTypeToJs(dbType) {
|
|
109
109
|
return jsDataTypesMap[dbType];
|
|
@@ -182,10 +182,6 @@ export class DialectMariaDB extends Dialect {
|
|
|
182
182
|
raw: true,
|
|
183
183
|
});
|
|
184
184
|
for (const column of columns) {
|
|
185
|
-
// Unknown data type
|
|
186
|
-
if (!this.mapDbTypeToSequelize(column.DATA_TYPE)) {
|
|
187
|
-
warnUnknownMappingForDataType(column.DATA_TYPE);
|
|
188
|
-
}
|
|
189
185
|
const sequelizeConstructor = this.mapDbTypeToSequelize(column.DATA_TYPE);
|
|
190
186
|
// Data type arguments (precision, length or ENUM values)
|
|
191
187
|
let dataTypeArgs = [];
|
|
@@ -228,8 +224,52 @@ export class DialectMariaDB extends Dialect {
|
|
|
228
224
|
};
|
|
229
225
|
columnsMetadata.push(columnMetadata);
|
|
230
226
|
}
|
|
227
|
+
// MariaDB reports a JSON column as longtext with a json_valid CHECK constraint.
|
|
228
|
+
// Reclassify those columns as JSON so they emit DataTypes.JSON and the Json type.
|
|
229
|
+
const jsonColumnNames = await this.fetchJsonColumnNames(connection, config, table);
|
|
230
|
+
for (const columnMetadata of columnsMetadata) {
|
|
231
|
+
if (columnMetadata.type !== 'longtext' || !jsonColumnNames.has(columnMetadata.originName)) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const jsonSequelizeType = buildSequelizeDataType(DataTypes.JSON, []);
|
|
235
|
+
if (jsonSequelizeType) {
|
|
236
|
+
columnMetadata.sequelizeType = jsonSequelizeType;
|
|
237
|
+
columnMetadata.dataType = renderDataTypeExpression(jsonSequelizeType, DATA_TYPE_NAMESPACES.decorators);
|
|
238
|
+
columnMetadata.isJson = true;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
231
241
|
return columnsMetadata;
|
|
232
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Fetch the database column names of a table backed by longtext with a
|
|
245
|
+
* `json_valid(...)` CHECK constraint. Detection reads information_schema.
|
|
246
|
+
* CHECK_CONSTRAINTS and falls back to parsing `SHOW CREATE TABLE` when that
|
|
247
|
+
* view is unavailable (MariaDB before 10.5).
|
|
248
|
+
* @param {Sequelize} connection
|
|
249
|
+
* @param {IConfig} config
|
|
250
|
+
* @param {string} table
|
|
251
|
+
* @returns {Promise<Set<string>>}
|
|
252
|
+
*/
|
|
253
|
+
async fetchJsonColumnNames(connection, config, table) {
|
|
254
|
+
try {
|
|
255
|
+
const rows = await connection.query(`
|
|
256
|
+
SELECT CHECK_CLAUSE
|
|
257
|
+
FROM information_schema.CHECK_CONSTRAINTS
|
|
258
|
+
WHERE CONSTRAINT_SCHEMA = '${config.connection.database}' AND TABLE_NAME = '${table}';
|
|
259
|
+
`, {
|
|
260
|
+
type: QueryTypes.SELECT,
|
|
261
|
+
raw: true,
|
|
262
|
+
});
|
|
263
|
+
return extractJsonColumnsFromCheckConstraints(rows);
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
const rows = await connection.query(`SHOW CREATE TABLE \`${table}\`;`, {
|
|
267
|
+
type: QueryTypes.SELECT,
|
|
268
|
+
raw: true,
|
|
269
|
+
});
|
|
270
|
+
return extractJsonColumnsFromCreateTable(getCreateTableStatement(rows[0]));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
233
273
|
/**
|
|
234
274
|
* Fetch index metadata for the provided table and column
|
|
235
275
|
* @param {Sequelize} connection
|
|
@@ -17,9 +17,9 @@ export declare class DialectMySQL extends Dialect {
|
|
|
17
17
|
/**
|
|
18
18
|
* Map database data type to javascript data type
|
|
19
19
|
* @param {string} dbType
|
|
20
|
-
* @returns {string}
|
|
20
|
+
* @returns {string | undefined}
|
|
21
21
|
*/
|
|
22
|
-
mapDbTypeToJs(dbType: string): string;
|
|
22
|
+
mapDbTypeToJs(dbType: string): string | undefined;
|
|
23
23
|
/**
|
|
24
24
|
* Map database default values to Sequelize type (e.g. uuid() => DataType.UUIDV4).
|
|
25
25
|
* @param {string} v
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { QueryTypes } from 'sequelize';
|
|
2
2
|
import { DataTypes } from 'sequelize';
|
|
3
3
|
import { Dialect } from './Dialect.js';
|
|
4
|
-
import { warnUnknownMappingForDataType } from './utils.js';
|
|
5
4
|
import { buildSequelizeDataType, renderDataTypeExpression, parseEnumValues, DATA_TYPE_NAMESPACES, } from './dataTypes.js';
|
|
6
5
|
import { groupForeignKeyRows, buildInformationSchemaForeignKeysQuery, mapForeignKeyQueryRow, } from './foreignKeys.js';
|
|
7
6
|
const sequelizeDataTypesMap = {
|
|
@@ -118,7 +117,7 @@ export class DialectMySQL extends Dialect {
|
|
|
118
117
|
/**
|
|
119
118
|
* Map database data type to javascript data type
|
|
120
119
|
* @param {string} dbType
|
|
121
|
-
* @returns {string}
|
|
120
|
+
* @returns {string | undefined}
|
|
122
121
|
*/
|
|
123
122
|
mapDbTypeToJs(dbType) {
|
|
124
123
|
return jsDataTypesMap[dbType];
|
|
@@ -198,10 +197,6 @@ export class DialectMySQL extends Dialect {
|
|
|
198
197
|
raw: true,
|
|
199
198
|
});
|
|
200
199
|
for (const column of columns) {
|
|
201
|
-
// Unknown data type
|
|
202
|
-
if (!this.mapDbTypeToSequelize(column.DATA_TYPE)) {
|
|
203
|
-
warnUnknownMappingForDataType(column.DATA_TYPE);
|
|
204
|
-
}
|
|
205
200
|
const sequelizeConstructor = this.mapDbTypeToSequelize(column.DATA_TYPE);
|
|
206
201
|
// Data type arguments (precision, length or ENUM values)
|
|
207
202
|
let dataTypeArgs = [];
|
|
@@ -236,6 +231,7 @@ export class DialectMySQL extends Dialect {
|
|
|
236
231
|
sequelizeType,
|
|
237
232
|
dataType: renderDataTypeExpression(sequelizeType, DATA_TYPE_NAMESPACES.decorators),
|
|
238
233
|
},
|
|
234
|
+
...column.DATA_TYPE === 'json' && { isJson: true },
|
|
239
235
|
allowNull: column.IS_NULLABLE === 'YES',
|
|
240
236
|
primaryKey: column.COLUMN_KEY === 'PRI',
|
|
241
237
|
autoIncrement: column.EXTRA === 'auto_increment',
|
|
@@ -7,7 +7,38 @@ import { IColumnMetadata, IIndexMetadata, IForeignKeyConstraintMetadata, Dialect
|
|
|
7
7
|
* @class DialectPostgres
|
|
8
8
|
*/
|
|
9
9
|
export declare class DialectPostgres extends Dialect {
|
|
10
|
+
/**
|
|
11
|
+
* Enum labels per schema, cached for the lifetime of a connection so a run
|
|
12
|
+
* queries the catalogs once per schema.
|
|
13
|
+
*/
|
|
14
|
+
private readonly enumLabelsByConnection;
|
|
10
15
|
constructor();
|
|
16
|
+
/**
|
|
17
|
+
* Query the labels of every enum type defined in a schema, in database sort order.
|
|
18
|
+
* @param {Sequelize} connection
|
|
19
|
+
* @param {string} schema
|
|
20
|
+
* @returns {Promise<EnumLabelsByTypeName>}
|
|
21
|
+
*/
|
|
22
|
+
private queryEnumLabels;
|
|
23
|
+
/**
|
|
24
|
+
* Fetch the enum labels of a schema, querying the catalogs only on the first
|
|
25
|
+
* request for that schema on the connection.
|
|
26
|
+
* @param {Sequelize} connection
|
|
27
|
+
* @param {string} schema
|
|
28
|
+
* @returns {Promise<EnumLabelsByTypeName>}
|
|
29
|
+
*/
|
|
30
|
+
private fetchEnumLabels;
|
|
31
|
+
/**
|
|
32
|
+
* Resolve the database enum type of a column whose element type has no
|
|
33
|
+
* mapping: the enum named after the element type in the column's UDT schema,
|
|
34
|
+
* or undefined when no such enum exists.
|
|
35
|
+
* @param {Sequelize} connection
|
|
36
|
+
* @param {string} udtSchema
|
|
37
|
+
* @param {string} elementTypeName
|
|
38
|
+
* @param {boolean} isArray
|
|
39
|
+
* @returns {Promise<IColumnEnumType | undefined>}
|
|
40
|
+
*/
|
|
41
|
+
private resolveEnumType;
|
|
11
42
|
/**
|
|
12
43
|
* Map database data type to sequelize data type
|
|
13
44
|
* @param {string} dbType
|
|
@@ -15,11 +46,13 @@ export declare class DialectPostgres extends Dialect {
|
|
|
15
46
|
*/
|
|
16
47
|
mapDbTypeToSequelize(dbType: string): AbstractDataTypeConstructor;
|
|
17
48
|
/**
|
|
18
|
-
* Map database data type to javascript data type
|
|
49
|
+
* Map database data type to javascript data type. An array type maps to the
|
|
50
|
+
* element JS type suffixed with `[]`; an array whose element has no scalar
|
|
51
|
+
* mapping degrades to `unknown[]`.
|
|
19
52
|
* @param {string} dbType
|
|
20
|
-
* @returns {string}
|
|
53
|
+
* @returns {string | undefined}
|
|
21
54
|
*/
|
|
22
|
-
mapDbTypeToJs(dbType: string): string;
|
|
55
|
+
mapDbTypeToJs(dbType: string): string | undefined;
|
|
23
56
|
/**
|
|
24
57
|
* Map database default values to Sequelize type (e.g. uuid() => DataType.UUIDV4).
|
|
25
58
|
* @param {string} v
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { QueryTypes } from 'sequelize';
|
|
2
2
|
import { DataTypes } from 'sequelize';
|
|
3
|
-
import { Dialect } from './Dialect.js';
|
|
4
|
-
import { warnUnknownMappingForDataType } from './utils.js';
|
|
3
|
+
import { Dialect, } from './Dialect.js';
|
|
5
4
|
import { buildSequelizeDataType, renderDataTypeExpression, DATA_TYPE_NAMESPACES, } from './dataTypes.js';
|
|
6
5
|
import { groupForeignKeyRows, mapForeignKeyQueryRow, } from './foreignKeys.js';
|
|
7
6
|
const sequelizeDataTypesMap = {
|
|
@@ -15,6 +14,7 @@ const sequelizeDataTypesMap = {
|
|
|
15
14
|
varchar: DataTypes.STRING,
|
|
16
15
|
bpchar: DataTypes.STRING,
|
|
17
16
|
text: DataTypes.STRING,
|
|
17
|
+
citext: DataTypes.CITEXT,
|
|
18
18
|
bytea: DataTypes.BLOB,
|
|
19
19
|
timestamp: DataTypes.DATE,
|
|
20
20
|
timestamptz: DataTypes.DATE,
|
|
@@ -43,6 +43,21 @@ const sequelizeDataTypesMap = {
|
|
|
43
43
|
jsonb: DataTypes.JSONB,
|
|
44
44
|
jsonpath: DataTypes.JSON,
|
|
45
45
|
};
|
|
46
|
+
// Postgres reports array columns with an underscore-prefixed udt_name (e.g. _int4,
|
|
47
|
+
// _text). The element type is the same name without the leading underscore.
|
|
48
|
+
const POSTGRES_ARRAY_PREFIX = '_';
|
|
49
|
+
/**
|
|
50
|
+
* Report whether a Postgres udt_name denotes an array type.
|
|
51
|
+
* @param {string} udtName
|
|
52
|
+
* @returns {boolean}
|
|
53
|
+
*/
|
|
54
|
+
const isPostgresArrayType = (udtName) => udtName.startsWith(POSTGRES_ARRAY_PREFIX);
|
|
55
|
+
/**
|
|
56
|
+
* Strip the leading underscore of an array udt_name to get its element type name.
|
|
57
|
+
* @param {string} udtName
|
|
58
|
+
* @returns {string}
|
|
59
|
+
*/
|
|
60
|
+
const stripPostgresArrayPrefix = (udtName) => udtName.slice(POSTGRES_ARRAY_PREFIX.length);
|
|
46
61
|
const jsDataTypesMap = {
|
|
47
62
|
int2: 'number',
|
|
48
63
|
int4: 'number',
|
|
@@ -54,6 +69,7 @@ const jsDataTypesMap = {
|
|
|
54
69
|
varchar: 'string',
|
|
55
70
|
bpchar: 'string',
|
|
56
71
|
text: 'string',
|
|
72
|
+
citext: 'string',
|
|
57
73
|
bytea: 'Uint8Array',
|
|
58
74
|
timestamp: 'Date',
|
|
59
75
|
timestamptz: 'Date',
|
|
@@ -87,9 +103,75 @@ const jsDataTypesMap = {
|
|
|
87
103
|
* @class DialectPostgres
|
|
88
104
|
*/
|
|
89
105
|
export class DialectPostgres extends Dialect {
|
|
106
|
+
/**
|
|
107
|
+
* Enum labels per schema, cached for the lifetime of a connection so a run
|
|
108
|
+
* queries the catalogs once per schema.
|
|
109
|
+
*/
|
|
110
|
+
enumLabelsByConnection = new WeakMap();
|
|
90
111
|
constructor() {
|
|
91
112
|
super('postgres');
|
|
92
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Query the labels of every enum type defined in a schema, in database sort order.
|
|
116
|
+
* @param {Sequelize} connection
|
|
117
|
+
* @param {string} schema
|
|
118
|
+
* @returns {Promise<EnumLabelsByTypeName>}
|
|
119
|
+
*/
|
|
120
|
+
async queryEnumLabels(connection, schema) {
|
|
121
|
+
const rows = await connection.query(`
|
|
122
|
+
SELECT
|
|
123
|
+
t.typname AS type_name,
|
|
124
|
+
e.enumlabel AS label
|
|
125
|
+
FROM pg_catalog.pg_type t
|
|
126
|
+
JOIN pg_catalog.pg_enum e ON e.enumtypid = t.oid
|
|
127
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
|
|
128
|
+
WHERE n.nspname = $1
|
|
129
|
+
ORDER BY t.typname, e.enumsortorder;
|
|
130
|
+
`, {
|
|
131
|
+
type: QueryTypes.SELECT,
|
|
132
|
+
raw: true,
|
|
133
|
+
bind: [schema],
|
|
134
|
+
});
|
|
135
|
+
const labelsByTypeName = new Map();
|
|
136
|
+
for (const { type_name, label } of rows) {
|
|
137
|
+
const labels = labelsByTypeName.get(type_name) ?? [];
|
|
138
|
+
labels.push(label);
|
|
139
|
+
labelsByTypeName.set(type_name, labels);
|
|
140
|
+
}
|
|
141
|
+
return labelsByTypeName;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Fetch the enum labels of a schema, querying the catalogs only on the first
|
|
145
|
+
* request for that schema on the connection.
|
|
146
|
+
* @param {Sequelize} connection
|
|
147
|
+
* @param {string} schema
|
|
148
|
+
* @returns {Promise<EnumLabelsByTypeName>}
|
|
149
|
+
*/
|
|
150
|
+
fetchEnumLabels(connection, schema) {
|
|
151
|
+
const labelsBySchema = this.enumLabelsByConnection.get(connection) ?? new Map();
|
|
152
|
+
this.enumLabelsByConnection.set(connection, labelsBySchema);
|
|
153
|
+
const cached = labelsBySchema.get(schema);
|
|
154
|
+
if (cached) {
|
|
155
|
+
return cached;
|
|
156
|
+
}
|
|
157
|
+
const pending = this.queryEnumLabels(connection, schema);
|
|
158
|
+
labelsBySchema.set(schema, pending);
|
|
159
|
+
return pending;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Resolve the database enum type of a column whose element type has no
|
|
163
|
+
* mapping: the enum named after the element type in the column's UDT schema,
|
|
164
|
+
* or undefined when no such enum exists.
|
|
165
|
+
* @param {Sequelize} connection
|
|
166
|
+
* @param {string} udtSchema
|
|
167
|
+
* @param {string} elementTypeName
|
|
168
|
+
* @param {boolean} isArray
|
|
169
|
+
* @returns {Promise<IColumnEnumType | undefined>}
|
|
170
|
+
*/
|
|
171
|
+
async resolveEnumType(connection, udtSchema, elementTypeName, isArray) {
|
|
172
|
+
const labels = (await this.fetchEnumLabels(connection, udtSchema)).get(elementTypeName);
|
|
173
|
+
return labels ? { schema: udtSchema, name: elementTypeName, labels, isArray } : undefined;
|
|
174
|
+
}
|
|
93
175
|
/**
|
|
94
176
|
* Map database data type to sequelize data type
|
|
95
177
|
* @param {string} dbType
|
|
@@ -99,11 +181,17 @@ export class DialectPostgres extends Dialect {
|
|
|
99
181
|
return sequelizeDataTypesMap[dbType];
|
|
100
182
|
}
|
|
101
183
|
/**
|
|
102
|
-
* Map database data type to javascript data type
|
|
184
|
+
* Map database data type to javascript data type. An array type maps to the
|
|
185
|
+
* element JS type suffixed with `[]`; an array whose element has no scalar
|
|
186
|
+
* mapping degrades to `unknown[]`.
|
|
103
187
|
* @param {string} dbType
|
|
104
|
-
* @returns {string}
|
|
188
|
+
* @returns {string | undefined}
|
|
105
189
|
*/
|
|
106
190
|
mapDbTypeToJs(dbType) {
|
|
191
|
+
if (isPostgresArrayType(dbType)) {
|
|
192
|
+
const elementJsType = jsDataTypesMap[stripPostgresArrayPrefix(dbType)];
|
|
193
|
+
return `${elementJsType ?? 'unknown'}[]`;
|
|
194
|
+
}
|
|
107
195
|
return jsDataTypesMap[dbType];
|
|
108
196
|
}
|
|
109
197
|
/**
|
|
@@ -198,14 +286,19 @@ export class DialectPostgres extends Dialect {
|
|
|
198
286
|
raw: true,
|
|
199
287
|
});
|
|
200
288
|
for (const column of columns) {
|
|
201
|
-
//
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
289
|
+
// For an array column the element type drives the mapping; the ARRAY
|
|
290
|
+
// wrapper is added afterwards.
|
|
291
|
+
const isArray = isPostgresArrayType(column.udt_name);
|
|
292
|
+
const elementTypeName = isArray ? stripPostgresArrayPrefix(column.udt_name) : column.udt_name;
|
|
293
|
+
const elementConstructor = this.mapDbTypeToSequelize(elementTypeName);
|
|
294
|
+
// An unmapped element type, absent from the type map, may be a
|
|
295
|
+
// user-defined enum, looked up in the schema the type is defined in.
|
|
296
|
+
const enumType = elementConstructor === undefined
|
|
297
|
+
? await this.resolveEnumType(connection, column.udt_schema, elementTypeName, isArray)
|
|
298
|
+
: undefined;
|
|
206
299
|
// Data type arguments (precision or length)
|
|
207
300
|
let dataTypeArgs = [];
|
|
208
|
-
switch (
|
|
301
|
+
switch (elementTypeName) {
|
|
209
302
|
case 'decimal':
|
|
210
303
|
case 'numeric':
|
|
211
304
|
case 'float':
|
|
@@ -221,9 +314,12 @@ export class DialectPostgres extends Dialect {
|
|
|
221
314
|
dataTypeArgs = [column.character_maximum_length];
|
|
222
315
|
break;
|
|
223
316
|
}
|
|
224
|
-
const
|
|
225
|
-
? buildSequelizeDataType(
|
|
226
|
-
:
|
|
317
|
+
const elementType = elementConstructor
|
|
318
|
+
? buildSequelizeDataType(elementConstructor, dataTypeArgs)
|
|
319
|
+
: enumType && { key: 'ENUM', args: [...enumType.labels] };
|
|
320
|
+
const sequelizeType = elementType && isArray
|
|
321
|
+
? buildSequelizeDataType(DataTypes.ARRAY, [elementType])
|
|
322
|
+
: elementType;
|
|
227
323
|
const columnMetadata = {
|
|
228
324
|
name: column.column_name,
|
|
229
325
|
originName: column.column_name,
|
|
@@ -233,6 +329,8 @@ export class DialectPostgres extends Dialect {
|
|
|
233
329
|
sequelizeType,
|
|
234
330
|
dataType: renderDataTypeExpression(sequelizeType, DATA_TYPE_NAMESPACES.decorators),
|
|
235
331
|
},
|
|
332
|
+
...!isArray && (elementTypeName === 'json' || elementTypeName === 'jsonb') && { isJson: true },
|
|
333
|
+
...enumType && { enumType },
|
|
236
334
|
allowNull: column.is_nullable === 'YES' && !column.is_primary,
|
|
237
335
|
primaryKey: column.is_primary,
|
|
238
336
|
autoIncrement: column.is_sequence,
|
|
@@ -29,9 +29,9 @@ export declare class DialectSQLite extends Dialect {
|
|
|
29
29
|
/**
|
|
30
30
|
* Map database data type to javascript data type
|
|
31
31
|
* @param {string} dbType
|
|
32
|
-
* @returns {string
|
|
32
|
+
* @returns {string | undefined}
|
|
33
33
|
*/
|
|
34
|
-
mapDbTypeToJs(dbType: string): string;
|
|
34
|
+
mapDbTypeToJs(dbType: string): string | undefined;
|
|
35
35
|
/**
|
|
36
36
|
* Map database default values to Sequelize type (e.g. uuid() => DataType.UUIDV4).
|
|
37
37
|
* @param {string} v
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { QueryTypes } from 'sequelize';
|
|
2
2
|
import { DataTypes } from 'sequelize';
|
|
3
3
|
import { Dialect } from './Dialect.js';
|
|
4
|
-
import { warnUnknownMappingForDataType } from './utils.js';
|
|
5
4
|
import { buildSequelizeDataType, renderDataTypeExpression, DATA_TYPE_NAMESPACES, } from './dataTypes.js';
|
|
6
5
|
import { groupForeignKeyRows } from './foreignKeys.js';
|
|
7
6
|
/**
|
|
@@ -54,7 +53,7 @@ export class DialectSQLite extends Dialect {
|
|
|
54
53
|
/**
|
|
55
54
|
* Map database data type to javascript data type
|
|
56
55
|
* @param {string} dbType
|
|
57
|
-
* @returns {string
|
|
56
|
+
* @returns {string | undefined}
|
|
58
57
|
*/
|
|
59
58
|
mapDbTypeToJs(dbType) {
|
|
60
59
|
// Affinity rules from https://www.sqlite.org/datatype3.html
|
|
@@ -113,10 +112,6 @@ export class DialectSQLite extends Dialect {
|
|
|
113
112
|
raw: true,
|
|
114
113
|
});
|
|
115
114
|
for (const column of columns) {
|
|
116
|
-
// Unknown data type
|
|
117
|
-
if (!this.mapDbTypeToSequelize(column.type)) {
|
|
118
|
-
warnUnknownMappingForDataType(column.type);
|
|
119
|
-
}
|
|
120
115
|
const sequelizeConstructor = this.mapDbTypeToSequelize(column.type);
|
|
121
116
|
const sequelizeType = sequelizeConstructor
|
|
122
117
|
? buildSequelizeDataType(sequelizeConstructor, [])
|
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
import { DataTypes } from 'sequelize';
|
|
2
2
|
import type { AbstractDataTypeConstructor } from 'sequelize';
|
|
3
3
|
export type SequelizeDataTypeKey = keyof typeof DataTypes;
|
|
4
|
-
export type DataTypeArgument = string | number;
|
|
5
4
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* A single argument of a Sequelize data type. A scalar argument is a number
|
|
6
|
+
* (precision, length) or a string (ENUM value); a nested argument is itself a
|
|
7
|
+
* data type, so an argument list can carry another data type such as the element
|
|
8
|
+
* type of `ARRAY(INTEGER)`.
|
|
9
|
+
*/
|
|
10
|
+
export type DataTypeArgument = string | number | ISequelizeDataType;
|
|
11
|
+
/**
|
|
12
|
+
* Format-neutral Sequelize data type: a DataTypes key plus its arguments. Scalar
|
|
13
|
+
* arguments render bare (numbers) or single-quoted (strings, for ENUM values); a
|
|
14
|
+
* nested data type argument renders as its own data type expression.
|
|
8
15
|
*/
|
|
9
16
|
export interface ISequelizeDataType {
|
|
10
17
|
key: SequelizeDataTypeKey;
|
|
11
18
|
args: DataTypeArgument[];
|
|
12
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Type guard for a nested data type argument.
|
|
22
|
+
* @param {DataTypeArgument} arg
|
|
23
|
+
* @returns {boolean}
|
|
24
|
+
*/
|
|
25
|
+
export declare const isNestedDataType: (arg: DataTypeArgument) => arg is ISequelizeDataType;
|
|
13
26
|
/**
|
|
14
27
|
* Namespaces under which a data type expression is rendered.
|
|
15
28
|
*/
|
|
@@ -24,6 +37,14 @@ export type DataTypeNamespace = typeof DATA_TYPE_NAMESPACES[keyof typeof DATA_TY
|
|
|
24
37
|
* @returns {boolean}
|
|
25
38
|
*/
|
|
26
39
|
export declare const isSequelizeDataTypeKey: (value: string) => value is SequelizeDataTypeKey;
|
|
40
|
+
/**
|
|
41
|
+
* Type guard for a DataTypes key naming a concrete data type usable as a column
|
|
42
|
+
* type: a data type class derived from ABSTRACT. Dialect namespaces (`postgres`,
|
|
43
|
+
* `mysql`, ...) and ABSTRACT itself are rejected.
|
|
44
|
+
* @param {string} value
|
|
45
|
+
* @returns {boolean}
|
|
46
|
+
*/
|
|
47
|
+
export declare const isColumnDataTypeKey: (value: string) => value is SequelizeDataTypeKey;
|
|
27
48
|
/**
|
|
28
49
|
* Resolve the DataTypes key for a constructor. The first token of the constructor
|
|
29
50
|
* key is used so that 'DOUBLE PRECISION' resolves to 'DOUBLE'. Returns undefined when
|
|
@@ -43,7 +64,8 @@ export declare const resolveSequelizeDataTypeKey: (ctor: AbstractDataTypeConstru
|
|
|
43
64
|
*/
|
|
44
65
|
export declare const buildSequelizeDataType: (ctor: AbstractDataTypeConstructor, args: Array<DataTypeArgument | null | undefined>) => ISequelizeDataType | undefined;
|
|
45
66
|
/**
|
|
46
|
-
* Render a data type expression, e.g. DataType.DECIMAL(7,3)
|
|
67
|
+
* Render a data type expression, e.g. DataType.DECIMAL(7,3), DataTypes.ENUM('AA','BB')
|
|
68
|
+
* or DataTypes.ARRAY(DataTypes.INTEGER).
|
|
47
69
|
* @param {ISequelizeDataType} dataType
|
|
48
70
|
* @param {DataTypeNamespace} namespace
|
|
49
71
|
* @returns {string}
|