Skip to content

fix(codemod): rename .connection on EntityMetadata, ColumnMetadata, IndexMetadata - #12383

Merged
pkuczynski merged 3 commits into
masterfrom
fix/codemod-metadata-connection-prop
Apr 17, 2026
Merged

pkuczynski merged 3 commits into
masterfrom
fix/codemod-metadata-connection-prop

Conversation

@pkuczynski

Copy link
Copy Markdown
Member

Reported by Lucian: meta.connection.getMetadata(...) was left unchanged when meta is typed EntityMetadata. Same for ColumnMetadata and IndexMetadata.

connection-to-datasource already renames .connection → .dataSource on typed references to QueryRunner, EntityManager, Repository, TreeRepository, MongoRepository, and the *QueryBuilder classes. The metadata classes were missed — their .connection property was renamed to .dataSource in #12249, same breaking change.

Extends typesWithConnectionProp with EntityMetadata, ColumnMetadata, and IndexMetadata. Fixture gains typed-parameter cases for EntityMetadata and ColumnMetadata to pin the behavior.

@pkg-pr-new

pkg-pr-new Bot commented Apr 17, 2026 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm/typeorm/@typeorm/codemod@12383
    

commit: 67726d9

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Apr 17, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Broken metadata rewrite 🐞 Bug ≡ Correctness ⭐ New
Description
Adding ColumnMetadata and IndexMetadata to typesWithConnectionProp makes the codemod rewrite
x.connection to x.dataSource for those types, but in this repo the DataSource is accessed via
x.entityMetadata.dataSource rather than a dataSource property on
ColumnMetadata/IndexMetadata. The resulting transformed code (and the new fixture expectation)
will not match the actual TypeORM API shape present here.
Code

packages/codemod/src/transforms/v1/connection-to-datasource.ts[R58-60]

+        "EntityMetadata",
+        "ColumnMetadata",
+        "IndexMetadata",
Evidence
The transform renames .connection→.dataSource for any identifier whose type annotation name is
in typesWithConnectionProp; this PR adds ColumnMetadata/IndexMetadata to that set. In the
TypeORM sources in this repo, ColumnMetadata and IndexMetadata use entityMetadata.dataSource
(not this.dataSource), and EntityMetadata is the class that actually has the dataSource
property. Therefore rewriting col.connection to col.dataSource is inconsistent with the repo’s
API and will generate invalid output for those metadata objects.

packages/codemod/src/transforms/v1/connection-to-datasource.ts[43-61]
packages/codemod/src/transforms/v1/connection-to-datasource.ts[180-234]
src/metadata/ColumnMetadata.ts[16-45]
src/metadata/ColumnMetadata.ts[349-362]
src/metadata/IndexMetadata.ts[9-26]
src/metadata/IndexMetadata.ts[140-157]
src/metadata/EntityMetadata.ts[37-50]
packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.output.ts[21-28]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`connection-to-datasource` now rewrites `.connection` to `.dataSource` on identifiers typed as `ColumnMetadata`/`IndexMetadata`, but in this repo those metadata objects access the DataSource via `entityMetadata.dataSource` (they do not use a `dataSource` property on the metadata object itself). The codemod should not emit `col.dataSource` / `idx.dataSource` for these types.

### Issue Context
- `ColumnMetadata` uses `this.entityMetadata.dataSource` internally.
- `IndexMetadata` uses `options.entityMetadata.dataSource`.
- Only `EntityMetadata` exposes a `dataSource` property directly.

### Fix Focus Areas
- packages/codemod/src/transforms/v1/connection-to-datasource.ts[43-61]
- packages/codemod/src/transforms/v1/connection-to-datasource.ts[180-234]
- src/metadata/ColumnMetadata.ts[16-45]
- src/metadata/ColumnMetadata.ts[349-362]
- src/metadata/IndexMetadata.ts[9-26]
- src/metadata/IndexMetadata.ts[140-157]
- packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.input.ts[21-28]
- packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.output.ts[21-28]

### Concrete direction
1. Track *which* TypeORM type each collected identifier is annotated with (e.g., map `varName -> typeName`).
2. When rewriting `obj.connection`:
  - If `obj` is annotated as `EntityMetadata`, rewrite to `obj.dataSource`.
  - If `obj` is annotated as `ColumnMetadata` or `IndexMetadata`, rewrite to `obj.entityMetadata.dataSource` (i.e., replace the MemberExpression with a nested MemberExpression chain).
3. Update/extend fixtures to assert the correct rewritten form for `ColumnMetadata`/`IndexMetadata` (and add an `IndexMetadata` case if intended).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Unscoped metadata type matching 🐞 Bug ≡ Correctness
Description
The codemod now renames .connection → .dataSource whenever a variable/param is typed
EntityMetadata/ColumnMetadata/IndexMetadata by name alone, without verifying the type is
imported from typeorm, so unrelated code with those common type names can be rewritten
incorrectly. It also fails to rename real TypeORM cases where these types are imported under an
alias or referenced via a qualified name, because only bare Identifier type references are
recognized.
Code

packages/codemod/src/transforms/v1/connection-to-datasource.ts[R57-60]

+        // Metadata classes (renamed in #12249)
+        "EntityMetadata",
+        "ColumnMetadata",
+        "IndexMetadata",
Evidence
The transform collects identifiers to rewrite by checking only the type annotation’s identifier name
against typesWithConnectionProp; it does not check whether that type name came from a typeorm
import. The updated fixture demonstrates this behavior by using EntityMetadata/ColumnMetadata
without importing them, yet expecting .connection to be transformed. Because the check only
matches exact names and only when the type is a bare Identifier, aliased imports (e.g. `import {
EntityMetadata as EM }) and qualified names (e.g. TypeORM.EntityMetadata`) are missed.

packages/codemod/src/transforms/v1/connection-to-datasource.ts[43-61]
packages/codemod/src/transforms/v1/connection-to-datasource.ts[180-197]
packages/codemod/src/transforms/v1/connection-to-datasource.ts[220-234]
packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.input.ts[1-28]
packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.output.ts[21-28]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`connection-to-datasource` decides whether to rename `.connection` → `.dataSource` by checking only the *spelling* of a type annotation (e.g. `EntityMetadata`) rather than verifying that the type actually comes from TypeORM. Adding `EntityMetadata`/`ColumnMetadata`/`IndexMetadata` increases the chance of false positives because these names are common.
It also produces false negatives for TypeORM metadata types imported under aliases (e.g. `import { EntityMetadata as EM }`) or referenced through a namespace/qualified name.
## Issue Context
The transform already inspects `ImportDeclaration` from `"typeorm"` for other renames, so it can similarly scope `.connection` property rewrites to identifiers whose type annotations refer to a local name imported from `typeorm` (including aliases).
## Fix Focus Areas
- packages/codemod/src/transforms/v1/connection-to-datasource.ts[43-61]
- packages/codemod/src/transforms/v1/connection-to-datasource.ts[63-93]
- packages/codemod/src/transforms/v1/connection-to-datasource.ts[180-234]
- packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.input.ts[1-33]
- packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.output.ts[1-33]
## Concrete implementation notes
- Build a `Set<string>` of *local* type names imported from `"typeorm"` whose `imported.name` is in `typesWithConnectionProp` (so `EntityMetadata as EM` adds `EM`).
- In `collectTypedIdentifier`, match against that local-name set (instead of raw `typesWithConnectionProp`) and consider supporting `TSQualifiedName` for `import * as TypeORM from "typeorm"`.
- Update the fixture to import `EntityMetadata`/`ColumnMetadata` from `typeorm`, and add an alias fixture case (e.g. `EntityMetadata as EM`) to prevent regressions.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Grey Divider

Previous review results

Review updated until commit 67726d9

Results up to commit 55c0842


🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider
Action required
1. Unscoped metadata type matching 🐞 Bug ≡ Correctness
Description
The codemod now renames .connection → .dataSource whenever a variable/param is typed
EntityMetadata/ColumnMetadata/IndexMetadata by name alone, without verifying the type is
imported from typeorm, so unrelated code with those common type names can be rewritten
incorrectly. It also fails to rename real TypeORM cases where these types are imported under an
alias or referenced via a qualified name, because only bare Identifier type references are
recognized.
Code

packages/codemod/src/transforms/v1/connection-to-datasource.ts[R57-60]

+        // Metadata classes (renamed in #12249)
+        "EntityMetadata",
+        "ColumnMetadata",
+        "IndexMetadata",
Evidence
The transform collects identifiers to rewrite by checking only the type annotation’s identifier name
against typesWithConnectionProp; it does not check whether that type name came from a typeorm
import. The updated fixture demonstrates this behavior by using EntityMetadata/ColumnMetadata
without importing them, yet expecting .connection to be transformed. Because the check only
matches exact names and only when the type is a bare Identifier, aliased imports (e.g. `import {
EntityMetadata as EM }) and qualified names (e.g. TypeORM.EntityMetadata`) are missed.

packages/codemod/src/transforms/v1/connection-to-datasource.ts[43-61]
packages/codemod/src/transforms/v1/connection-to-datasource.ts[180-197]
packages/codemod/src/transforms/v1/connection-to-datasource.ts[220-234]
packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.input.ts[1-28]
packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.output.ts[21-28]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`connection-to-datasource` decides whether to rename `.connection` → `.dataSource` by checking only the *spelling* of a type annotation (e.g. `EntityMetadata`) rather than verifying that the type actually comes from TypeORM. Adding `EntityMetadata`/`ColumnMetadata`/`IndexMetadata` increases the chance of false positives because these names are common.

It also produces false negatives for TypeORM metadata types imported under aliases (e.g. `import { EntityMetadata as EM }`) or referenced through a namespace/qualified name.

## Issue Context
The transform already inspects `ImportDeclaration` from `"typeorm"` for other renames, so it can similarly scope `.connection` property rewrites to identifiers whose type annotations refer to a local name imported from `typeorm` (including aliases).

## Fix Focus Areas
- packages/codemod/src/transforms/v1/connection-to-datasource.ts[43-61]
- packages/codemod/src/transforms/v1/connection-to-datasource.ts[63-93]
- packages/codemod/src/transforms/v1/connection-to-datasource.ts[180-234]
- packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.input.ts[1-33]
- packages/codemod/test/transforms/v1/fixtures/connection-to-datasource/connection-to-datasource.output.ts[1-33]

## Concrete implementation notes
- Build a `Set<string>` of *local* type names imported from `"typeorm"` whose `imported.name` is in `typesWithConnectionProp` (so `EntityMetadata as EM` adds `EM`).
- In `collectTypedIdentifier`, match against that local-name set (instead of raw `typesWithConnectionProp`) and consider supporting `TSQualifiedName` for `import * as TypeORM from "typeorm"`.
- Update the fixture to import `EntityMetadata`/`ColumnMetadata` from `typeorm`, and add an alias fixture case (e.g. `EntityMetadata as EM`) to prevent regressions.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider Grey Divider

Qodo Logo

@alumni

alumni commented Apr 17, 2026 •

Copy link
Copy Markdown
Collaborator

I actually reported it in the constructor (because I have dynamic entities):

  • new EntityMetadata({ connection, ... } should be new EntityMetadata({ dataSource: connection, ... } (connection was renamed in constructor options).
  • new ColumnMetadata({ connection, entityMetadata, ... }) should be new ColumnMetadata({ entityMetadata, ... }) (connection was removed from constructor options, because it can be taken from entityMetadata).

But it's not really important, it's probably less effort to change those by hand than implement it in the codemod :)

@pkuczynski
pkuczynski enabled auto-merge (squash) April 17, 2026 22:38
@sonarqubecloud

Copy link
Copy Markdown

@pkuczynski
pkuczynski merged commit 8a51e30 into master Apr 17, 2026
16 checks passed
@pkuczynski
pkuczynski deleted the fix/codemod-metadata-connection-prop branch April 17, 2026 22:40
@github-actions github-actions Bot added this to the 1.0 milestone Apr 17, 2026
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 67726d9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants