sequelize-typescript-generator 12.0.1 → 13.0.1
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 +832 -204
- package/build/bin/cli.js +65 -47
- package/build/bin/utils.d.ts +5 -2
- package/build/bin/utils.js +87 -104
- package/build/builders/Builder.d.ts +2 -2
- package/build/builders/Builder.js +3 -5
- package/build/builders/ModelBuilder.d.ts +43 -8
- package/build/builders/ModelBuilder.js +120 -122
- package/build/builders/NativeModelRenderer.d.ts +98 -0
- package/build/builders/NativeModelRenderer.js +353 -0
- package/build/builders/associationNaming.d.ts +9 -0
- package/build/builders/associationNaming.js +15 -0
- package/build/builders/decoratorsDependency.d.ts +27 -0
- package/build/builders/decoratorsDependency.js +45 -0
- package/build/builders/defaultValues.d.ts +40 -0
- package/build/builders/defaultValues.js +56 -0
- package/build/builders/generatedFile.d.ts +23 -0
- package/build/builders/generatedFile.js +31 -0
- package/build/builders/index.d.ts +1 -1
- package/build/builders/index.js +1 -5
- package/build/builders/nativeAssociations.d.ts +109 -0
- package/build/builders/nativeAssociations.js +308 -0
- package/build/builders/nativeAttributes.d.ts +96 -0
- package/build/builders/nativeAttributes.js +174 -0
- package/build/builders/nativeWiring.d.ts +51 -0
- package/build/builders/nativeWiring.js +108 -0
- package/build/builders/utils.d.ts +75 -0
- package/build/builders/utils.js +142 -48
- package/build/config/IConfig.d.ts +11 -2
- package/build/config/IConfig.js +4 -6
- package/build/config/format.d.ts +27 -0
- package/build/config/format.js +26 -0
- package/build/config/index.d.ts +1 -1
- package/build/config/index.js +1 -2
- package/build/connection/createConnection.d.ts +8 -1
- package/build/connection/createConnection.js +30 -7
- package/build/connection/index.d.ts +1 -1
- package/build/connection/index.js +1 -17
- package/build/dialects/AssociationsParser.d.ts +10 -2
- package/build/dialects/AssociationsParser.js +16 -19
- package/build/dialects/Dialect.d.ts +52 -9
- package/build/dialects/Dialect.js +93 -57
- package/build/dialects/DialectMSSQL.d.ts +22 -3
- package/build/dialects/DialectMSSQL.js +169 -75
- package/build/dialects/DialectMariaDB.d.ts +13 -3
- package/build/dialects/DialectMariaDB.js +98 -79
- package/build/dialects/DialectMySQL.d.ts +13 -3
- package/build/dialects/DialectMySQL.js +102 -84
- package/build/dialects/DialectPostgres.d.ts +14 -3
- package/build/dialects/DialectPostgres.js +129 -75
- package/build/dialects/DialectSQLite.d.ts +41 -3
- package/build/dialects/DialectSQLite.js +166 -24
- package/build/dialects/associationDiscovery.d.ts +64 -0
- package/build/dialects/associationDiscovery.js +275 -0
- package/build/dialects/associationsFileMerge.d.ts +21 -0
- package/build/dialects/associationsFileMerge.js +107 -0
- package/build/dialects/createDialect.d.ts +8 -0
- package/build/dialects/createDialect.js +24 -0
- package/build/dialects/dataTypes.d.ts +58 -0
- package/build/dialects/dataTypes.js +108 -0
- package/build/dialects/foreignKeys.d.ts +115 -0
- package/build/dialects/foreignKeys.js +198 -0
- package/build/dialects/index.d.ts +5 -5
- package/build/dialects/index.js +5 -13
- package/build/dialects/paranoid.d.ts +24 -0
- package/build/dialects/paranoid.js +29 -0
- package/build/dialects/utils.d.ts +2 -8
- package/build/dialects/utils.js +61 -58
- package/build/index.d.ts +11 -3
- package/build/index.js +5 -19
- package/build/lint/Linter.d.ts +14 -3
- package/build/lint/Linter.js +114 -14
- package/build/lint/eslintDefaultConfig.d.ts +2 -2
- package/build/lint/eslintDefaultConfig.js +28 -23
- package/build/lint/index.d.ts +1 -1
- package/build/lint/index.js +1 -5
- package/build/utils/errors.d.ts +6 -0
- package/build/utils/errors.js +6 -0
- package/package.json +48 -28
- package/.github/FUNDING.yml +0 -1
- package/.github/workflows/ci.yml +0 -199
- package/CHANGELOG.md +0 -30
- package/tsconfig.json +0 -21
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const utils_1 = require("./utils");
|
|
1
|
+
import { QueryTypes } from 'sequelize';
|
|
2
|
+
import { DataTypes } from 'sequelize';
|
|
3
|
+
import { Dialect } from './Dialect.js';
|
|
4
|
+
import { warnUnknownMappingForDataType } from './utils.js';
|
|
5
|
+
import { buildSequelizeDataType, renderDataTypeExpression, DATA_TYPE_NAMESPACES, } from './dataTypes.js';
|
|
6
|
+
import { groupForeignKeyRows, mapForeignKeyQueryRow, } from './foreignKeys.js';
|
|
8
7
|
const jsDataTypesMap = {
|
|
9
8
|
int: 'number',
|
|
10
9
|
bigint: 'string',
|
|
@@ -37,41 +36,41 @@ const jsDataTypesMap = {
|
|
|
37
36
|
geography: 'object',
|
|
38
37
|
};
|
|
39
38
|
const sequelizeDataTypesMap = {
|
|
40
|
-
int:
|
|
41
|
-
bigint:
|
|
42
|
-
tinyint:
|
|
43
|
-
smallint:
|
|
44
|
-
numeric:
|
|
45
|
-
decimal:
|
|
46
|
-
float:
|
|
47
|
-
real:
|
|
48
|
-
money:
|
|
49
|
-
smallmoney:
|
|
50
|
-
char:
|
|
51
|
-
nchar:
|
|
52
|
-
varchar:
|
|
53
|
-
nvarchar:
|
|
54
|
-
text:
|
|
55
|
-
ntext:
|
|
56
|
-
date:
|
|
57
|
-
datetime:
|
|
58
|
-
datetime2:
|
|
59
|
-
timestamp:
|
|
60
|
-
datetimeoffset:
|
|
61
|
-
time:
|
|
62
|
-
smalldatetime:
|
|
63
|
-
bit:
|
|
64
|
-
binary:
|
|
65
|
-
varbinary:
|
|
66
|
-
uniqueidentifier:
|
|
67
|
-
xml:
|
|
68
|
-
geography:
|
|
39
|
+
int: DataTypes.INTEGER,
|
|
40
|
+
bigint: DataTypes.BIGINT,
|
|
41
|
+
tinyint: DataTypes.INTEGER,
|
|
42
|
+
smallint: DataTypes.INTEGER,
|
|
43
|
+
numeric: DataTypes.DECIMAL,
|
|
44
|
+
decimal: DataTypes.DECIMAL,
|
|
45
|
+
float: DataTypes.FLOAT,
|
|
46
|
+
real: DataTypes.REAL,
|
|
47
|
+
money: DataTypes.STRING,
|
|
48
|
+
smallmoney: DataTypes.STRING,
|
|
49
|
+
char: DataTypes.STRING,
|
|
50
|
+
nchar: DataTypes.STRING,
|
|
51
|
+
varchar: DataTypes.STRING,
|
|
52
|
+
nvarchar: DataTypes.STRING,
|
|
53
|
+
text: DataTypes.STRING,
|
|
54
|
+
ntext: DataTypes.STRING,
|
|
55
|
+
date: DataTypes.DATEONLY,
|
|
56
|
+
datetime: DataTypes.DATE,
|
|
57
|
+
datetime2: DataTypes.DATE,
|
|
58
|
+
timestamp: DataTypes.DATE,
|
|
59
|
+
datetimeoffset: DataTypes.STRING,
|
|
60
|
+
time: DataTypes.TIME,
|
|
61
|
+
smalldatetime: DataTypes.DATE,
|
|
62
|
+
bit: DataTypes.STRING,
|
|
63
|
+
binary: DataTypes.STRING,
|
|
64
|
+
varbinary: DataTypes.STRING,
|
|
65
|
+
uniqueidentifier: DataTypes.STRING,
|
|
66
|
+
xml: DataTypes.STRING,
|
|
67
|
+
geography: DataTypes.GEOGRAPHY,
|
|
69
68
|
};
|
|
70
69
|
/**
|
|
71
70
|
* Dialect for Postgres
|
|
72
71
|
* @class DialectPostgres
|
|
73
72
|
*/
|
|
74
|
-
class DialectMSSQL extends
|
|
73
|
+
export class DialectMSSQL extends Dialect {
|
|
75
74
|
constructor() {
|
|
76
75
|
super('mssql');
|
|
77
76
|
}
|
|
@@ -106,24 +105,27 @@ class DialectMSSQL extends Dialect_1.Dialect {
|
|
|
106
105
|
* @returns {Promise<ITable[]>}
|
|
107
106
|
*/
|
|
108
107
|
async fetchTables(connection, config) {
|
|
108
|
+
const schema = config.connection.schema;
|
|
109
109
|
const query = `
|
|
110
110
|
SELECT
|
|
111
111
|
t.name AS [table_name],
|
|
112
|
+
s.name AS [table_schema],
|
|
112
113
|
td.value AS [table_comment]
|
|
113
|
-
FROM
|
|
114
|
-
INNER JOIN
|
|
115
|
-
ON
|
|
114
|
+
FROM sys.tables t
|
|
115
|
+
INNER JOIN sys.schemas s
|
|
116
|
+
ON s.schema_id = t.schema_id
|
|
116
117
|
LEFT OUTER JOIN sys.extended_properties td
|
|
117
|
-
ON td.major_id = t.
|
|
118
|
-
WHERE t.
|
|
118
|
+
ON td.major_id = t.object_id AND td.minor_id = 0 AND td.name = 'MS_Description'
|
|
119
|
+
WHERE t.is_ms_shipped = 0${schema ? ` AND s.name = N'${schema}'` : ''};
|
|
119
120
|
`;
|
|
120
121
|
const tables = (await connection.query(query, {
|
|
121
|
-
type:
|
|
122
|
+
type: QueryTypes.SELECT,
|
|
122
123
|
raw: true,
|
|
123
|
-
})).map(({ table_name, table_comment }) => {
|
|
124
|
+
})).map(({ table_name, table_schema, table_comment }) => {
|
|
124
125
|
const t = {
|
|
125
126
|
name: table_name,
|
|
126
|
-
|
|
127
|
+
schema: table_schema,
|
|
128
|
+
comment: table_comment ?? undefined,
|
|
127
129
|
};
|
|
128
130
|
return t;
|
|
129
131
|
});
|
|
@@ -137,10 +139,10 @@ class DialectMSSQL extends Dialect_1.Dialect {
|
|
|
137
139
|
* @returns {Promise<IColumnMetadata[]>}
|
|
138
140
|
*/
|
|
139
141
|
async fetchColumnsMetadata(connection, config, table) {
|
|
140
|
-
var _a, _b, _c;
|
|
141
142
|
const columnsMetadata = [];
|
|
143
|
+
const schema = config.connection.schema;
|
|
142
144
|
const query = `
|
|
143
|
-
SELECT
|
|
145
|
+
SELECT
|
|
144
146
|
c.*,
|
|
145
147
|
CASE WHEN COLUMNPROPERTY(object_id(c.TABLE_SCHEMA +'.' + c.TABLE_NAME), c.COLUMN_NAME, 'IsIdentity') = 1 THEN 'YES' ELSE 'NO' END AS IS_IDENTITY,
|
|
146
148
|
tc.CONSTRAINT_NAME,
|
|
@@ -159,60 +161,65 @@ class DialectMSSQL extends Dialect_1.Dialect {
|
|
|
159
161
|
ON sc.id = t.id AND sc.name = c.COLUMN_NAME
|
|
160
162
|
LEFT OUTER JOIN sys.extended_properties ep
|
|
161
163
|
ON ep.major_id = sc.id AND ep.minor_id = sc.colid AND ep.name = 'MS_Description'
|
|
162
|
-
WHERE c.TABLE_CATALOG = N'${config.connection.database}' AND c.TABLE_NAME = N'${table}'
|
|
164
|
+
WHERE c.TABLE_CATALOG = N'${config.connection.database}' AND c.TABLE_NAME = N'${table}'${schema ? ` AND c.TABLE_SCHEMA = N'${schema}'` : ''}
|
|
163
165
|
ORDER BY c.ORDINAL_POSITION;
|
|
164
166
|
`;
|
|
165
167
|
const columns = await connection.query(query, {
|
|
166
|
-
type:
|
|
168
|
+
type: QueryTypes.SELECT,
|
|
167
169
|
raw: true,
|
|
168
170
|
});
|
|
169
171
|
for (const column of columns) {
|
|
170
172
|
// Unknown data type
|
|
171
173
|
if (!this.mapDbTypeToSequelize(column.DATA_TYPE)) {
|
|
172
|
-
|
|
174
|
+
warnUnknownMappingForDataType(column.DATA_TYPE);
|
|
173
175
|
}
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
type: column.DATA_TYPE,
|
|
178
|
-
typeExt: column.DATA_TYPE,
|
|
179
|
-
...this.mapDbTypeToSequelize(column.DATA_TYPE) && {
|
|
180
|
-
dataType: 'DataType.' +
|
|
181
|
-
this.mapDbTypeToSequelize(column.DATA_TYPE).key
|
|
182
|
-
.split(' ')[0], // avoids 'DOUBLE PRECISION' key to include PRECISION in the mapping
|
|
183
|
-
},
|
|
184
|
-
allowNull: column.IS_NULLABLE.toUpperCase() === 'YES' &&
|
|
185
|
-
((_a = column.CONSTRAINT_TYPE) === null || _a === void 0 ? void 0 : _a.toUpperCase()) !== 'PRIMARY KEY',
|
|
186
|
-
primaryKey: ((_b = column.CONSTRAINT_TYPE) === null || _b === void 0 ? void 0 : _b.toUpperCase()) === 'PRIMARY KEY',
|
|
187
|
-
autoIncrement: column.IS_IDENTITY === 'YES',
|
|
188
|
-
indices: [],
|
|
189
|
-
comment: (_c = column.COLUMN_COMMENT) !== null && _c !== void 0 ? _c : undefined,
|
|
190
|
-
};
|
|
191
|
-
// Additional data type information
|
|
176
|
+
const sequelizeConstructor = this.mapDbTypeToSequelize(column.DATA_TYPE);
|
|
177
|
+
// Data type arguments (precision or length)
|
|
178
|
+
let dataTypeArgs = [];
|
|
192
179
|
switch (column.DATA_TYPE) {
|
|
193
180
|
case 'decimal':
|
|
194
181
|
case 'numeric':
|
|
195
182
|
case 'float':
|
|
196
183
|
case 'double':
|
|
197
|
-
|
|
198
|
-
(0, utils_1.generatePrecisionSignature)(column.NUMERIC_PRECISION, column.NUMERIC_SCALE);
|
|
184
|
+
dataTypeArgs = [column.NUMERIC_PRECISION, column.NUMERIC_SCALE];
|
|
199
185
|
break;
|
|
200
186
|
case 'datetime2':
|
|
201
|
-
|
|
187
|
+
dataTypeArgs = [column.DATETIME_PRECISION];
|
|
202
188
|
break;
|
|
203
189
|
case 'char':
|
|
204
190
|
case 'nchar':
|
|
205
191
|
case 'varchar':
|
|
206
192
|
case 'nvarchar':
|
|
207
|
-
|
|
193
|
+
dataTypeArgs = [column.CHARACTER_MAXIMUM_LENGTH];
|
|
208
194
|
break;
|
|
209
195
|
}
|
|
196
|
+
const sequelizeType = sequelizeConstructor
|
|
197
|
+
? buildSequelizeDataType(sequelizeConstructor, dataTypeArgs)
|
|
198
|
+
: undefined;
|
|
199
|
+
const columnMetadata = {
|
|
200
|
+
name: column.COLUMN_NAME,
|
|
201
|
+
originName: column.COLUMN_NAME,
|
|
202
|
+
type: column.DATA_TYPE,
|
|
203
|
+
typeExt: column.DATA_TYPE,
|
|
204
|
+
...sequelizeType && {
|
|
205
|
+
sequelizeType,
|
|
206
|
+
dataType: renderDataTypeExpression(sequelizeType, DATA_TYPE_NAMESPACES.decorators),
|
|
207
|
+
},
|
|
208
|
+
allowNull: column.IS_NULLABLE.toUpperCase() === 'YES' &&
|
|
209
|
+
column.CONSTRAINT_TYPE?.toUpperCase() !== 'PRIMARY KEY',
|
|
210
|
+
primaryKey: column.CONSTRAINT_TYPE?.toUpperCase() === 'PRIMARY KEY',
|
|
211
|
+
autoIncrement: column.IS_IDENTITY === 'YES',
|
|
212
|
+
indices: [],
|
|
213
|
+
comment: column.COLUMN_COMMENT ?? undefined,
|
|
214
|
+
};
|
|
210
215
|
columnsMetadata.push(columnMetadata);
|
|
211
216
|
}
|
|
212
217
|
return columnsMetadata;
|
|
213
218
|
}
|
|
214
219
|
async fetchColumnIndexMetadata(connection, config, table, column) {
|
|
215
220
|
const indicesMetadata = [];
|
|
221
|
+
const schema = config.connection.schema;
|
|
222
|
+
const qualifiedTable = schema ? `${schema}.${table}` : table;
|
|
216
223
|
const query = `
|
|
217
224
|
SELECT
|
|
218
225
|
c.column_id,
|
|
@@ -235,11 +242,11 @@ class DialectMSSQL extends Dialect_1.Dialect {
|
|
|
235
242
|
ON ic.object_id = c.object_id AND ic.column_id = c.column_id
|
|
236
243
|
JOIN sys.tables t
|
|
237
244
|
ON t.object_id = c.object_id
|
|
238
|
-
WHERE t.object_id = object_id(N'${
|
|
245
|
+
WHERE t.object_id = object_id(N'${qualifiedTable}') AND c.name=N'${column}'
|
|
239
246
|
ORDER BY ic.column_id;
|
|
240
247
|
`;
|
|
241
248
|
const indices = await connection.query(query, {
|
|
242
|
-
type:
|
|
249
|
+
type: QueryTypes.SELECT,
|
|
243
250
|
raw: true,
|
|
244
251
|
});
|
|
245
252
|
for (const index of indices) {
|
|
@@ -250,5 +257,92 @@ class DialectMSSQL extends Dialect_1.Dialect {
|
|
|
250
257
|
}
|
|
251
258
|
return indicesMetadata;
|
|
252
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* Fetch foreign key constraints for the provided table. Constraints are read from
|
|
262
|
+
* sys.foreign_keys and sys.foreign_key_columns, one row per column position, and a
|
|
263
|
+
* source column is flagged unique when it is covered by a single-column non-filtered
|
|
264
|
+
* unique index. Referential actions come from the *_referential_action_desc columns.
|
|
265
|
+
* @param {Sequelize} connection
|
|
266
|
+
* @param {IConfig} config
|
|
267
|
+
* @param {ITable} table
|
|
268
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
269
|
+
*/
|
|
270
|
+
async fetchForeignKeysMetadata(connection, config, table) {
|
|
271
|
+
const schema = table.schema ?? config.connection.schema;
|
|
272
|
+
const foreignKeyRows = await connection.query(`
|
|
273
|
+
SELECT
|
|
274
|
+
fk.name AS constraint_name,
|
|
275
|
+
st.name AS source_table,
|
|
276
|
+
sc.name AS source_column,
|
|
277
|
+
ts.name AS target_schema,
|
|
278
|
+
tt.name AS target_table,
|
|
279
|
+
tc.name AS target_column,
|
|
280
|
+
fkc.constraint_column_id AS ordinal_position,
|
|
281
|
+
fk.delete_referential_action_desc AS on_delete,
|
|
282
|
+
fk.update_referential_action_desc AS on_update,
|
|
283
|
+
CASE WHEN EXISTS (
|
|
284
|
+
SELECT 1
|
|
285
|
+
FROM sys.indexes i
|
|
286
|
+
JOIN sys.index_columns ic
|
|
287
|
+
ON ic.object_id = i.object_id AND ic.index_id = i.index_id
|
|
288
|
+
WHERE i.object_id = fk.parent_object_id
|
|
289
|
+
AND i.is_unique = 1
|
|
290
|
+
AND i.has_filter = 0
|
|
291
|
+
AND ic.column_id = fkc.parent_column_id
|
|
292
|
+
AND ic.key_ordinal > 0
|
|
293
|
+
AND (
|
|
294
|
+
SELECT COUNT(*)
|
|
295
|
+
FROM sys.index_columns ic2
|
|
296
|
+
WHERE ic2.object_id = i.object_id
|
|
297
|
+
AND ic2.index_id = i.index_id
|
|
298
|
+
AND ic2.key_ordinal > 0
|
|
299
|
+
) = 1
|
|
300
|
+
) THEN 1 ELSE 0 END AS is_source_column_unique
|
|
301
|
+
FROM sys.foreign_keys fk
|
|
302
|
+
JOIN sys.foreign_key_columns fkc
|
|
303
|
+
ON fkc.constraint_object_id = fk.object_id
|
|
304
|
+
JOIN sys.tables st
|
|
305
|
+
ON st.object_id = fk.parent_object_id
|
|
306
|
+
JOIN sys.schemas ss
|
|
307
|
+
ON ss.schema_id = st.schema_id
|
|
308
|
+
JOIN sys.columns sc
|
|
309
|
+
ON sc.object_id = fkc.parent_object_id AND sc.column_id = fkc.parent_column_id
|
|
310
|
+
JOIN sys.tables tt
|
|
311
|
+
ON tt.object_id = fk.referenced_object_id
|
|
312
|
+
JOIN sys.schemas ts
|
|
313
|
+
ON ts.schema_id = tt.schema_id
|
|
314
|
+
JOIN sys.columns tc
|
|
315
|
+
ON tc.object_id = fkc.referenced_object_id AND tc.column_id = fkc.referenced_column_id
|
|
316
|
+
WHERE ss.name = N'${schema}' AND st.name = N'${table.name}'
|
|
317
|
+
ORDER BY fk.name, fkc.constraint_column_id;
|
|
318
|
+
`, {
|
|
319
|
+
type: QueryTypes.SELECT,
|
|
320
|
+
raw: true,
|
|
321
|
+
});
|
|
322
|
+
const rows = foreignKeyRows.map(mapForeignKeyQueryRow);
|
|
323
|
+
return groupForeignKeyRows(rows);
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Report whether the table has at least one enabled trigger, read from sys.triggers.
|
|
327
|
+
* @param {Sequelize} connection
|
|
328
|
+
* @param {IConfig} config
|
|
329
|
+
* @param {ITable} table
|
|
330
|
+
* @returns {Promise<boolean>}
|
|
331
|
+
*/
|
|
332
|
+
async fetchTableHasTrigger(connection, config, table) {
|
|
333
|
+
const schema = table.schema ?? config.connection.schema;
|
|
334
|
+
const rows = await connection.query(`
|
|
335
|
+
SELECT COUNT(*) AS trigger_count
|
|
336
|
+
FROM sys.triggers tr
|
|
337
|
+
JOIN sys.tables t
|
|
338
|
+
ON t.object_id = tr.parent_id
|
|
339
|
+
JOIN sys.schemas s
|
|
340
|
+
ON s.schema_id = t.schema_id
|
|
341
|
+
WHERE s.name = N'${schema}' AND t.name = N'${table.name}' AND tr.is_disabled = 0;
|
|
342
|
+
`, {
|
|
343
|
+
type: QueryTypes.SELECT,
|
|
344
|
+
raw: true,
|
|
345
|
+
});
|
|
346
|
+
return (rows[0]?.trigger_count ?? 0) > 0;
|
|
347
|
+
}
|
|
253
348
|
}
|
|
254
|
-
exports.DialectMSSQL = DialectMSSQL;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AbstractDataTypeConstructor } from 'sequelize';
|
|
2
|
-
import { Sequelize } from 'sequelize
|
|
3
|
-
import { IConfig } from '../config';
|
|
4
|
-
import { IColumnMetadata, Dialect, IIndexMetadata, ITable } from './Dialect';
|
|
2
|
+
import { Sequelize } from 'sequelize';
|
|
3
|
+
import { IConfig } from '../config/index.js';
|
|
4
|
+
import { IColumnMetadata, Dialect, IIndexMetadata, IForeignKeyConstraintMetadata, ITable } from './Dialect.js';
|
|
5
5
|
/**
|
|
6
6
|
* Dialect for MariaDB
|
|
7
7
|
* @class DialectMariaDB
|
|
@@ -50,4 +50,14 @@ export declare class DialectMariaDB extends Dialect {
|
|
|
50
50
|
* @returns {Promise<IIndexMetadata[]>}
|
|
51
51
|
*/
|
|
52
52
|
protected fetchColumnIndexMetadata(connection: Sequelize, config: IConfig, table: string, column: string): Promise<IIndexMetadata[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Fetch foreign key constraints for the provided table. Constraints are read from
|
|
55
|
+
* information_schema.referential_constraints joined to key_column_usage, and a
|
|
56
|
+
* source column is flagged unique when it is covered by a single-column unique index.
|
|
57
|
+
* @param {Sequelize} connection
|
|
58
|
+
* @param {IConfig} config
|
|
59
|
+
* @param {ITable} table
|
|
60
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
61
|
+
*/
|
|
62
|
+
protected fetchForeignKeysMetadata(connection: Sequelize, config: IConfig, table: ITable): Promise<IForeignKeyConstraintMetadata[]>;
|
|
53
63
|
}
|
|
@@ -1,47 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const utils_1 = require("./utils");
|
|
1
|
+
import { QueryTypes } from 'sequelize';
|
|
2
|
+
import { DataTypes } from 'sequelize';
|
|
3
|
+
import { Dialect } from './Dialect.js';
|
|
4
|
+
import { warnUnknownMappingForDataType } from './utils.js';
|
|
5
|
+
import { buildSequelizeDataType, renderDataTypeExpression, parseEnumValues, DATA_TYPE_NAMESPACES, } from './dataTypes.js';
|
|
6
|
+
import { groupForeignKeyRows, buildInformationSchemaForeignKeysQuery, mapForeignKeyQueryRow, } from './foreignKeys.js';
|
|
8
7
|
const sequelizeDataTypesMap = {
|
|
9
|
-
bigint:
|
|
10
|
-
int:
|
|
11
|
-
smallint:
|
|
12
|
-
mediumint:
|
|
13
|
-
tinyint:
|
|
14
|
-
decimal:
|
|
15
|
-
float:
|
|
16
|
-
double:
|
|
17
|
-
bit:
|
|
18
|
-
varchar:
|
|
19
|
-
char:
|
|
20
|
-
text:
|
|
21
|
-
tinytext:
|
|
22
|
-
mediumtext:
|
|
23
|
-
longtext:
|
|
24
|
-
date:
|
|
25
|
-
datetime:
|
|
26
|
-
time:
|
|
27
|
-
timestamp:
|
|
28
|
-
year:
|
|
29
|
-
enum:
|
|
30
|
-
set:
|
|
31
|
-
binary:
|
|
32
|
-
blob:
|
|
33
|
-
tinyblob:
|
|
34
|
-
mediumblob:
|
|
35
|
-
longblob:
|
|
36
|
-
point:
|
|
37
|
-
multipoint:
|
|
38
|
-
linestring:
|
|
39
|
-
multilinestring:
|
|
40
|
-
polygon:
|
|
41
|
-
multipolygon:
|
|
42
|
-
geometry:
|
|
43
|
-
geometrycollection:
|
|
44
|
-
json:
|
|
8
|
+
bigint: DataTypes.BIGINT,
|
|
9
|
+
int: DataTypes.INTEGER,
|
|
10
|
+
smallint: DataTypes.SMALLINT,
|
|
11
|
+
mediumint: DataTypes.MEDIUMINT,
|
|
12
|
+
tinyint: DataTypes.TINYINT,
|
|
13
|
+
decimal: DataTypes.DECIMAL,
|
|
14
|
+
float: DataTypes.FLOAT,
|
|
15
|
+
double: DataTypes.DOUBLE,
|
|
16
|
+
bit: DataTypes.INTEGER,
|
|
17
|
+
varchar: DataTypes.STRING,
|
|
18
|
+
char: DataTypes.CHAR,
|
|
19
|
+
text: DataTypes.STRING,
|
|
20
|
+
tinytext: DataTypes.STRING,
|
|
21
|
+
mediumtext: DataTypes.STRING,
|
|
22
|
+
longtext: DataTypes.STRING,
|
|
23
|
+
date: DataTypes.DATEONLY,
|
|
24
|
+
datetime: DataTypes.DATE,
|
|
25
|
+
time: DataTypes.TIME,
|
|
26
|
+
timestamp: DataTypes.DATE,
|
|
27
|
+
year: DataTypes.INTEGER,
|
|
28
|
+
enum: DataTypes.ENUM,
|
|
29
|
+
set: DataTypes.STRING,
|
|
30
|
+
binary: DataTypes.BLOB,
|
|
31
|
+
blob: DataTypes.BLOB,
|
|
32
|
+
tinyblob: DataTypes.BLOB,
|
|
33
|
+
mediumblob: DataTypes.BLOB,
|
|
34
|
+
longblob: DataTypes.BLOB,
|
|
35
|
+
point: DataTypes.GEOMETRY,
|
|
36
|
+
multipoint: DataTypes.GEOMETRY,
|
|
37
|
+
linestring: DataTypes.GEOMETRY,
|
|
38
|
+
multilinestring: DataTypes.GEOMETRY,
|
|
39
|
+
polygon: DataTypes.GEOMETRY,
|
|
40
|
+
multipolygon: DataTypes.GEOMETRY,
|
|
41
|
+
geometry: DataTypes.GEOMETRY,
|
|
42
|
+
geometrycollection: DataTypes.GEOMETRY,
|
|
43
|
+
json: DataTypes.JSON,
|
|
45
44
|
};
|
|
46
45
|
const jsDataTypesMap = {
|
|
47
46
|
bigint: 'string',
|
|
@@ -89,7 +88,7 @@ const defaultValuesMap = {
|
|
|
89
88
|
* Dialect for MariaDB
|
|
90
89
|
* @class DialectMariaDB
|
|
91
90
|
*/
|
|
92
|
-
class DialectMariaDB extends
|
|
91
|
+
export class DialectMariaDB extends Dialect {
|
|
93
92
|
constructor() {
|
|
94
93
|
super('mariadb');
|
|
95
94
|
}
|
|
@@ -124,22 +123,23 @@ class DialectMariaDB extends Dialect_1.Dialect {
|
|
|
124
123
|
* @returns {Promise<string[]>}
|
|
125
124
|
*/
|
|
126
125
|
async fetchTables(connection, config) {
|
|
127
|
-
var _a;
|
|
128
126
|
const query = `
|
|
129
127
|
SELECT
|
|
130
|
-
table_name AS table_name,
|
|
131
|
-
table_comment AS table_comment
|
|
128
|
+
table_name AS table_name,
|
|
129
|
+
table_comment AS table_comment,
|
|
130
|
+
table_type AS table_type
|
|
132
131
|
FROM information_schema.tables
|
|
133
132
|
WHERE table_schema = '${config.connection.database}'
|
|
134
|
-
${
|
|
133
|
+
${config.metadata?.noViews ? 'AND table_type <> \'VIEW\'' : ''};
|
|
135
134
|
`;
|
|
136
135
|
const tables = (await connection.query(query, {
|
|
137
|
-
type:
|
|
136
|
+
type: QueryTypes.SELECT,
|
|
138
137
|
raw: true,
|
|
139
|
-
})).map(({ table_name, table_comment }) => {
|
|
138
|
+
})).map(({ table_name, table_comment, table_type }) => {
|
|
140
139
|
const t = {
|
|
141
140
|
name: table_name,
|
|
142
|
-
comment: table_comment
|
|
141
|
+
comment: table_comment ?? undefined,
|
|
142
|
+
isView: table_type === 'VIEW',
|
|
143
143
|
};
|
|
144
144
|
return t;
|
|
145
145
|
});
|
|
@@ -178,51 +178,54 @@ class DialectMariaDB extends Dialect_1.Dialect {
|
|
|
178
178
|
ORDER BY c.ORDINAL_POSITION;
|
|
179
179
|
`;
|
|
180
180
|
const columns = await connection.query(query, {
|
|
181
|
-
type:
|
|
181
|
+
type: QueryTypes.SELECT,
|
|
182
182
|
raw: true,
|
|
183
183
|
});
|
|
184
184
|
for (const column of columns) {
|
|
185
185
|
// Unknown data type
|
|
186
186
|
if (!this.mapDbTypeToSequelize(column.DATA_TYPE)) {
|
|
187
|
-
|
|
187
|
+
warnUnknownMappingForDataType(column.DATA_TYPE);
|
|
188
188
|
}
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
type: column.DATA_TYPE,
|
|
193
|
-
typeExt: column.COLUMN_TYPE,
|
|
194
|
-
...this.mapDbTypeToSequelize(column.DATA_TYPE) && { dataType: 'DataType.' +
|
|
195
|
-
this.mapDbTypeToSequelize(column.DATA_TYPE).key
|
|
196
|
-
.split(' ')[0], // avoids 'DOUBLE PRECISION' key to include PRECISION in the mapping
|
|
197
|
-
},
|
|
198
|
-
allowNull: column.IS_NULLABLE === 'YES',
|
|
199
|
-
primaryKey: column.COLUMN_KEY === 'PRI',
|
|
200
|
-
autoIncrement: column.EXTRA === 'auto_increment',
|
|
201
|
-
indices: [],
|
|
202
|
-
comment: column.COLUMN_COMMENT,
|
|
203
|
-
};
|
|
204
|
-
// Additional data type informations
|
|
189
|
+
const sequelizeConstructor = this.mapDbTypeToSequelize(column.DATA_TYPE);
|
|
190
|
+
// Data type arguments (precision, length or ENUM values)
|
|
191
|
+
let dataTypeArgs = [];
|
|
205
192
|
switch (column.DATA_TYPE) {
|
|
206
193
|
case 'decimal':
|
|
207
194
|
case 'numeric':
|
|
208
195
|
case 'float':
|
|
209
196
|
case 'double':
|
|
210
|
-
|
|
211
|
-
(0, utils_1.generatePrecisionSignature)(column.NUMERIC_PRECISION, column.NUMERIC_SCALE);
|
|
197
|
+
dataTypeArgs = [column.NUMERIC_PRECISION, column.NUMERIC_SCALE];
|
|
212
198
|
break;
|
|
213
199
|
case 'datetime':
|
|
214
200
|
case 'timestamp':
|
|
215
|
-
|
|
201
|
+
dataTypeArgs = [column.DATETIME_PRECISION];
|
|
216
202
|
break;
|
|
217
203
|
case 'char':
|
|
218
204
|
case 'varchar':
|
|
219
|
-
|
|
205
|
+
dataTypeArgs = [column.CHARACTER_MAXIMUM_LENGTH];
|
|
206
|
+
break;
|
|
207
|
+
case 'enum':
|
|
208
|
+
dataTypeArgs = parseEnumValues(column.COLUMN_TYPE);
|
|
220
209
|
break;
|
|
221
210
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
211
|
+
const sequelizeType = sequelizeConstructor
|
|
212
|
+
? buildSequelizeDataType(sequelizeConstructor, dataTypeArgs)
|
|
213
|
+
: undefined;
|
|
214
|
+
const columnMetadata = {
|
|
215
|
+
name: column.COLUMN_NAME,
|
|
216
|
+
originName: column.COLUMN_NAME,
|
|
217
|
+
type: column.DATA_TYPE,
|
|
218
|
+
typeExt: column.COLUMN_TYPE,
|
|
219
|
+
...sequelizeType && {
|
|
220
|
+
sequelizeType,
|
|
221
|
+
dataType: renderDataTypeExpression(sequelizeType, DATA_TYPE_NAMESPACES.decorators),
|
|
222
|
+
},
|
|
223
|
+
allowNull: column.IS_NULLABLE === 'YES',
|
|
224
|
+
primaryKey: column.COLUMN_KEY === 'PRI',
|
|
225
|
+
autoIncrement: column.EXTRA === 'auto_increment',
|
|
226
|
+
indices: [],
|
|
227
|
+
comment: column.COLUMN_COMMENT,
|
|
228
|
+
};
|
|
226
229
|
columnsMetadata.push(columnMetadata);
|
|
227
230
|
}
|
|
228
231
|
return columnsMetadata;
|
|
@@ -244,7 +247,7 @@ class DialectMariaDB extends Dialect_1.Dialect {
|
|
|
244
247
|
AND COLUMN_NAME = '${column}';
|
|
245
248
|
`;
|
|
246
249
|
const indices = await connection.query(query, {
|
|
247
|
-
type:
|
|
250
|
+
type: QueryTypes.SELECT,
|
|
248
251
|
raw: true,
|
|
249
252
|
});
|
|
250
253
|
for (const index of indices) {
|
|
@@ -258,5 +261,21 @@ class DialectMariaDB extends Dialect_1.Dialect {
|
|
|
258
261
|
}
|
|
259
262
|
return indicesMetadata;
|
|
260
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Fetch foreign key constraints for the provided table. Constraints are read from
|
|
266
|
+
* information_schema.referential_constraints joined to key_column_usage, and a
|
|
267
|
+
* source column is flagged unique when it is covered by a single-column unique index.
|
|
268
|
+
* @param {Sequelize} connection
|
|
269
|
+
* @param {IConfig} config
|
|
270
|
+
* @param {ITable} table
|
|
271
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
272
|
+
*/
|
|
273
|
+
async fetchForeignKeysMetadata(connection, config, table) {
|
|
274
|
+
const foreignKeyRows = await connection.query(buildInformationSchemaForeignKeysQuery(config.connection.database, table.name), {
|
|
275
|
+
type: QueryTypes.SELECT,
|
|
276
|
+
raw: true,
|
|
277
|
+
});
|
|
278
|
+
const rows = foreignKeyRows.map(mapForeignKeyQueryRow);
|
|
279
|
+
return groupForeignKeyRows(rows);
|
|
280
|
+
}
|
|
261
281
|
}
|
|
262
|
-
exports.DialectMariaDB = DialectMariaDB;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AbstractDataTypeConstructor } from 'sequelize';
|
|
2
|
-
import { Sequelize } from 'sequelize
|
|
3
|
-
import { IConfig } from '../config';
|
|
4
|
-
import { IColumnMetadata, Dialect, IIndexMetadata, ITable } from './Dialect';
|
|
2
|
+
import { Sequelize } from 'sequelize';
|
|
3
|
+
import { IConfig } from '../config/index.js';
|
|
4
|
+
import { IColumnMetadata, Dialect, IIndexMetadata, IForeignKeyConstraintMetadata, ITable } from './Dialect.js';
|
|
5
5
|
/**
|
|
6
6
|
* Dialect for MySQL
|
|
7
7
|
* @class DialectMySQL
|
|
@@ -50,4 +50,14 @@ export declare class DialectMySQL extends Dialect {
|
|
|
50
50
|
* @returns {Promise<IIndexMetadata[]>}
|
|
51
51
|
*/
|
|
52
52
|
protected fetchColumnIndexMetadata(connection: Sequelize, config: IConfig, table: string, column: string): Promise<IIndexMetadata[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Fetch foreign key constraints for the provided table. Constraints are read from
|
|
55
|
+
* information_schema.referential_constraints joined to key_column_usage, and a
|
|
56
|
+
* source column is flagged unique when it is covered by a single-column unique index.
|
|
57
|
+
* @param {Sequelize} connection
|
|
58
|
+
* @param {IConfig} config
|
|
59
|
+
* @param {ITable} table
|
|
60
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
61
|
+
*/
|
|
62
|
+
protected fetchForeignKeysMetadata(connection: Sequelize, config: IConfig, table: ITable): Promise<IForeignKeyConstraintMetadata[]>;
|
|
53
63
|
}
|