sequelize-typescript-generator 13.0.1 → 13.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +274 -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 +2 -1
  6. package/build/builders/ModelBuilder.js +155 -15
  7. package/build/builders/NativeModelRenderer.d.ts +4 -3
  8. package/build/builders/NativeModelRenderer.js +41 -13
  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 +12 -1
  13. package/build/builders/jsonSupport.d.ts +69 -0
  14. package/build/builders/jsonSupport.js +89 -0
  15. package/build/builders/typeOverrides.d.ts +30 -0
  16. package/build/builders/typeOverrides.js +93 -0
  17. package/build/builders/typeOverridesParser.d.ts +48 -0
  18. package/build/builders/typeOverridesParser.js +305 -0
  19. package/build/builders/unmappedTypes.d.ts +50 -0
  20. package/build/builders/unmappedTypes.js +62 -0
  21. package/build/builders/utils.d.ts +20 -2
  22. package/build/builders/utils.js +42 -5
  23. package/build/config/IConfig.d.ts +22 -0
  24. package/build/dialects/Dialect.d.ts +57 -3
  25. package/build/dialects/DialectMSSQL.d.ts +2 -2
  26. package/build/dialects/DialectMSSQL.js +1 -6
  27. package/build/dialects/DialectMariaDB.d.ts +13 -2
  28. package/build/dialects/DialectMariaDB.js +46 -6
  29. package/build/dialects/DialectMySQL.d.ts +2 -2
  30. package/build/dialects/DialectMySQL.js +2 -6
  31. package/build/dialects/DialectPostgres.d.ts +36 -3
  32. package/build/dialects/DialectPostgres.js +111 -13
  33. package/build/dialects/DialectSQLite.d.ts +2 -2
  34. package/build/dialects/DialectSQLite.js +1 -6
  35. package/build/dialects/dataTypes.d.ts +26 -4
  36. package/build/dialects/dataTypes.js +50 -5
  37. package/build/dialects/mariadbJson.d.ts +39 -0
  38. package/build/dialects/mariadbJson.js +52 -0
  39. package/build/dialects/utils.d.ts +0 -6
  40. package/build/dialects/utils.js +0 -10
  41. package/build/index.d.ts +2 -2
  42. package/package.json +3 -3
@@ -1,4 +1,10 @@
1
1
  import { DataTypes } from 'sequelize';
2
+ /**
3
+ * Type guard for a nested data type argument.
4
+ * @param {DataTypeArgument} arg
5
+ * @returns {boolean}
6
+ */
7
+ export const isNestedDataType = (arg) => typeof arg === 'object' && arg !== null && 'key' in arg && 'args' in arg;
2
8
  /**
3
9
  * Namespaces under which a data type expression is rendered.
4
10
  */
@@ -12,6 +18,20 @@ export const DATA_TYPE_NAMESPACES = {
12
18
  * @returns {boolean}
13
19
  */
14
20
  export const isSequelizeDataTypeKey = (value) => Object.prototype.hasOwnProperty.call(DataTypes, value);
21
+ /**
22
+ * Type guard for a DataTypes key naming a concrete data type usable as a column
23
+ * type: a data type class derived from ABSTRACT. Dialect namespaces (`postgres`,
24
+ * `mysql`, ...) and ABSTRACT itself are rejected.
25
+ * @param {string} value
26
+ * @returns {boolean}
27
+ */
28
+ export const isColumnDataTypeKey = (value) => {
29
+ if (!isSequelizeDataTypeKey(value)) {
30
+ return false;
31
+ }
32
+ const dataType = DataTypes[value];
33
+ return typeof dataType === 'function' && dataType.prototype instanceof DataTypes.ABSTRACT;
34
+ };
15
35
  /**
16
36
  * Resolve the DataTypes key for a constructor. The first token of the constructor
17
37
  * key is used so that 'DOUBLE PRECISION' resolves to 'DOUBLE'. Returns undefined when
@@ -46,21 +66,46 @@ export const buildSequelizeDataType = (ctor, args) => {
46
66
  return { key, args: filteredArgs };
47
67
  };
48
68
  /**
49
- * Render a single data type argument. Numbers render bare; strings render
50
- * single-quoted with internal single quotes doubled.
69
+ * Render a string as a single-quoted TypeScript string literal. Backslashes,
70
+ * single quotes and control characters are backslash-escaped; double quotes
71
+ * stay bare.
72
+ * @param {string} value
73
+ * @returns {string}
74
+ */
75
+ const renderStringLiteral = (value) => {
76
+ const escaped = JSON.stringify(value)
77
+ .slice(1, -1)
78
+ .replace(/\\"/g, '"')
79
+ .replace(/'/g, "\\'");
80
+ return `'${escaped}'`;
81
+ };
82
+ /**
83
+ * Render a single data type argument. Numbers render bare; strings render as
84
+ * single-quoted TypeScript string literals; a nested data type renders as its
85
+ * own expression under the same namespace.
51
86
  * @param {DataTypeArgument} arg
87
+ * @param {DataTypeNamespace} namespace
52
88
  * @returns {string}
53
89
  */
54
- const renderDataTypeArgument = (arg) => typeof arg === 'number' ? String(arg) : `'${arg.replace(/'/g, "''")}'`;
90
+ const renderDataTypeArgument = (arg, namespace) => {
91
+ if (typeof arg === 'number') {
92
+ return String(arg);
93
+ }
94
+ if (typeof arg === 'string') {
95
+ return renderStringLiteral(arg);
96
+ }
97
+ return renderDataTypeExpression(arg, namespace);
98
+ };
55
99
  /**
56
- * Render a data type expression, e.g. DataType.DECIMAL(7,3) or DataTypes.ENUM('AA','BB').
100
+ * Render a data type expression, e.g. DataType.DECIMAL(7,3), DataTypes.ENUM('AA','BB')
101
+ * or DataTypes.ARRAY(DataTypes.INTEGER).
57
102
  * @param {ISequelizeDataType} dataType
58
103
  * @param {DataTypeNamespace} namespace
59
104
  * @returns {string}
60
105
  */
61
106
  export const renderDataTypeExpression = (dataType, namespace) => {
62
107
  const signature = dataType.args.length
63
- ? `(${dataType.args.map(renderDataTypeArgument).join(',')})`
108
+ ? `(${dataType.args.map(arg => renderDataTypeArgument(arg, namespace)).join(',')})`
64
109
  : '';
65
110
  return `${namespace}.${dataType.key}${signature}`;
66
111
  };
@@ -0,0 +1,39 @@
1
+ /**
2
+ * MariaDB stores a `JSON` column as `longtext` with a `CHECK (json_valid(<col>))`
3
+ * constraint. These helpers recover the JSON columns of a table from the check
4
+ * clauses (information_schema.CHECK_CONSTRAINTS) or, as a fallback for MariaDB
5
+ * versions without that view, from the `SHOW CREATE TABLE` statement.
6
+ */
7
+ /**
8
+ * A row of information_schema.CHECK_CONSTRAINTS relevant to JSON detection.
9
+ */
10
+ export interface ICheckConstraintRow {
11
+ CHECK_CLAUSE: string;
12
+ }
13
+ /**
14
+ * A row of `SHOW CREATE TABLE`, whose second column holds the create statement.
15
+ */
16
+ export interface IShowCreateTableRow {
17
+ 'Create Table'?: string;
18
+ 'CREATE TABLE'?: string;
19
+ }
20
+ /**
21
+ * Extract the column names guarded by a `json_valid(...)` check from a set of
22
+ * check clauses.
23
+ * @param {ICheckConstraintRow[]} rows
24
+ * @returns {Set<string>}
25
+ */
26
+ export declare const extractJsonColumnsFromCheckConstraints: (rows: ICheckConstraintRow[]) => Set<string>;
27
+ /**
28
+ * Extract the column names guarded by a `json_valid(...)` check from a
29
+ * `SHOW CREATE TABLE` statement.
30
+ * @param {string} createTableStatement
31
+ * @returns {Set<string>}
32
+ */
33
+ export declare const extractJsonColumnsFromCreateTable: (createTableStatement: string) => Set<string>;
34
+ /**
35
+ * Read the create statement out of a `SHOW CREATE TABLE` result row.
36
+ * @param {IShowCreateTableRow | undefined} row
37
+ * @returns {string}
38
+ */
39
+ export declare const getCreateTableStatement: (row: IShowCreateTableRow | undefined) => string;
@@ -0,0 +1,52 @@
1
+ /**
2
+ * MariaDB stores a `JSON` column as `longtext` with a `CHECK (json_valid(<col>))`
3
+ * constraint. These helpers recover the JSON columns of a table from the check
4
+ * clauses (information_schema.CHECK_CONSTRAINTS) or, as a fallback for MariaDB
5
+ * versions without that view, from the `SHOW CREATE TABLE` statement.
6
+ */
7
+ /**
8
+ * Match a `json_valid(<column>)` call, capturing the column name with its
9
+ * optional surrounding backticks stripped. Case-insensitive and global so every
10
+ * occurrence in a statement can be scanned.
11
+ */
12
+ const JSON_VALID_PATTERN = /json_valid\(\s*`?([^`()\s]+)`?\s*\)/gi;
13
+ /**
14
+ * Extract the column names guarded by a `json_valid(...)` check from a set of
15
+ * check clauses.
16
+ * @param {ICheckConstraintRow[]} rows
17
+ * @returns {Set<string>}
18
+ */
19
+ export const extractJsonColumnsFromCheckConstraints = (rows) => {
20
+ const columns = new Set();
21
+ for (const row of rows) {
22
+ for (const columnName of matchJsonValidColumns(row.CHECK_CLAUSE ?? '')) {
23
+ columns.add(columnName);
24
+ }
25
+ }
26
+ return columns;
27
+ };
28
+ /**
29
+ * Extract the column names guarded by a `json_valid(...)` check from a
30
+ * `SHOW CREATE TABLE` statement.
31
+ * @param {string} createTableStatement
32
+ * @returns {Set<string>}
33
+ */
34
+ export const extractJsonColumnsFromCreateTable = (createTableStatement) => new Set(matchJsonValidColumns(createTableStatement));
35
+ /**
36
+ * Read the create statement out of a `SHOW CREATE TABLE` result row.
37
+ * @param {IShowCreateTableRow | undefined} row
38
+ * @returns {string}
39
+ */
40
+ export const getCreateTableStatement = (row) => row?.['Create Table'] ?? row?.['CREATE TABLE'] ?? '';
41
+ /**
42
+ * Collect every column name captured by a `json_valid(...)` call in a string.
43
+ * @param {string} value
44
+ * @returns {string[]}
45
+ */
46
+ const matchJsonValidColumns = (value) => {
47
+ const columns = [];
48
+ for (const match of value.matchAll(JSON_VALID_PATTERN)) {
49
+ columns.push(match[1]);
50
+ }
51
+ return columns;
52
+ };
@@ -27,9 +27,3 @@ export declare const getTransformer: (transformCase: TransformCase | TransformMa
27
27
  * @returns {ITableMetadata}
28
28
  */
29
29
  export declare const caseTransformer: (tableMetadata: ITableMetadata, transformCase: TransformCase | TransformMap | TransformFn) => ITableMetadata;
30
- /**
31
- * Unknown mapping warning
32
- * @param {string} dataType
33
- * @returns {string}
34
- */
35
- export declare const warnUnknownMappingForDataType: (dataType: string) => void;
@@ -140,13 +140,3 @@ export const caseTransformer = (tableMetadata, transformCase) => {
140
140
  }
141
141
  return transformed;
142
142
  };
143
- /**
144
- * Unknown mapping warning
145
- * @param {string} dataType
146
- * @returns {string}
147
- */
148
- export const warnUnknownMappingForDataType = (dataType) => {
149
- console.warn(`[Warning]`, `Unknown data type mapping for type '${dataType}'.
150
- You should define the data type manually.
151
- `);
152
- };
package/build/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  export { Dialect } from './dialects/Dialect.js';
2
- export type { DialectName, ITablesMetadata, ITableMetadata, IColumnMetadata, IIndexMetadata, ITable, IColumnForeignKeyMetadata, IForeignKeyConstraintMetadata, } from './dialects/Dialect.js';
2
+ export type { DialectName, ITablesMetadata, ITableMetadata, IColumnMetadata, IColumnTypeOverride, IIndexMetadata, ITable, IColumnForeignKeyMetadata, IForeignKeyConstraintMetadata, } from './dialects/Dialect.js';
3
3
  export { createDialect } from './dialects/createDialect.js';
4
4
  export type { ReferentialAction } from './dialects/foreignKeys.js';
5
5
  export type { ISequelizeDataType, SequelizeDataTypeKey } from './dialects/dataTypes.js';
6
6
  export type { IAssociationMetadata } from './dialects/AssociationsParser.js';
7
7
  export { ModelBuilder } from './builders/index.js';
8
8
  export { TransformTarget, TransformCases } from './config/IConfig.js';
9
- export type { IConfig, IConfigMetadata, IConfigOutput, TransformCase, TransformMap, TransformFn, } from './config/IConfig.js';
9
+ export type { IConfig, IConfigMetadata, IConfigOutput, TransformCase, TransformMap, TransformFn, ITypeOverride, ITypeOverrides, } from './config/IConfig.js';
10
10
  export { FORMATS, DEFAULT_FORMAT, isFormat } from './config/format.js';
11
11
  export type { Format } from './config/format.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sequelize-typescript-generator",
3
- "version": "13.0.1",
3
+ "version": "13.2.0",
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",
@@ -63,7 +63,7 @@
63
63
  "reflect-metadata": "^0.2.1",
64
64
  "sequelize-typescript": "^2.1.6",
65
65
  "@vscode/sqlite3": "^5.1.14-vscode",
66
- "tedious": "^18.6.1",
66
+ "tedious": "^20.3.3",
67
67
  "ts-jest": "^29.4.12",
68
68
  "tsx": "^4.23.13",
69
69
  "wkx": "^0.5.0"
@@ -76,7 +76,7 @@
76
76
  "pluralize": "^8.0.0",
77
77
  "sequelize": "^6.37.5",
78
78
  "typescript": ">=5.7 <6.1",
79
- "yargs": "^17.7.2"
79
+ "yargs": "^18.2.0"
80
80
  },
81
81
  "peerDependencies": {
82
82
  "typescript": ">=5.7 <6.1",