sequelize-typescript-generator 13.0.0 → 13.0.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.
@@ -1,168 +0,0 @@
1
- # Phase 5 (issue #79) implementation plan: automatic association discovery
2
-
3
- Status: planned, not started
4
- Implements: #79 (phase 5 of docs/plans/v13-upgrade-plan.md)
5
- Depends on: phase 4 (#78, done on v13)
6
- Open judgment call: with no `--case`, singular/plural aliases keep the database spelling and only composed aliases are camelCased.
7
-
8
- Branch `v13`.
9
-
10
- ## Conventions
11
-
12
- This plan follows the project conventions in the repository's CLAUDE.md files: no `any`, no type assertions (`as`, `as unknown as T`, non-null `!`), `satisfies` for checked literals, and `unknown` only at untyped boundaries then narrowed; library types first, derived second, custom last; comments in English describing the code as it is now (no history or "phase" markers); names like `calculateTotalRevenue`, booleans `is/has/should`, constants `UPPER_SNAKE_CASE`; the CONTEXT.md vocabulary (Association, Alias, Association discovery, Associations file, Junction table, Case); TDD at pure seams; the TypeScript compiler API for code emission; one commit per step with `v13 phase N: ...` subjects, with a separate agent creating the commits.
13
-
14
- ## 0. Facts that shape the design
15
-
16
- - Pipeline today (`src/dialects/Dialect.ts` `buildTablesMetadata`): fetch → filter → per-table columns/indices/FKs → `applyForeignKeyConstraintsToColumns` → CSV block **overwrites** `tablesMetadata[t].associations = association.associations` (would wipe discovered associations) and rewrites `columns[c].foreignKey` → `caseTransformer`.
17
- - `caseTransformer` mutates association objects in place (`src/dialects/utils.ts`) and `AssociationsParser.parse` returns one static cached object regardless of path. Together: a second `buildTablesMetadata` with a different case double-transforms `targetModel`. Must be fixed in this phase (copy-on-transform + per-path cache returning copies).
18
- - `IAssociationMetadata` already has optional `alias`, `foreignKey`, `targetKey`, `onDelete`, `onUpdate`; nothing reads them yet.
19
- - Emission (`ModelBuilder.buildAssociationPropertyDecl`): property name is `pluralize.plural/singular(targetModel)`; only decorator option emitted is `sourceKey`; imports of target models and the self-import guard exist; decorator names are imported from the `associations` list.
20
- - `generateArrowDecorator` (`src/builders/utils.ts`) renders object props as literals in insertion order, so option key order is controlled by the props object.
21
- - `pluralize` and `change-case` are already dependencies.
22
- - sequelize-typescript sets `options.as = propertyName` when `as` is absent.
23
- - yargs 17: `.option('associations', { boolean: true, default: true })` yields `associations: false` for `--no-associations`. Side finding: existing `--no-views`/`--no-strict` long forms are parsed as `views: false` so only `-V`/`-R` work today; pre-existing bug, out of scope, file a follow-up issue.
24
- - A composite primary key containing the FK column sets `primaryKey: true` on that column, so the "FK column is the PK" 1:1 rule must require a single-column PK.
25
- - Fixtures in all five dialects: `units.race_id → races` (CASCADE/RESTRICT; MSSQL NO ACTION for update), `employees.manager_id → employees` (SET NULL; MSSQL may differ, check mssql test metadata), `profiles.person_id UNIQUE → person` (CASCADE/CASCADE), `shipments(order_id,line_no) → order_lines` composite; `person`/`passport` have no DB FK (CSV puts FK on `passport.passport_id → person`). Golden is generated WITH the CSV.
26
-
27
- ## 1. Data model
28
-
29
- Discovery **builds on** `applyForeignKeyConstraintsToColumns`; column-level `IColumnMetadata.foreignKey` stays as the `@ForeignKey`/`references` record. Extract the eligibility test into a shared classifier in `src/dialects/foreignKeys.ts`:
30
-
31
- ```ts
32
- export const FOREIGN_KEY_SKIP_REASONS = ['composite', 'cross-schema', 'excluded-target'] as const;
33
- export type ForeignKeySkipReason = typeof FOREIGN_KEY_SKIP_REASONS[number];
34
- export type ForeignKeyClassification = { isEligible: true } | { isEligible: false; reason: ForeignKeySkipReason };
35
- /** Single-column, generated target, same schema -> eligible; otherwise the first failing rule. */
36
- export const classifyForeignKeyConstraint = (constraint: IForeignKeyConstraintMetadata, sourceSchema: string | undefined, generatedTables: ReadonlySet<string>): ForeignKeyClassification;
37
- ```
38
- `applyForeignKeyConstraintsToColumns` uses the classifier with identical behaviour.
39
-
40
- Discovered association for `units.race_id → races(race_id)` (pre-case, database names):
41
-
42
- Source side (`units.associations`): `{ associationName: 'BelongsTo', targetModel: 'races', alias: 'race', foreignKey: 'race_id', targetKey: 'race_id', onDelete: 'CASCADE', onUpdate: 'RESTRICT' }`
43
- Target side (`races.associations`): `{ associationName: 'HasMany', targetModel: 'units', alias: 'units', foreignKey: 'race_id', sourceKey: 'race_id', onDelete: 'CASCADE', onUpdate: 'RESTRICT' }`
44
-
45
- - `HasOne` on target when `constraint.isSourceColumnUnique` or (`column.primaryKey` and the source table has exactly one PK column); `HasMany` otherwise. `BelongsTo` always on source.
46
- - `foreignKey` on both sides; `targetKey` (BelongsTo) / `sourceKey` (Has*) = referenced column.
47
- - `onDelete`/`onUpdate` set only when normalized rule is not `'NO ACTION'`, on both sides.
48
- - CSV-derived associations keep `alias` undefined → emission falls back to today's plural/singular rule (golden compat).
49
- - `IForeignKey` in AssociationsParser gains `isJunctionForeignKey: boolean` (true for N:N rows).
50
-
51
- ## 2. Pure module `src/dialects/associationDiscovery.ts`
52
-
53
- ```ts
54
- export interface IAssociationDiscoveryResult { tablesMetadata: ITablesMetadata; /* new objects */ warnings: string[]; /* one per skipped constraint */ }
55
- export const discoverAssociations = (tablesMetadata: ITablesMetadata): IAssociationDiscoveryResult;
56
- export const FOREIGN_KEY_COLUMN_SUFFIXES = ['_id', '_fk', 'Id', 'Fk'] as const;
57
- /** 'race_id' -> 'race'; undefined when no suffix matches or remainder is empty. */
58
- export const stripForeignKeyColumnSuffix = (columnName: string): string | undefined;
59
- export interface IAliasPair { belongsToAlias: string; hasAlias: string; }
60
- export const resolveAssociationAliases = (input: { sourceModel: string; targetModel: string; foreignKeyColumn: string; isAmbiguous: boolean; isOneToOne: boolean; }): IAliasPair;
61
- export const formatForeignKeySkipWarning = (constraint: IForeignKeyConstraintMetadata, sourceSchema: string | undefined, reason: ForeignKeySkipReason): string;
62
- export const isAssociationDiscoveryEnabled = (metadata: IConfigMetadata | undefined): boolean => metadata?.associations !== false;
63
- ```
64
- No `console` inside. Input is post-filter, pre-case.
65
-
66
- Algorithm, per table S in key order, per constraint in `foreignKeys` order:
67
- 1. classify; not eligible → push warning, continue.
68
- 2. `c = sourceColumns[0]`, `T = targetTable`, `k = targetColumns[0]`; find column by `originName === c` (skip silently if missing).
69
- 3. `isOneToOne = constraint.isSourceColumnUnique || (column.primaryKey && S has a single PK column)`.
70
- 4. `isAmbiguous = (#eligible constraints on S with targetTable === T) > 1 || S.originName === T`.
71
- 5. Aliases:
72
- - Unambiguous: `belongsToAlias = singular(T)`; `hasAlias = isOneToOne ? singular(S) : plural(S)`.
73
- - Ambiguous: `stripped = stripForeignKeyColumnSuffix(c)`. If defined: `belongsToAlias = stripped`, `hasAlias = camelCase(`${stripped} ${isOneToOne ? singular(S) : plural(S)}`)`. Else: `belongsToAlias = camelCase(`${singular(T)} ${c}`)`, `hasAlias = camelCase(`${c} ${plural|singular(S)}`)`.
74
- - Collision: each model keeps a Set of taken names seeded with its column field names and aliases already assigned. Candidates tried in order: primary, ambiguous form, fallback form, fallback + numeric suffix 2,3,... First free wins.
75
- 6. Push BelongsTo onto S and Has* onto T (self-reference: both onto S, BelongsTo first).
76
-
77
- Examples (no --case): `units.race` / `races.units`; `profiles.person` / `person.profile` (HasOne); `employees.manager` / `employees.managerEmployees`; hypothetical `books(author_id, editor_id) → authors`: `books.author`, `books.editor`, `authors.authorBooks`, `authors.editorBooks`.
78
-
79
- `--case` on aliases: applied by `caseTransformer` with `TransformTarget.COLUMN`. With no `--case`, singular/plural aliases keep database spelling (12.x compatible) and only composed aliases are camelCased. This is the open judgment call surfaced in the header.
80
-
81
- `caseTransformer` (`src/dialects/utils.ts`): build new association objects (`map` with spread), transforming `targetModel`/`joinModel` (MODEL) and `sourceKey`/`alias`/`foreignKey`/`targetKey` (COLUMN); rebuild `columns[x].foreignKey` without mutating the input.
82
-
83
- ## 3. Associations file on top: `src/dialects/associationsFileMerge.ts` (pure)
84
-
85
- ```ts
86
- export interface IAssociationsFileMergeResult { tablesMetadata: ITablesMetadata; warnings: string[]; }
87
- export const applyAssociationsFile = (tablesMetadata: ITablesMetadata, parsed: IAssociationsParsed): IAssociationsFileMergeResult;
88
- ```
89
- Replaces the inline CSV block in `Dialect.ts`; existing warning texts move into `warnings`.
90
-
91
- Per parsed table S:
92
- 1. Table missing → warning, continue.
93
- 2. For each `foreignKeys` entry `{ name: c, targetModel: T, isJunctionForeignKey }`:
94
- - If not junction: remove the discovered BelongsTo on S with `foreignKey === c` and its paired Has* on the discovered targetModel whose `targetModel === S && foreignKey === c`. Keyed on (source table, FK column) only.
95
- - Junction row: remove nothing.
96
- - Column missing → warning; else `columns[c].foreignKey = { ...(discovered?.targetModel === T ? discovered : {}), name: c, targetModel: T }`.
97
- 3. **Append** `parsed[S].associations` to `tablesMetadata[S].associations` (instead of overwrite).
98
- 4. Alias collision after merge: if a CSV association's effective property name (plural/singular of targetModel) equals a discovered `alias` on the same model, drop the discovered association with warning `Association alias '<x>' on table '<S>' is used by both a discovered association and the associations file; the associations file entry is kept`.
99
-
100
- Fixture consequences (golden with CSV): `units.ts`/`races.ts` unchanged (CSV replaces discovered pair); `person.ts` gains `HasOne profiles` alias `profile` + import; `profiles.ts` gains BelongsTo; `employees.ts` gains BelongsTo + HasMany.
101
-
102
- `AssociationsParser`: replace static cache with `Map<string, IAssociationsParsed>` keyed by path, return structural copies; set `isJunctionForeignKey`.
103
-
104
- ## 4. `--no-associations`
105
-
106
- - `IConfigMetadata.associations?: boolean; // Discover associations from foreign keys; undefined means enabled`.
107
- - `src/bin/utils.ts`: `ASSOCIATIONS: 'associations'`; `buildConfig`: `...(argv[aliasesMap.ASSOCIATIONS] === false && { associations: false })`.
108
- - `src/bin/cli.ts`: `.option(aliasesMap.ASSOCIATIONS, { boolean: true, default: true, describe: 'Discover one-to-one and one-to-many associations from foreign keys. Use --no-associations to disable.' })`, no short letter. Usage string gains `--no-associations`.
109
- - With `associations: false`: skip `discoverAssociations` only; column `@ForeignKey` stays; associations file still works.
110
-
111
- ## 5. Emission in `src/builders/ModelBuilder.ts`
112
-
113
- Exported pure helpers:
114
- ```ts
115
- import type { BelongsToOptions, HasManyOptions, HasOneOptions } from 'sequelize';
116
- export type AssociationDecoratorOptions = Partial<BelongsToOptions & HasManyOptions & HasOneOptions>;
117
- /** Alias when discovered, otherwise plural/singular of the target model. */
118
- export const resolveAssociationPropertyName = (association: IAssociationMetadata): string;
119
- /** Options for @BelongsTo/@HasOne/@HasMany; undefined when empty. Key order: as, foreignKey, targetKey, sourceKey, onDelete, onUpdate. `as` is emitted only when alias is set (discovered associations). */
120
- export const buildAssociationDecoratorProps = (association: IAssociationMetadata): AssociationDecoratorOptions | undefined;
121
- ```
122
- Discovered associations emit `as: alias` (issue #79 says "with foreignKey and as"); CSV associations without alias emit no `as` (golden compat). `@ForeignKey(() => T)` on the column unchanged. Imports already handled (self-import guard exists).
123
-
124
- Expected golden after change (generated with CSV): `units.ts`, `races.ts` unchanged. `profiles.ts` gains
125
- ```ts
126
- @BelongsTo(() => person, { as: "person", foreignKey: "person_id", targetKey: "person_id", onDelete: "CASCADE", onUpdate: "CASCADE" })
127
- person?: person;
128
- ```
129
- `person.ts` gains `import { profiles }` and `@HasOne(() => profiles, { as: "profile", foreignKey: "person_id", sourceKey: "person_id", onDelete: "CASCADE", onUpdate: "CASCADE" }) profile?: profiles;`
130
- `employees.ts` gains `@BelongsTo(() => employees, { as: "manager", foreignKey: "manager_id", targetKey: "employee_id", onDelete: "SET NULL" }) manager?: employees;` and `@HasMany(() => employees, { as: "managerEmployees", foreignKey: "manager_id", sourceKey: "employee_id", onDelete: "SET NULL" }) managerEmployees?: employees[];` (linter formatting applies).
131
-
132
- ## 6. Warnings (emitted once per constraint via `console.warn('[WARNING]', message)` in `buildTablesMetadata`)
133
-
134
- - composite: `Foreign key constraint '<name>' on table '<table>' spans columns (<a>, <b>); Sequelize associations need a single column, so the columns are generated as plain attributes`
135
- - cross-schema: `Foreign key constraint '<name>' on table '<schema>.<table>' references table '<targetSchema>.<target>' in another schema; the column is generated as a plain attribute`
136
- - excluded target: `Foreign key constraint '<name>' on table '<table>' references table '<target>', which is not among the generated tables; the column is generated as a plain attribute`
137
-
138
- `buildTablesMetadata` order after change: column FK copy inside the try; then (outside the fetch try) `if (isAssociationDiscoveryEnabled(config.metadata))` discover + warn; then `applyAssociationsFile` when `associationsFile` set (+ warn); then case transform.
139
-
140
- ## 7. Tests
141
-
142
- Unit: `associationDiscovery.test.ts` (cardinality incl. composite PK → HasMany; on-rules only when not NO ACTION; alias rule cases; collisions; three skip warnings with exact wording; no mutation; deterministic order), `associationsFileMerge.test.ts`, `foreignKeys.test.ts` (classifier), `caseTransformer.test.ts` (alias/foreignKey/targetKey; no compounding on repeat), `associationsParser.test.ts` (isJunctionForeignKey; per-path cache; copies), `builders.test.ts` (resolveAssociationPropertyName, buildAssociationDecoratorProps key order, nodeToString snapshot), `cliConfig.test.ts` (associations false mapping; isAssociationDiscoveryEnabled).
143
-
144
- Integration (`TestRunner.ts`, new `describe('Association discovery')` after `Foreign keys`, per dialect, both formats): build WITHOUT associationsFile (`indices: true`), assert via `Model.associations` (`associationType`, `foreignKey`, `as`): 1:N units/races (+ include counts); 1:1 profiles/person (HasOne; include yields object); composite skip (shipments/order_lines associations empty; `console.warn` spy captured the constraint name); self-reference alias (`manager`, `managerEmployees`; include 'manager' works with seeded rows); referential actions (`units.associations.race.options.onDelete === 'CASCADE'`; onUpdate asserted only when expectedForeignKeys says not NO ACTION). CSV override (existing Associations block, build with CSV): `Object.keys(units.associations)` equals `['race']`, `races` → `['units']`, `person` → `['passport','profile']`, `units.ts` text has no `onDelete`. `--no-associations` build: all `associations` empty; `units.ts` still has `@ForeignKey(() => races)`. `--case CAMEL` build: file text contains `managerEmployees?: employees[]` and `foreignKey: "managerId"`. Golden update and review.
145
-
146
- ## 8. README
147
-
148
- Options block: `--associations ... [boolean] [default: true]` + usage line `--no-associations`. `## Associations` section rewrite: discovery default, cardinality rule, skipped FK kinds with warnings, N:N never inferred; "Alias rule" subsection; "Associations file precedence" paragraph; programmatic example shows `associations: false`. Check `src/tests/unit/readmeProgrammaticExample.test.ts` still passes.
149
-
150
- ## 9. Release `13.0.0-beta.2` on `next`
151
-
152
- State: package.json version `12.0.1`; no publishConfig; `.github/workflows/ci.yml` publish job runs on `v*` tags, `--tag next` when tag contains `-`, needs `secrets.NPM_TOKEN`; `13.0.0-beta.1` was never published (#84 open, ready-for-human, token missing). Agent-doable: `npm version 13.0.0-beta.2 --no-git-tag-version`, commit. Human: add NPM_TOKEN, `git tag v13.0.0-beta.2 && git push origin v13 v13.0.0-beta.2`; or `npm publish --access public --tag next` manually.
153
-
154
- ## 10. Ordered steps
155
-
156
- 1. Classifier + discovery module (+ unit tests). Commit: `v13 phase 5: derive associations and aliases from foreign key constraints`.
157
- 2. Associations file merge + parser per-path cache/copies + caseTransformer copy-on-transform (+ unit tests). Commit: `v13 phase 5: apply the associations file on top of discovered associations`.
158
- 3. Emission helpers in ModelBuilder (+ unit tests; golden unchanged at this step). Commit: `v13 phase 5: emit as, foreignKey, keys and referential actions on association decorators`.
159
- 4. Config flag + CLI + pipeline wiring in Dialect.ts + golden update. Commit: `v13 phase 5: enable association discovery by default with --no-associations`.
160
- 5. Integration harness block; run all five dialects. Commit: `v13 phase 5: cover association discovery in the integration harness`.
161
- 6. README. Commit: `v13 phase 5: document association discovery and --no-associations`.
162
- 7. Version bump to 13.0.0-beta.2. Commit: `v13 phase 5: bump version to 13.0.0-beta.2`. Comment on #79/#84; follow-up issue for `--no-views`/`--no-strict` long forms.
163
-
164
- ## Risks
165
- - Discovery on by default means every existing integration block emits associations; self-ref/1:1 emission bugs show up as compile failures everywhere.
166
- - MSSQL reports `NO ACTION` for `ON UPDATE RESTRICT`; key onUpdate assertions off `expectedForeignKeys`.
167
- - Composite-PK guard matters for junction tables with `PRIMARY KEY (a, b)`.
168
- - Publishing depends on a human adding NPM_TOKEN.