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,50 +1,49 @@
|
|
|
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
|
-
bigint: '
|
|
46
|
+
bigint: 'string',
|
|
48
47
|
smallint: 'number',
|
|
49
48
|
mediumint: 'number',
|
|
50
49
|
tinyint: 'number',
|
|
@@ -104,7 +103,7 @@ const getDefaultValue = (columnDefault) => {
|
|
|
104
103
|
* Dialect for MySQL
|
|
105
104
|
* @class DialectMySQL
|
|
106
105
|
*/
|
|
107
|
-
class DialectMySQL extends
|
|
106
|
+
export class DialectMySQL extends Dialect {
|
|
108
107
|
constructor() {
|
|
109
108
|
super('mysql');
|
|
110
109
|
}
|
|
@@ -139,22 +138,23 @@ class DialectMySQL extends Dialect_1.Dialect {
|
|
|
139
138
|
* @returns {Promise<ITable[]>}
|
|
140
139
|
*/
|
|
141
140
|
async fetchTables(connection, config) {
|
|
142
|
-
var _a;
|
|
143
141
|
const query = `
|
|
144
|
-
SELECT
|
|
145
|
-
table_name AS table_name,
|
|
146
|
-
table_comment AS table_comment
|
|
142
|
+
SELECT
|
|
143
|
+
table_name AS table_name,
|
|
144
|
+
table_comment AS table_comment,
|
|
145
|
+
table_type AS table_type
|
|
147
146
|
FROM information_schema.tables
|
|
148
|
-
WHERE table_schema = '${config.connection.database}'
|
|
149
|
-
${
|
|
147
|
+
WHERE table_schema = '${config.connection.database}'
|
|
148
|
+
${config.metadata?.noViews ? 'AND table_type <> \'VIEW\'' : ''};
|
|
150
149
|
`;
|
|
151
150
|
const tables = (await connection.query(query, {
|
|
152
|
-
type:
|
|
151
|
+
type: QueryTypes.SELECT,
|
|
153
152
|
raw: true,
|
|
154
|
-
})).map(({ table_name, table_comment }) => {
|
|
153
|
+
})).map(({ table_name, table_comment, table_type }) => {
|
|
155
154
|
const t = {
|
|
156
155
|
name: table_name,
|
|
157
|
-
comment: table_comment
|
|
156
|
+
comment: table_comment ?? undefined,
|
|
157
|
+
isView: table_type === 'VIEW',
|
|
158
158
|
};
|
|
159
159
|
return t;
|
|
160
160
|
});
|
|
@@ -194,53 +194,55 @@ class DialectMySQL extends Dialect_1.Dialect {
|
|
|
194
194
|
ORDER BY c.ORDINAL_POSITION;
|
|
195
195
|
`;
|
|
196
196
|
const columns = await connection.query(query, {
|
|
197
|
-
type:
|
|
197
|
+
type: QueryTypes.SELECT,
|
|
198
198
|
raw: true,
|
|
199
199
|
});
|
|
200
200
|
for (const column of columns) {
|
|
201
201
|
// Unknown data type
|
|
202
202
|
if (!this.mapDbTypeToSequelize(column.DATA_TYPE)) {
|
|
203
|
-
|
|
203
|
+
warnUnknownMappingForDataType(column.DATA_TYPE);
|
|
204
204
|
}
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
type: column.DATA_TYPE,
|
|
209
|
-
typeExt: column.COLUMN_TYPE,
|
|
210
|
-
...this.mapDbTypeToSequelize(column.DATA_TYPE) && {
|
|
211
|
-
dataType: 'DataType.' +
|
|
212
|
-
this.mapDbTypeToSequelize(column.DATA_TYPE).key
|
|
213
|
-
.split(' ')[0], // avoids 'DOUBLE PRECISION' key to include PRECISION in the mapping
|
|
214
|
-
},
|
|
215
|
-
allowNull: column.IS_NULLABLE === 'YES',
|
|
216
|
-
primaryKey: column.COLUMN_KEY === 'PRI',
|
|
217
|
-
autoIncrement: column.EXTRA === 'auto_increment',
|
|
218
|
-
indices: [],
|
|
219
|
-
comment: column.COLUMN_COMMENT,
|
|
220
|
-
...column.COLUMN_DEFAULT && { defaultValue: getDefaultValue(column.COLUMN_DEFAULT) },
|
|
221
|
-
};
|
|
222
|
-
// Additional data type informations
|
|
205
|
+
const sequelizeConstructor = this.mapDbTypeToSequelize(column.DATA_TYPE);
|
|
206
|
+
// Data type arguments (precision, length or ENUM values)
|
|
207
|
+
let dataTypeArgs = [];
|
|
223
208
|
switch (column.DATA_TYPE) {
|
|
224
209
|
case 'decimal':
|
|
225
210
|
case 'numeric':
|
|
226
211
|
case 'float':
|
|
227
212
|
case 'double':
|
|
228
|
-
|
|
229
|
-
(0, utils_1.generatePrecisionSignature)(column.NUMERIC_PRECISION, column.NUMERIC_SCALE);
|
|
213
|
+
dataTypeArgs = [column.NUMERIC_PRECISION, column.NUMERIC_SCALE];
|
|
230
214
|
break;
|
|
231
215
|
case 'datetime':
|
|
232
216
|
case 'timestamp':
|
|
233
|
-
|
|
217
|
+
dataTypeArgs = [column.DATETIME_PRECISION];
|
|
234
218
|
break;
|
|
235
219
|
case 'char':
|
|
236
220
|
case 'varchar':
|
|
237
|
-
|
|
221
|
+
dataTypeArgs = [column.CHARACTER_MAXIMUM_LENGTH];
|
|
222
|
+
break;
|
|
223
|
+
case 'enum':
|
|
224
|
+
dataTypeArgs = parseEnumValues(column.COLUMN_TYPE);
|
|
238
225
|
break;
|
|
239
226
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
227
|
+
const sequelizeType = sequelizeConstructor
|
|
228
|
+
? buildSequelizeDataType(sequelizeConstructor, dataTypeArgs)
|
|
229
|
+
: undefined;
|
|
230
|
+
const columnMetadata = {
|
|
231
|
+
name: column.COLUMN_NAME,
|
|
232
|
+
originName: column.COLUMN_NAME,
|
|
233
|
+
type: column.DATA_TYPE,
|
|
234
|
+
typeExt: column.COLUMN_TYPE,
|
|
235
|
+
...sequelizeType && {
|
|
236
|
+
sequelizeType,
|
|
237
|
+
dataType: renderDataTypeExpression(sequelizeType, DATA_TYPE_NAMESPACES.decorators),
|
|
238
|
+
},
|
|
239
|
+
allowNull: column.IS_NULLABLE === 'YES',
|
|
240
|
+
primaryKey: column.COLUMN_KEY === 'PRI',
|
|
241
|
+
autoIncrement: column.EXTRA === 'auto_increment',
|
|
242
|
+
indices: [],
|
|
243
|
+
comment: column.COLUMN_COMMENT,
|
|
244
|
+
...column.COLUMN_DEFAULT && { defaultValue: getDefaultValue(column.COLUMN_DEFAULT) },
|
|
245
|
+
};
|
|
244
246
|
columnsMetadata.push(columnMetadata);
|
|
245
247
|
}
|
|
246
248
|
return columnsMetadata;
|
|
@@ -262,7 +264,7 @@ class DialectMySQL extends Dialect_1.Dialect {
|
|
|
262
264
|
AND COLUMN_NAME = '${column}';
|
|
263
265
|
`;
|
|
264
266
|
const indices = await connection.query(query, {
|
|
265
|
-
type:
|
|
267
|
+
type: QueryTypes.SELECT,
|
|
266
268
|
raw: true,
|
|
267
269
|
});
|
|
268
270
|
for (const index of indices) {
|
|
@@ -276,5 +278,21 @@ class DialectMySQL extends Dialect_1.Dialect {
|
|
|
276
278
|
}
|
|
277
279
|
return indicesMetadata;
|
|
278
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Fetch foreign key constraints for the provided table. Constraints are read from
|
|
283
|
+
* information_schema.referential_constraints joined to key_column_usage, and a
|
|
284
|
+
* source column is flagged unique when it is covered by a single-column unique index.
|
|
285
|
+
* @param {Sequelize} connection
|
|
286
|
+
* @param {IConfig} config
|
|
287
|
+
* @param {ITable} table
|
|
288
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
289
|
+
*/
|
|
290
|
+
async fetchForeignKeysMetadata(connection, config, table) {
|
|
291
|
+
const foreignKeyRows = await connection.query(buildInformationSchemaForeignKeysQuery(config.connection.database, table.name), {
|
|
292
|
+
type: QueryTypes.SELECT,
|
|
293
|
+
raw: true,
|
|
294
|
+
});
|
|
295
|
+
const rows = foreignKeyRows.map(mapForeignKeyQueryRow);
|
|
296
|
+
return groupForeignKeyRows(rows);
|
|
297
|
+
}
|
|
279
298
|
}
|
|
280
|
-
exports.DialectMySQL = DialectMySQL;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AbstractDataTypeConstructor } from 'sequelize';
|
|
2
|
-
import { Sequelize } from 'sequelize
|
|
3
|
-
import { IConfig } from '../config';
|
|
4
|
-
import { IColumnMetadata, IIndexMetadata, Dialect, ITable } from './Dialect';
|
|
2
|
+
import { Sequelize } from 'sequelize';
|
|
3
|
+
import { IConfig } from '../config/index.js';
|
|
4
|
+
import { IColumnMetadata, IIndexMetadata, IForeignKeyConstraintMetadata, Dialect, ITable } from './Dialect.js';
|
|
5
5
|
/**
|
|
6
6
|
* Dialect for Postgres
|
|
7
7
|
* @class DialectPostgres
|
|
@@ -50,4 +50,15 @@ export declare class DialectPostgres 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
|
+
* pg_constraint, expanded to one row per column position with unnest ... WITH
|
|
56
|
+
* ORDINALITY, and a source column is flagged unique when it is covered by a
|
|
57
|
+
* single-column non-partial unique index.
|
|
58
|
+
* @param {Sequelize} connection
|
|
59
|
+
* @param {IConfig} config
|
|
60
|
+
* @param {ITable} table
|
|
61
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
62
|
+
*/
|
|
63
|
+
protected fetchForeignKeysMetadata(connection: Sequelize, config: IConfig, table: ITable): Promise<IForeignKeyConstraintMetadata[]>;
|
|
53
64
|
}
|
|
@@ -1,48 +1,47 @@
|
|
|
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 sequelizeDataTypesMap = {
|
|
9
|
-
int2:
|
|
10
|
-
int4:
|
|
11
|
-
int8:
|
|
12
|
-
numeric:
|
|
13
|
-
float4:
|
|
14
|
-
float8:
|
|
15
|
-
money:
|
|
16
|
-
varchar:
|
|
17
|
-
bpchar:
|
|
18
|
-
text:
|
|
19
|
-
bytea:
|
|
20
|
-
timestamp:
|
|
21
|
-
timestamptz:
|
|
22
|
-
date:
|
|
23
|
-
time:
|
|
24
|
-
timetz:
|
|
25
|
-
// interval:
|
|
26
|
-
bool:
|
|
27
|
-
point:
|
|
28
|
-
line:
|
|
29
|
-
lseg:
|
|
30
|
-
box:
|
|
31
|
-
path:
|
|
32
|
-
polygon:
|
|
33
|
-
circle:
|
|
34
|
-
geometry:
|
|
35
|
-
cidr:
|
|
36
|
-
inet:
|
|
37
|
-
macaddr:
|
|
38
|
-
macaddr8:
|
|
39
|
-
bit:
|
|
40
|
-
varbit:
|
|
41
|
-
uuid:
|
|
42
|
-
xml:
|
|
43
|
-
json:
|
|
44
|
-
jsonb:
|
|
45
|
-
jsonpath:
|
|
8
|
+
int2: DataTypes.INTEGER,
|
|
9
|
+
int4: DataTypes.INTEGER,
|
|
10
|
+
int8: DataTypes.BIGINT,
|
|
11
|
+
numeric: DataTypes.DECIMAL,
|
|
12
|
+
float4: DataTypes.FLOAT,
|
|
13
|
+
float8: DataTypes.DOUBLE,
|
|
14
|
+
money: DataTypes.NUMBER,
|
|
15
|
+
varchar: DataTypes.STRING,
|
|
16
|
+
bpchar: DataTypes.STRING,
|
|
17
|
+
text: DataTypes.STRING,
|
|
18
|
+
bytea: DataTypes.BLOB,
|
|
19
|
+
timestamp: DataTypes.DATE,
|
|
20
|
+
timestamptz: DataTypes.DATE,
|
|
21
|
+
date: DataTypes.STRING,
|
|
22
|
+
time: DataTypes.STRING,
|
|
23
|
+
timetz: DataTypes.STRING,
|
|
24
|
+
// interval: DataTypes.STRING,
|
|
25
|
+
bool: DataTypes.BOOLEAN,
|
|
26
|
+
point: DataTypes.GEOMETRY,
|
|
27
|
+
line: DataTypes.GEOMETRY,
|
|
28
|
+
lseg: DataTypes.GEOMETRY,
|
|
29
|
+
box: DataTypes.GEOMETRY,
|
|
30
|
+
path: DataTypes.GEOMETRY,
|
|
31
|
+
polygon: DataTypes.GEOMETRY,
|
|
32
|
+
circle: DataTypes.GEOMETRY,
|
|
33
|
+
geometry: DataTypes.GEOMETRY,
|
|
34
|
+
cidr: DataTypes.STRING,
|
|
35
|
+
inet: DataTypes.STRING,
|
|
36
|
+
macaddr: DataTypes.STRING,
|
|
37
|
+
macaddr8: DataTypes.STRING,
|
|
38
|
+
bit: DataTypes.STRING,
|
|
39
|
+
varbit: DataTypes.STRING,
|
|
40
|
+
uuid: DataTypes.UUID,
|
|
41
|
+
xml: DataTypes.STRING,
|
|
42
|
+
json: DataTypes.JSON,
|
|
43
|
+
jsonb: DataTypes.JSONB,
|
|
44
|
+
jsonpath: DataTypes.JSON,
|
|
46
45
|
};
|
|
47
46
|
const jsDataTypesMap = {
|
|
48
47
|
int2: 'number',
|
|
@@ -87,7 +86,7 @@ const jsDataTypesMap = {
|
|
|
87
86
|
* Dialect for Postgres
|
|
88
87
|
* @class DialectPostgres
|
|
89
88
|
*/
|
|
90
|
-
class DialectPostgres extends
|
|
89
|
+
export class DialectPostgres extends Dialect {
|
|
91
90
|
constructor() {
|
|
92
91
|
super('postgres');
|
|
93
92
|
}
|
|
@@ -132,12 +131,12 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
132
131
|
WHERE t.table_schema='${config.connection.schema}' AND pc.relkind = 'r';
|
|
133
132
|
`;
|
|
134
133
|
const tables = (await connection.query(query, {
|
|
135
|
-
type:
|
|
134
|
+
type: QueryTypes.SELECT,
|
|
136
135
|
raw: true,
|
|
137
136
|
})).map(({ table_name, table_comment }) => {
|
|
138
137
|
const t = {
|
|
139
138
|
name: table_name,
|
|
140
|
-
comment: table_comment
|
|
139
|
+
comment: table_comment ?? undefined,
|
|
141
140
|
};
|
|
142
141
|
return t;
|
|
143
142
|
});
|
|
@@ -151,7 +150,6 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
151
150
|
* @returns {Promise<IColumnMetadata[]>}
|
|
152
151
|
*/
|
|
153
152
|
async fetchColumnsMetadata(connection, config, table) {
|
|
154
|
-
var _a;
|
|
155
153
|
const columnsMetadata = [];
|
|
156
154
|
const query = `
|
|
157
155
|
SELECT
|
|
@@ -196,51 +194,54 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
196
194
|
ORDER BY c.ordinal_position;
|
|
197
195
|
`;
|
|
198
196
|
const columns = await connection.query(query, {
|
|
199
|
-
type:
|
|
197
|
+
type: QueryTypes.SELECT,
|
|
200
198
|
raw: true,
|
|
201
199
|
});
|
|
202
200
|
for (const column of columns) {
|
|
203
201
|
// Unknown data type
|
|
204
202
|
if (!this.mapDbTypeToSequelize(column.udt_name)) {
|
|
205
|
-
|
|
203
|
+
warnUnknownMappingForDataType(column.udt_name);
|
|
206
204
|
}
|
|
205
|
+
const sequelizeConstructor = this.mapDbTypeToSequelize(column.udt_name);
|
|
206
|
+
// Data type arguments (precision or length)
|
|
207
|
+
let dataTypeArgs = [];
|
|
208
|
+
switch (column.udt_name) {
|
|
209
|
+
case 'decimal':
|
|
210
|
+
case 'numeric':
|
|
211
|
+
case 'float':
|
|
212
|
+
case 'double':
|
|
213
|
+
dataTypeArgs = [column.numeric_precision, column.numeric_scale];
|
|
214
|
+
break;
|
|
215
|
+
case 'timestamp':
|
|
216
|
+
case 'timestampz':
|
|
217
|
+
dataTypeArgs = [column.datetime_precision];
|
|
218
|
+
break;
|
|
219
|
+
case 'bpchar':
|
|
220
|
+
case 'varchar':
|
|
221
|
+
dataTypeArgs = [column.character_maximum_length];
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
const sequelizeType = sequelizeConstructor
|
|
225
|
+
? buildSequelizeDataType(sequelizeConstructor, dataTypeArgs)
|
|
226
|
+
: undefined;
|
|
207
227
|
const columnMetadata = {
|
|
208
228
|
name: column.column_name,
|
|
209
229
|
originName: column.column_name,
|
|
210
230
|
type: column.udt_name,
|
|
211
231
|
typeExt: column.data_type,
|
|
212
|
-
...
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
.split(' ')[0], // avoids 'DOUBLE PRECISION' key to include PRECISION in the mapping
|
|
232
|
+
...sequelizeType && {
|
|
233
|
+
sequelizeType,
|
|
234
|
+
dataType: renderDataTypeExpression(sequelizeType, DATA_TYPE_NAMESPACES.decorators),
|
|
216
235
|
},
|
|
217
236
|
allowNull: column.is_nullable === 'YES' && !column.is_primary,
|
|
218
237
|
primaryKey: column.is_primary,
|
|
219
238
|
autoIncrement: column.is_sequence,
|
|
220
239
|
indices: [],
|
|
221
|
-
comment:
|
|
240
|
+
comment: column.description ?? undefined,
|
|
222
241
|
};
|
|
223
242
|
if (column.column_default) {
|
|
224
243
|
columnMetadata.defaultValue = `Sequelize.literal("${column.column_default.replace(/\"/g, '\\\"')}")`;
|
|
225
244
|
}
|
|
226
|
-
// Additional data type information
|
|
227
|
-
switch (column.udt_name) {
|
|
228
|
-
case 'decimal':
|
|
229
|
-
case 'numeric':
|
|
230
|
-
case 'float':
|
|
231
|
-
case 'double':
|
|
232
|
-
columnMetadata.dataType +=
|
|
233
|
-
(0, utils_1.generatePrecisionSignature)(column.numeric_precision, column.numeric_scale);
|
|
234
|
-
break;
|
|
235
|
-
case 'timestamp':
|
|
236
|
-
case 'timestampz':
|
|
237
|
-
columnMetadata.dataType += (0, utils_1.generatePrecisionSignature)(column.datetime_precision);
|
|
238
|
-
break;
|
|
239
|
-
case 'bpchar':
|
|
240
|
-
case 'varchar':
|
|
241
|
-
columnMetadata.dataType += (0, utils_1.generatePrecisionSignature)(column.character_maximum_length);
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
245
|
columnsMetadata.push(columnMetadata);
|
|
245
246
|
}
|
|
246
247
|
return columnsMetadata;
|
|
@@ -274,7 +275,7 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
274
275
|
AND a.attname = '${column}';
|
|
275
276
|
`;
|
|
276
277
|
const indices = await connection.query(query, {
|
|
277
|
-
type:
|
|
278
|
+
type: QueryTypes.SELECT,
|
|
278
279
|
raw: true,
|
|
279
280
|
});
|
|
280
281
|
for (const index of indices) {
|
|
@@ -286,5 +287,58 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
286
287
|
}
|
|
287
288
|
return indicesMetadata;
|
|
288
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Fetch foreign key constraints for the provided table. Constraints are read from
|
|
292
|
+
* pg_constraint, expanded to one row per column position with unnest ... WITH
|
|
293
|
+
* ORDINALITY, and a source column is flagged unique when it is covered by a
|
|
294
|
+
* single-column non-partial unique index.
|
|
295
|
+
* @param {Sequelize} connection
|
|
296
|
+
* @param {IConfig} config
|
|
297
|
+
* @param {ITable} table
|
|
298
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
299
|
+
*/
|
|
300
|
+
async fetchForeignKeysMetadata(connection, config, table) {
|
|
301
|
+
const foreignKeyRows = await connection.query(`
|
|
302
|
+
SELECT
|
|
303
|
+
con.conname AS constraint_name,
|
|
304
|
+
src.relname AS source_table,
|
|
305
|
+
src_att.attname AS source_column,
|
|
306
|
+
tgt_ns.nspname AS target_schema,
|
|
307
|
+
tgt.relname AS target_table,
|
|
308
|
+
tgt_att.attname AS target_column,
|
|
309
|
+
cols.ordinal_position AS ordinal_position,
|
|
310
|
+
con.confdeltype AS on_delete,
|
|
311
|
+
con.confupdtype AS on_update,
|
|
312
|
+
EXISTS (
|
|
313
|
+
SELECT 1
|
|
314
|
+
FROM pg_index x
|
|
315
|
+
WHERE x.indrelid = con.conrelid
|
|
316
|
+
AND x.indisunique
|
|
317
|
+
AND x.indisvalid
|
|
318
|
+
AND x.indpred IS NULL
|
|
319
|
+
AND x.indnatts = 1
|
|
320
|
+
AND x.indkey[0] = src_att.attnum
|
|
321
|
+
) AS is_source_column_unique
|
|
322
|
+
FROM pg_constraint con
|
|
323
|
+
JOIN pg_class src ON src.oid = con.conrelid
|
|
324
|
+
JOIN pg_namespace src_ns ON src_ns.oid = src.relnamespace
|
|
325
|
+
JOIN pg_class tgt ON tgt.oid = con.confrelid
|
|
326
|
+
JOIN pg_namespace tgt_ns ON tgt_ns.oid = tgt.relnamespace
|
|
327
|
+
CROSS JOIN LATERAL unnest(con.conkey, con.confkey)
|
|
328
|
+
WITH ORDINALITY AS cols(src_attnum, tgt_attnum, ordinal_position)
|
|
329
|
+
JOIN pg_attribute src_att
|
|
330
|
+
ON src_att.attrelid = con.conrelid AND src_att.attnum = cols.src_attnum
|
|
331
|
+
JOIN pg_attribute tgt_att
|
|
332
|
+
ON tgt_att.attrelid = con.confrelid AND tgt_att.attnum = cols.tgt_attnum
|
|
333
|
+
WHERE con.contype = 'f'
|
|
334
|
+
AND src_ns.nspname = '${config.connection.schema}'
|
|
335
|
+
AND src.relname = '${table.name}'
|
|
336
|
+
ORDER BY con.conname, cols.ordinal_position;
|
|
337
|
+
`, {
|
|
338
|
+
type: QueryTypes.SELECT,
|
|
339
|
+
raw: true,
|
|
340
|
+
});
|
|
341
|
+
const rows = foreignKeyRows.map(mapForeignKeyQueryRow);
|
|
342
|
+
return groupForeignKeyRows(rows);
|
|
343
|
+
}
|
|
289
344
|
}
|
|
290
|
-
exports.DialectPostgres = DialectPostgres;
|
|
@@ -1,7 +1,19 @@
|
|
|
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
|
+
/**
|
|
6
|
+
* Build a synthesized constraint name for a SQLite foreign key from its ordered source
|
|
7
|
+
* columns. SQLite foreign keys are unnamed and two of them may cover the same column set,
|
|
8
|
+
* so a name already produced for the table is disambiguated with the pragma_foreign_key_list
|
|
9
|
+
* id; the first occurrence of a name keeps the plain form.
|
|
10
|
+
* @param {string} table
|
|
11
|
+
* @param {string[]} columns
|
|
12
|
+
* @param {number} id
|
|
13
|
+
* @param {ReadonlySet<string>} takenNames
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
export declare const synthesizeSqliteConstraintName: (table: string, columns: string[], id: number, takenNames: ReadonlySet<string>) => string;
|
|
5
17
|
/**
|
|
6
18
|
* Dialect for SQLite
|
|
7
19
|
* @class DialectSQLite
|
|
@@ -35,4 +47,30 @@ export declare class DialectSQLite extends Dialect {
|
|
|
35
47
|
protected fetchTables(connection: Sequelize, config: IConfig): Promise<ITable[]>;
|
|
36
48
|
protected fetchColumnsMetadata(connection: Sequelize, config: IConfig, table: string): Promise<IColumnMetadata[]>;
|
|
37
49
|
protected fetchColumnIndexMetadata(connection: Sequelize, config: IConfig, table: string, column: string): Promise<IIndexMetadata[]>;
|
|
50
|
+
/**
|
|
51
|
+
* Fetch foreign key constraints for the provided table. SQLite constraints have no
|
|
52
|
+
* name, so each one is given the synthesized name `${table}_${sourceColumns}_fkey`,
|
|
53
|
+
* built from its ordered source columns. A constraint that omits the referenced
|
|
54
|
+
* column targets the primary key of the referenced table, resolved in key order.
|
|
55
|
+
* @param {Sequelize} connection
|
|
56
|
+
* @param {IConfig} config
|
|
57
|
+
* @param {ITable} table
|
|
58
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
59
|
+
*/
|
|
60
|
+
protected fetchForeignKeysMetadata(connection: Sequelize, config: IConfig, table: ITable): Promise<IForeignKeyConstraintMetadata[]>;
|
|
61
|
+
/**
|
|
62
|
+
* Collect the source columns covered by a single-column unique index or primary key,
|
|
63
|
+
* used to flag one-to-one foreign keys.
|
|
64
|
+
* @param {Sequelize} connection
|
|
65
|
+
* @param {string} table
|
|
66
|
+
* @returns {Promise<Set<string>>}
|
|
67
|
+
*/
|
|
68
|
+
private fetchSingleColumnUniqueColumns;
|
|
69
|
+
/**
|
|
70
|
+
* Fetch the primary key column names of a table, ordered by their key position.
|
|
71
|
+
* @param {Sequelize} connection
|
|
72
|
+
* @param {string} table
|
|
73
|
+
* @returns {Promise<string[]>}
|
|
74
|
+
*/
|
|
75
|
+
private fetchPrimaryKeyColumns;
|
|
38
76
|
}
|