sequelize-typescript-generator 13.2.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 CHANGED
@@ -302,6 +302,22 @@ export function initModels(sequelize: Sequelize) {
302
302
  export type Models = ReturnType<typeof initModels>;
303
303
  ```
304
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
+
305
321
  Wire the models against a `Sequelize` instance and use them straight away:
306
322
 
307
323
  ```ts
@@ -1626,7 +1642,7 @@ export type PlanTier = "free" | "pro" | "enterprise";
1626
1642
  ```
1627
1643
 
1628
1644
  Models import the shared types they use with a type-only import (`import type { PlanTier } from "./enums";`)
1629
- and type enum columns with them. The data type is `ENUM` with the database labels in database order,
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,
1630
1646
  and enum arrays are typed as arrays of the shared type with an `ARRAY` of `ENUM` data type:
1631
1647
 
1632
1648
  ```ts
@@ -54,7 +54,8 @@ 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
  */
@@ -13,6 +13,7 @@ import { attachColumnCommentJsDoc, buildDataTypeExpression, nodeToString, create
13
13
  import { buildJsonTypeImport, JSON_SUPPORT_FILE_NAME, JSON_TYPE_NAME, renderJsonSupportFile, tableHasJsonColumn, tablesHaveJsonColumn, warnJsonSupportFileNameCollision, } from './jsonSupport.js';
14
14
  import { assignSharedEnumTypeNames, buildEnumColumnTypeNode, buildEnumTypesImport, ENUM_SUPPORT_FILE_NAME, renderEnumSupportFile, tablesHaveSharedEnumType, warnEnumSupportFileNameCollision, } from './enumSupport.js';
15
15
  import { indexTablesByModelName } from './nativeAttributes.js';
16
+ import { buildSharedTypesIndexExports } from './sharedTypesIndexExports.js';
16
17
  import { collectSequelizeImports } from './nativeAssociations.js';
17
18
  import { UNMAPPED_TS_TYPE_BY_FORMAT, warnUnmappedTypes } from './unmappedTypes.js';
18
19
  import { loadTypeOverrides } from './typeOverridesParser.js';
@@ -315,13 +316,17 @@ export class ModelBuilder extends Builder {
315
316
  return generatedCode;
316
317
  }
317
318
  /**
318
- * Build main index file
319
+ * Build main index file: every model file, then the shared type files the
320
+ * models use (type-only)
319
321
  * @param {ITableMetadata[]} tablesMetadata
320
322
  * @returns {string}
321
323
  */
322
324
  static buildIndexExports(tablesMetadata) {
323
- return Object.values(tablesMetadata)
324
- .map(t => nodeToString(generateIndexExport(t.name)))
325
+ return [
326
+ ...Object.values(tablesMetadata).map(t => generateIndexExport(t.name)),
327
+ ...buildSharedTypesIndexExports(tablesMetadata),
328
+ ]
329
+ .map(exportDeclaration => nodeToString(exportDeclaration))
325
330
  .join('\n');
326
331
  }
327
332
  /**
@@ -44,7 +44,8 @@ export declare const buildModelsTypeAlias: () => ts.TypeAliasDeclaration;
44
44
  */
45
45
  export declare const renderInitModelsFile: (tablesMetadata: ITablesMetadata, tablesByModel: ReadonlyMap<string, ITableMetadata>) => string;
46
46
  /**
47
- * Render the `index.ts` barrel re-exporting every model file and the wiring file.
47
+ * Render the `index.ts` barrel re-exporting every model file, the shared type
48
+ * files the models use (type-only) and the wiring file.
48
49
  * @param {ITablesMetadata} tablesMetadata
49
50
  * @returns {string}
50
51
  */
@@ -1,6 +1,7 @@
1
1
  import * as ts from 'typescript';
2
2
  import { createGenericTypeReference, generateIndexExport, generateNamedImports, nodeToString, } from './utils.js';
3
3
  import { resolveAssociationWiring, } from './nativeAssociations.js';
4
+ import { buildSharedTypesIndexExports } from './sharedTypesIndexExports.js';
4
5
  /**
5
6
  * Build the `<model>.initModel(sequelize);` statement wiring a model into the
6
7
  * shared connection.
@@ -98,11 +99,16 @@ export const renderInitModelsFile = (tablesMetadata, tablesByModel) => {
98
99
  return code;
99
100
  };
100
101
  /**
101
- * Render the `index.ts` barrel re-exporting every model file and the wiring file.
102
+ * Render the `index.ts` barrel re-exporting every model file, the shared type
103
+ * files the models use (type-only) and the wiring file.
102
104
  * @param {ITablesMetadata} tablesMetadata
103
105
  * @returns {string}
104
106
  */
105
107
  export const renderNativeIndexFile = (tablesMetadata) => {
106
- const exportNames = [...Object.values(tablesMetadata).map(table => table.name), 'initModels'];
107
- return exportNames.map(name => nodeToString(generateIndexExport(name))).join('\n');
108
+ const exports = [
109
+ ...Object.values(tablesMetadata).map(table => generateIndexExport(table.name)),
110
+ ...buildSharedTypesIndexExports(tablesMetadata),
111
+ generateIndexExport('initModels'),
112
+ ];
113
+ return exports.map(exportDeclaration => nodeToString(exportDeclaration)).join('\n');
108
114
  };
@@ -0,0 +1,11 @@
1
+ import * as ts from 'typescript';
2
+ import type { ITablesMetadata } from '../dialects/Dialect.js';
3
+ /**
4
+ * Build the type-only index re-exports of the shared type files emitted next to
5
+ * the models: the `Json` support file when a column uses the `Json` type, then
6
+ * the enum support file when a column uses an enum shared type. A file that is
7
+ * not emitted gets no re-export.
8
+ * @param {ITablesMetadata} tablesMetadata
9
+ * @returns {ts.ExportDeclaration[]}
10
+ */
11
+ export declare const buildSharedTypesIndexExports: (tablesMetadata: ITablesMetadata) => ts.ExportDeclaration[];
@@ -0,0 +1,15 @@
1
+ import { generateTypeOnlyIndexExport } from './utils.js';
2
+ import { JSON_SUPPORT_FILE_BASENAME, tablesHaveJsonColumn } from './jsonSupport.js';
3
+ import { ENUM_SUPPORT_FILE_BASENAME, tablesHaveSharedEnumType } from './enumSupport.js';
4
+ /**
5
+ * Build the type-only index re-exports of the shared type files emitted next to
6
+ * the models: the `Json` support file when a column uses the `Json` type, then
7
+ * the enum support file when a column uses an enum shared type. A file that is
8
+ * not emitted gets no re-export.
9
+ * @param {ITablesMetadata} tablesMetadata
10
+ * @returns {ts.ExportDeclaration[]}
11
+ */
12
+ export const buildSharedTypesIndexExports = (tablesMetadata) => [
13
+ ...tablesHaveJsonColumn(tablesMetadata) ? [generateTypeOnlyIndexExport(JSON_SUPPORT_FILE_BASENAME)] : [],
14
+ ...tablesHaveSharedEnumType(tablesMetadata) ? [generateTypeOnlyIndexExport(ENUM_SUPPORT_FILE_BASENAME)] : [],
15
+ ];
@@ -111,6 +111,13 @@ export declare const buildDataTypeExpression: (dataType: ISequelizeDataType, nam
111
111
  * @returns {ts.ExportDeclaration}
112
112
  */
113
113
  export declare const generateIndexExport: (modelFileName: string) => ts.ExportDeclaration;
114
+ /**
115
+ * Generate the type-only re-export of a shared type file for the index file,
116
+ * e.g. `export type * from "./enums";`
117
+ * @param {string} fileBaseName
118
+ * @returns {ts.ExportDeclaration}
119
+ */
120
+ export declare const generateTypeOnlyIndexExport: (fileBaseName: string) => ts.ExportDeclaration;
114
121
  /**
115
122
  * Generate object literal decorator
116
123
  * @param {string} decoratorIdentifier
@@ -203,6 +203,15 @@ export const buildDataTypeExpression = (dataType, namespace) => {
203
203
  export const generateIndexExport = (modelFileName) => {
204
204
  return ts.factory.createExportDeclaration(undefined, false, undefined, ts.factory.createStringLiteral(`./${modelFileName}`));
205
205
  };
206
+ /**
207
+ * Generate the type-only re-export of a shared type file for the index file,
208
+ * e.g. `export type * from "./enums";`
209
+ * @param {string} fileBaseName
210
+ * @returns {ts.ExportDeclaration}
211
+ */
212
+ export const generateTypeOnlyIndexExport = (fileBaseName) => {
213
+ return ts.factory.createExportDeclaration(undefined, true, undefined, ts.factory.createStringLiteral(`./${fileBaseName}`));
214
+ };
206
215
  /**
207
216
  * Generate object literal decorator
208
217
  * @param {string} decoratorIdentifier
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sequelize-typescript-generator",
3
- "version": "13.2.0",
3
+ "version": "13.2.1",
4
4
  "description": "Automatically generate type-safe Sequelize models from your source database via the TypeScript compiler API — native or sequelize-typescript (decorators) format.",
5
5
  "type": "module",
6
6
  "main": "build/index.js",