sequelize-typescript-generator 13.1.0 → 13.2.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 (44) hide show
  1. package/README.md +290 -20
  2. package/build/bin/cli.js +3 -3
  3. package/build/bin/utils.d.ts +13 -0
  4. package/build/bin/utils.js +25 -0
  5. package/build/builders/ModelBuilder.d.ts +4 -2
  6. package/build/builders/ModelBuilder.js +143 -19
  7. package/build/builders/NativeModelRenderer.d.ts +4 -3
  8. package/build/builders/NativeModelRenderer.js +31 -11
  9. package/build/builders/enumSupport.d.ts +104 -0
  10. package/build/builders/enumSupport.js +323 -0
  11. package/build/builders/generatedFile.d.ts +2 -1
  12. package/build/builders/generatedFile.js +7 -1
  13. package/build/builders/jsonSupport.d.ts +10 -3
  14. package/build/builders/jsonSupport.js +10 -3
  15. package/build/builders/nativeWiring.d.ts +2 -1
  16. package/build/builders/nativeWiring.js +9 -3
  17. package/build/builders/sharedTypesIndexExports.d.ts +11 -0
  18. package/build/builders/sharedTypesIndexExports.js +15 -0
  19. package/build/builders/typeOverrides.d.ts +30 -0
  20. package/build/builders/typeOverrides.js +93 -0
  21. package/build/builders/typeOverridesParser.d.ts +48 -0
  22. package/build/builders/typeOverridesParser.js +305 -0
  23. package/build/builders/unmappedTypes.d.ts +50 -0
  24. package/build/builders/unmappedTypes.js +62 -0
  25. package/build/builders/utils.d.ts +7 -0
  26. package/build/builders/utils.js +12 -0
  27. package/build/config/IConfig.d.ts +22 -0
  28. package/build/dialects/Dialect.d.ts +56 -3
  29. package/build/dialects/DialectMSSQL.d.ts +2 -2
  30. package/build/dialects/DialectMSSQL.js +1 -6
  31. package/build/dialects/DialectMariaDB.d.ts +2 -2
  32. package/build/dialects/DialectMariaDB.js +1 -6
  33. package/build/dialects/DialectMySQL.d.ts +2 -2
  34. package/build/dialects/DialectMySQL.js +1 -6
  35. package/build/dialects/DialectPostgres.d.ts +33 -2
  36. package/build/dialects/DialectPostgres.js +77 -10
  37. package/build/dialects/DialectSQLite.d.ts +2 -2
  38. package/build/dialects/DialectSQLite.js +1 -6
  39. package/build/dialects/dataTypes.d.ts +8 -0
  40. package/build/dialects/dataTypes.js +32 -4
  41. package/build/dialects/utils.d.ts +0 -6
  42. package/build/dialects/utils.js +0 -10
  43. package/build/index.d.ts +2 -2
  44. package/package.json +3 -3
package/README.md CHANGED
@@ -27,6 +27,17 @@
27
27
  * [One to One](#one-to-one)
28
28
  * [One to Many](#one-to-many)
29
29
  * [Many to Many](#many-to-many)
30
+ * [Type overrides](#type-overrides)
31
+ * [Type overrides file](#type-overrides-file)
32
+ * [Matching and precedence](#matching-and-precedence)
33
+ * [Type keys per dialect](#type-keys-per-dialect)
34
+ * [Validation and warnings](#validation-and-warnings)
35
+ * [Unmapped types](#unmapped-types)
36
+ * [Postgres user-defined types](#postgres-user-defined-types)
37
+ * [Enums](#enums)
38
+ * [Enum type names](#enum-type-names)
39
+ * [Domains, citext and other types](#domains-citext-and-other-types)
40
+ * [Caveats](#caveats)
30
41
  * [Lint](#lint)
31
42
 
32
43
  <!-- toc stop -->
@@ -95,13 +106,14 @@ stg --help
95
106
 
96
107
  ```shell
97
108
  Usage: stg -D <dialect> -d [database] -u [username] -x [password] -h [host] -p
98
- [port] -o [out-dir] -s [schema] -a [associations-file]-t [tables] -T
99
- [skip-tables] -i [indices] -C [case] -S [storage] -L [lint-file] -l [ssl] -r
100
- [protocol] -c [clean] -F [format] --no-associations
109
+ [port] -o [out-dir] -s [schema] -a [associations-file] -y [type-overrides-file]
110
+ -t [tables] -T [skip-tables] -V [no-views] -i [indices] -P [paranoid] -C [case]
111
+ -S [storage] -L [lint-file] -l [ssl] -r [protocol] -n [dialect-options] -c
112
+ [clean] -g [logs] -F [format] --no-associations
101
113
 
102
114
  Options:
103
- --help Show help [boolean]
104
- --version Show version number [boolean]
115
+ --help Show help [boolean]
116
+ --version Show version number [boolean]
105
117
  -h, --host Database IP/hostname [string]
106
118
  -p, --port Database port. Defaults:
107
119
  - MySQL/MariaDB: 3306
@@ -126,9 +138,9 @@ Options:
126
138
  - output-models [string]
127
139
  -c, --clean Clean output directory before running [boolean]
128
140
  -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]
141
+ -P, --paranoid Emit paranoid table options for tables with a
142
+ deleted_at or deletedAt column. Requires
143
+ --timestamps. [boolean]
132
144
  -C, --case Transform tables and fields names
133
145
  with one of the following cases:
134
146
  - underscore
@@ -149,27 +161,31 @@ Options:
149
161
  -r, --protocol Protocol used: Default:
150
162
  - tcp [string]
151
163
  -a, --associations-file Associations file path [string]
164
+ -y, --type-overrides-file Type overrides file path: a JSON file declaring
165
+ the TypeScript type and/or the Sequelize data type
166
+ of database types ("types") or single columns
167
+ ("columns"). Overrides always win over the
168
+ generated types. [string]
152
169
  -g, --logs Enable Sequelize logs [boolean]
153
170
  -n, --dialect-options Dialect native options passed as json string.
154
171
  [string]
155
172
  -f, --dialect-options-file Dialect native options passed as json file path.
156
173
  [string]
174
+ -R, --no-strict Disable strict typescript class declaration
175
+ (decorators format only). [boolean]
157
176
  -F, --format Output format:
158
177
  - native: plain Sequelize classes with declare
159
- fields, Model.init and an initModels wiring
160
- file (default)
178
+ fields, Model.init and an initModels wiring file
179
+ (default)
161
180
  - 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]
168
- -V, --no-views Disable view generation. Available for: MySQL and MariaDB.
169
- [boolean]
170
- --associations Discover one-to-one and one-to-many associations
181
+ (requires sequelize-typescript in the target
182
+ project)
183
+ [string] [choices: "native", "decorators"] [default: "native"]
184
+ -V, --no-views Disable views generation. Available for: MySQL and
185
+ MariaDB. [boolean]
186
+ --associations Discover one-to-one and one-to-many associations
171
187
  from foreign keys. Use --no-associations to
172
- disable. [boolean] [default: true]
188
+ disable. [boolean] [default: true]
173
189
  ```
174
190
 
175
191
  Local usage example:
@@ -286,6 +302,22 @@ export function initModels(sequelize: Sequelize) {
286
302
  export type Models = ReturnType<typeof initModels>;
287
303
  ```
288
304
 
305
+ The `index.ts` barrel re-exports every model file and `initModels`, plus the shared type files emitted
306
+ next to the models whenever they are generated: `jsonType.ts` (the `Json` type of JSON columns) and
307
+ `enums.ts` (the [enum](#enums) shared types). Shared type files are re-exported type-only, so the barrel
308
+ stays safe under `isolatedModules` and `verbatimModuleSyntax`:
309
+
310
+ ```ts
311
+ export * from "./races";
312
+ export * from "./units";
313
+ // ... other models
314
+ export type * from "./jsonType";
315
+ export type * from "./enums";
316
+ export * from "./initModels";
317
+ ```
318
+
319
+ The decorators format emits the same barrel without the `initModels` re-export.
320
+
289
321
  Wire the models against a `Sequelize` instance and use them straight away:
290
322
 
291
323
  ```ts
@@ -1424,6 +1456,244 @@ export class authors_books extends Model<authors_booksAttributes, authors_booksA
1424
1456
  }
1425
1457
  ```
1426
1458
 
1459
+ ## Type overrides
1460
+ A type override sets the TypeScript type, the Sequelize data type, or both, of every column of a
1461
+ database type or of a single column. Use it for types the generator cannot map (extension types such
1462
+ as `tsvector`, `vector` or `ltree`) or for columns you want typed differently. Overrides work in every
1463
+ dialect and both output formats, and always win over what the generator derives.
1464
+
1465
+ ### Type overrides file
1466
+ Type overrides live in a JSON file passed with `-y` / `--type-overrides-file`:
1467
+
1468
+ ```shell
1469
+ npx stg -D postgres -d myDatabase -u myUsername -x myPassword --type-overrides-file type-overrides.json
1470
+ ```
1471
+
1472
+ The file has two optional sections; any other top-level key is an error:
1473
+
1474
+ - `types`: keyed by `<type>` or `<schema>.<type>`; the entry applies to every column of that database type.
1475
+ - `columns`: keyed by `<table>.<column>` or `<schema>.<table>.<column>`; the entry applies to one column.
1476
+ Table and column are the database names, not the [transformed](#transform-case) ones.
1477
+
1478
+ Each entry sets `tsType`, `dataType` or both:
1479
+
1480
+ - `tsType`: any TypeScript type expression, including unions, object types and `import('pkg').Type`.
1481
+ It is emitted as written; no import statements are generated, so use an `import()` type for
1482
+ external types.
1483
+ - `dataType`: a Sequelize data type expression, such as `"TSVECTOR"`, `"STRING(80)"` or
1484
+ `"ARRAY(ENUM('a', 'b'))"`, with an optional `DataTypes.` / `DataType.` prefix. Only data type
1485
+ identifiers, calls, and string or number literal arguments are allowed, and every data type must
1486
+ exist in Sequelize `DataTypes`. It is emitted with the namespace of the output format
1487
+ (`DataTypes.` for native, `DataType.` for decorators).
1488
+
1489
+ A side you do not set keeps what the generator would have produced, so a partial override changes only
1490
+ the part that is wrong. Neither side is derived from the other.
1491
+
1492
+ ```json
1493
+ {
1494
+ "types": {
1495
+ "tsvector": { "tsType": "string", "dataType": "TSVECTOR" },
1496
+ "geometry": { "tsType": "import('geojson').Geometry" }
1497
+ },
1498
+ "columns": {
1499
+ "public.places.settings": { "tsType": "{ theme: 'light' | 'dark'; beta?: boolean }" }
1500
+ }
1501
+ }
1502
+ ```
1503
+
1504
+ Here `tsvector` (unmapped by the generator) gets both sides, `geometry` keeps its generated `GEOMETRY`
1505
+ data type, and `settings` (a `jsonb` column) keeps its `JSONB` data type with a precise TypeScript type
1506
+ in place of `Json`. In the native format the `places` model becomes:
1507
+
1508
+ ```ts
1509
+ export class places extends Model<InferAttributes<places>, InferCreationAttributes<places>> {
1510
+
1511
+ // ...
1512
+
1513
+ declare location: import("geojson").Geometry | null;
1514
+
1515
+ declare search: string | null;
1516
+
1517
+ declare settings: {
1518
+ theme: "light" | "dark";
1519
+ beta?: boolean;
1520
+ } | null;
1521
+
1522
+ static initModel(sequelize: Sequelize): typeof places {
1523
+ places.init({
1524
+ // ...
1525
+ location: {
1526
+ type: DataTypes.GEOMETRY,
1527
+ allowNull: true
1528
+ },
1529
+ search: {
1530
+ type: DataTypes.TSVECTOR,
1531
+ allowNull: true
1532
+ },
1533
+ settings: {
1534
+ type: DataTypes.JSONB,
1535
+ allowNull: true
1536
+ }
1537
+ }, {
1538
+ // ...
1539
+ });
1540
+ return places;
1541
+ }
1542
+
1543
+ }
1544
+ ```
1545
+
1546
+ Nullability is still applied to an overridden TypeScript type (`| null` in the native format, an
1547
+ optional property in the decorators format).
1548
+
1549
+ Programmatically, set either `metadata.typeOverridesFile` (a file path) or `metadata.typeOverrides`
1550
+ (an object of the same shape); setting both is a validation error. The `ITypeOverride` and
1551
+ `ITypeOverrides` types are exported from the package:
1552
+
1553
+ ```ts
1554
+ import { IConfig, ITypeOverrides } from 'sequelize-typescript-generator';
1555
+
1556
+ const typeOverrides: ITypeOverrides = {
1557
+ types: {
1558
+ tsvector: { tsType: 'string', dataType: 'TSVECTOR' },
1559
+ },
1560
+ columns: {
1561
+ 'users.status': { tsType: "'active' | 'banned'" },
1562
+ },
1563
+ };
1564
+
1565
+ const config: IConfig = {
1566
+ // ...
1567
+ metadata: {
1568
+ typeOverrides,
1569
+ },
1570
+ };
1571
+ ```
1572
+
1573
+ ### Matching and precedence
1574
+ Keys match case-insensitively, like the table filters; two keys of a section that differ only by case
1575
+ are a validation error. When several entries match a column, the most specific wins:
1576
+
1577
+ 1. `<schema>.<table>.<column>`
1578
+ 2. `<table>.<column>`
1579
+ 3. `<schema>.<type>`
1580
+ 4. `<type>`
1581
+
1582
+ So a column override beats a type override, a schema-qualified key beats an unqualified one, and any
1583
+ override beats the generator. `tsType` and `dataType` are resolved independently: each comes from the
1584
+ most specific matching entry that sets it, and falls back to the generator when none does.
1585
+
1586
+ ### Type keys per dialect
1587
+ A type key matches the raw database type name the dialect reports for the column:
1588
+
1589
+ | Dialect | Type name matched | Schema segment |
1590
+ | --- | --- | --- |
1591
+ | Postgres | `udt_name`, e.g. `int4`, `varchar`, `tsvector` or an enum name; array types are prefixed with `_` (`_int4`, `_plan_tier`); a domain reports its underlying type | The table schema; for an enum column, the schema the enum is defined in |
1592
+ | MySQL / MariaDB | `DATA_TYPE`, e.g. `varchar`, `int` | `connection.schema`, normally unset, so schema-qualified keys do not match |
1593
+ | MSSQL | `DATA_TYPE`, e.g. `nvarchar`, `int` | The table schema, e.g. `dbo` |
1594
+ | SQLite | The full declared type, e.g. `VARCHAR(80)` | None; use unqualified keys |
1595
+
1596
+ The schema segment of a column key follows the same rule as the table schema in the table above.
1597
+
1598
+ ### Validation and warnings
1599
+ Type overrides are read and validated when generation starts, for both the CLI and programmatic usage.
1600
+ The CLI also fails fast if the `-y` path does not exist. Any of the following is a `[ValidationError]`
1601
+ naming the offending entry, and stops generation:
1602
+
1603
+ - the file cannot be read or is not valid JSON;
1604
+ - the root is not an object, or has a section other than `types` and `columns`;
1605
+ - a section or an entry is not an object, or an entry has a field other than `tsType` and `dataType`;
1606
+ - an entry sets neither `tsType` nor `dataType`, or sets one to a non-string;
1607
+ - a key is malformed (wrong number of segments or an empty segment) or duplicates another key of the
1608
+ section case-insensitively;
1609
+ - `tsType` is not a valid TypeScript type;
1610
+ - `dataType` does not parse, uses a disallowed expression, or names an unknown Sequelize data type;
1611
+ - both `typeOverridesFile` and `typeOverrides` are set.
1612
+
1613
+ Entries that matched no column are listed in a single warning, so typos are noticed even when table
1614
+ filters legitimately exclude the tables they target:
1615
+
1616
+ ```
1617
+ [WARNING] Type overrides matched no column: columns["plcaes.name"]. Check them for typos; table filters may exclude the tables they target.
1618
+ ```
1619
+
1620
+ ### Unmapped types
1621
+ A column whose database type the dialect cannot map is typed `unknown` in the native format and `any`
1622
+ in the decorators format, and generation continues. A warning names the column as
1623
+ `schema.table.column` and the type it received:
1624
+
1625
+ ```
1626
+ [WARNING] Unmapped type 'tsvector' for column public.places.search: typed as 'unknown'. Declare a type override with --type-overrides-file to set its type.
1627
+ ```
1628
+
1629
+ Fix it with a type override; columns covered by an override get no unmapped-type warning. In the
1630
+ native format an unmapped column has no `type` in `Model.init`, so set a `dataType` as well as a
1631
+ `tsType`.
1632
+
1633
+ ## Postgres user-defined types
1634
+
1635
+ ### Enums
1636
+ The Postgres dialect detects enum types, including enums defined in another schema than the table.
1637
+ Each enum becomes a shared type named after it, collected in a generated `enums.ts` used by both
1638
+ output formats:
1639
+
1640
+ ```ts
1641
+ export type PlanTier = "free" | "pro" | "enterprise";
1642
+ ```
1643
+
1644
+ Models import the shared types they use with a type-only import (`import type { PlanTier } from "./enums";`)
1645
+ and type enum columns with them; the `index.ts` barrel re-exports them (`export type * from "./enums";`). The data type is `ENUM` with the database labels in database order,
1646
+ and enum arrays are typed as arrays of the shared type with an `ARRAY` of `ENUM` data type:
1647
+
1648
+ ```ts
1649
+ declare plan: PlanTier;
1650
+
1651
+ declare previous_plans: PlanTier[] | null;
1652
+
1653
+ // in Model.init
1654
+ plan: {
1655
+ type: DataTypes.ENUM("free", "pro", "enterprise"),
1656
+ allowNull: false
1657
+ },
1658
+ previous_plans: {
1659
+ type: DataTypes.ARRAY(DataTypes.ENUM("free", "pro", "enterprise")),
1660
+ allowNull: true
1661
+ }
1662
+ ```
1663
+
1664
+ An enum used by several tables produces a single shared type. `enums.ts` is generated only when at
1665
+ least one generated column uses an enum shared type: a [type override](#type-overrides) whose `tsType`
1666
+ replaces an enum on every column removes that enum from `enums.ts`. MySQL and MariaDB enums are typed as
1667
+ before (an inline union in the native format, `string` in the decorators format).
1668
+
1669
+ ### Enum type names
1670
+ The shared type name is the Pascal case of the database type name (`plan_tier` → `PlanTier`); a name
1671
+ that does not start with a valid identifier character is prefixed with `Enum`. Clashes rename only the
1672
+ clashing enums, with a warning for each renamed one:
1673
+
1674
+ - when enums of the same name exist in several schemas, an enum from a schema none of the generated
1675
+ tables belong to is prefixed with its Pascal-cased schema (`auth.plan_tier` → `AuthPlanTier`);
1676
+ - a name equal to a generated model name, or to any other identifier a model file declares or imports,
1677
+ gets an `Enum` suffix (`PlanTierEnum`);
1678
+ - names still equal after that are ordered by schema then type name: the first keeps the name, the
1679
+ next ones get `2`, `3`, and so on.
1680
+
1681
+ A warning is also printed if a model would be written to `enums.ts` itself.
1682
+
1683
+ ### Domains, citext and other types
1684
+ - Domain columns are typed as their underlying type.
1685
+ - `citext` columns are typed `string` with the `CITEXT` data type.
1686
+ - Composite types and extension types outside the type map (`vector`, `ltree`, `tsvector`, `hstore`,
1687
+ range types, ...) remain [unmapped types](#unmapped-types); declare a [type override](#type-overrides)
1688
+ for them.
1689
+
1690
+ ### Caveats
1691
+ - Sequelize v6 cannot name the enum type of an attribute, so `sync()` creates its own
1692
+ `enum_<table>_<column>` types instead of reusing your named database enums. The generated models
1693
+ target existing databases; manage the schema's enum types with migrations rather than `sync()`.
1694
+ - Sequelize reads the enum type OIDs once per `Sequelize` instance. If enum types are created after the
1695
+ instance connected, enum array values come back as raw strings until a new instance is created.
1696
+
1427
1697
  ## Lint
1428
1698
  By default each generated model will be linted with a predefined ESLint flat config to improve readability:
1429
1699
 
package/build/bin/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import yargs from 'yargs';
3
3
  import { hideBin } from 'yargs/helpers';
4
4
  import { ModelBuilder } from '../builders/index.js';
5
- import { defaultOutputDir, aliasesMap, validateArgs, buildConfig, buildDialect, } from './utils.js';
5
+ import { defaultOutputDir, aliasesMap, validateArgs, buildConfig, buildDialect, TYPE_OVERRIDES_FILE_FLAG, typeOverridesFileOption, } from './utils.js';
6
6
  import { FORMATS, DEFAULT_FORMAT } from '../config/format.js';
7
7
  process.on('unhandledRejection', (reason, promise) => {
8
8
  console.error(reason, promise);
@@ -10,7 +10,7 @@ process.on('unhandledRejection', (reason, promise) => {
10
10
  });
11
11
  export const cli = async () => {
12
12
  let usage = `Usage: stg -D <dialect> -d [database] -u [username] -x [password] `;
13
- usage += `-h [host] -p [port] -o [out-dir] -s [schema] -a [associations-file]`;
13
+ usage += `-h [host] -p [port] -o [out-dir] -s [schema] -a [associations-file] -y [type-overrides-file] `;
14
14
  usage += `-t [tables] -T [skip-tables] -V [no-views] -i [indices] -P [paranoid] -C [case] -S [storage] -L [lint-file] `;
15
15
  usage += `-l [ssl] -r [protocol] -n [dialect-options] -c [clean] -g [logs] -F [format] --no-associations`;
16
16
  const { argv } = yargs(hideBin(process.argv))
@@ -119,7 +119,7 @@ export const cli = async () => {
119
119
  alias: aliasesMap.ASSOCIATIONS_FILE,
120
120
  string: true,
121
121
  describe: `Associations file path`,
122
- }).option('g', {
122
+ }).option(TYPE_OVERRIDES_FILE_FLAG, typeOverridesFileOption).option('g', {
123
123
  alias: aliasesMap.ENABLE_SEQUELIZE_LOGS,
124
124
  boolean: true,
125
125
  describe: `Enable Sequelize logs`,
@@ -25,6 +25,7 @@ export declare const aliasesMap: {
25
25
  SSL: string;
26
26
  PROTOCOL: string;
27
27
  ASSOCIATIONS_FILE: string;
28
+ TYPE_OVERRIDES_FILE: string;
28
29
  ASSOCIATIONS: string;
29
30
  ENABLE_SEQUELIZE_LOGS: string;
30
31
  DIALECT_OPTIONS: string;
@@ -33,6 +34,18 @@ export declare const aliasesMap: {
33
34
  DISABLE_VIEWS: string;
34
35
  FORMAT: string;
35
36
  };
37
+ /**
38
+ * Short flag of the type overrides file option.
39
+ */
40
+ export declare const TYPE_OVERRIDES_FILE_FLAG = "y";
41
+ /**
42
+ * Definition of the type overrides file option.
43
+ */
44
+ export declare const typeOverridesFileOption: {
45
+ alias: string;
46
+ string: true;
47
+ describe: string;
48
+ };
36
49
  /**
37
50
  * Diplay error message and exit
38
51
  * @param {string} msg
@@ -26,6 +26,7 @@ export const aliasesMap = {
26
26
  SSL: 'ssl',
27
27
  PROTOCOL: 'protocol',
28
28
  ASSOCIATIONS_FILE: 'associations-file',
29
+ TYPE_OVERRIDES_FILE: 'type-overrides-file',
29
30
  ASSOCIATIONS: 'associations',
30
31
  ENABLE_SEQUELIZE_LOGS: 'logs',
31
32
  DIALECT_OPTIONS: 'dialect-options',
@@ -34,6 +35,20 @@ export const aliasesMap = {
34
35
  DISABLE_VIEWS: 'no-views',
35
36
  FORMAT: 'format',
36
37
  };
38
+ /**
39
+ * Short flag of the type overrides file option.
40
+ */
41
+ export const TYPE_OVERRIDES_FILE_FLAG = 'y';
42
+ /**
43
+ * Definition of the type overrides file option.
44
+ */
45
+ export const typeOverridesFileOption = {
46
+ alias: aliasesMap.TYPE_OVERRIDES_FILE,
47
+ string: true,
48
+ describe: `Type overrides file path: a JSON file declaring the TypeScript type and/or the Sequelize ` +
49
+ `data type of database types ("types") or single columns ("columns"). Overrides always win over ` +
50
+ `the generated types.`,
51
+ };
37
52
  /**
38
53
  * Diplay error message and exit
39
54
  * @param {string} msg
@@ -148,6 +163,7 @@ export const buildConfig = (argv) => {
148
163
  paranoid: !!argv[aliasesMap.PARANOID],
149
164
  ...argv[aliasesMap.CASE] && { case: parseCase(argv[aliasesMap.CASE]) },
150
165
  ...argv[aliasesMap.ASSOCIATIONS_FILE] && { associationsFile: argv[aliasesMap.ASSOCIATIONS_FILE] },
166
+ ...argv[aliasesMap.TYPE_OVERRIDES_FILE] && { typeOverridesFile: String(argv[aliasesMap.TYPE_OVERRIDES_FILE]) },
151
167
  ...(argv[aliasesMap.ASSOCIATIONS] === false && { associations: false }),
152
168
  noViews: !!argv[aliasesMap.DISABLE_VIEWS] || argv['views'] === false,
153
169
  },
@@ -239,5 +255,14 @@ export const validateArgs = (argv) => {
239
255
  error(`Argument -a [associations-file] '${argv[aliasesMap.ASSOCIATIONS_FILE]}' is not a valid path`);
240
256
  }
241
257
  }
258
+ // Validate type overrides file
259
+ if (argv[aliasesMap.TYPE_OVERRIDES_FILE]) {
260
+ try {
261
+ fs.accessSync(argv[aliasesMap.TYPE_OVERRIDES_FILE]);
262
+ }
263
+ catch (err) {
264
+ error(`Argument -y [type-overrides-file] '${argv[aliasesMap.TYPE_OVERRIDES_FILE]}' is not a valid path`);
265
+ }
266
+ }
242
267
  // TODO Validate schema if dialect is postgres ?
243
268
  };
@@ -54,13 +54,15 @@ export declare class ModelBuilder extends Builder {
54
54
  */
55
55
  private static buildTableClassDeclaration;
56
56
  /**
57
- * Build main index file
57
+ * Build main index file: every model file, then the shared type files the
58
+ * models use (type-only)
58
59
  * @param {ITableMetadata[]} tablesMetadata
59
60
  * @returns {string}
60
61
  */
61
62
  private static buildIndexExports;
62
63
  /**
63
- * Render the decorators output as one file per table plus the index barrel.
64
+ * Render the decorators output as one file per table, the shared type files the
65
+ * models use, and the index barrel.
64
66
  * @param {ITablesMetadata} tablesMetadata
65
67
  * @param {Dialect} dialect
66
68
  * @param {boolean | undefined} strict