Skip to content

Generating js migration files via CLI with --outputJs produces an unused import from typeorm #9149

@mishabruml

Description

@mishabruml

Issue Description

Generating js migration files via CLI with --outputJs produces an unused import from typeorm.
I'm running the command: npm run typeorm -- migration:generate src/migrations/migration --dataSource src/datasource.ts --pretty --outputJs
Which produces a file src/migrations/12345-migration.js file as expected, and works with the migration:run command locally. However, the file contains unused require referring to the MigrationInterface and QueryRunner on the first line:

const { MigrationInterface, QueryRunner } = require("typeorm");

module.exports = class migration12345 {
    name = 'migration12345';
    ...etc

This causes 2 problems for me, although I'm sure there are other manifestations of this problem too.

  1. It fails eslint no-unused-vars rule
  2. I am running these migrations in an AWS lambda function. Typeorm is bundled with the migration lambda function code using esbuild. The migrations cannot be bundled unfortunately due to esbuild not being to resolve the imports from the migrations directory glob at build time. Therefore, the lambda fails when running migrations because the imported migration file does not have access to typeorm dependency (its bundled with the migration code).
    "errorMessage": "Cannot find module 'typeorm'\nRequire stack:\n- /var/task/src/migrations/12345-migration.js\n- /var/task/src/handlers/migrate/index.js\n- /var/runtime/index.mjs",

There are 2 workarounds to this for me:

  1. Exclude typeorm dependency from the bundle (not keen on doing this)
  2. Manually remove the require statement

Obviously number 2 is preferred for now, but it would be great if the line wasn't generated at all!

Expected Behavior

The line const { MigrationInterface, QueryRunner } = require("typeorm"); should be omitted from generated js migration files.

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions