sequelize-typescript-generator 13.2.1 → 13.3.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.
package/README.md CHANGED
@@ -11,6 +11,7 @@
11
11
  * [Prerequisites](#prerequisites)
12
12
  * [Installation](#installation)
13
13
  * [CLI usage](#cli-usage)
14
+ * [Filtering tables](#filtering-tables)
14
15
  * [Output formats](#output-formats)
15
16
  * [Native format](#native-format)
16
17
  * [Decorators format](#decorators-format)
@@ -130,8 +131,12 @@ Options:
130
131
  - mssql [string] [required]
131
132
  -u, --username Database username [string]
132
133
  -x, --password Database password [string]
133
- -t, --tables Comma-separated names of tables to process[string]
134
- -T, --skip-tables Comma-separated names of tables to skip [string]
134
+ -t, --tables Comma-separated names of tables to process. Names
135
+ may contain * wildcards (e.g. 'user_*'), matched
136
+ case-insensitively [string]
137
+ -T, --skip-tables Comma-separated names of tables to skip. Names may
138
+ contain * wildcards (e.g. 'audit_*,tmp_*'),
139
+ matched case-insensitively [string]
135
140
  -i, --indices Include index annotations in the generated models
136
141
  [boolean]
137
142
  -o, --out-dir Output directory. Default:
@@ -198,6 +203,24 @@ Global usage example:
198
203
  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
199
204
  ```
200
205
 
206
+ ### Filtering tables
207
+ `-t` / `--tables` generates only the listed tables and `-T` / `--skip-tables` generates every table except the
208
+ listed ones. Both take a comma-separated list whose entries are exact table names or patterns containing `*`
209
+ wildcards, freely mixed. A `*` matches any sequence of characters, including none. Names and patterns are
210
+ matched case-insensitively. When both options are given, a table is generated if it matches `--tables` and
211
+ does not match `--skip-tables`. Quote the list so the shell does not expand the `*`:
212
+
213
+ ```shell
214
+ # Only the users table and every table starting with "order_"
215
+ stg -D postgres -d myDatabase -u myUsername -x myPassword -t 'users,order_*'
216
+
217
+ # Every table except the audit and temporary ones
218
+ stg -D postgres -d myDatabase -u myUsername -x myPassword -T 'audit_*,tmp_*'
219
+ ```
220
+
221
+ The same entries are accepted by the `tables` and `skipTables` metadata options in
222
+ [programmatic usage](#programmatic-usage). On MySQL and MariaDB, filters apply to views as well.
223
+
201
224
  ## Output formats
202
225
  The generator emits models in one of two formats, selected with `-F` / `--format` (or the `format`
203
226
  option in [programmatic usage](#programmatic-usage)):
package/build/bin/cli.js CHANGED
@@ -54,12 +54,12 @@ export const cli = async () => {
54
54
  .option('t', {
55
55
  alias: aliasesMap.TABLES,
56
56
  string: true,
57
- describe: `Comma-separated names of tables to process`,
57
+ describe: `Comma-separated names of tables to process. Names may contain * wildcards (e.g. 'user_*'), matched case-insensitively`,
58
58
  })
59
59
  .option('T', {
60
60
  alias: aliasesMap.SKIP_TABLES,
61
61
  string: true,
62
- describe: `Comma-separated names of tables to skip`,
62
+ describe: `Comma-separated names of tables to skip. Names may contain * wildcards (e.g. 'audit_*,tmp_*'), matched case-insensitively`,
63
63
  })
64
64
  .option('i', {
65
65
  alias: aliasesMap.INDICES,
@@ -5,6 +5,7 @@ import { applyForeignKeyConstraintsToColumns } from './foreignKeys.js';
5
5
  import { discoverAssociations, isAssociationDiscoveryEnabled } from './associationDiscovery.js';
6
6
  import { applyAssociationsFile } from './associationsFileMerge.js';
7
7
  import { findParanoidColumn, resolveParanoidOption } from './paranoid.js';
8
+ import { filterTablesByName } from './tableFilters.js';
8
9
  export const DIALECT_NAMES = [
9
10
  'postgres',
10
11
  'mysql',
@@ -56,23 +57,7 @@ export class Dialect {
56
57
  if (paranoidResolution.warning) {
57
58
  console.warn('[WARNING]', paranoidResolution.warning);
58
59
  }
59
- // Apply filters
60
- tables = tables
61
- .filter(({ name }) => {
62
- if (config.metadata?.tables?.length) {
63
- return config.metadata.tables.includes(name.toLowerCase());
64
- }
65
- else {
66
- return true;
67
- }
68
- }).filter(({ name }) => {
69
- if (config.metadata?.skipTables?.length) {
70
- return !(config.metadata.skipTables.includes(name.toLowerCase()));
71
- }
72
- else {
73
- return true;
74
- }
75
- });
60
+ tables = filterTablesByName(tables, config.metadata);
76
61
  for (const table of tables) {
77
62
  const { name: tableName, comment: tableComment } = table;
78
63
  const columnsMetadata = await this.fetchColumnsMetadata(connection, config, tableName);
@@ -0,0 +1,31 @@
1
+ import type { IConfigMetadata } from '../config/IConfig.js';
2
+ /**
3
+ * Wildcard character in a table filter pattern, matching any sequence of
4
+ * characters, including the empty one.
5
+ */
6
+ export declare const TABLE_FILTER_WILDCARD = "*";
7
+ /**
8
+ * Compile a table filter entry into a case-insensitive, fully anchored regular
9
+ * expression. An entry without a wildcard matches only the exact name.
10
+ * @param {string} pattern Exact table name or pattern containing `*` wildcards
11
+ * @returns {RegExp}
12
+ */
13
+ export declare const compileTableFilterPattern: (pattern: string) => RegExp;
14
+ /**
15
+ * Build a predicate reporting whether a table name matches at least one entry
16
+ * of a table filter. Entries are exact names or `*` wildcard patterns, matched
17
+ * case-insensitively.
18
+ * @param {string[]} patterns
19
+ * @returns {(name: string) => boolean}
20
+ */
21
+ export declare const createTableNameMatcher: (patterns: string[]) => (name: string) => boolean;
22
+ /**
23
+ * Keep the tables selected by the `tables` filter and not excluded by the
24
+ * `skipTables` filter. An absent or empty filter selects or excludes nothing.
25
+ * @param {T[]} tables
26
+ * @param {Pick<IConfigMetadata, 'tables' | 'skipTables'> | undefined} metadata
27
+ * @returns {T[]}
28
+ */
29
+ export declare const filterTablesByName: <T extends {
30
+ name: string;
31
+ }>(tables: T[], metadata: Pick<IConfigMetadata, "tables" | "skipTables"> | undefined) => T[];
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Wildcard character in a table filter pattern, matching any sequence of
3
+ * characters, including the empty one.
4
+ */
5
+ export const TABLE_FILTER_WILDCARD = '*';
6
+ /**
7
+ * Compile a table filter entry into a case-insensitive, fully anchored regular
8
+ * expression. An entry without a wildcard matches only the exact name.
9
+ * @param {string} pattern Exact table name or pattern containing `*` wildcards
10
+ * @returns {RegExp}
11
+ */
12
+ export const compileTableFilterPattern = (pattern) => {
13
+ const source = pattern
14
+ .split(TABLE_FILTER_WILDCARD)
15
+ .map(part => part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
16
+ .join('.*');
17
+ return new RegExp(`^${source}$`, 'is');
18
+ };
19
+ /**
20
+ * Build a predicate reporting whether a table name matches at least one entry
21
+ * of a table filter. Entries are exact names or `*` wildcard patterns, matched
22
+ * case-insensitively.
23
+ * @param {string[]} patterns
24
+ * @returns {(name: string) => boolean}
25
+ */
26
+ export const createTableNameMatcher = (patterns) => {
27
+ const regexps = patterns.map(compileTableFilterPattern);
28
+ return (name) => regexps.some(regexp => regexp.test(name));
29
+ };
30
+ /**
31
+ * Keep the tables selected by the `tables` filter and not excluded by the
32
+ * `skipTables` filter. An absent or empty filter selects or excludes nothing.
33
+ * @param {T[]} tables
34
+ * @param {Pick<IConfigMetadata, 'tables' | 'skipTables'> | undefined} metadata
35
+ * @returns {T[]}
36
+ */
37
+ export const filterTablesByName = (tables, metadata) => {
38
+ const isIncluded = metadata?.tables?.length
39
+ ? createTableNameMatcher(metadata.tables)
40
+ : () => true;
41
+ const isSkipped = metadata?.skipTables?.length
42
+ ? createTableNameMatcher(metadata.skipTables)
43
+ : () => false;
44
+ return tables.filter(({ name }) => isIncluded(name) && !isSkipped(name));
45
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sequelize-typescript-generator",
3
- "version": "13.2.1",
3
+ "version": "13.3.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",
@@ -49,30 +49,30 @@
49
49
  "homepage": "https://github.com/spinlud/sequelize-typescript-generator.git#readme",
50
50
  "devDependencies": {
51
51
  "@types/bluebird": "^3.5.42",
52
- "@types/estree": "^1.0.6",
52
+ "@types/estree": "^1.0.10",
53
53
  "@types/jest": "^30.0.0",
54
54
  "@types/node": "^22.13.0",
55
55
  "@types/pluralize": "0.0.33",
56
56
  "@types/validator": "^13.12.2",
57
57
  "@types/yargs": "^17.0.33",
58
58
  "jest": "^30.5.1",
59
- "mariadb": "^3.4.0",
60
- "mysql2": "^3.12.0",
59
+ "mariadb": "^3.5.5",
60
+ "mysql2": "^3.24.6",
61
61
  "pg": "^8.13.1",
62
62
  "pg-hstore": "^2.3.4",
63
63
  "reflect-metadata": "^0.2.1",
64
64
  "sequelize-typescript": "^2.1.6",
65
65
  "@vscode/sqlite3": "^5.1.14-vscode",
66
- "tedious": "^20.3.3",
67
- "ts-jest": "^29.4.12",
66
+ "tedious": "^20.4.1",
67
+ "ts-jest": "^29.4.15",
68
68
  "tsx": "^4.23.13",
69
69
  "wkx": "^0.5.0"
70
70
  },
71
71
  "dependencies": {
72
- "@stylistic/eslint-plugin": "^5.10.0",
73
- "@typescript-eslint/parser": "^8.70.0",
74
- "change-case": "^4.1.2",
75
- "eslint": "^10.10.0",
72
+ "@stylistic/eslint-plugin": "^6.0.1",
73
+ "@typescript-eslint/parser": "^8.73.0",
74
+ "change-case": "^5.4.4",
75
+ "eslint": "^10.15.0",
76
76
  "pluralize": "^8.0.0",
77
77
  "sequelize": "^6.37.5",
78
78
  "typescript": ">=5.7 <6.1",