Skip to content

Using async DataSource is broken #10168

@nover

Description

@nover

Issue description

When defining an async datasource to use for migrations the typeorm CLI breaks

Expected Behavior

It should be possible to define async DataSource according to:
#8914

And use that for configuring TypeOrm.

Actual Behavior

When declaring an async data source as mentioned in the original issue #8914

const buildDataSource = async () => {
  await ConfigModule.envVariablesLoaded;

  return new DataSource({
    type: 'postgres',
    url: process.env.DATABASE_URL,
    entities: ['dist/domain/**/*.entity.js'],
    migrations: ['dist/database/migrations/*.js'],
  });
};

export default buildDataSource();

It fails with

Error during migration generation:
TypeError: dataSourceFileExports is not iterable
    at Function.loadDataSource (/Some/Path/backend/node_modules/src/commands/CommandUtils.ts:37:34)
    at async Object.handler ((/Some/Path/backend/node_modules/src/commands/MigrationGenerateCommand.ts:73:26)

I have tried numerous approaches to the export but they basically all fail.

Steps to reproduce

Define an async DataSource and run the migrate:generate or migrate:run command.

My Environment

Dependency Version
Operating System
Node.js version v18.15.0
Typescript version 4.5.5
TypeORM version 0.3.17

Additional Context

After some debugging it turns out that the problem lies in the CommandUtils.ts - checking for someting instanceof Promise is not stable and it does not work on my machine ™️. The check fails and thus passes the promise up the chain to the InstanceChecker which naturally fails.

I have a PR coming.

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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

Yes, I have the time, and I know how to start.

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