sequelize-typescript-generator 12.0.0 → 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
package/build/bin/cli.js
CHANGED
|
@@ -1,92 +1,93 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
exports.cli = void 0;
|
|
8
|
-
const yargs_1 = __importDefault(require("yargs"));
|
|
9
|
-
const builders_1 = require("../builders");
|
|
10
|
-
const utils_1 = require("./utils");
|
|
2
|
+
import yargs from 'yargs';
|
|
3
|
+
import { hideBin } from 'yargs/helpers';
|
|
4
|
+
import { ModelBuilder } from '../builders/index.js';
|
|
5
|
+
import { defaultOutputDir, aliasesMap, validateArgs, buildConfig, buildDialect, } from './utils.js';
|
|
6
|
+
import { FORMATS, DEFAULT_FORMAT } from '../config/format.js';
|
|
11
7
|
process.on('unhandledRejection', (reason, promise) => {
|
|
12
8
|
console.error(reason, promise);
|
|
13
9
|
process.exit(1);
|
|
14
10
|
});
|
|
15
|
-
const cli = async () => {
|
|
11
|
+
export const cli = async () => {
|
|
16
12
|
let usage = `Usage: stg -D <dialect> -d [database] -u [username] -x [password] `;
|
|
17
13
|
usage += `-h [host] -p [port] -o [out-dir] -s [schema] -a [associations-file]`;
|
|
18
|
-
usage += `-t [tables] -T [skip-tables] -V [no-views] -i [indices] -C [case] -S [storage] -L [lint-file] `;
|
|
19
|
-
usage += `-l [ssl] -r [protocol] -n [dialect-options] -c [clean] -g [logs]`;
|
|
20
|
-
const { argv } =
|
|
14
|
+
usage += `-t [tables] -T [skip-tables] -V [no-views] -i [indices] -P [paranoid] -C [case] -S [storage] -L [lint-file] `;
|
|
15
|
+
usage += `-l [ssl] -r [protocol] -n [dialect-options] -c [clean] -g [logs] -F [format] --no-associations`;
|
|
16
|
+
const { argv } = yargs(hideBin(process.argv))
|
|
21
17
|
.usage(usage)
|
|
22
18
|
.demand(['dialect'])
|
|
23
19
|
.option('h', {
|
|
24
|
-
alias:
|
|
20
|
+
alias: aliasesMap.HOST,
|
|
25
21
|
string: true,
|
|
26
22
|
describe: `Database IP/hostname`,
|
|
27
23
|
})
|
|
28
24
|
.option('p', {
|
|
29
|
-
alias:
|
|
25
|
+
alias: aliasesMap.PORT,
|
|
30
26
|
number: true,
|
|
31
27
|
describe: `Database port. Defaults: \n - MySQL/MariaDB: 3306 \n - Postgres: 5432 \n - MSSQL: 1433`,
|
|
32
28
|
})
|
|
33
29
|
.option('d', {
|
|
34
|
-
alias:
|
|
30
|
+
alias: aliasesMap.DATABASE,
|
|
35
31
|
string: true,
|
|
36
32
|
describe: `Database name`,
|
|
37
33
|
})
|
|
38
34
|
.option('s', {
|
|
39
|
-
alias:
|
|
35
|
+
alias: aliasesMap.SCHEMA,
|
|
40
36
|
string: true,
|
|
41
|
-
describe: `Schema name (Postgres
|
|
37
|
+
describe: `Schema name (Postgres and SQL Server). Default: \n - public`,
|
|
42
38
|
})
|
|
43
39
|
.option('D', {
|
|
44
|
-
alias:
|
|
40
|
+
alias: aliasesMap.DIALECT,
|
|
45
41
|
string: true,
|
|
46
42
|
describe: `Dialect: \n - postgres \n - mysql \n - mariadb \n - sqlite \n - mssql`,
|
|
47
43
|
})
|
|
48
44
|
.option('u', {
|
|
49
|
-
alias:
|
|
45
|
+
alias: aliasesMap.USERNAME,
|
|
50
46
|
string: true,
|
|
51
47
|
describe: `Database username`,
|
|
52
48
|
})
|
|
53
49
|
.option('x', {
|
|
54
|
-
alias:
|
|
50
|
+
alias: aliasesMap.PASSWORD,
|
|
55
51
|
string: true,
|
|
56
52
|
describe: `Database password`,
|
|
57
53
|
})
|
|
58
54
|
.option('t', {
|
|
59
|
-
alias:
|
|
55
|
+
alias: aliasesMap.TABLES,
|
|
60
56
|
string: true,
|
|
61
57
|
describe: `Comma-separated names of tables to process`,
|
|
62
58
|
})
|
|
63
59
|
.option('T', {
|
|
64
|
-
alias:
|
|
60
|
+
alias: aliasesMap.SKIP_TABLES,
|
|
65
61
|
string: true,
|
|
66
62
|
describe: `Comma-separated names of tables to skip`,
|
|
67
63
|
})
|
|
68
64
|
.option('i', {
|
|
69
|
-
alias:
|
|
65
|
+
alias: aliasesMap.INDICES,
|
|
70
66
|
boolean: true,
|
|
71
67
|
describe: `Include index annotations in the generated models`,
|
|
72
68
|
})
|
|
73
69
|
.option('o', {
|
|
74
|
-
alias:
|
|
70
|
+
alias: aliasesMap.OUTPUT_DIR,
|
|
75
71
|
string: true,
|
|
76
|
-
describe: `Output directory. Default: \n - ${
|
|
72
|
+
describe: `Output directory. Default: \n - ${defaultOutputDir}`,
|
|
77
73
|
})
|
|
78
74
|
.option('c', {
|
|
79
|
-
alias:
|
|
75
|
+
alias: aliasesMap.OUTPUT_DIR_CLEAN,
|
|
80
76
|
boolean: true,
|
|
81
77
|
describe: `Clean output directory before running`,
|
|
82
78
|
})
|
|
83
79
|
.option('m', {
|
|
84
|
-
alias:
|
|
80
|
+
alias: aliasesMap.TIMESTAMPS,
|
|
85
81
|
boolean: true,
|
|
86
82
|
describe: `Add default timestamps to tables`,
|
|
83
|
+
})
|
|
84
|
+
.option('P', {
|
|
85
|
+
alias: aliasesMap.PARANOID,
|
|
86
|
+
boolean: true,
|
|
87
|
+
describe: `Emit paranoid table options for tables with a deleted_at or deletedAt column. Requires --timestamps.`,
|
|
87
88
|
})
|
|
88
89
|
.option('C', {
|
|
89
|
-
alias:
|
|
90
|
+
alias: aliasesMap.CASE,
|
|
90
91
|
string: true,
|
|
91
92
|
describe: `Transform tables and fields names with one of the following cases:
|
|
92
93
|
- underscore
|
|
@@ -99,54 +100,71 @@ const cli = async () => {
|
|
|
99
100
|
<model case>:<column case>
|
|
100
101
|
`,
|
|
101
102
|
}).option('S', {
|
|
102
|
-
alias:
|
|
103
|
+
alias: aliasesMap.STORAGE,
|
|
103
104
|
string: true,
|
|
104
105
|
describe: `SQLite storage. Default: \n - memory`,
|
|
105
106
|
}).option('L', {
|
|
106
|
-
alias:
|
|
107
|
+
alias: aliasesMap.LINT_FILE,
|
|
107
108
|
string: true,
|
|
108
109
|
describe: `ES Lint file path`,
|
|
109
110
|
}).option('l', {
|
|
110
|
-
alias:
|
|
111
|
+
alias: aliasesMap.SSL,
|
|
111
112
|
boolean: true,
|
|
112
113
|
describe: `Enable SSL`,
|
|
113
114
|
}).option('r', {
|
|
114
|
-
alias:
|
|
115
|
+
alias: aliasesMap.PROTOCOL,
|
|
115
116
|
string: true,
|
|
116
117
|
describe: `Protocol used: Default: \n - tcp`,
|
|
117
118
|
}).option('a', {
|
|
118
|
-
alias:
|
|
119
|
+
alias: aliasesMap.ASSOCIATIONS_FILE,
|
|
119
120
|
string: true,
|
|
120
121
|
describe: `Associations file path`,
|
|
121
122
|
}).option('g', {
|
|
122
|
-
alias:
|
|
123
|
+
alias: aliasesMap.ENABLE_SEQUELIZE_LOGS,
|
|
123
124
|
boolean: true,
|
|
124
125
|
describe: `Enable Sequelize logs`,
|
|
125
126
|
}).option('n', {
|
|
126
|
-
alias:
|
|
127
|
+
alias: aliasesMap.DIALECT_OPTIONS,
|
|
127
128
|
type: 'string',
|
|
128
129
|
describe: `Dialect native options passed as json string.`,
|
|
129
130
|
}).option('f', {
|
|
130
|
-
alias:
|
|
131
|
+
alias: aliasesMap.DIALECT_OPTIONS_FILE,
|
|
131
132
|
type: 'string',
|
|
132
133
|
describe: `Dialect native options passed as json file path.`,
|
|
133
134
|
}).option('R', {
|
|
134
|
-
alias:
|
|
135
|
+
alias: aliasesMap.DISABLE_STRICT,
|
|
135
136
|
boolean: true,
|
|
136
|
-
describe: `Disable strict typescript class declaration.`,
|
|
137
|
+
describe: `Disable strict typescript class declaration (decorators format only).`,
|
|
138
|
+
}).option('F', {
|
|
139
|
+
alias: aliasesMap.FORMAT,
|
|
140
|
+
string: true,
|
|
141
|
+
choices: FORMATS,
|
|
142
|
+
default: DEFAULT_FORMAT,
|
|
143
|
+
describe: `Output format:
|
|
144
|
+
- native: plain Sequelize classes with declare fields, Model.init and an initModels wiring file (default)
|
|
145
|
+
- decorators: sequelize-typescript decorators (requires sequelize-typescript in the target project)`,
|
|
137
146
|
}).option('V', {
|
|
138
|
-
alias:
|
|
147
|
+
alias: aliasesMap.DISABLE_VIEWS,
|
|
139
148
|
boolean: true,
|
|
140
149
|
describe: `Disable views generation. Available for: MySQL and MariaDB.`,
|
|
150
|
+
}).option(aliasesMap.ASSOCIATIONS, {
|
|
151
|
+
boolean: true,
|
|
152
|
+
default: true,
|
|
153
|
+
describe: `Discover one-to-one and one-to-many associations from foreign keys. Use --no-associations to disable.`,
|
|
141
154
|
});
|
|
142
|
-
|
|
143
|
-
const config =
|
|
144
|
-
const dialect =
|
|
145
|
-
const builder = new
|
|
146
|
-
|
|
155
|
+
validateArgs(argv);
|
|
156
|
+
const config = buildConfig(argv);
|
|
157
|
+
const dialect = buildDialect(argv);
|
|
158
|
+
const builder = new ModelBuilder(config, dialect);
|
|
159
|
+
try {
|
|
160
|
+
await builder.build();
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
console.error(err);
|
|
164
|
+
process.exit(1);
|
|
165
|
+
}
|
|
147
166
|
console.log(`All done!`);
|
|
148
167
|
};
|
|
149
|
-
exports.cli = cli;
|
|
150
168
|
(async () => {
|
|
151
|
-
await
|
|
169
|
+
await cli();
|
|
152
170
|
})();
|
package/build/bin/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Dialect } from '../dialects/Dialect';
|
|
2
|
-
import { IConfig, TransformCase, TransformMap } from '../config/IConfig';
|
|
1
|
+
import { Dialect } from '../dialects/Dialect.js';
|
|
2
|
+
import { IConfig, TransformCase, TransformMap } from '../config/IConfig.js';
|
|
3
3
|
export type ArgvType = {
|
|
4
4
|
[key: string]: any;
|
|
5
5
|
};
|
|
@@ -18,17 +18,20 @@ export declare const aliasesMap: {
|
|
|
18
18
|
OUTPUT_DIR_CLEAN: string;
|
|
19
19
|
INDICES: string;
|
|
20
20
|
TIMESTAMPS: string;
|
|
21
|
+
PARANOID: string;
|
|
21
22
|
CASE: string;
|
|
22
23
|
STORAGE: string;
|
|
23
24
|
LINT_FILE: string;
|
|
24
25
|
SSL: string;
|
|
25
26
|
PROTOCOL: string;
|
|
26
27
|
ASSOCIATIONS_FILE: string;
|
|
28
|
+
ASSOCIATIONS: string;
|
|
27
29
|
ENABLE_SEQUELIZE_LOGS: string;
|
|
28
30
|
DIALECT_OPTIONS: string;
|
|
29
31
|
DIALECT_OPTIONS_FILE: string;
|
|
30
32
|
DISABLE_STRICT: string;
|
|
31
33
|
DISABLE_VIEWS: string;
|
|
34
|
+
FORMAT: string;
|
|
32
35
|
};
|
|
33
36
|
/**
|
|
34
37
|
* Diplay error message and exit
|
package/build/bin/utils.js
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const Dialect_1 = require("../dialects/Dialect");
|
|
10
|
-
const dialects_1 = require("../dialects");
|
|
11
|
-
const IConfig_1 = require("../config/IConfig");
|
|
12
|
-
exports.defaultOutputDir = 'output-models';
|
|
13
|
-
exports.aliasesMap = {
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { Dialect } from '../dialects/Dialect.js';
|
|
4
|
+
import { createDialect } from '../dialects/createDialect.js';
|
|
5
|
+
import { TransformCases, TransformTarget } from '../config/IConfig.js';
|
|
6
|
+
import { isFormat } from '../config/format.js';
|
|
7
|
+
export const defaultOutputDir = 'output-models';
|
|
8
|
+
export const aliasesMap = {
|
|
14
9
|
HOST: 'host',
|
|
15
10
|
PORT: 'port',
|
|
16
11
|
DATABASE: 'database',
|
|
@@ -24,46 +19,47 @@ exports.aliasesMap = {
|
|
|
24
19
|
OUTPUT_DIR_CLEAN: 'clean',
|
|
25
20
|
INDICES: 'indices',
|
|
26
21
|
TIMESTAMPS: 'timestamps',
|
|
22
|
+
PARANOID: 'paranoid',
|
|
27
23
|
CASE: 'case',
|
|
28
24
|
STORAGE: 'storage',
|
|
29
25
|
LINT_FILE: 'lint-file',
|
|
30
26
|
SSL: 'ssl',
|
|
31
27
|
PROTOCOL: 'protocol',
|
|
32
28
|
ASSOCIATIONS_FILE: 'associations-file',
|
|
29
|
+
ASSOCIATIONS: 'associations',
|
|
33
30
|
ENABLE_SEQUELIZE_LOGS: 'logs',
|
|
34
31
|
DIALECT_OPTIONS: 'dialect-options',
|
|
35
32
|
DIALECT_OPTIONS_FILE: 'dialect-options-file',
|
|
36
33
|
DISABLE_STRICT: 'no-strict',
|
|
37
34
|
DISABLE_VIEWS: 'no-views',
|
|
35
|
+
FORMAT: 'format',
|
|
38
36
|
};
|
|
39
37
|
/**
|
|
40
38
|
* Diplay error message and exit
|
|
41
39
|
* @param {string} msg
|
|
42
40
|
* @returns {void}
|
|
43
41
|
*/
|
|
44
|
-
const error = (msg) => {
|
|
42
|
+
export const error = (msg) => {
|
|
45
43
|
console.error('[ValidationError]', msg);
|
|
46
44
|
process.exit(1);
|
|
47
45
|
};
|
|
48
|
-
exports.error = error;
|
|
49
46
|
/**
|
|
50
47
|
* Parse case argument
|
|
51
48
|
* @param {string} arg
|
|
52
49
|
* @returns { TransformCase | TransformMap }
|
|
53
50
|
*/
|
|
54
|
-
const parseCase = (arg) => {
|
|
51
|
+
export const parseCase = (arg) => {
|
|
55
52
|
if (arg.includes(':')) {
|
|
56
53
|
const tokens = arg.split(':');
|
|
57
54
|
const modelCase = tokens[0].toUpperCase();
|
|
58
55
|
const columnCase = tokens[1].toUpperCase();
|
|
59
56
|
return {
|
|
60
|
-
[
|
|
61
|
-
[
|
|
57
|
+
[TransformTarget.MODEL]: modelCase,
|
|
58
|
+
[TransformTarget.COLUMN]: columnCase
|
|
62
59
|
};
|
|
63
60
|
}
|
|
64
61
|
return arg.toUpperCase();
|
|
65
62
|
};
|
|
66
|
-
exports.parseCase = parseCase;
|
|
67
63
|
/**
|
|
68
64
|
* Parse dialect options from json string
|
|
69
65
|
* @param {string} json
|
|
@@ -89,10 +85,10 @@ const buildDialectOptionsFromFile = (path) => {
|
|
|
89
85
|
let content;
|
|
90
86
|
let parsed;
|
|
91
87
|
try {
|
|
92
|
-
content =
|
|
88
|
+
content = fs.readFileSync(path).toString();
|
|
93
89
|
}
|
|
94
90
|
catch (err) {
|
|
95
|
-
|
|
91
|
+
error(`Argument -f [--dialect-options-file] '${path}' is not a valid path`);
|
|
96
92
|
}
|
|
97
93
|
try {
|
|
98
94
|
parsed = JSON.parse(content);
|
|
@@ -108,153 +104,140 @@ const buildDialectOptionsFromFile = (path) => {
|
|
|
108
104
|
* @param { [key: string]: any } argv
|
|
109
105
|
* Returns {IConfig}
|
|
110
106
|
*/
|
|
111
|
-
const buildConfig = (argv) => {
|
|
112
|
-
var _a;
|
|
107
|
+
export const buildConfig = (argv) => {
|
|
113
108
|
const config = {
|
|
114
109
|
connection: {
|
|
115
|
-
dialect: argv[
|
|
116
|
-
...argv[
|
|
117
|
-
...argv[
|
|
118
|
-
...argv[
|
|
119
|
-
...argv[
|
|
120
|
-
...argv[
|
|
121
|
-
...argv[
|
|
122
|
-
...argv[
|
|
123
|
-
...argv[
|
|
110
|
+
dialect: argv[aliasesMap.DIALECT],
|
|
111
|
+
...argv[aliasesMap.HOST] && { host: argv[aliasesMap.HOST] },
|
|
112
|
+
...argv[aliasesMap.PORT] && { port: argv[aliasesMap.PORT] },
|
|
113
|
+
...argv[aliasesMap.DATABASE] && { database: argv[aliasesMap.DATABASE] },
|
|
114
|
+
...argv[aliasesMap.SCHEMA] && { schema: argv[aliasesMap.SCHEMA] },
|
|
115
|
+
...argv[aliasesMap.USERNAME] && { username: argv[aliasesMap.USERNAME] },
|
|
116
|
+
...argv[aliasesMap.PASSWORD] && { password: argv[aliasesMap.PASSWORD] },
|
|
117
|
+
...argv[aliasesMap.SSL] && { ssl: true },
|
|
118
|
+
...argv[aliasesMap.PROTOCOL] && { protocol: argv[aliasesMap.PROTOCOL] },
|
|
119
|
+
...argv[aliasesMap.DIALECT] === 'mariadb' && { dialectOptions: {
|
|
124
120
|
timezone: 'Etc/GMT-3',
|
|
125
121
|
}
|
|
126
122
|
},
|
|
127
|
-
...argv[
|
|
128
|
-
storage:
|
|
123
|
+
...argv[aliasesMap.DIALECT] === 'sqlite' && {
|
|
124
|
+
storage: argv[aliasesMap.STORAGE] ?? 'memory',
|
|
129
125
|
},
|
|
130
|
-
...argv[
|
|
131
|
-
dialectOptions: buildDialectOptionsFromFile(argv[
|
|
126
|
+
...argv[aliasesMap.DIALECT_OPTIONS_FILE] && {
|
|
127
|
+
dialectOptions: buildDialectOptionsFromFile(argv[aliasesMap.DIALECT_OPTIONS_FILE]),
|
|
132
128
|
},
|
|
133
|
-
...argv[
|
|
134
|
-
dialectOptions: buildDialectOptionsFromString(argv[
|
|
129
|
+
...argv[aliasesMap.DIALECT_OPTIONS] && {
|
|
130
|
+
dialectOptions: buildDialectOptionsFromString(argv[aliasesMap.DIALECT_OPTIONS]),
|
|
135
131
|
},
|
|
136
132
|
logQueryParameters: true,
|
|
137
|
-
logging: argv[
|
|
133
|
+
logging: argv[aliasesMap.ENABLE_SEQUELIZE_LOGS],
|
|
138
134
|
},
|
|
139
135
|
metadata: {
|
|
140
|
-
...argv[
|
|
141
|
-
|
|
142
|
-
tables: argv[exports.aliasesMap.TABLES]
|
|
136
|
+
...argv[aliasesMap.TABLES] && {
|
|
137
|
+
tables: argv[aliasesMap.TABLES]
|
|
143
138
|
.split(',')
|
|
144
139
|
.map(tableName => tableName.toLowerCase())
|
|
145
140
|
},
|
|
146
|
-
...argv[
|
|
147
|
-
skipTables: argv[
|
|
141
|
+
...argv[aliasesMap.SKIP_TABLES] && {
|
|
142
|
+
skipTables: argv[aliasesMap.SKIP_TABLES]
|
|
148
143
|
.split(',')
|
|
149
144
|
.map(tableName => tableName.toLowerCase())
|
|
150
145
|
},
|
|
151
|
-
indices: !!argv[
|
|
152
|
-
timestamps: !!argv[
|
|
153
|
-
|
|
154
|
-
...argv[
|
|
155
|
-
|
|
146
|
+
indices: !!argv[aliasesMap.INDICES],
|
|
147
|
+
timestamps: !!argv[aliasesMap.TIMESTAMPS],
|
|
148
|
+
paranoid: !!argv[aliasesMap.PARANOID],
|
|
149
|
+
...argv[aliasesMap.CASE] && { case: parseCase(argv[aliasesMap.CASE]) },
|
|
150
|
+
...argv[aliasesMap.ASSOCIATIONS_FILE] && { associationsFile: argv[aliasesMap.ASSOCIATIONS_FILE] },
|
|
151
|
+
...(argv[aliasesMap.ASSOCIATIONS] === false && { associations: false }),
|
|
152
|
+
noViews: !!argv[aliasesMap.DISABLE_VIEWS],
|
|
156
153
|
},
|
|
157
154
|
output: {
|
|
158
|
-
outDir: argv[
|
|
159
|
-
|
|
160
|
-
argv[
|
|
161
|
-
:
|
|
162
|
-
:
|
|
163
|
-
clean: !!argv[
|
|
155
|
+
outDir: argv[aliasesMap.OUTPUT_DIR] ?
|
|
156
|
+
path.isAbsolute(argv[aliasesMap.OUTPUT_DIR]) ?
|
|
157
|
+
argv[aliasesMap.OUTPUT_DIR]
|
|
158
|
+
: path.join(process.cwd(), argv[aliasesMap.OUTPUT_DIR])
|
|
159
|
+
: path.join(process.cwd(), defaultOutputDir),
|
|
160
|
+
clean: !!argv[aliasesMap.OUTPUT_DIR_CLEAN],
|
|
164
161
|
},
|
|
165
|
-
strict
|
|
166
|
-
|
|
162
|
+
// yargs exposes -R through the no-strict alias, while --no-strict is parsed
|
|
163
|
+
// as strict: false; honour both spellings.
|
|
164
|
+
strict: !(!!argv[aliasesMap.DISABLE_STRICT] || argv['strict'] === false),
|
|
165
|
+
...isFormat(argv[aliasesMap.FORMAT]) && { format: argv[aliasesMap.FORMAT] },
|
|
166
|
+
...argv[aliasesMap.LINT_FILE] && {
|
|
167
167
|
lintOptions: {
|
|
168
|
-
configFile: argv[
|
|
168
|
+
configFile: argv[aliasesMap.LINT_FILE],
|
|
169
169
|
fix: true,
|
|
170
170
|
}
|
|
171
171
|
},
|
|
172
172
|
};
|
|
173
173
|
return config;
|
|
174
174
|
};
|
|
175
|
-
exports.buildConfig = buildConfig;
|
|
176
175
|
/**
|
|
177
176
|
* Build dialect object from parsed arguments
|
|
178
177
|
* @param { [key: string]: any } argv
|
|
179
178
|
* Returns {Dialect}
|
|
180
179
|
*/
|
|
181
|
-
const buildDialect = (argv) => {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
dialect = new dialects_1.DialectMySQL();
|
|
189
|
-
break;
|
|
190
|
-
case 'mariadb':
|
|
191
|
-
dialect = new dialects_1.DialectMariaDB();
|
|
192
|
-
break;
|
|
193
|
-
case 'sqlite':
|
|
194
|
-
dialect = new dialects_1.DialectSQLite();
|
|
195
|
-
break;
|
|
196
|
-
case 'mssql':
|
|
197
|
-
dialect = new dialects_1.DialectMSSQL();
|
|
198
|
-
break;
|
|
199
|
-
default:
|
|
200
|
-
(0, exports.error)(`Unknown dialect ${argv[exports.aliasesMap.DIALECT]}`);
|
|
180
|
+
export const buildDialect = (argv) => {
|
|
181
|
+
try {
|
|
182
|
+
return createDialect(argv[aliasesMap.DIALECT]);
|
|
183
|
+
}
|
|
184
|
+
catch (err) {
|
|
185
|
+
error(err instanceof Error ? err.message : String(err));
|
|
186
|
+
throw err;
|
|
201
187
|
}
|
|
202
|
-
return dialect;
|
|
203
188
|
};
|
|
204
|
-
exports.buildDialect = buildDialect;
|
|
205
189
|
/**
|
|
206
190
|
* Validate arguments
|
|
207
191
|
* @param { [key: string]: any } argv
|
|
208
192
|
* @returns {void}
|
|
209
193
|
*/
|
|
210
|
-
const validateArgs = (argv) => {
|
|
194
|
+
export const validateArgs = (argv) => {
|
|
211
195
|
// Validate dialect
|
|
212
|
-
if (!
|
|
213
|
-
|
|
196
|
+
if (!Dialect.dialects.has(argv[aliasesMap.DIALECT])) {
|
|
197
|
+
error(`Required argument -D <dialect> must be one of (${Array.from(Dialect.dialects).join(', ')})`);
|
|
214
198
|
}
|
|
215
199
|
// Validate database
|
|
216
|
-
if (argv[
|
|
217
|
-
|
|
200
|
+
if (argv[aliasesMap.DIALECT] !== 'sqlite' && !argv[aliasesMap.DATABASE]) {
|
|
201
|
+
error(`Argument -d [database] is required for dialect ${argv[aliasesMap.DIALECT]}`);
|
|
218
202
|
}
|
|
219
203
|
// Validate port
|
|
220
|
-
if (argv[
|
|
221
|
-
|
|
204
|
+
if (argv[aliasesMap.PORT] && (!Number.isInteger(argv[aliasesMap.PORT]) || argv[aliasesMap.PORT] <= 0)) {
|
|
205
|
+
error(`Argument -p [port] must be a positive integer (${argv[aliasesMap.PORT]})`);
|
|
222
206
|
}
|
|
223
207
|
// Validate case
|
|
224
|
-
if (argv[
|
|
225
|
-
if (argv[
|
|
226
|
-
const tokens = argv[
|
|
208
|
+
if (argv[aliasesMap.CASE]) {
|
|
209
|
+
if (argv[aliasesMap.CASE].includes(':')) {
|
|
210
|
+
const tokens = argv[aliasesMap.CASE].split(':');
|
|
227
211
|
const modelCase = tokens[0].toUpperCase();
|
|
228
212
|
const columnCase = tokens[1].toUpperCase();
|
|
229
|
-
if (!
|
|
230
|
-
|
|
213
|
+
if (!TransformCases.has(modelCase)) {
|
|
214
|
+
error(`Unknown case '${modelCase}': must be one of (${Array.from(TransformCases).join(', ').toLowerCase()})`);
|
|
231
215
|
}
|
|
232
|
-
if (!
|
|
233
|
-
|
|
216
|
+
if (!TransformCases.has(columnCase)) {
|
|
217
|
+
error(`Unknown case '${columnCase}': must be one of (${Array.from(TransformCases).join(', ').toLowerCase()})`);
|
|
234
218
|
}
|
|
235
219
|
}
|
|
236
|
-
else if (!
|
|
237
|
-
|
|
220
|
+
else if (!TransformCases.has(argv[aliasesMap.CASE].toUpperCase())) {
|
|
221
|
+
error(`Argument -c [case] must be one of (${Array.from(TransformCases).join(', ').toLowerCase()})`);
|
|
238
222
|
}
|
|
239
223
|
}
|
|
240
224
|
// Validate lint file
|
|
241
|
-
if (argv[
|
|
225
|
+
if (argv[aliasesMap.LINT_FILE]) {
|
|
242
226
|
try {
|
|
243
|
-
|
|
227
|
+
fs.accessSync(argv[aliasesMap.LINT_FILE]);
|
|
244
228
|
}
|
|
245
229
|
catch (err) {
|
|
246
|
-
|
|
230
|
+
error(`Argument -L [lint-file] '${argv[aliasesMap.LINT_FILE]}' is not a valid path`);
|
|
247
231
|
}
|
|
248
232
|
}
|
|
249
233
|
// Validate associations file
|
|
250
|
-
if (argv[
|
|
234
|
+
if (argv[aliasesMap.ASSOCIATIONS_FILE]) {
|
|
251
235
|
try {
|
|
252
|
-
|
|
236
|
+
fs.accessSync(argv[aliasesMap.ASSOCIATIONS_FILE]);
|
|
253
237
|
}
|
|
254
238
|
catch (err) {
|
|
255
|
-
|
|
239
|
+
error(`Argument -a [associations-file] '${argv[aliasesMap.ASSOCIATIONS_FILE]}' is not a valid path`);
|
|
256
240
|
}
|
|
257
241
|
}
|
|
258
242
|
// TODO Validate schema if dialect is postgres ?
|
|
259
243
|
};
|
|
260
|
-
exports.validateArgs = validateArgs;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Builder = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* @class Builder
|
|
6
3
|
* @constructor
|
|
7
4
|
* @param {IConfig} config
|
|
8
5
|
* @param {Dialect} dialect
|
|
9
6
|
*/
|
|
10
|
-
class Builder {
|
|
7
|
+
export class Builder {
|
|
8
|
+
_config;
|
|
9
|
+
_dialect;
|
|
11
10
|
constructor(config, dialect) {
|
|
12
11
|
this._config = config;
|
|
13
12
|
this._dialect = dialect;
|
|
@@ -29,4 +28,3 @@ class Builder {
|
|
|
29
28
|
this._dialect = value;
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
|
-
exports.Builder = Builder;
|
|
@@ -1,6 +1,38 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import type { BelongsToOptions, HasManyOptions, HasOneOptions } from 'sequelize';
|
|
3
|
+
import type { TableOptions } from 'sequelize-typescript';
|
|
4
|
+
import { IConfig } from '../config/index.js';
|
|
5
|
+
import { ITableMetadata, Dialect } from '../dialects/Dialect.js';
|
|
6
|
+
import { IAssociationMetadata } from '../dialects/AssociationsParser.js';
|
|
7
|
+
import { Builder } from './Builder.js';
|
|
8
|
+
export { resolveAssociationPropertyName } from './associationNaming.js';
|
|
9
|
+
/**
|
|
10
|
+
* Build the `@Table` decorator options for a table. The `hasTrigger` flag is
|
|
11
|
+
* emitted only when the source table carries at least one enabled trigger.
|
|
12
|
+
* @param {ITableMetadata} tableMetadata
|
|
13
|
+
* @returns {Partial<TableOptions>}
|
|
14
|
+
*/
|
|
15
|
+
export declare const buildTableDecoratorProps: (tableMetadata: ITableMetadata) => Partial<TableOptions>;
|
|
16
|
+
/**
|
|
17
|
+
* Decorator options accepted by `@BelongsTo`, `@HasOne` and `@HasMany`.
|
|
18
|
+
*/
|
|
19
|
+
export type AssociationDecoratorOptions = Partial<BelongsToOptions & HasManyOptions & HasOneOptions>;
|
|
20
|
+
/**
|
|
21
|
+
* Build the decorator options for `@BelongsTo`, `@HasOne` and `@HasMany`. Keys
|
|
22
|
+
* are emitted in the fixed order as, foreignKey, targetKey, sourceKey, onDelete,
|
|
23
|
+
* onUpdate, and `as` is emitted only when the association carries an alias.
|
|
24
|
+
* Returns undefined when no option applies so the decorator is rendered without
|
|
25
|
+
* an options argument.
|
|
26
|
+
* @param {IAssociationMetadata} association
|
|
27
|
+
* @returns {AssociationDecoratorOptions | undefined}
|
|
28
|
+
*/
|
|
29
|
+
export declare const buildAssociationDecoratorProps: (association: IAssociationMetadata) => AssociationDecoratorOptions | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Build the association class member for a model.
|
|
32
|
+
* @param {IAssociationMetadata} association
|
|
33
|
+
* @returns {ts.PropertyDeclaration}
|
|
34
|
+
*/
|
|
35
|
+
export declare const buildAssociationPropertyDecl: (association: IAssociationMetadata) => ts.PropertyDeclaration;
|
|
4
36
|
/**
|
|
5
37
|
* @class ModelGenerator
|
|
6
38
|
* @constructor
|
|
@@ -14,11 +46,6 @@ export declare class ModelBuilder extends Builder {
|
|
|
14
46
|
* @param {Dialect} dialect
|
|
15
47
|
*/
|
|
16
48
|
private static buildColumnPropertyDecl;
|
|
17
|
-
/**
|
|
18
|
-
* Build association class member
|
|
19
|
-
* @param {IAssociationMetadata} association
|
|
20
|
-
*/
|
|
21
|
-
private static buildAssociationPropertyDecl;
|
|
22
49
|
/**
|
|
23
50
|
* Build table class declaration
|
|
24
51
|
* @param {ITableMetadata} tableMetadata
|
|
@@ -32,6 +59,14 @@ export declare class ModelBuilder extends Builder {
|
|
|
32
59
|
* @returns {string}
|
|
33
60
|
*/
|
|
34
61
|
private static buildIndexExports;
|
|
62
|
+
/**
|
|
63
|
+
* Render the decorators output as one file per table plus the index barrel.
|
|
64
|
+
* @param {ITablesMetadata} tablesMetadata
|
|
65
|
+
* @param {Dialect} dialect
|
|
66
|
+
* @param {boolean | undefined} strict
|
|
67
|
+
* @returns {IGeneratedFile[]}
|
|
68
|
+
*/
|
|
69
|
+
private static renderDecoratorsFiles;
|
|
35
70
|
/**
|
|
36
71
|
* Build models files using the given configuration and dialect
|
|
37
72
|
* @returns {Promise<void>}
|