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.
- package/README.md +290 -20
- package/build/bin/cli.js +3 -3
- package/build/bin/utils.d.ts +13 -0
- package/build/bin/utils.js +25 -0
- package/build/builders/ModelBuilder.d.ts +4 -2
- package/build/builders/ModelBuilder.js +143 -19
- package/build/builders/NativeModelRenderer.d.ts +4 -3
- package/build/builders/NativeModelRenderer.js +31 -11
- package/build/builders/enumSupport.d.ts +104 -0
- package/build/builders/enumSupport.js +323 -0
- package/build/builders/generatedFile.d.ts +2 -1
- package/build/builders/generatedFile.js +7 -1
- package/build/builders/jsonSupport.d.ts +10 -3
- package/build/builders/jsonSupport.js +10 -3
- package/build/builders/nativeWiring.d.ts +2 -1
- package/build/builders/nativeWiring.js +9 -3
- package/build/builders/sharedTypesIndexExports.d.ts +11 -0
- package/build/builders/sharedTypesIndexExports.js +15 -0
- package/build/builders/typeOverrides.d.ts +30 -0
- package/build/builders/typeOverrides.js +93 -0
- package/build/builders/typeOverridesParser.d.ts +48 -0
- package/build/builders/typeOverridesParser.js +305 -0
- package/build/builders/unmappedTypes.d.ts +50 -0
- package/build/builders/unmappedTypes.js +62 -0
- package/build/builders/utils.d.ts +7 -0
- package/build/builders/utils.js +12 -0
- package/build/config/IConfig.d.ts +22 -0
- package/build/dialects/Dialect.d.ts +56 -3
- package/build/dialects/DialectMSSQL.d.ts +2 -2
- package/build/dialects/DialectMSSQL.js +1 -6
- package/build/dialects/DialectMariaDB.d.ts +2 -2
- package/build/dialects/DialectMariaDB.js +1 -6
- package/build/dialects/DialectMySQL.d.ts +2 -2
- package/build/dialects/DialectMySQL.js +1 -6
- package/build/dialects/DialectPostgres.d.ts +33 -2
- package/build/dialects/DialectPostgres.js +77 -10
- package/build/dialects/DialectSQLite.d.ts +2 -2
- package/build/dialects/DialectSQLite.js +1 -6
- package/build/dialects/dataTypes.d.ts +8 -0
- package/build/dialects/dataTypes.js +32 -4
- package/build/dialects/utils.d.ts +0 -6
- package/build/dialects/utils.js +0 -10
- package/build/index.d.ts +2 -2
- package/package.json +3 -3
|
@@ -3,25 +3,131 @@ import path from 'path';
|
|
|
3
3
|
import * as ts from 'typescript';
|
|
4
4
|
import { Linter } from '../lint/index.js';
|
|
5
5
|
import { resolveFormat, shouldNoticeIgnoredStrict, STRICT_IGNORED_NOTICE } from '../config/format.js';
|
|
6
|
+
import { DATA_TYPE_NAMESPACES } from '../dialects/dataTypes.js';
|
|
6
7
|
import { isErrnoException } from '../utils/errors.js';
|
|
7
8
|
import { Builder } from './Builder.js';
|
|
8
9
|
import { resolveAssociationPropertyName } from './associationNaming.js';
|
|
9
10
|
import { renderNativeFiles, writeGeneratedFiles } from './generatedFile.js';
|
|
10
11
|
import { warnWhenDecoratorsDependencyIsMissing } from './decoratorsDependency.js';
|
|
11
|
-
import { attachColumnCommentJsDoc, nodeToString, createGenericTypeReference, createTypeNodeFromName, generateArrowDecorator, generateNamedImports, generateObjectLiteralDecorator, generateIndexExport, } from './utils.js';
|
|
12
|
+
import { attachColumnCommentJsDoc, buildDataTypeExpression, nodeToString, createGenericTypeReference, createTypeNodeFromName, generateArrowDecorator, generateNamedImports, generateObjectLiteralDecorator, generateIndexExport, } from './utils.js';
|
|
12
13
|
import { buildJsonTypeImport, JSON_SUPPORT_FILE_NAME, JSON_TYPE_NAME, renderJsonSupportFile, tableHasJsonColumn, tablesHaveJsonColumn, warnJsonSupportFileNameCollision, } from './jsonSupport.js';
|
|
14
|
+
import { assignSharedEnumTypeNames, buildEnumColumnTypeNode, buildEnumTypesImport, ENUM_SUPPORT_FILE_NAME, renderEnumSupportFile, tablesHaveSharedEnumType, warnEnumSupportFileNameCollision, } from './enumSupport.js';
|
|
15
|
+
import { indexTablesByModelName } from './nativeAttributes.js';
|
|
16
|
+
import { buildSharedTypesIndexExports } from './sharedTypesIndexExports.js';
|
|
17
|
+
import { collectSequelizeImports } from './nativeAssociations.js';
|
|
18
|
+
import { UNMAPPED_TS_TYPE_BY_FORMAT, warnUnmappedTypes } from './unmappedTypes.js';
|
|
19
|
+
import { loadTypeOverrides } from './typeOverridesParser.js';
|
|
20
|
+
import { applyTypeOverrides, warnUnmatchedTypeOverrides } from './typeOverrides.js';
|
|
13
21
|
export { resolveAssociationPropertyName } from './associationNaming.js';
|
|
14
22
|
const foreignKeyDecorator = 'ForeignKey';
|
|
15
23
|
/**
|
|
16
|
-
*
|
|
17
|
-
|
|
24
|
+
* Names every decorators model file imports from `sequelize-typescript`.
|
|
25
|
+
*/
|
|
26
|
+
const DECORATORS_BASE_IMPORTS = [
|
|
27
|
+
'Model',
|
|
28
|
+
'Table',
|
|
29
|
+
'Column',
|
|
30
|
+
'DataType',
|
|
31
|
+
'Index',
|
|
32
|
+
'Sequelize',
|
|
33
|
+
foreignKeyDecorator,
|
|
34
|
+
];
|
|
35
|
+
/**
|
|
36
|
+
* Association decorators a decorators model file may import from `sequelize-typescript`.
|
|
37
|
+
*/
|
|
38
|
+
const ASSOCIATION_DECORATORS = ['BelongsTo', 'BelongsToMany', 'HasMany', 'HasOne'];
|
|
39
|
+
/**
|
|
40
|
+
* Global type names a generated model references.
|
|
41
|
+
*/
|
|
42
|
+
const REFERENCED_GLOBAL_TYPE_NAMES = ['Date'];
|
|
43
|
+
/**
|
|
44
|
+
* Leftmost identifier of an entity name, e.g. `ns` for `ns.Inner.Type`.
|
|
45
|
+
* @param {ts.EntityName} name
|
|
46
|
+
* @returns {ts.Identifier}
|
|
47
|
+
*/
|
|
48
|
+
const leftmostIdentifier = (name) => ts.isIdentifier(name) ? name : leftmostIdentifier(name.left);
|
|
49
|
+
/**
|
|
50
|
+
* Collect the identifiers a type node references by name: the leftmost
|
|
51
|
+
* identifier of every type reference and type query.
|
|
52
|
+
* @param {ts.Node} node
|
|
53
|
+
* @param {Set<string>} identifiers
|
|
54
|
+
* @returns {void}
|
|
55
|
+
*/
|
|
56
|
+
const collectReferencedIdentifiers = (node, identifiers) => {
|
|
57
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
58
|
+
identifiers.add(leftmostIdentifier(node.typeName).text);
|
|
59
|
+
}
|
|
60
|
+
else if (ts.isTypeQueryNode(node)) {
|
|
61
|
+
identifiers.add(leftmostIdentifier(node.exprName).text);
|
|
62
|
+
}
|
|
63
|
+
ts.forEachChild(node, child => collectReferencedIdentifiers(child, identifiers));
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Collect the identifiers the enum shared type names must not take in either
|
|
67
|
+
* format: the generated model names, and every other name a model file declares
|
|
68
|
+
* (decorators attributes interfaces), imports (`sequelize`/`sequelize-typescript`
|
|
69
|
+
* names, the `Json` shared type) or references (global types, dialect type
|
|
70
|
+
* names, type override TypeScript types).
|
|
71
|
+
* @param {ITablesMetadata} tablesMetadata
|
|
72
|
+
* @param {Dialect} dialect
|
|
73
|
+
* @returns {IReservedIdentifiers}
|
|
74
|
+
*/
|
|
75
|
+
const collectReservedIdentifiers = (tablesMetadata, dialect) => {
|
|
76
|
+
const tables = Object.values(tablesMetadata);
|
|
77
|
+
const tablesByModel = indexTablesByModelName(tablesMetadata);
|
|
78
|
+
const nonModelIdentifiers = new Set([
|
|
79
|
+
...DECORATORS_BASE_IMPORTS,
|
|
80
|
+
...ASSOCIATION_DECORATORS,
|
|
81
|
+
...REFERENCED_GLOBAL_TYPE_NAMES,
|
|
82
|
+
JSON_TYPE_NAME,
|
|
83
|
+
]);
|
|
84
|
+
for (const table of tables) {
|
|
85
|
+
nonModelIdentifiers.add(`${table.name}Attributes`);
|
|
86
|
+
collectSequelizeImports(table, tablesByModel).forEach(name => nonModelIdentifiers.add(name));
|
|
87
|
+
for (const column of Object.values(table.columns)) {
|
|
88
|
+
const jsType = dialect.mapDbTypeToJs(column.type);
|
|
89
|
+
if (jsType) {
|
|
90
|
+
nonModelIdentifiers.add(jsType.replace(/(\[\])+$/, ''));
|
|
91
|
+
}
|
|
92
|
+
if (column.typeOverride?.tsType) {
|
|
93
|
+
collectReferencedIdentifiers(column.typeOverride.tsType, nonModelIdentifiers);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return { modelNames: new Set(tables.map(table => table.name)), nonModelIdentifiers };
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Build the TypeScript type node of a column: a type override's TypeScript type,
|
|
101
|
+
* the shared `Json` type for a JSON column, the enum shared type for a database
|
|
102
|
+
* enum column, otherwise the dialect JS mapping, or `any` for an unmapped type.
|
|
18
103
|
* @param {IColumnMetadata} col
|
|
19
104
|
* @param {Dialect} dialect
|
|
20
105
|
* @returns {ts.TypeNode}
|
|
21
106
|
*/
|
|
22
|
-
const buildColumnTypeNode = (col, dialect) =>
|
|
23
|
-
|
|
24
|
-
|
|
107
|
+
const buildColumnTypeNode = (col, dialect) => {
|
|
108
|
+
const overriddenTsType = col.typeOverride?.tsType;
|
|
109
|
+
if (overriddenTsType) {
|
|
110
|
+
return overriddenTsType;
|
|
111
|
+
}
|
|
112
|
+
if (col.isJson) {
|
|
113
|
+
return createGenericTypeReference(JSON_TYPE_NAME, []);
|
|
114
|
+
}
|
|
115
|
+
return col.enumType
|
|
116
|
+
? buildEnumColumnTypeNode(col.enumType)
|
|
117
|
+
: createTypeNodeFromName(dialect.mapDbTypeToJs(col.type) ?? UNMAPPED_TS_TYPE_BY_FORMAT.decorators);
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Build the `type` option of the `@Column` decorator: a type override's data type
|
|
121
|
+
* is built as a compiler expression, otherwise the rendered data type expression.
|
|
122
|
+
* @param {IColumnMetadata} col
|
|
123
|
+
* @returns {string | ts.Expression | undefined}
|
|
124
|
+
*/
|
|
125
|
+
const buildColumnDecoratorType = (col) => {
|
|
126
|
+
const overriddenDataType = col.typeOverride?.dataType;
|
|
127
|
+
return overriddenDataType
|
|
128
|
+
? buildDataTypeExpression(overriddenDataType, DATA_TYPE_NAMESPACES.decorators)
|
|
129
|
+
: col.dataType;
|
|
130
|
+
};
|
|
25
131
|
/**
|
|
26
132
|
* Build the `@Table` decorator options for a table. The `hasTrigger` flag is
|
|
27
133
|
* emitted only when the source table carries at least one enabled trigger.
|
|
@@ -91,12 +197,13 @@ export class ModelBuilder extends Builder {
|
|
|
91
197
|
*/
|
|
92
198
|
static buildColumnPropertyDecl(col, dialect) {
|
|
93
199
|
const buildColumnDecoratorProps = (col) => {
|
|
200
|
+
const columnType = buildColumnDecoratorType(col);
|
|
94
201
|
const props = {
|
|
95
202
|
...col.originName && col.name !== col.originName && { field: col.originName },
|
|
96
203
|
...col.primaryKey && { primaryKey: col.primaryKey },
|
|
97
204
|
...col.autoIncrement && { autoIncrement: col.autoIncrement },
|
|
98
205
|
...col.allowNull && { allowNull: col.allowNull },
|
|
99
|
-
...
|
|
206
|
+
...columnType && { type: columnType },
|
|
100
207
|
...col.comment && { comment: col.comment },
|
|
101
208
|
...col.defaultValue !== undefined && { defaultValue: dialect.mapDefaultValueToSequelize(col.defaultValue) },
|
|
102
209
|
};
|
|
@@ -134,13 +241,7 @@ export class ModelBuilder extends Builder {
|
|
|
134
241
|
let generatedCode = '';
|
|
135
242
|
// Named imports from sequelize-typescript
|
|
136
243
|
generatedCode += nodeToString(generateNamedImports([
|
|
137
|
-
|
|
138
|
-
'Table',
|
|
139
|
-
'Column',
|
|
140
|
-
'DataType',
|
|
141
|
-
'Index',
|
|
142
|
-
'Sequelize',
|
|
143
|
-
foreignKeyDecorator,
|
|
244
|
+
...DECORATORS_BASE_IMPORTS,
|
|
144
245
|
...new Set(tableMetadata.associations?.map(a => a.associationName)),
|
|
145
246
|
], 'sequelize-typescript'));
|
|
146
247
|
generatedCode += '\n';
|
|
@@ -166,6 +267,12 @@ export class ModelBuilder extends Builder {
|
|
|
166
267
|
generatedCode += nodeToString(buildJsonTypeImport());
|
|
167
268
|
generatedCode += '\n';
|
|
168
269
|
}
|
|
270
|
+
// Type-only import of the enum shared types the model uses.
|
|
271
|
+
const enumTypesImport = buildEnumTypesImport(tableMetadata);
|
|
272
|
+
if (enumTypesImport) {
|
|
273
|
+
generatedCode += nodeToString(enumTypesImport);
|
|
274
|
+
generatedCode += '\n';
|
|
275
|
+
}
|
|
169
276
|
const attributesInterfaceName = `${name}Attributes`;
|
|
170
277
|
if (strict) {
|
|
171
278
|
generatedCode += '\n';
|
|
@@ -209,17 +316,22 @@ export class ModelBuilder extends Builder {
|
|
|
209
316
|
return generatedCode;
|
|
210
317
|
}
|
|
211
318
|
/**
|
|
212
|
-
* Build main index file
|
|
319
|
+
* Build main index file: every model file, then the shared type files the
|
|
320
|
+
* models use (type-only)
|
|
213
321
|
* @param {ITableMetadata[]} tablesMetadata
|
|
214
322
|
* @returns {string}
|
|
215
323
|
*/
|
|
216
324
|
static buildIndexExports(tablesMetadata) {
|
|
217
|
-
return
|
|
218
|
-
.map(t =>
|
|
325
|
+
return [
|
|
326
|
+
...Object.values(tablesMetadata).map(t => generateIndexExport(t.name)),
|
|
327
|
+
...buildSharedTypesIndexExports(tablesMetadata),
|
|
328
|
+
]
|
|
329
|
+
.map(exportDeclaration => nodeToString(exportDeclaration))
|
|
219
330
|
.join('\n');
|
|
220
331
|
}
|
|
221
332
|
/**
|
|
222
|
-
* Render the decorators output as one file per table
|
|
333
|
+
* Render the decorators output as one file per table, the shared type files the
|
|
334
|
+
* models use, and the index barrel.
|
|
223
335
|
* @param {ITablesMetadata} tablesMetadata
|
|
224
336
|
* @param {Dialect} dialect
|
|
225
337
|
* @param {boolean | undefined} strict
|
|
@@ -234,6 +346,10 @@ export class ModelBuilder extends Builder {
|
|
|
234
346
|
warnJsonSupportFileNameCollision(tablesMetadata);
|
|
235
347
|
files.push({ fileName: JSON_SUPPORT_FILE_NAME, content: renderJsonSupportFile() });
|
|
236
348
|
}
|
|
349
|
+
if (tablesHaveSharedEnumType(tablesMetadata)) {
|
|
350
|
+
warnEnumSupportFileNameCollision(tablesMetadata);
|
|
351
|
+
files.push({ fileName: ENUM_SUPPORT_FILE_NAME, content: renderEnumSupportFile(tablesMetadata) });
|
|
352
|
+
}
|
|
237
353
|
files.push({ fileName: 'index.ts', content: ModelBuilder.buildIndexExports(tablesMetadata) });
|
|
238
354
|
return files;
|
|
239
355
|
}
|
|
@@ -250,12 +366,20 @@ export class ModelBuilder extends Builder {
|
|
|
250
366
|
if (shouldNoticeIgnoredStrict(this.config)) {
|
|
251
367
|
console.warn(STRICT_IGNORED_NOTICE);
|
|
252
368
|
}
|
|
369
|
+
const typeOverrides = await loadTypeOverrides(this.config.metadata);
|
|
253
370
|
console.log(`Fetching metadata from source`);
|
|
254
|
-
|
|
371
|
+
let tablesMetadata = await this.dialect.buildTablesMetadata(this.config);
|
|
255
372
|
if (Object.keys(tablesMetadata).length === 0) {
|
|
256
373
|
console.warn(`Couldn't find any table for database ${this.config.connection.database} and provided filters`);
|
|
257
374
|
return;
|
|
258
375
|
}
|
|
376
|
+
if (typeOverrides) {
|
|
377
|
+
const application = applyTypeOverrides(tablesMetadata, typeOverrides);
|
|
378
|
+
tablesMetadata = application.tablesMetadata;
|
|
379
|
+
warnUnmatchedTypeOverrides(application.unmatchedEntries);
|
|
380
|
+
}
|
|
381
|
+
tablesMetadata = assignSharedEnumTypeNames(tablesMetadata, collectReservedIdentifiers(tablesMetadata, this.dialect));
|
|
382
|
+
warnUnmappedTypes(tablesMetadata, this.dialect, format);
|
|
259
383
|
// Check if output dir exists
|
|
260
384
|
try {
|
|
261
385
|
await fs.access(outDir);
|
|
@@ -6,8 +6,9 @@ import { INativeIndex } from './nativeAttributes.js';
|
|
|
6
6
|
import { IMixinDeclaration } from './nativeAssociations.js';
|
|
7
7
|
/**
|
|
8
8
|
* Build the declared type node of a column attribute. Foreign keys to generated
|
|
9
|
-
* models are branded with `ForeignKey<Target["key"]
|
|
10
|
-
*
|
|
9
|
+
* models are branded with `ForeignKey<Target["key"]>`, or with the type override's
|
|
10
|
+
* TypeScript type when one is set; other columns are wrapped with
|
|
11
|
+
* `CreationOptional` and `| null` according to their attribute kind.
|
|
11
12
|
* @param {IColumnMetadata} column
|
|
12
13
|
* @param {ITableMetadata} table
|
|
13
14
|
* @param {Dialect} dialect
|
|
@@ -89,7 +90,7 @@ export declare const buildInitModelMethod: (table: ITableMetadata, dialect: Dial
|
|
|
89
90
|
export declare const buildModelClassDeclaration: (table: ITableMetadata, dialect: Dialect, tablesByModel: ReadonlyMap<string, ITableMetadata>) => ts.ClassDeclaration;
|
|
90
91
|
/**
|
|
91
92
|
* Render one native model source file: the `sequelize` named import, the
|
|
92
|
-
* cross-model `import type` declarations and the model class.
|
|
93
|
+
* cross-model and shared type `import type` declarations and the model class.
|
|
93
94
|
* @param {ITableMetadata} table
|
|
94
95
|
* @param {Dialect} dialect
|
|
95
96
|
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
@@ -2,10 +2,12 @@ import * as ts from 'typescript';
|
|
|
2
2
|
import { DATA_TYPE_NAMESPACES } from '../dialects/dataTypes.js';
|
|
3
3
|
import { attachColumnCommentJsDoc, buildDataTypeExpression, buildObjectLiteralExpression, createGenericTypeReference, createIndexedAccessTypeNode, createTypeNodeFromName, generateNamedImports, generateTypeOnlyImport, nodeToString, } from './utils.js';
|
|
4
4
|
import { buildDefaultValueExpression, parseDefaultValue } from './defaultValues.js';
|
|
5
|
+
import { UNMAPPED_TS_TYPE_BY_FORMAT } from './unmappedTypes.js';
|
|
5
6
|
import { classifyAttribute, collectTableIndexes, COLUMN_OPTION_KEYS, isParanoidColumn, resolveForeignKeyTargetAttribute, resolvePrimaryKeyAttribute, TIMESTAMP_ATTRIBUTES, } from './nativeAttributes.js';
|
|
6
7
|
import { buildAssociationMixinDeclarations, collectModelTypeImports, collectSequelizeImports, isToManyAssociation, resolveAssociationForeignKey, } from './nativeAssociations.js';
|
|
7
8
|
import { resolveAssociationPropertyName } from './associationNaming.js';
|
|
8
9
|
import { buildJsonTypeImport, JSON_TYPE_NAME, tableHasJsonColumn } from './jsonSupport.js';
|
|
10
|
+
import { buildEnumColumnTypeNode, buildEnumTypesImport } from './enumSupport.js';
|
|
9
11
|
const NATIVE_NAMESPACE = DATA_TYPE_NAMESPACES.native;
|
|
10
12
|
/**
|
|
11
13
|
* Union a type node with the null literal type, flattening an existing union so
|
|
@@ -34,30 +36,41 @@ const buildDeclareField = (name, type, options = {}) => {
|
|
|
34
36
|
};
|
|
35
37
|
/**
|
|
36
38
|
* Build the base TypeScript type node of a column, before nullability and brand
|
|
37
|
-
* wrapping.
|
|
38
|
-
*
|
|
39
|
+
* wrapping. A type override's TypeScript type wins; otherwise paranoid soft-delete
|
|
40
|
+
* columns are always `Date`, JSON columns use the shared `Json` type, database
|
|
41
|
+
* enum columns use their enum shared type, other enum columns become a
|
|
42
|
+
* string-literal union, and everything else maps through the dialect JS mapping,
|
|
43
|
+
* or is `unknown` for an unmapped type.
|
|
39
44
|
* @param {IColumnMetadata} column
|
|
40
45
|
* @param {ITableMetadata} table
|
|
41
46
|
* @param {Dialect} dialect
|
|
42
47
|
* @returns {ts.TypeNode}
|
|
43
48
|
*/
|
|
44
49
|
const buildBaseTypeNode = (column, table, dialect) => {
|
|
50
|
+
const overriddenTsType = column.typeOverride?.tsType;
|
|
51
|
+
if (overriddenTsType) {
|
|
52
|
+
return overriddenTsType;
|
|
53
|
+
}
|
|
45
54
|
if (isParanoidColumn(column, table)) {
|
|
46
55
|
return createTypeNodeFromName('Date');
|
|
47
56
|
}
|
|
48
57
|
if (column.isJson) {
|
|
49
58
|
return createGenericTypeReference(JSON_TYPE_NAME, []);
|
|
50
59
|
}
|
|
60
|
+
if (column.enumType) {
|
|
61
|
+
return buildEnumColumnTypeNode(column.enumType);
|
|
62
|
+
}
|
|
51
63
|
const { sequelizeType } = column;
|
|
52
64
|
if (sequelizeType && sequelizeType.key === 'ENUM' && sequelizeType.args.length > 0) {
|
|
53
65
|
return ts.factory.createUnionTypeNode(sequelizeType.args.map(arg => ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(String(arg)))));
|
|
54
66
|
}
|
|
55
|
-
return createTypeNodeFromName(dialect.mapDbTypeToJs(column.type));
|
|
67
|
+
return createTypeNodeFromName(dialect.mapDbTypeToJs(column.type) ?? UNMAPPED_TS_TYPE_BY_FORMAT.native);
|
|
56
68
|
};
|
|
57
69
|
/**
|
|
58
70
|
* Build the declared type node of a column attribute. Foreign keys to generated
|
|
59
|
-
* models are branded with `ForeignKey<Target["key"]
|
|
60
|
-
*
|
|
71
|
+
* models are branded with `ForeignKey<Target["key"]>`, or with the type override's
|
|
72
|
+
* TypeScript type when one is set; other columns are wrapped with
|
|
73
|
+
* `CreationOptional` and `| null` according to their attribute kind.
|
|
61
74
|
* @param {IColumnMetadata} column
|
|
62
75
|
* @param {ITableMetadata} table
|
|
63
76
|
* @param {Dialect} dialect
|
|
@@ -73,9 +86,9 @@ export const buildAttributeTypeNode = (column, table, dialect, tablesByModel) =>
|
|
|
73
86
|
const targetAttribute = target
|
|
74
87
|
? resolveForeignKeyTargetAttribute(column.foreignKey, target)
|
|
75
88
|
: undefined;
|
|
76
|
-
const referenced = targetAttribute
|
|
89
|
+
const referenced = column.typeOverride?.tsType ?? (targetAttribute
|
|
77
90
|
? createIndexedAccessTypeNode(column.foreignKey.targetModel, targetAttribute)
|
|
78
|
-
: ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
|
|
91
|
+
: ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword));
|
|
79
92
|
const inner = kind === 'foreignKeyNullable' ? buildNullableTypeNode(referenced) : referenced;
|
|
80
93
|
return createGenericTypeReference('ForeignKey', [inner]);
|
|
81
94
|
}
|
|
@@ -176,11 +189,13 @@ export const buildColumnInitOptions = (column, table, dialect) => {
|
|
|
176
189
|
const options = {};
|
|
177
190
|
for (const key of COLUMN_OPTION_KEYS) {
|
|
178
191
|
switch (key) {
|
|
179
|
-
case 'type':
|
|
180
|
-
|
|
181
|
-
|
|
192
|
+
case 'type': {
|
|
193
|
+
const dataType = column.typeOverride?.dataType ?? column.sequelizeType;
|
|
194
|
+
if (dataType) {
|
|
195
|
+
options.type = buildDataTypeExpression(dataType, NATIVE_NAMESPACE);
|
|
182
196
|
}
|
|
183
197
|
break;
|
|
198
|
+
}
|
|
184
199
|
case 'primaryKey':
|
|
185
200
|
if (column.primaryKey) {
|
|
186
201
|
options.primaryKey = true;
|
|
@@ -338,7 +353,7 @@ export const buildModelClassDeclaration = (table, dialect, tablesByModel) => {
|
|
|
338
353
|
};
|
|
339
354
|
/**
|
|
340
355
|
* Render one native model source file: the `sequelize` named import, the
|
|
341
|
-
* cross-model `import type` declarations and the model class.
|
|
356
|
+
* cross-model and shared type `import type` declarations and the model class.
|
|
342
357
|
* @param {ITableMetadata} table
|
|
343
358
|
* @param {Dialect} dialect
|
|
344
359
|
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
@@ -355,6 +370,11 @@ export const renderNativeModelFile = (table, dialect, tablesByModel) => {
|
|
|
355
370
|
code += nodeToString(buildJsonTypeImport());
|
|
356
371
|
code += '\n';
|
|
357
372
|
}
|
|
373
|
+
const enumTypesImport = buildEnumTypesImport(table);
|
|
374
|
+
if (enumTypesImport) {
|
|
375
|
+
code += nodeToString(enumTypesImport);
|
|
376
|
+
code += '\n';
|
|
377
|
+
}
|
|
358
378
|
code += '\n';
|
|
359
379
|
code += nodeToString(buildModelClassDeclaration(table, dialect, tablesByModel));
|
|
360
380
|
return code;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import type { IColumnEnumType, IColumnMetadata, ITableMetadata, ITablesMetadata } from '../dialects/Dialect.js';
|
|
3
|
+
/**
|
|
4
|
+
* Base name of the shared support file holding the enum shared types. A
|
|
5
|
+
* collision with a model name is reported by `warnEnumSupportFileNameCollision`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const ENUM_SUPPORT_FILE_BASENAME = "enums";
|
|
8
|
+
/**
|
|
9
|
+
* File name of the shared enum support file.
|
|
10
|
+
*/
|
|
11
|
+
export declare const ENUM_SUPPORT_FILE_NAME = "enums.ts";
|
|
12
|
+
/**
|
|
13
|
+
* Sibling module specifier used by model files to import enum shared types.
|
|
14
|
+
*/
|
|
15
|
+
export declare const ENUM_SUPPORT_MODULE_SPECIFIER = "./enums";
|
|
16
|
+
/**
|
|
17
|
+
* Suffix appended to an enum shared type name that clashes with a generated
|
|
18
|
+
* model name or another identifier declared or imported by a model file.
|
|
19
|
+
*/
|
|
20
|
+
export declare const ENUM_RESERVED_NAME_SUFFIX = "Enum";
|
|
21
|
+
/**
|
|
22
|
+
* A database enum used by at least one generated column, with the identifier of
|
|
23
|
+
* its shared type.
|
|
24
|
+
*/
|
|
25
|
+
export type ISharedEnumType = Pick<IColumnEnumType, 'schema' | 'name' | 'labels'> & {
|
|
26
|
+
typeName: string;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Report whether a column is typed with an enum shared type: an enum column
|
|
30
|
+
* whose TypeScript type no type override replaces.
|
|
31
|
+
* @param {IColumnMetadata} column
|
|
32
|
+
* @returns {boolean}
|
|
33
|
+
*/
|
|
34
|
+
export declare const columnUsesSharedEnumType: (column: IColumnMetadata) => boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Turn a database name into a Pascal-case identifier, dropping characters that
|
|
37
|
+
* are not valid in an identifier and prefixing a name that does not start with
|
|
38
|
+
* a valid identifier character.
|
|
39
|
+
* @param {string} name
|
|
40
|
+
* @returns {string}
|
|
41
|
+
*/
|
|
42
|
+
export declare const toPascalCaseIdentifier: (name: string) => string;
|
|
43
|
+
/**
|
|
44
|
+
* Identifiers the shared enum type names must not take: the generated model
|
|
45
|
+
* names and every other identifier a model file declares, imports or
|
|
46
|
+
* references.
|
|
47
|
+
*/
|
|
48
|
+
export interface IReservedIdentifiers {
|
|
49
|
+
modelNames: ReadonlySet<string>;
|
|
50
|
+
nonModelIdentifiers: ReadonlySet<string>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Name the enum shared types of the run and record each name on the columns
|
|
54
|
+
* typed with it. Names are resolved over every database enum of the generated
|
|
55
|
+
* columns, so a type override never renames another enum; an enum whose
|
|
56
|
+
* TypeScript type a type override replaces on every column gets no shared type.
|
|
57
|
+
* A warning is emitted for every renamed enum that has a shared type.
|
|
58
|
+
* @param {ITablesMetadata} tablesMetadata
|
|
59
|
+
* @param {IReservedIdentifiers} reserved
|
|
60
|
+
* @returns {ITablesMetadata}
|
|
61
|
+
*/
|
|
62
|
+
export declare const assignSharedEnumTypeNames: (tablesMetadata: ITablesMetadata, reserved: IReservedIdentifiers) => ITablesMetadata;
|
|
63
|
+
/**
|
|
64
|
+
* Build the TypeScript type of an enum column: a reference to its shared type,
|
|
65
|
+
* or the union of its labels when no shared type name is assigned, as an array
|
|
66
|
+
* for an enum array column.
|
|
67
|
+
* @param {IColumnEnumType} enumType
|
|
68
|
+
* @returns {ts.TypeNode}
|
|
69
|
+
*/
|
|
70
|
+
export declare const buildEnumColumnTypeNode: (enumType: IColumnEnumType) => ts.TypeNode;
|
|
71
|
+
/**
|
|
72
|
+
* Report whether any table has a column typed with an enum shared type.
|
|
73
|
+
* @param {ITablesMetadata} tablesMetadata
|
|
74
|
+
* @returns {boolean}
|
|
75
|
+
*/
|
|
76
|
+
export declare const tablesHaveSharedEnumType: (tablesMetadata: ITablesMetadata) => boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Build the exported type alias of an enum shared type, e.g.
|
|
79
|
+
* `export type PlanTier = "free" | "pro";`, with the labels in database order.
|
|
80
|
+
* @param {ISharedEnumType} sharedEnumType
|
|
81
|
+
* @returns {ts.TypeAliasDeclaration}
|
|
82
|
+
*/
|
|
83
|
+
export declare const buildSharedEnumTypeAliasDeclaration: (sharedEnumType: ISharedEnumType) => ts.TypeAliasDeclaration;
|
|
84
|
+
/**
|
|
85
|
+
* Render the shared enum support file: one exported type alias per enum shared
|
|
86
|
+
* type used by a generated column, sorted by type name.
|
|
87
|
+
* @param {ITablesMetadata} tablesMetadata
|
|
88
|
+
* @returns {string}
|
|
89
|
+
*/
|
|
90
|
+
export declare const renderEnumSupportFile: (tablesMetadata: ITablesMetadata) => string;
|
|
91
|
+
/**
|
|
92
|
+
* Build the type-only import of the enum shared types a model uses, or
|
|
93
|
+
* undefined when it uses none.
|
|
94
|
+
* @param {ITableMetadata} table
|
|
95
|
+
* @returns {ts.ImportDeclaration | undefined}
|
|
96
|
+
*/
|
|
97
|
+
export declare const buildEnumTypesImport: (table: ITableMetadata) => ts.ImportDeclaration | undefined;
|
|
98
|
+
/**
|
|
99
|
+
* Warn when a generated model name collides with the shared enum support file
|
|
100
|
+
* name, which would make both write to the same file.
|
|
101
|
+
* @param {ITablesMetadata} tablesMetadata
|
|
102
|
+
* @returns {void}
|
|
103
|
+
*/
|
|
104
|
+
export declare const warnEnumSupportFileNameCollision: (tablesMetadata: ITablesMetadata) => void;
|