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.
Files changed (83) hide show
  1. package/README.md +832 -204
  2. package/build/bin/cli.js +65 -47
  3. package/build/bin/utils.d.ts +5 -2
  4. package/build/bin/utils.js +87 -104
  5. package/build/builders/Builder.d.ts +2 -2
  6. package/build/builders/Builder.js +3 -5
  7. package/build/builders/ModelBuilder.d.ts +43 -8
  8. package/build/builders/ModelBuilder.js +120 -122
  9. package/build/builders/NativeModelRenderer.d.ts +98 -0
  10. package/build/builders/NativeModelRenderer.js +353 -0
  11. package/build/builders/associationNaming.d.ts +9 -0
  12. package/build/builders/associationNaming.js +15 -0
  13. package/build/builders/decoratorsDependency.d.ts +27 -0
  14. package/build/builders/decoratorsDependency.js +45 -0
  15. package/build/builders/defaultValues.d.ts +40 -0
  16. package/build/builders/defaultValues.js +56 -0
  17. package/build/builders/generatedFile.d.ts +23 -0
  18. package/build/builders/generatedFile.js +31 -0
  19. package/build/builders/index.d.ts +1 -1
  20. package/build/builders/index.js +1 -5
  21. package/build/builders/nativeAssociations.d.ts +109 -0
  22. package/build/builders/nativeAssociations.js +308 -0
  23. package/build/builders/nativeAttributes.d.ts +96 -0
  24. package/build/builders/nativeAttributes.js +174 -0
  25. package/build/builders/nativeWiring.d.ts +51 -0
  26. package/build/builders/nativeWiring.js +108 -0
  27. package/build/builders/utils.d.ts +75 -0
  28. package/build/builders/utils.js +142 -48
  29. package/build/config/IConfig.d.ts +11 -2
  30. package/build/config/IConfig.js +4 -6
  31. package/build/config/format.d.ts +27 -0
  32. package/build/config/format.js +26 -0
  33. package/build/config/index.d.ts +1 -1
  34. package/build/config/index.js +1 -2
  35. package/build/connection/createConnection.d.ts +8 -1
  36. package/build/connection/createConnection.js +30 -7
  37. package/build/connection/index.d.ts +1 -1
  38. package/build/connection/index.js +1 -17
  39. package/build/dialects/AssociationsParser.d.ts +10 -2
  40. package/build/dialects/AssociationsParser.js +16 -19
  41. package/build/dialects/Dialect.d.ts +52 -9
  42. package/build/dialects/Dialect.js +93 -57
  43. package/build/dialects/DialectMSSQL.d.ts +22 -3
  44. package/build/dialects/DialectMSSQL.js +169 -75
  45. package/build/dialects/DialectMariaDB.d.ts +13 -3
  46. package/build/dialects/DialectMariaDB.js +98 -79
  47. package/build/dialects/DialectMySQL.d.ts +13 -3
  48. package/build/dialects/DialectMySQL.js +102 -84
  49. package/build/dialects/DialectPostgres.d.ts +14 -3
  50. package/build/dialects/DialectPostgres.js +129 -75
  51. package/build/dialects/DialectSQLite.d.ts +41 -3
  52. package/build/dialects/DialectSQLite.js +166 -24
  53. package/build/dialects/associationDiscovery.d.ts +64 -0
  54. package/build/dialects/associationDiscovery.js +275 -0
  55. package/build/dialects/associationsFileMerge.d.ts +21 -0
  56. package/build/dialects/associationsFileMerge.js +107 -0
  57. package/build/dialects/createDialect.d.ts +8 -0
  58. package/build/dialects/createDialect.js +24 -0
  59. package/build/dialects/dataTypes.d.ts +58 -0
  60. package/build/dialects/dataTypes.js +108 -0
  61. package/build/dialects/foreignKeys.d.ts +115 -0
  62. package/build/dialects/foreignKeys.js +198 -0
  63. package/build/dialects/index.d.ts +5 -5
  64. package/build/dialects/index.js +5 -13
  65. package/build/dialects/paranoid.d.ts +24 -0
  66. package/build/dialects/paranoid.js +29 -0
  67. package/build/dialects/utils.d.ts +2 -8
  68. package/build/dialects/utils.js +61 -58
  69. package/build/index.d.ts +11 -3
  70. package/build/index.js +5 -19
  71. package/build/lint/Linter.d.ts +14 -3
  72. package/build/lint/Linter.js +114 -14
  73. package/build/lint/eslintDefaultConfig.d.ts +2 -2
  74. package/build/lint/eslintDefaultConfig.js +28 -23
  75. package/build/lint/index.d.ts +1 -1
  76. package/build/lint/index.js +1 -5
  77. package/build/utils/errors.d.ts +6 -0
  78. package/build/utils/errors.js +6 -0
  79. package/package.json +48 -28
  80. package/.github/FUNDING.yml +0 -1
  81. package/.github/workflows/ci.yml +0 -199
  82. package/CHANGELOG.md +0 -30
  83. package/tsconfig.json +0 -21
package/README.md CHANGED
@@ -1,21 +1,32 @@
1
1
  # sequelize-typescript-generator
2
- > Automatically generates typescript models compatible with [sequelize-typescript](https://www.npmjs.com/package/sequelize-typescript) library directly from your source database.
2
+ > Generates TypeScript [Sequelize](https://www.npmjs.com/package/sequelize) models directly from an existing database. Emits framework-free native models by default, with [`sequelize-typescript`](https://www.npmjs.com/package/sequelize-typescript) decorated classes available as an opt-in format.
3
+
4
+ > Upgrading from 12.x? See the [migration guide](docs/migration/12-to-13.md).
3
5
 
4
6
  ## Table of Contents
5
7
 
6
8
  <!-- toc -->
7
9
 
8
- * [Supported databases](#supported-databases)
10
+ * [Tested databases](#tested-databases)
9
11
  * [Prerequisites](#prerequisites)
10
12
  * [Installation](#installation)
11
13
  * [CLI usage](#cli-usage)
14
+ * [Output formats](#output-formats)
15
+ * [Native format](#native-format)
16
+ * [Decorators format](#decorators-format)
12
17
  * [Programmatic usage](#programmatic-usage)
13
18
  * [Strict mode](#strict-mode)
14
19
  * [Transform case](#transform-case)
15
20
  * [Associations](#associations)
16
- * [One to One](#one-to-one)
17
- * [One to Many](#one-to-many)
18
- * [Many to Many](#many-to-many)
21
+ * [Cardinality](#cardinality)
22
+ * [Skipped foreign keys](#skipped-foreign-keys)
23
+ * [Alias rule](#alias-rule)
24
+ * [Generated associations](#generated-associations)
25
+ * [Disabling discovery](#disabling-discovery)
26
+ * [Associations file](#associations-file)
27
+ * [One to One](#one-to-one)
28
+ * [One to Many](#one-to-many)
29
+ * [Many to Many](#many-to-many)
19
30
  * [Lint](#lint)
20
31
 
21
32
  <!-- toc stop -->
@@ -24,26 +35,30 @@
24
35
 
25
36
  This library is tested on the following databases:
26
37
 
27
- - Postgres (11, 14, 16)
28
- - Mysql (5, 8)
29
- - MariaDB (10, 11)
30
- - SQL Server (2019, 2022)
38
+ - Postgres (16, 18)
39
+ - Mysql (8, 9)
40
+ - MariaDB (11, 12)
41
+ - SQL Server (2022, 2025)
31
42
  - SQLite (3)
32
43
 
33
44
  ## Prerequisites
34
45
  See [sequelize-typescript installation](https://www.npmjs.com/package/sequelize-typescript#installation).
35
46
 
36
47
  You should also install the specific driver library for your database, see
37
- [sequelize documentation](https://sequelize.org/v5/manual/getting-started.html):
48
+ [sequelize documentation](https://sequelize.org/docs/v6/getting-started/):
38
49
  ```shell
39
50
  npm install -S pg pg-hstore # Postgres
40
51
  npm install -S mysql2 # MySQL
41
52
  npm install -S mariadb # MariaDB
42
- npm install -S sqlite3 # SQLite
53
+ npm install -S @vscode/sqlite3 # SQLite
43
54
  npm install -S tedious # Microsoft SQL Server
44
55
  ```
45
56
 
46
57
  ## Installation
58
+ This package is ESM-only and requires Node `>=22.13`. CommonJS consumers can load it through
59
+ `require()` of the ESM entry. SQLite support requires the `@vscode/sqlite3` driver installed in
60
+ your project (see [Prerequisites](#prerequisites)).
61
+
47
62
  Local install
48
63
  ```shell
49
64
  npm install -S sequelize-typescript-generator
@@ -82,7 +97,7 @@ stg --help
82
97
  Usage: stg -D <dialect> -d [database] -u [username] -x [password] -h [host] -p
83
98
  [port] -o [out-dir] -s [schema] -a [associations-file]-t [tables] -T
84
99
  [skip-tables] -i [indices] -C [case] -S [storage] -L [lint-file] -l [ssl] -r
85
- [protocol] -c [clean]
100
+ [protocol] -c [clean] -F [format] --no-associations
86
101
 
87
102
  Options:
88
103
  --help Show help [boolean]
@@ -93,7 +108,7 @@ Options:
93
108
  - Postgres: 5432
94
109
  - MSSQL: 1433 [number]
95
110
  -d, --database Database name [string]
96
- -s, --schema Schema name (Postgres only). Default:
111
+ -s, --schema Schema name (Postgres and SQL Server). Default:
97
112
  - public [string]
98
113
  -D, --dialect Dialect:
99
114
  - postgres
@@ -111,6 +126,9 @@ Options:
111
126
  - output-models [string]
112
127
  -c, --clean Clean output directory before running [boolean]
113
128
  -m, --timestamps Add default timestamps to tables [boolean]
129
+ -P, --paranoid Emit paranoid table options for tables
130
+ with a deleted_at or deletedAt column.
131
+ Requires --timestamps. [boolean]
114
132
  -C, --case Transform tables and fields names
115
133
  with one of the following cases:
116
134
  - underscore
@@ -136,10 +154,22 @@ Options:
136
154
  [string]
137
155
  -f, --dialect-options-file Dialect native options passed as json file path.
138
156
  [string]
139
- -R, --no-strict Disable strict typescript class declaration.
140
- [boolean]
157
+ -F, --format Output format:
158
+ - native: plain Sequelize classes with declare
159
+ fields, Model.init and an initModels wiring
160
+ file (default)
161
+ - decorators: sequelize-typescript decorators
162
+ (requires sequelize-typescript in the target
163
+ project)
164
+ [string] [choices: "native",
165
+ "decorators"] [default: "native"]
166
+ -R, --no-strict Disable strict typescript class declaration
167
+ (decorators format only). [boolean]
141
168
  -V, --no-views Disable view generation. Available for: MySQL and MariaDB.
142
- [boolean]
169
+ [boolean]
170
+ --associations Discover one-to-one and one-to-many associations
171
+ from foreign keys. Use --no-associations to
172
+ disable. [boolean] [default: true]
143
173
  ```
144
174
 
145
175
  Local usage example:
@@ -152,11 +182,148 @@ Global usage example:
152
182
  stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -x myPassword --indices --dialect-options-file path/to/dialectOptions.json --case camel --out-dir models --clean
153
183
  ```
154
184
 
185
+ ## Output formats
186
+ The generator emits models in one of two formats, selected with `-F` / `--format` (or the `format`
187
+ option in [programmatic usage](#programmatic-usage)):
188
+
189
+ - `native` (default): framework-free [`sequelize`](https://www.npmjs.com/package/sequelize) models.
190
+ - `decorators`: [`sequelize-typescript`](https://www.npmjs.com/package/sequelize-typescript) decorated classes.
191
+
192
+ `native` is the default. Pass `--format decorators` (or `format: 'decorators'` programmatically) to emit
193
+ decorated classes instead.
194
+
195
+ ### Native format
196
+ Each model is a plain `sequelize` class. Attributes are typed with `InferAttributes` and
197
+ `InferCreationAttributes`, `declare`d rather than assigned, and wrapped with `CreationOptional`,
198
+ `ForeignKey` and `NonAttribute` where appropriate. Every model exposes a `static initModel(sequelize)`
199
+ method instead of decorator metadata:
200
+
201
+ ```ts
202
+ import {
203
+ Association, BelongsToCreateAssociationMixin, BelongsToGetAssociationMixin, BelongsToSetAssociationMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize
204
+ } from "sequelize";
205
+ import type { races } from "./races";
206
+
207
+ export class units extends Model<InferAttributes<units>, InferCreationAttributes<units>> {
208
+
209
+ declare unit_id: CreationOptional<number | null>;
210
+
211
+ declare unit_name: string;
212
+
213
+ declare race_id: ForeignKey<races["race_id"]>;
214
+
215
+ declare getRace: BelongsToGetAssociationMixin<races>;
216
+
217
+ declare setRace: BelongsToSetAssociationMixin<races, races["race_id"]>;
218
+
219
+ declare createRace: BelongsToCreateAssociationMixin<races>;
220
+
221
+ declare race?: NonAttribute<races>;
222
+
223
+ declare static associations: {
224
+ race: Association<units, races>;
225
+ };
226
+
227
+ static initModel(sequelize: Sequelize): typeof units {
228
+ units.init({
229
+ unit_id: {
230
+ type: DataTypes.INTEGER,
231
+ primaryKey: true,
232
+ autoIncrement: true
233
+ },
234
+ unit_name: {
235
+ type: DataTypes.STRING,
236
+ allowNull: false
237
+ },
238
+ race_id: {
239
+ type: DataTypes.INTEGER,
240
+ allowNull: false
241
+ }
242
+ }, {
243
+ sequelize,
244
+ tableName: "units",
245
+ freezeTableName: true,
246
+ timestamps: false
247
+ });
248
+ return units;
249
+ }
250
+
251
+ }
252
+ ```
253
+
254
+ Alongside the model files the generator emits an `initModels.ts` wiring file and an `index.ts` barrel.
255
+ The wiring file calls every model's `initModel` and declares all associations, and exports a `Models`
256
+ type derived from its return value:
257
+
258
+ ```ts
259
+ import { Sequelize } from "sequelize";
260
+ import { races } from "./races";
261
+ import { units } from "./units";
262
+ // ... other model imports
263
+
264
+ export function initModels(sequelize: Sequelize) {
265
+ races.initModel(sequelize);
266
+ units.initModel(sequelize);
267
+ // ... other initModel calls
268
+ races.hasMany(units, {
269
+ as: "units",
270
+ foreignKey: "race_id",
271
+ sourceKey: "race_id"
272
+ });
273
+ units.belongsTo(races, {
274
+ as: "race",
275
+ foreignKey: "race_id",
276
+ targetKey: "race_id"
277
+ });
278
+ // ... other associations
279
+ return {
280
+ races,
281
+ units
282
+ // ... other models
283
+ };
284
+ }
285
+
286
+ export type Models = ReturnType<typeof initModels>;
287
+ ```
288
+
289
+ Wire the models against a `Sequelize` instance and use them straight away:
290
+
291
+ ```ts
292
+ import { Sequelize } from "sequelize";
293
+ import { initModels } from "./models/initModels";
294
+
295
+ const sequelize = new Sequelize("myDatabase", "myUsername", "myPassword", { dialect: "mysql" });
296
+
297
+ const models = initModels(sequelize);
298
+
299
+ const units = await models.units.findAll({ include: models.races });
300
+ ```
301
+
302
+ `initModels`, the `Models` type and the `static initModel` methods belong to the native format. Native
303
+ models depend on `sequelize` alone; `sequelize-typescript` is not required.
304
+
305
+ ### Decorators format
306
+ Pass `--format decorators` to emit `sequelize-typescript` decorated classes (the output shown in the
307
+ [Associations](#associations) examples below). This format requires `sequelize-typescript` to be
308
+ installed in the target project; if it cannot be resolved from the output directory a warning is
309
+ printed (the generator never throws for this). Register the generated models with `sequelize-typescript`
310
+ rather than a wiring file:
311
+
312
+ ```ts
313
+ import { Sequelize } from "sequelize-typescript";
314
+ import { units } from "./models/units";
315
+ import { races } from "./models/races";
316
+
317
+ const sequelize = new Sequelize("myDatabase", "myUsername", "myPassword", { dialect: "mysql" });
318
+
319
+ sequelize.addModels([units, races]);
320
+ ```
321
+
155
322
  ## Programmatic usage
156
323
  You can use the library programmatically, as shown in the following example:
157
324
 
158
325
  ```ts
159
- import { IConfig, ModelBuilder, DialectMySQL } from 'sequelize-typescript-generator';
326
+ import { IConfig, ModelBuilder, createDialect } from 'sequelize-typescript-generator';
160
327
 
161
328
  (async () => {
162
329
  const config: IConfig = {
@@ -174,10 +341,10 @@ import { IConfig, ModelBuilder, DialectMySQL } from 'sequelize-typescript-genera
174
341
  clean: true,
175
342
  outDir: 'models'
176
343
  },
177
- strict: true,
344
+ format: 'native',
178
345
  };
179
346
 
180
- const dialect = new DialectMySQL();
347
+ const dialect = createDialect('mysql');
181
348
 
182
349
  const builder = new ModelBuilder(config, dialect);
183
350
 
@@ -187,11 +354,17 @@ import { IConfig, ModelBuilder, DialectMySQL } from 'sequelize-typescript-genera
187
354
  catch(err) {
188
355
  console.error(err);
189
356
  process.exit(1);
190
- }
357
+ }
191
358
  })();
192
359
  ```
193
360
 
361
+ The `format` option accepts `'native'` (default) or `'decorators'` and mirrors the `--format` CLI flag;
362
+ omit it to get the native format. See [Output formats](#output-formats) for the difference between them.
363
+
194
364
  ## Strict mode
365
+ Strict mode applies to the [decorators format](#decorators-format). It controls whether the generated
366
+ decorated classes declare an explicit attributes interface and implement it on the model class.
367
+
195
368
  By default strict mode will be used for models class declaration:
196
369
 
197
370
  `STRICT ENABLED`
@@ -365,24 +538,208 @@ const config: IConfig = {
365
538
  NB: please note that currently case transformation is not supported for non ASCII strings.
366
539
 
367
540
  ## Associations
368
- Including associations in the generated models requires a bit of manual work unfortunately, but hopefully
369
- it will buy you some time instead of defining them from scratch.
370
-
371
- First you have to define a csv-like text file, let's call it `associations.csv` (but you can call it however you want). In this file you have to put an entry for each association you want to define.
372
- The following associations are supported:
541
+ Associations are discovered automatically from the foreign keys found in your schema, so in most cases you get
542
+ one-to-one and one-to-many relations in the generated models without any extra configuration. Discovery is on by
543
+ default; disable it with `--no-associations`. You can still declare associations explicitly in an
544
+ [associations file](#associations-file), which is applied on top of whatever was discovered.
545
+
546
+ ### Cardinality
547
+ Each foreign key produces a `BelongsTo` on the table that holds the foreign key and a matching relation on the
548
+ referenced table:
549
+
550
+ - **One to one** when the foreign key column is the single-column primary key of its table, or is covered by a
551
+ single-column unique constraint or unique index. The referenced table gets a `HasOne`.
552
+ - **One to many** in every other case. The referenced table gets a `HasMany`.
553
+
554
+ Many-to-many is never inferred. Junction tables are generated as plain models, and you declare the
555
+ many-to-many relation yourself in the [associations file](#associations-file).
556
+
557
+ ### Skipped foreign keys
558
+ Some foreign keys cannot be turned into a Sequelize association. In these cases the column is generated as a
559
+ plain attribute and a single warning is printed:
560
+
561
+ - **Composite foreign keys** that span more than one column. Sequelize associations need a single column.
562
+ - **Cross-schema foreign keys** whose referenced table lives in another schema.
563
+ - **Foreign keys to a table that is not generated**, for example one excluded with `--skip-tables` or by a
564
+ `--tables` filter.
565
+
566
+ ### Alias rule
567
+ Every association is exposed under an alias, used as the class property name and in `include` queries.
568
+
569
+ When a single foreign key links the two tables (unambiguous):
570
+
571
+ - the `BelongsTo` side uses the singular of the target model;
572
+ - the reverse side uses the plural of the source model, or its singular for a one-to-one relation.
573
+
574
+ For example `units.race_id → races` gives `units.race` and `races.units`, and the one-to-one
575
+ `profiles.person_id → person` gives `profiles.person` and `person.profile`.
576
+
577
+ When two foreign keys point at the same table, or a table references itself (ambiguous), the alias is derived
578
+ from the foreign key column instead. The `_id`, `_fk`, `Id` and `Fk` suffixes are stripped for the `BelongsTo`
579
+ side, and the reverse side is the camelCased composition of that stem and the source model name. For example the
580
+ self-referencing `employees.manager_id → employees` gives `employees.manager` and `employees.managerEmployees`,
581
+ and a `books` table with `author_id` and `editor_id` both referencing `authors` gives `books.author`,
582
+ `books.editor`, `authors.authorBooks` and `authors.editorBooks`. When the column has no strippable suffix the
583
+ alias falls back to the target model combined with the column name. Aliases that would collide with an existing
584
+ field or another alias on the same model receive a numeric suffix.
585
+
586
+ Aliases follow `--case`: with a case set they are transformed like column names. Without `--case`, singular and
587
+ plural aliases keep the database spelling and only composed aliases are camelCased.
588
+
589
+ The alias surfaces as the `as` option on the association and as the model property that exposes the related
590
+ record. For the one-to-one `profiles.person_id → person` relation (giving `profiles.person` and `person.profile`):
591
+
592
+ **Native** — the alias is the `as` option in `initModels.ts` and the name of the `NonAttribute` property on each
593
+ model:
594
+
595
+ ```ts
596
+ person.hasOne(profiles, {
597
+ as: "profile",
598
+ foreignKey: "person_id",
599
+ sourceKey: "person_id",
600
+ onDelete: "CASCADE",
601
+ onUpdate: "CASCADE"
602
+ });
603
+ profiles.belongsTo(person, {
604
+ as: "person",
605
+ foreignKey: "person_id",
606
+ targetKey: "person_id",
607
+ onDelete: "CASCADE",
608
+ onUpdate: "CASCADE"
609
+ });
610
+ ```
611
+
612
+ **Decorators** — the alias is the `as` option on the decorator and the name of the decorated property:
613
+
614
+ ```ts
615
+ // person.ts
616
+ @HasOne(() => profiles, {
617
+ as: "profile",
618
+ foreignKey: "person_id",
619
+ sourceKey: "person_id",
620
+ onDelete: "CASCADE",
621
+ onUpdate: "CASCADE"
622
+ })
623
+ profile?: profiles;
624
+ ```
625
+
626
+ ```ts
627
+ // profiles.ts
628
+ @BelongsTo(() => person, {
629
+ as: "person",
630
+ foreignKey: "person_id",
631
+ targetKey: "person_id",
632
+ onDelete: "CASCADE",
633
+ onUpdate: "CASCADE"
634
+ })
635
+ person?: person;
636
+ ```
637
+
638
+ ### Generated associations
639
+ For each discovered foreign key the generator wires both sides of the relation. Each side carries the resolved
640
+ `as` alias, the `foreignKey`, and the referenced column as `targetKey` (owning side) or `sourceKey` (inverse
641
+ side); `onDelete`/`onUpdate` are added whenever the referential action is not `NO ACTION`.
642
+
643
+ **Native** — the foreign key column is typed with `ForeignKey<...>`, each side exposes a typed `NonAttribute`
644
+ property, and the relation is wired in `initModels.ts`:
645
+
646
+ ```ts
647
+ // units.ts
648
+ declare race_id: ForeignKey<races["race_id"]>;
649
+ declare race?: NonAttribute<races>;
650
+ // races.ts
651
+ declare units?: NonAttribute<units[]>;
652
+ ```
653
+
654
+ ```ts
655
+ // initModels.ts
656
+ races.hasMany(units, {
657
+ as: "units",
658
+ foreignKey: "race_id",
659
+ sourceKey: "race_id"
660
+ });
661
+ units.belongsTo(races, {
662
+ as: "race",
663
+ foreignKey: "race_id",
664
+ targetKey: "race_id"
665
+ });
666
+ ```
667
+
668
+ **Decorators** — the foreign key column keeps its `@ForeignKey(() => Target)` decorator, and the association is
669
+ emitted with `@BelongsTo`, `@HasOne` or `@HasMany`:
670
+
671
+ ```ts
672
+ // units.ts
673
+ @ForeignKey(() => races)
674
+ @Column({
675
+ type: DataType.INTEGER
676
+ })
677
+ race_id!: number;
678
+
679
+ @BelongsTo(() => races)
680
+ race?: races;
681
+ ```
682
+
683
+ ```ts
684
+ // races.ts
685
+ @HasMany(() => units, {
686
+ sourceKey: "race_id"
687
+ })
688
+ units?: units[];
689
+ ```
690
+
691
+ ### Disabling discovery
692
+ Pass `--no-associations` to skip discovery entirely. Foreign key columns still get their `@ForeignKey`
693
+ decorator, and an [associations file](#associations-file) is still applied if provided.
694
+
695
+ ```shell
696
+ npx stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -x myPassword --no-associations --out-dir models --clean
697
+ ```
698
+
699
+ Programmatically, set `associations: false` in the metadata:
700
+
701
+ ```ts
702
+ const config: IConfig = {
703
+ connection: {
704
+ dialect: 'mysql',
705
+ database: 'myDatabase',
706
+ username: 'myUsername',
707
+ password: 'myPassword'
708
+ },
709
+ metadata: {
710
+ indices: true,
711
+ associations: false, // Disable automatic association discovery
712
+ },
713
+ output: {
714
+ clean: true,
715
+ outDir: 'models'
716
+ },
717
+ };
718
+ ```
719
+
720
+ ### Associations file
721
+ You can declare associations explicitly in a csv-like text file, let's call it `associations.csv` (but you can
722
+ call it however you want). Put an entry for each association you want to define. The following associations are
723
+ supported:
373
724
 
374
725
  - `1:1`
375
726
  - `1:N`
376
727
  - `N:N`
377
728
 
378
- Some rules for the association file:
729
+ Some rules for the associations file:
379
730
 
380
731
  - Names of tables and columns in the associations file must be the native names on the database, not the
381
732
  transformed names generated when using a custom case transformation with the flag `--case`.
382
733
  - Only `,` separator is supported.
383
734
  - Do not use enclosing quotes.
384
735
 
385
- Note that fields generated by associations will be pluralized or singularized based on cardinality.
736
+ Note that fields generated by associations file entries will be pluralized or singularized based on cardinality.
737
+
738
+ The associations file is applied on top of the discovered associations. A row that names the same source table
739
+ and foreign key column replaces the association discovered for that column; any other discovered associations
740
+ are kept. If a file entry ends up with the same alias as a discovered association on the same model, the
741
+ discovered one is dropped in favour of the file entry and a warning is printed. Use this to override discovery
742
+ where you need a different shape, and to add the many-to-many relations that are never inferred.
386
743
 
387
744
  #### One to One
388
745
  In the associations file include an entry with the following structure:
@@ -434,7 +791,7 @@ stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -x myPassword --as
434
791
  Or programmatically:
435
792
 
436
793
  ```ts
437
- import { IConfig, ModelBuilder, DialectMySQL } from 'sequelize-typescript-generator';
794
+ import { IConfig, ModelBuilder, createDialect } from 'sequelize-typescript-generator';
438
795
 
439
796
  (async () => {
440
797
  const config: IConfig = {
@@ -454,7 +811,7 @@ import { IConfig, ModelBuilder, DialectMySQL } from 'sequelize-typescript-genera
454
811
  }
455
812
  };
456
813
 
457
- const dialect = new DialectMySQL();
814
+ const dialect = createDialect('mysql');
458
815
 
459
816
  const builder = new ModelBuilder(config, dialect);
460
817
 
@@ -464,85 +821,153 @@ import { IConfig, ModelBuilder, DialectMySQL } from 'sequelize-typescript-genera
464
821
  catch(err) {
465
822
  console.error(err);
466
823
  process.exit(1);
467
- }
824
+ }
468
825
  })();
469
826
  ```
470
827
 
471
- This will generate the following models:
828
+ This will generate the following models.
829
+
830
+ **Native** — `person` gets a `HasOne` and `passport` gets the `BelongsTo`, wired in `initModels.ts`
831
+ (association mixins and the `static initModel` body are trimmed for brevity):
832
+
833
+ ```ts
834
+ // person.ts
835
+ export class person extends Model<InferAttributes<person>, InferCreationAttributes<person>> {
836
+
837
+ declare person_id: CreationOptional<number | null>;
838
+
839
+ declare name: string;
840
+
841
+ declare passport_id: number;
842
+
843
+ declare passport?: NonAttribute<passport>;
844
+
845
+ declare static associations: {
846
+ passport: Association<person, passport>;
847
+ };
848
+
849
+ // static initModel(sequelize) ...
850
+ }
851
+ ```
852
+
853
+ ```ts
854
+ // passport.ts
855
+ export class passport extends Model<InferAttributes<passport>, InferCreationAttributes<passport>> {
856
+
857
+ declare passport_id: ForeignKey<person["person_id"] | null>;
858
+
859
+ declare code: string;
860
+
861
+ declare person?: NonAttribute<person>;
862
+
863
+ declare static associations: {
864
+ person: Association<passport, person>;
865
+ };
866
+
867
+ // static initModel(sequelize) ...
868
+ }
869
+ ```
870
+
871
+ ```ts
872
+ // initModels.ts
873
+ person.hasOne(passport, {
874
+ as: "passport",
875
+ foreignKey: "passport_id",
876
+ sourceKey: "passport_id"
877
+ });
878
+ passport.belongsTo(person, {
879
+ as: "person",
880
+ foreignKey: "passport_id"
881
+ });
882
+ ```
883
+
884
+ **Decorators**:
472
885
 
473
886
  ```ts
474
887
  import {
475
- Model, Table, Column, DataType, Index, Sequelize, ForeignKey, HasOne
888
+ Model, Table, Column, DataType, Index, Sequelize, ForeignKey, HasOne
476
889
  } from "sequelize-typescript";
477
890
  import { passport } from "./passport";
478
891
 
479
892
  export interface personAttributes {
480
- person_id: number;
481
- name: string;
482
- passport_id: number;
893
+ person_id?: number;
894
+ name: string;
895
+ passport_id: number;
483
896
  }
484
897
 
485
898
  @Table({
486
- tableName: "person",
487
- timestamps: false
899
+ tableName: "person",
900
+ timestamps: false
488
901
  })
489
902
  export class person extends Model<personAttributes, personAttributes> implements personAttributes {
490
903
 
491
- @Column({
492
- primaryKey: true,
493
- type: DataType.INTEGER
494
- })
495
- person_id!: number;
496
-
497
- @Column({
498
- type: DataType.STRING(80)
499
- })
500
- name!: string;
501
-
502
- @Column({
503
- type: DataType.INTEGER
504
- })
505
- passport_id!: number;
506
-
507
- @HasOne(() => passport, {
508
- sourceKey: "passport_id"
509
- })
510
- passport?: passport;
904
+ @Column({
905
+ primaryKey: true,
906
+ autoIncrement: true,
907
+ allowNull: true,
908
+ type: DataType.INTEGER
909
+ })
910
+ @Index({
911
+ name: "sqlite_autoindex_person_1",
912
+ unique: true
913
+ })
914
+ person_id?: number;
915
+
916
+ @Column({
917
+ type: DataType.STRING
918
+ })
919
+ name!: string;
920
+
921
+ @Column({
922
+ type: DataType.INTEGER
923
+ })
924
+ passport_id!: number;
925
+
926
+ @HasOne(() => passport, {
927
+ sourceKey: "passport_id"
928
+ })
929
+ passport?: passport;
511
930
 
512
931
  }
513
932
  ```
514
933
 
515
934
  ```ts
516
935
  import {
517
- Model, Table, Column, DataType, Index, Sequelize, ForeignKey, BelongsTo
936
+ Model, Table, Column, DataType, Index, Sequelize, ForeignKey, BelongsTo
518
937
  } from "sequelize-typescript";
519
938
  import { person } from "./person";
520
939
 
521
940
  export interface passportAttributes {
522
- passport_id: number;
523
- code: string;
941
+ passport_id?: number;
942
+ code: string;
524
943
  }
525
944
 
526
945
  @Table({
527
- tableName: "passport",
528
- timestamps: false
946
+ tableName: "passport",
947
+ timestamps: false
529
948
  })
530
949
  export class passport extends Model<passportAttributes, passportAttributes> implements passportAttributes {
531
950
 
532
- @ForeignKey(() => person)
533
- @Column({
534
- primaryKey: true,
535
- type: DataType.INTEGER
536
- })
537
- passport_id!: number;
538
-
539
- @Column({
540
- type: DataType.STRING(80)
541
- })
542
- code!: string;
543
-
544
- @BelongsTo(() => person)
545
- person?: person;
951
+ @ForeignKey(() => person)
952
+ @Column({
953
+ primaryKey: true,
954
+ autoIncrement: true,
955
+ allowNull: true,
956
+ type: DataType.INTEGER
957
+ })
958
+ @Index({
959
+ name: "sqlite_autoindex_passport_1",
960
+ unique: true
961
+ })
962
+ passport_id?: number;
963
+
964
+ @Column({
965
+ type: DataType.STRING
966
+ })
967
+ code!: string;
968
+
969
+ @BelongsTo(() => person)
970
+ person?: person;
546
971
 
547
972
  }
548
973
  ```
@@ -590,81 +1015,150 @@ Build models:
590
1015
  npx stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -x myPassword --indices --associations-file path/to/associations.csv --out-dir models --clean
591
1016
  ```
592
1017
 
593
- This will generate the following models:
1018
+ This will generate the following models.
1019
+
1020
+ **Native** — `races` gets a `HasMany` and `units` gets the `BelongsTo`, wired in `initModels.ts`
1021
+ (association mixins and the `static initModel` body are trimmed for brevity):
1022
+
1023
+ ```ts
1024
+ // races.ts
1025
+ export class races extends Model<InferAttributes<races>, InferCreationAttributes<races>> {
1026
+
1027
+ declare race_id: CreationOptional<number | null>;
1028
+
1029
+ declare race_name: string;
1030
+
1031
+ declare units?: NonAttribute<units[]>;
1032
+
1033
+ declare static associations: {
1034
+ units: Association<races, units>;
1035
+ };
1036
+
1037
+ // static initModel(sequelize) ...
1038
+ }
1039
+ ```
1040
+
1041
+ ```ts
1042
+ // units.ts
1043
+ export class units extends Model<InferAttributes<units>, InferCreationAttributes<units>> {
1044
+
1045
+ declare unit_id: CreationOptional<number | null>;
1046
+
1047
+ declare unit_name: string;
1048
+
1049
+ declare race_id: ForeignKey<races["race_id"]>;
1050
+
1051
+ declare race?: NonAttribute<races>;
1052
+
1053
+ declare static associations: {
1054
+ race: Association<units, races>;
1055
+ };
1056
+
1057
+ // static initModel(sequelize) ...
1058
+ }
1059
+ ```
1060
+
1061
+ ```ts
1062
+ // initModels.ts
1063
+ races.hasMany(units, {
1064
+ as: "units",
1065
+ foreignKey: "race_id",
1066
+ sourceKey: "race_id"
1067
+ });
1068
+ units.belongsTo(races, {
1069
+ as: "race",
1070
+ foreignKey: "race_id",
1071
+ targetKey: "race_id"
1072
+ });
1073
+ ```
1074
+
1075
+ **Decorators**:
594
1076
 
595
1077
  ```ts
596
1078
  import {
597
- Model, Table, Column, DataType, Index, Sequelize, ForeignKey, HasMany
1079
+ Model, Table, Column, DataType, Index, Sequelize, ForeignKey, HasMany
598
1080
  } from "sequelize-typescript";
599
1081
  import { units } from "./units";
600
1082
 
601
1083
  export interface racesAttributes {
602
- race_id: number;
603
- race_name: string;
1084
+ race_id?: number;
1085
+ race_name: string;
604
1086
  }
605
1087
 
606
1088
  @Table({
607
- tableName: "races",
608
- timestamps: false
1089
+ tableName: "races",
1090
+ timestamps: false
609
1091
  })
610
1092
  export class races extends Model<racesAttributes, racesAttributes> implements racesAttributes {
611
1093
 
612
- @Column({
613
- primaryKey: true,
614
- type: DataType.INTEGER
615
- })
616
- race_id!: number;
617
-
618
- @Column({
619
- type: DataType.STRING(80)
620
- })
621
- race_name!: string;
622
-
623
- @HasMany(() => units, {
624
- sourceKey: "race_id"
625
- })
626
- units?: units[];
1094
+ @Column({
1095
+ primaryKey: true,
1096
+ autoIncrement: true,
1097
+ allowNull: true,
1098
+ type: DataType.INTEGER
1099
+ })
1100
+ @Index({
1101
+ name: "sqlite_autoindex_races_1",
1102
+ unique: true
1103
+ })
1104
+ race_id?: number;
1105
+
1106
+ @Column({
1107
+ type: DataType.STRING
1108
+ })
1109
+ race_name!: string;
1110
+
1111
+ @HasMany(() => units, {
1112
+ sourceKey: "race_id"
1113
+ })
1114
+ units?: units[];
627
1115
 
628
1116
  }
629
1117
  ```
630
1118
 
631
1119
  ```ts
632
1120
  import {
633
- Model, Table, Column, DataType, Index, Sequelize, ForeignKey, BelongsTo
1121
+ Model, Table, Column, DataType, Index, Sequelize, ForeignKey, BelongsTo
634
1122
  } from "sequelize-typescript";
635
1123
  import { races } from "./races";
636
1124
 
637
1125
  export interface unitsAttributes {
638
- unit_id: number;
639
- unit_name: string;
640
- race_id: number;
1126
+ unit_id?: number;
1127
+ unit_name: string;
1128
+ race_id: number;
641
1129
  }
642
1130
 
643
1131
  @Table({
644
- tableName: "units",
645
- timestamps: false
1132
+ tableName: "units",
1133
+ timestamps: false
646
1134
  })
647
1135
  export class units extends Model<unitsAttributes, unitsAttributes> implements unitsAttributes {
648
1136
 
649
- @Column({
650
- primaryKey: true,
651
- type: DataType.INTEGER
652
- })
653
- unit_id!: number;
654
-
655
- @Column({
656
- type: DataType.STRING(80)
657
- })
658
- unit_name!: string;
659
-
660
- @ForeignKey(() => races)
661
- @Column({
662
- type: DataType.INTEGER
663
- })
664
- race_id!: number;
665
-
666
- @BelongsTo(() => races)
667
- race?: races;
1137
+ @Column({
1138
+ primaryKey: true,
1139
+ autoIncrement: true,
1140
+ allowNull: true,
1141
+ type: DataType.INTEGER
1142
+ })
1143
+ @Index({
1144
+ name: "sqlite_autoindex_units_1",
1145
+ unique: true
1146
+ })
1147
+ unit_id?: number;
1148
+
1149
+ @Column({
1150
+ type: DataType.STRING
1151
+ })
1152
+ unit_name!: string;
1153
+
1154
+ @ForeignKey(() => races)
1155
+ @Column({
1156
+ type: DataType.INTEGER
1157
+ })
1158
+ race_id!: number;
1159
+
1160
+ @BelongsTo(() => races)
1161
+ race?: races;
668
1162
 
669
1163
  }
670
1164
  ```
@@ -719,157 +1213,291 @@ Build models:
719
1213
  npx stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -x myPassword --indices --associations-file path/to/associations.csv --out-dir models --clean
720
1214
  ```
721
1215
 
722
- This will generate the following models:
1216
+ This will generate the following models.
1217
+
1218
+ **Native** — `authors` and `books` each get a `BelongsToMany` through the `authors_books` junction model,
1219
+ wired in `initModels.ts` (association mixins and the `static initModel` bodies of `authors`/`books` are
1220
+ trimmed for brevity; the junction model is shown in full):
1221
+
1222
+ ```ts
1223
+ // authors.ts
1224
+ export class authors extends Model<InferAttributes<authors>, InferCreationAttributes<authors>> {
1225
+
1226
+ declare author_id: CreationOptional<number | null>;
1227
+
1228
+ declare full_name: string;
1229
+
1230
+ declare books?: NonAttribute<books[]>;
1231
+
1232
+ declare static associations: {
1233
+ books: Association<authors, books>;
1234
+ };
1235
+
1236
+ // static initModel(sequelize) ...
1237
+ }
1238
+ ```
1239
+
1240
+ ```ts
1241
+ // books.ts
1242
+ export class books extends Model<InferAttributes<books>, InferCreationAttributes<books>> {
1243
+
1244
+ declare book_id: CreationOptional<number | null>;
1245
+
1246
+ declare title: string;
1247
+
1248
+ declare authors?: NonAttribute<authors[]>;
1249
+
1250
+ declare static associations: {
1251
+ authors: Association<books, authors>;
1252
+ };
1253
+
1254
+ // static initModel(sequelize) ...
1255
+ }
1256
+ ```
1257
+
1258
+ ```ts
1259
+ // authors_books.ts
1260
+ import {
1261
+ DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, Sequelize
1262
+ } from "sequelize";
1263
+ import type { authors } from "./authors";
1264
+ import type { books } from "./books";
1265
+
1266
+ export class authors_books extends Model<InferAttributes<authors_books>, InferCreationAttributes<authors_books>> {
1267
+
1268
+ declare author_id: ForeignKey<authors["author_id"]>;
1269
+
1270
+ declare book_id: ForeignKey<books["book_id"]>;
1271
+
1272
+ static initModel(sequelize: Sequelize): typeof authors_books {
1273
+ authors_books.init({
1274
+ author_id: {
1275
+ type: DataTypes.INTEGER,
1276
+ allowNull: false
1277
+ },
1278
+ book_id: {
1279
+ type: DataTypes.INTEGER,
1280
+ allowNull: false
1281
+ }
1282
+ }, {
1283
+ sequelize,
1284
+ tableName: "authors_books",
1285
+ freezeTableName: true,
1286
+ timestamps: false
1287
+ });
1288
+ return authors_books;
1289
+ }
1290
+
1291
+ }
1292
+ ```
1293
+
1294
+ ```ts
1295
+ // initModels.ts
1296
+ authors.belongsToMany(books, {
1297
+ as: "books",
1298
+ through: authors_books,
1299
+ foreignKey: "author_id",
1300
+ otherKey: "book_id"
1301
+ });
1302
+ books.belongsToMany(authors, {
1303
+ as: "authors",
1304
+ through: authors_books,
1305
+ foreignKey: "book_id",
1306
+ otherKey: "author_id"
1307
+ });
1308
+ ```
1309
+
1310
+ **Decorators**:
723
1311
 
724
1312
  ```ts
725
1313
  import {
726
- Model, Table, Column, DataType, Index, Sequelize, ForeignKey, BelongsToMany
1314
+ Model, Table, Column, DataType, Index, Sequelize, ForeignKey, BelongsToMany
727
1315
  } from "sequelize-typescript";
728
1316
  import { books } from "./books";
729
1317
  import { authors_books } from "./authors_books";
730
1318
 
731
1319
  export interface authorsAttributes {
732
- author_id: number;
733
- full_name: string;
1320
+ author_id?: number;
1321
+ full_name: string;
734
1322
  }
735
1323
 
736
1324
  @Table({
737
- tableName: "authors",
738
- timestamps: false
1325
+ tableName: "authors",
1326
+ timestamps: false
739
1327
  })
740
1328
  export class authors extends Model<authorsAttributes, authorsAttributes> implements authorsAttributes {
741
1329
 
742
- @Column({
743
- primaryKey: true,
744
- type: DataType.INTEGER
745
- })
746
- author_id!: number;
747
-
748
- @Column({
749
- type: DataType.STRING(80)
750
- })
751
- full_name!: string;
752
-
753
- @BelongsToMany(() => books, () => authors_books)
754
- books?: books[];
1330
+ @Column({
1331
+ primaryKey: true,
1332
+ autoIncrement: true,
1333
+ allowNull: true,
1334
+ type: DataType.INTEGER
1335
+ })
1336
+ @Index({
1337
+ name: "sqlite_autoindex_authors_1",
1338
+ unique: true
1339
+ })
1340
+ author_id?: number;
1341
+
1342
+ @Column({
1343
+ type: DataType.STRING
1344
+ })
1345
+ full_name!: string;
1346
+
1347
+ @BelongsToMany(() => books, () => authors_books)
1348
+ books?: books[];
755
1349
 
756
1350
  }
757
1351
  ```
758
1352
 
759
1353
  ```ts
760
1354
  import {
761
- Model, Table, Column, DataType, Index, Sequelize, ForeignKey, BelongsToMany
1355
+ Model, Table, Column, DataType, Index, Sequelize, ForeignKey, BelongsToMany
762
1356
  } from "sequelize-typescript";
763
1357
  import { authors } from "./authors";
764
1358
  import { authors_books } from "./authors_books";
765
1359
 
766
1360
  export interface booksAttributes {
767
- book_id: number;
768
- title: string;
1361
+ book_id?: number;
1362
+ title: string;
769
1363
  }
770
1364
 
771
1365
  @Table({
772
- tableName: "books",
773
- timestamps: false
1366
+ tableName: "books",
1367
+ timestamps: false
774
1368
  })
775
1369
  export class books extends Model<booksAttributes, booksAttributes> implements booksAttributes {
776
1370
 
777
- @Column({
778
- primaryKey: true,
779
- type: DataType.INTEGER
780
- })
781
- book_id!: number;
782
-
783
- @Column({
784
- type: DataType.STRING(80)
785
- })
786
- title!: string;
787
-
788
- @BelongsToMany(() => authors, () => authors_books)
789
- authors?: authors[];
1371
+ @Column({
1372
+ primaryKey: true,
1373
+ autoIncrement: true,
1374
+ allowNull: true,
1375
+ type: DataType.INTEGER
1376
+ })
1377
+ @Index({
1378
+ name: "sqlite_autoindex_books_1",
1379
+ unique: true
1380
+ })
1381
+ book_id?: number;
1382
+
1383
+ @Column({
1384
+ type: DataType.STRING
1385
+ })
1386
+ title!: string;
1387
+
1388
+ @BelongsToMany(() => authors, () => authors_books)
1389
+ authors?: authors[];
790
1390
 
791
1391
  }
792
1392
  ```
793
1393
 
794
1394
  ```ts
795
1395
  import {
796
- Model, Table, Column, DataType, Index, Sequelize, ForeignKey
1396
+ Model, Table, Column, DataType, Index, Sequelize, ForeignKey
797
1397
  } from "sequelize-typescript";
798
1398
  import { authors } from "./authors";
799
1399
  import { books } from "./books";
800
1400
 
801
1401
  export interface authors_booksAttributes {
802
- author_id: number;
803
- book_id: number;
1402
+ author_id: number;
1403
+ book_id: number;
804
1404
  }
805
1405
 
806
1406
  @Table({
807
- tableName: "authors_books",
808
- timestamps: false
1407
+ tableName: "authors_books",
1408
+ timestamps: false
809
1409
  })
810
1410
  export class authors_books extends Model<authors_booksAttributes, authors_booksAttributes> implements authors_booksAttributes {
811
1411
 
812
- @ForeignKey(() => authors)
813
- @Column({
814
- primaryKey: true,
815
- type: DataType.INTEGER
816
- })
817
- author_id!: number;
1412
+ @ForeignKey(() => authors)
1413
+ @Column({
1414
+ type: DataType.INTEGER
1415
+ })
1416
+ author_id!: number;
818
1417
 
819
- @ForeignKey(() => books)
820
- @Column({
821
- primaryKey: true,
822
- type: DataType.INTEGER
823
- })
824
- book_id!: number;
1418
+ @ForeignKey(() => books)
1419
+ @Column({
1420
+ type: DataType.INTEGER
1421
+ })
1422
+ book_id!: number;
825
1423
 
826
1424
  }
827
1425
  ```
828
1426
 
829
1427
  ## Lint
830
- By default each generated model will be linted with a predefined set of rules to improve readability:
1428
+ By default each generated model will be linted with a predefined ESLint flat config to improve readability:
831
1429
 
832
1430
  ```ts
833
- export const eslintDefaultConfig = {
834
- parser: '@typescript-eslint/parser',
835
- parserOptions: {
836
- ecmaVersion: 2018,
837
- sourceType: 'module',
1431
+ import stylistic from '@stylistic/eslint-plugin';
1432
+ import tsParser from '@typescript-eslint/parser';
1433
+
1434
+ export const eslintDefaultConfig = [
1435
+ {
1436
+ files: ['**/*.ts', '**/*.tsx'],
1437
+ languageOptions: {
1438
+ parser: tsParser,
1439
+ parserOptions: {
1440
+ ecmaVersion: 2019,
1441
+ sourceType: 'module',
1442
+ },
1443
+ },
1444
+ plugins: {
1445
+ '@stylistic': stylistic,
1446
+ },
1447
+ rules: {
1448
+ '@stylistic/padded-blocks': ['error', { blocks: 'never', classes: 'always', switches: 'always' }],
1449
+ '@stylistic/lines-between-class-members': ['error', 'always'],
1450
+ '@stylistic/object-curly-newline': ['error', {
1451
+ 'ObjectExpression': 'always',
1452
+ 'ObjectPattern': { 'multiline': true },
1453
+ 'ImportDeclaration': { 'multiline': true, 'minProperties': 3 },
1454
+ 'ExportDeclaration': { 'multiline': true, 'minProperties': 3 },
1455
+ }],
1456
+ '@stylistic/object-property-newline': ['error'],
1457
+ '@stylistic/indent': ['error', 'tab'],
1458
+ },
838
1459
  },
839
- plugins: [
840
- '@typescript-eslint',
841
- ],
842
- extends: [],
843
- rules: {
844
- 'padded-blocks': ['error', { blocks: 'always', classes: 'always', switches: 'always' }],
845
- 'lines-between-class-members': ['error', 'always' ],
846
- 'object-curly-newline': ['error', {
847
- 'ObjectExpression': 'always',
848
- 'ObjectPattern': { 'multiline': true },
849
- 'ImportDeclaration': { 'multiline': true, 'minProperties': 3 },
850
- 'ExportDeclaration': { 'multiline': true, 'minProperties': 3 },
851
- }],
852
- 'object-property-newline': ['error'],
853
- 'indent': ['error', 'tab'],
1460
+ ];
1461
+ ```
1462
+
1463
+ You can provide your own rules by passing a config file to `--lint-file` / `-L`. The file must be a JS module that
1464
+ exports an ESLint flat config array (loaded via a dynamic `import()`). Legacy `.eslintrc*` files, and configs using
1465
+ `extends`, `env`, or a string `parser`, are rejected — see the
1466
+ [ESLint flat config migration guide](https://eslint.org/docs/latest/use/configure/migration-guide).
1467
+
1468
+ A minimal flat config looks like this:
1469
+
1470
+ ```js
1471
+ import stylistic from '@stylistic/eslint-plugin';
1472
+ import parser from '@typescript-eslint/parser';
1473
+
1474
+ export default [
1475
+ {
1476
+ files: ['**/*.ts', '**/*.tsx'],
1477
+ languageOptions: {
1478
+ parser,
1479
+ parserOptions: { ecmaVersion: 2019, sourceType: 'module' },
1480
+ },
1481
+ plugins: { '@stylistic': stylistic },
1482
+ rules: { '@stylistic/indent': ['error', 'tab'] },
854
1483
  },
855
- };
1484
+ ];
856
1485
  ```
857
1486
 
858
- You can provide your own set of rules that matches your coding style. Just define a file with the linting rules
859
- (see [eslint](https://www.npmjs.com/package/eslint) docs) and pass it to the `cli` like the following:
1487
+ Pass it to the `cli` like the following:
860
1488
  ```shell
861
- npx stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -x myPassword --lint-file path/to/lint-file --out-dir models --clean
1489
+ npx stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -x myPassword --lint-file path/to/flat.config.mjs --out-dir models --clean
862
1490
  ```
863
1491
 
864
1492
  Globally:
865
1493
  ```shell
866
- stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -x myPassword --lint-file path/to/lint-file --out-dir models --clean
1494
+ stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -x myPassword --lint-file path/to/flat.config.mjs --out-dir models --clean
867
1495
  ```
868
1496
 
869
1497
  Or you can pass `eslint` options programmatically:
870
1498
 
871
1499
  ```ts
872
- import { IConfig, ModelBuilder, DialectMySQL } from 'sequelize-typescript-generator';
1500
+ import { IConfig, ModelBuilder, createDialect } from 'sequelize-typescript-generator';
873
1501
 
874
1502
  (async () => {
875
1503
  const config: IConfig = {
@@ -889,7 +1517,7 @@ import { IConfig, ModelBuilder, DialectMySQL } from 'sequelize-typescript-genera
889
1517
  },
890
1518
  };
891
1519
 
892
- const dialect = new DialectMySQL();
1520
+ const dialect = createDialect('mysql');
893
1521
 
894
1522
  const builder = new ModelBuilder(config, dialect);
895
1523