sequelize-typescript-generator 12.0.1 → 13.0.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/.github/workflows/ci.yml +175 -150
- package/.vscode/launch.json +212 -0
- package/.vscode/tasks.json +95 -0
- package/CHANGELOG.md +17 -0
- package/CLAUDE.md +13 -0
- package/CONTEXT.md +67 -0
- package/README.md +827 -199
- 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 +101 -83
- 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/docker-compose.yml +65 -0
- package/docs/adr/0001-esm-only-package.md +36 -0
- package/docs/adr/0002-native-default-output-format.md +41 -0
- package/docs/agents/domain.md +51 -0
- package/docs/agents/issue-tracker.md +45 -0
- package/docs/agents/triage-labels.md +15 -0
- package/docs/migration/12-to-13.md +248 -0
- package/docs/plans/v13-phase5-association-discovery.md +168 -0
- package/docs/plans/v13-phase6-native-emitter.md +537 -0
- package/docs/plans/v13-upgrade-plan.md +137 -0
- package/jest.config.cjs +31 -0
- package/package.json +44 -27
- package/scripts/check-no-sequelize-typescript.cjs +68 -0
- package/tsconfig.json +4 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './createConnection';
|
|
1
|
+
export * from './createConnection.js';
|
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./createConnection"), exports);
|
|
1
|
+
export * from './createConnection.js';
|
|
@@ -1,12 +1,19 @@
|
|
|
1
|
+
import type { ReferentialAction } from './foreignKeys.js';
|
|
1
2
|
export interface IAssociationMetadata {
|
|
2
3
|
associationName: 'HasOne' | 'HasMany' | 'BelongsTo' | 'BelongsToMany';
|
|
3
4
|
targetModel: string;
|
|
4
5
|
joinModel?: string;
|
|
5
6
|
sourceKey?: string;
|
|
7
|
+
alias?: string;
|
|
8
|
+
foreignKey?: string;
|
|
9
|
+
targetKey?: string;
|
|
10
|
+
onDelete?: ReferentialAction;
|
|
11
|
+
onUpdate?: ReferentialAction;
|
|
6
12
|
}
|
|
7
13
|
export interface IForeignKey {
|
|
8
14
|
name: string;
|
|
9
15
|
targetModel: string;
|
|
16
|
+
isJunctionForeignKey: boolean;
|
|
10
17
|
}
|
|
11
18
|
export interface IAssociationsParsed {
|
|
12
19
|
[tableName: string]: {
|
|
@@ -18,9 +25,10 @@ export interface IAssociationsParsed {
|
|
|
18
25
|
* @class AssociationsParser
|
|
19
26
|
*/
|
|
20
27
|
export declare class AssociationsParser {
|
|
21
|
-
private static
|
|
28
|
+
private static readonly parsedByPath;
|
|
22
29
|
/**
|
|
23
|
-
* Parse associations file
|
|
30
|
+
* Parse associations file. Results are cached per path and every call returns a
|
|
31
|
+
* structural copy, so callers cannot mutate the cached value.
|
|
24
32
|
* @param {string} path
|
|
25
33
|
* @returns {IAssociationsParsed}
|
|
26
34
|
*/
|
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.AssociationsParser = void 0;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
1
|
+
import fs from 'fs';
|
|
8
2
|
const cardinalities = new Set([
|
|
9
3
|
'1:1',
|
|
10
4
|
'1:N',
|
|
@@ -34,19 +28,21 @@ const validateRow = (row) => {
|
|
|
34
28
|
/**
|
|
35
29
|
* @class AssociationsParser
|
|
36
30
|
*/
|
|
37
|
-
class AssociationsParser {
|
|
31
|
+
export class AssociationsParser {
|
|
32
|
+
static parsedByPath = new Map();
|
|
38
33
|
/**
|
|
39
|
-
* Parse associations file
|
|
34
|
+
* Parse associations file. Results are cached per path and every call returns a
|
|
35
|
+
* structural copy, so callers cannot mutate the cached value.
|
|
40
36
|
* @param {string} path
|
|
41
37
|
* @returns {IAssociationsParsed}
|
|
42
38
|
*/
|
|
43
39
|
static parse(path) {
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
return
|
|
40
|
+
const cached = this.parsedByPath.get(path);
|
|
41
|
+
if (cached) {
|
|
42
|
+
return structuredClone(cached);
|
|
47
43
|
}
|
|
48
44
|
const associationsMetadata = {};
|
|
49
|
-
const lines =
|
|
45
|
+
const lines = fs.readFileSync(path)
|
|
50
46
|
.toString()
|
|
51
47
|
.split('\n')
|
|
52
48
|
.filter(line => line.length); // Filter empty lines
|
|
@@ -86,6 +82,7 @@ class AssociationsParser {
|
|
|
86
82
|
associationsMetadata[rightModel].foreignKeys.push({
|
|
87
83
|
name: rightKey,
|
|
88
84
|
targetModel: leftModel,
|
|
85
|
+
isJunctionForeignKey: false,
|
|
89
86
|
});
|
|
90
87
|
}
|
|
91
88
|
// N:N association
|
|
@@ -109,17 +106,17 @@ class AssociationsParser {
|
|
|
109
106
|
});
|
|
110
107
|
associationsMetadata[joinModel].foreignKeys.push({
|
|
111
108
|
name: leftKey,
|
|
112
|
-
targetModel: leftModel
|
|
109
|
+
targetModel: leftModel,
|
|
110
|
+
isJunctionForeignKey: true,
|
|
113
111
|
});
|
|
114
112
|
associationsMetadata[joinModel].foreignKeys.push({
|
|
115
113
|
name: rightKey,
|
|
116
|
-
targetModel: rightModel
|
|
114
|
+
targetModel: rightModel,
|
|
115
|
+
isJunctionForeignKey: true,
|
|
117
116
|
});
|
|
118
117
|
}
|
|
119
118
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return this.associationsMetadata;
|
|
119
|
+
this.parsedByPath.set(path, associationsMetadata);
|
|
120
|
+
return structuredClone(associationsMetadata);
|
|
123
121
|
}
|
|
124
122
|
}
|
|
125
|
-
exports.AssociationsParser = AssociationsParser;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { IndexType, IndexMethod, AbstractDataTypeConstructor } from 'sequelize';
|
|
2
|
-
import { Sequelize } from 'sequelize
|
|
3
|
-
import { IConfig } from '../config';
|
|
4
|
-
import { IAssociationMetadata } from './AssociationsParser';
|
|
2
|
+
import { Sequelize } from 'sequelize';
|
|
3
|
+
import { IConfig } from '../config/index.js';
|
|
4
|
+
import { IAssociationMetadata } from './AssociationsParser.js';
|
|
5
|
+
import type { ReferentialAction } from './foreignKeys.js';
|
|
6
|
+
import type { ISequelizeDataType } from './dataTypes.js';
|
|
5
7
|
export interface ITablesMetadata {
|
|
6
8
|
[tableName: string]: ITableMetadata;
|
|
7
9
|
}
|
|
@@ -10,23 +12,45 @@ export interface ITableMetadata {
|
|
|
10
12
|
originName: string;
|
|
11
13
|
schema?: 'public' | string;
|
|
12
14
|
timestamps?: boolean;
|
|
15
|
+
paranoid?: boolean;
|
|
16
|
+
deletedAt?: string;
|
|
17
|
+
hasTrigger?: boolean;
|
|
13
18
|
columns: {
|
|
14
19
|
[columnName: string]: IColumnMetadata;
|
|
15
20
|
};
|
|
21
|
+
foreignKeys?: IForeignKeyConstraintMetadata[];
|
|
16
22
|
associations?: IAssociationMetadata[];
|
|
17
23
|
comment?: string;
|
|
18
24
|
}
|
|
25
|
+
export interface IColumnForeignKeyMetadata {
|
|
26
|
+
name: string;
|
|
27
|
+
targetModel: string;
|
|
28
|
+
targetKey?: string;
|
|
29
|
+
constraintName?: string;
|
|
30
|
+
onDelete?: ReferentialAction;
|
|
31
|
+
onUpdate?: ReferentialAction;
|
|
32
|
+
isUnique?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export interface IForeignKeyConstraintMetadata {
|
|
35
|
+
constraintName: string;
|
|
36
|
+
sourceTable: string;
|
|
37
|
+
sourceColumns: string[];
|
|
38
|
+
targetSchema?: string;
|
|
39
|
+
targetTable: string;
|
|
40
|
+
targetColumns: string[];
|
|
41
|
+
onDelete: ReferentialAction;
|
|
42
|
+
onUpdate: ReferentialAction;
|
|
43
|
+
isSourceColumnUnique: boolean;
|
|
44
|
+
}
|
|
19
45
|
export interface IColumnMetadata {
|
|
20
46
|
name: string;
|
|
21
47
|
originName: string;
|
|
22
48
|
type: string;
|
|
23
49
|
typeExt: string;
|
|
24
50
|
dataType?: string;
|
|
51
|
+
sequelizeType?: ISequelizeDataType;
|
|
25
52
|
primaryKey: boolean;
|
|
26
|
-
foreignKey?:
|
|
27
|
-
name: string;
|
|
28
|
-
targetModel: string;
|
|
29
|
-
};
|
|
53
|
+
foreignKey?: IColumnForeignKeyMetadata;
|
|
30
54
|
allowNull: boolean;
|
|
31
55
|
autoIncrement: boolean;
|
|
32
56
|
indices?: IIndexMetadata[];
|
|
@@ -43,9 +67,12 @@ export interface IIndexMetadata {
|
|
|
43
67
|
}
|
|
44
68
|
export interface ITable {
|
|
45
69
|
name: string;
|
|
70
|
+
schema?: string;
|
|
71
|
+
isView?: boolean;
|
|
46
72
|
comment?: string;
|
|
47
73
|
}
|
|
48
|
-
|
|
74
|
+
export declare const DIALECT_NAMES: readonly ["postgres", "mysql", "mariadb", "sqlite", "mssql"];
|
|
75
|
+
export type DialectName = typeof DIALECT_NAMES[number];
|
|
49
76
|
export declare abstract class Dialect {
|
|
50
77
|
/**
|
|
51
78
|
* Accepted dialects
|
|
@@ -103,6 +130,23 @@ export declare abstract class Dialect {
|
|
|
103
130
|
* @returns {Promise<IIndexMetadata[]>}
|
|
104
131
|
*/
|
|
105
132
|
protected abstract fetchColumnIndexMetadata(connection: Sequelize, config: IConfig, table: string, column: string): Promise<IIndexMetadata[]>;
|
|
133
|
+
/**
|
|
134
|
+
* Fetch foreign key constraints for the provided table
|
|
135
|
+
* @param {Sequelize} connection
|
|
136
|
+
* @param {IConfig} config
|
|
137
|
+
* @param {ITable} table
|
|
138
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
139
|
+
*/
|
|
140
|
+
protected abstract fetchForeignKeysMetadata(connection: Sequelize, config: IConfig, table: ITable): Promise<IForeignKeyConstraintMetadata[]>;
|
|
141
|
+
/**
|
|
142
|
+
* Report whether the table has at least one enabled trigger. Overridden by
|
|
143
|
+
* SQL Server; every other dialect keeps the default of no triggers.
|
|
144
|
+
* @param {Sequelize} connection
|
|
145
|
+
* @param {IConfig} config
|
|
146
|
+
* @param {ITable} table
|
|
147
|
+
* @returns {Promise<boolean>}
|
|
148
|
+
*/
|
|
149
|
+
protected fetchTableHasTrigger(connection: Sequelize, config: IConfig, table: ITable): Promise<boolean>;
|
|
106
150
|
/**
|
|
107
151
|
* Build tables metadata for the specific dialect and schema
|
|
108
152
|
* @param {IConfig} config
|
|
@@ -110,4 +154,3 @@ export declare abstract class Dialect {
|
|
|
110
154
|
*/
|
|
111
155
|
buildTablesMetadata(config: IConfig): Promise<ITablesMetadata>;
|
|
112
156
|
}
|
|
113
|
-
export {};
|
|
@@ -1,10 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { createConnection } from "../connection/index.js";
|
|
2
|
+
import { AssociationsParser } from './AssociationsParser.js';
|
|
3
|
+
import { caseTransformer } from './utils.js';
|
|
4
|
+
import { applyForeignKeyConstraintsToColumns } from './foreignKeys.js';
|
|
5
|
+
import { discoverAssociations, isAssociationDiscoveryEnabled } from './associationDiscovery.js';
|
|
6
|
+
import { applyAssociationsFile } from './associationsFileMerge.js';
|
|
7
|
+
import { findParanoidColumn, resolveParanoidOption } from './paranoid.js';
|
|
8
|
+
export const DIALECT_NAMES = [
|
|
9
|
+
'postgres',
|
|
10
|
+
'mysql',
|
|
11
|
+
'mariadb',
|
|
12
|
+
'sqlite',
|
|
13
|
+
'mssql',
|
|
14
|
+
];
|
|
15
|
+
export class Dialect {
|
|
16
|
+
/**
|
|
17
|
+
* Accepted dialects
|
|
18
|
+
*/
|
|
19
|
+
static dialects = new Set(DIALECT_NAMES);
|
|
20
|
+
/**
|
|
21
|
+
* Dialect name
|
|
22
|
+
*/
|
|
23
|
+
name;
|
|
8
24
|
/**
|
|
9
25
|
* @constructor
|
|
10
26
|
* @param {DialectName} name
|
|
@@ -13,108 +29,128 @@ class Dialect {
|
|
|
13
29
|
constructor(name) {
|
|
14
30
|
this.name = name;
|
|
15
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Report whether the table has at least one enabled trigger. Overridden by
|
|
34
|
+
* SQL Server; every other dialect keeps the default of no triggers.
|
|
35
|
+
* @param {Sequelize} connection
|
|
36
|
+
* @param {IConfig} config
|
|
37
|
+
* @param {ITable} table
|
|
38
|
+
* @returns {Promise<boolean>}
|
|
39
|
+
*/
|
|
40
|
+
async fetchTableHasTrigger(connection, config, table) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
16
43
|
/**
|
|
17
44
|
* Build tables metadata for the specific dialect and schema
|
|
18
45
|
* @param {IConfig} config
|
|
19
46
|
* @returns {Promise<ITableMetadata[]>}
|
|
20
47
|
*/
|
|
21
48
|
async buildTablesMetadata(config) {
|
|
22
|
-
var _a, _b, _c, _d, _e, _f;
|
|
23
49
|
let connection;
|
|
24
50
|
const tablesMetadata = {};
|
|
25
51
|
try {
|
|
26
|
-
connection =
|
|
52
|
+
connection = createConnection(config.connection);
|
|
27
53
|
await connection.authenticate();
|
|
28
54
|
let tables = await this.fetchTables(connection, config);
|
|
55
|
+
const paranoidResolution = resolveParanoidOption(config.metadata);
|
|
56
|
+
if (paranoidResolution.warning) {
|
|
57
|
+
console.warn('[WARNING]', paranoidResolution.warning);
|
|
58
|
+
}
|
|
29
59
|
// Apply filters
|
|
30
60
|
tables = tables
|
|
31
61
|
.filter(({ name }) => {
|
|
32
|
-
|
|
33
|
-
if ((_b = (_a = config.metadata) === null || _a === void 0 ? void 0 : _a.tables) === null || _b === void 0 ? void 0 : _b.length) {
|
|
62
|
+
if (config.metadata?.tables?.length) {
|
|
34
63
|
return config.metadata.tables.includes(name.toLowerCase());
|
|
35
64
|
}
|
|
36
65
|
else {
|
|
37
66
|
return true;
|
|
38
67
|
}
|
|
39
68
|
}).filter(({ name }) => {
|
|
40
|
-
|
|
41
|
-
if ((_b = (_a = config.metadata) === null || _a === void 0 ? void 0 : _a.skipTables) === null || _b === void 0 ? void 0 : _b.length) {
|
|
69
|
+
if (config.metadata?.skipTables?.length) {
|
|
42
70
|
return !(config.metadata.skipTables.includes(name.toLowerCase()));
|
|
43
71
|
}
|
|
44
72
|
else {
|
|
45
73
|
return true;
|
|
46
74
|
}
|
|
47
75
|
});
|
|
48
|
-
for (const
|
|
76
|
+
for (const table of tables) {
|
|
77
|
+
const { name: tableName, comment: tableComment } = table;
|
|
49
78
|
const columnsMetadata = await this.fetchColumnsMetadata(connection, config, tableName);
|
|
50
79
|
// Fetch indices metadata if required
|
|
51
|
-
if (
|
|
80
|
+
if (config.metadata?.indices) {
|
|
52
81
|
for (const column of columnsMetadata) {
|
|
53
82
|
column.indices = await this.fetchColumnIndexMetadata(connection, config, tableName, column.name);
|
|
54
83
|
}
|
|
55
84
|
}
|
|
85
|
+
const foreignKeys = table.isView
|
|
86
|
+
? []
|
|
87
|
+
: await this.fetchForeignKeysMetadata(connection, config, table);
|
|
88
|
+
const hasTrigger = await this.fetchTableHasTrigger(connection, config, table);
|
|
89
|
+
let paranoid = false;
|
|
90
|
+
let deletedAt;
|
|
91
|
+
if (paranoidResolution.enabled && !table.isView) {
|
|
92
|
+
const paranoidColumn = findParanoidColumn(columnsMetadata);
|
|
93
|
+
if (paranoidColumn) {
|
|
94
|
+
paranoid = true;
|
|
95
|
+
deletedAt = paranoidColumn.originName;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
56
98
|
const tableMetadata = {
|
|
57
99
|
originName: tableName,
|
|
58
100
|
name: tableName,
|
|
59
|
-
schema: config.connection.schema,
|
|
60
|
-
timestamps:
|
|
101
|
+
schema: table.schema ?? config.connection.schema,
|
|
102
|
+
timestamps: config.metadata?.timestamps ?? false,
|
|
103
|
+
...paranoid && { paranoid: true },
|
|
104
|
+
...deletedAt && { deletedAt },
|
|
61
105
|
columns: {},
|
|
62
|
-
|
|
106
|
+
foreignKeys,
|
|
107
|
+
...hasTrigger && { hasTrigger: true },
|
|
108
|
+
comment: tableComment ?? undefined,
|
|
63
109
|
};
|
|
64
110
|
for (const columnMetadata of columnsMetadata) {
|
|
65
111
|
tableMetadata.columns[columnMetadata.name] = columnMetadata;
|
|
66
112
|
}
|
|
67
113
|
tablesMetadata[tableMetadata.originName] = tableMetadata;
|
|
68
114
|
}
|
|
115
|
+
// Copy single-column foreign key constraints onto their source columns,
|
|
116
|
+
// limited to constraints whose target table is generated.
|
|
117
|
+
const generatedTables = new Set(Object.keys(tablesMetadata));
|
|
118
|
+
for (const [tableName, tableMetadata] of Object.entries(tablesMetadata)) {
|
|
119
|
+
tablesMetadata[tableName] = applyForeignKeyConstraintsToColumns(tableMetadata, generatedTables);
|
|
120
|
+
}
|
|
69
121
|
}
|
|
70
122
|
catch (err) {
|
|
71
|
-
|
|
72
|
-
process.exit(1);
|
|
123
|
+
throw new Error('Failed to build tables metadata from the source database', { cause: err });
|
|
73
124
|
}
|
|
74
125
|
finally {
|
|
75
126
|
connection && await connection.close();
|
|
76
127
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
columns[columnName].foreignKey = {
|
|
95
|
-
name: columnName,
|
|
96
|
-
targetModel: targetModel
|
|
97
|
-
};
|
|
98
|
-
}
|
|
128
|
+
let finalTablesMetadata = tablesMetadata;
|
|
129
|
+
// Discover associations from foreign key constraints unless disabled.
|
|
130
|
+
if (isAssociationDiscoveryEnabled(config.metadata)) {
|
|
131
|
+
const discovery = discoverAssociations(finalTablesMetadata);
|
|
132
|
+
finalTablesMetadata = discovery.tablesMetadata;
|
|
133
|
+
for (const warning of discovery.warnings) {
|
|
134
|
+
console.warn('[WARNING]', warning);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// Apply the associations file on top of the discovered associations.
|
|
138
|
+
if (config.metadata?.associationsFile) {
|
|
139
|
+
const parsedAssociations = AssociationsParser.parse(config.metadata.associationsFile);
|
|
140
|
+
const merge = applyAssociationsFile(finalTablesMetadata, parsedAssociations);
|
|
141
|
+
finalTablesMetadata = merge.tablesMetadata;
|
|
142
|
+
for (const warning of merge.warnings) {
|
|
143
|
+
console.warn('[WARNING]', warning);
|
|
99
144
|
}
|
|
100
145
|
}
|
|
101
146
|
// Apply transformations if required
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
|
|
147
|
+
if (config.metadata?.case) {
|
|
148
|
+
const transformed = {};
|
|
149
|
+
for (const [tableName, tableMetadata] of Object.entries(finalTablesMetadata)) {
|
|
150
|
+
transformed[tableName] = caseTransformer(tableMetadata, config.metadata.case);
|
|
105
151
|
}
|
|
152
|
+
finalTablesMetadata = transformed;
|
|
106
153
|
}
|
|
107
|
-
return
|
|
154
|
+
return finalTablesMetadata;
|
|
108
155
|
}
|
|
109
156
|
}
|
|
110
|
-
exports.Dialect = Dialect;
|
|
111
|
-
/**
|
|
112
|
-
* Accepted dialects
|
|
113
|
-
*/
|
|
114
|
-
Dialect.dialects = new Set([
|
|
115
|
-
'postgres',
|
|
116
|
-
'mysql',
|
|
117
|
-
'mariadb',
|
|
118
|
-
'sqlite',
|
|
119
|
-
'mssql',
|
|
120
|
-
]);
|
|
@@ -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
|
|
@@ -42,4 +42,23 @@ export declare class DialectMSSQL extends Dialect {
|
|
|
42
42
|
*/
|
|
43
43
|
protected fetchColumnsMetadata(connection: Sequelize, config: IConfig, table: string): Promise<IColumnMetadata[]>;
|
|
44
44
|
protected fetchColumnIndexMetadata(connection: Sequelize, config: IConfig, table: string, column: string): Promise<IIndexMetadata[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Fetch foreign key constraints for the provided table. Constraints are read from
|
|
47
|
+
* sys.foreign_keys and sys.foreign_key_columns, one row per column position, and a
|
|
48
|
+
* source column is flagged unique when it is covered by a single-column non-filtered
|
|
49
|
+
* unique index. Referential actions come from the *_referential_action_desc columns.
|
|
50
|
+
* @param {Sequelize} connection
|
|
51
|
+
* @param {IConfig} config
|
|
52
|
+
* @param {ITable} table
|
|
53
|
+
* @returns {Promise<IForeignKeyConstraintMetadata[]>}
|
|
54
|
+
*/
|
|
55
|
+
protected fetchForeignKeysMetadata(connection: Sequelize, config: IConfig, table: ITable): Promise<IForeignKeyConstraintMetadata[]>;
|
|
56
|
+
/**
|
|
57
|
+
* Report whether the table has at least one enabled trigger, read from sys.triggers.
|
|
58
|
+
* @param {Sequelize} connection
|
|
59
|
+
* @param {IConfig} config
|
|
60
|
+
* @param {ITable} table
|
|
61
|
+
* @returns {Promise<boolean>}
|
|
62
|
+
*/
|
|
63
|
+
protected fetchTableHasTrigger(connection: Sequelize, config: IConfig, table: ITable): Promise<boolean>;
|
|
45
64
|
}
|