Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using async DataSource is broken #10168

Closed
18 tasks
nover opened this issue Jun 30, 2023 · 1 comment · Fixed by #10170
Closed
18 tasks

Using async DataSource is broken #10168

nover opened this issue Jun 30, 2023 · 1 comment · Fixed by #10170

Comments

@nover
Copy link
Contributor

nover commented Jun 30, 2023

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.

@nover
Copy link
Contributor Author

nover commented Jun 30, 2023

According to MDN await is defined in a way that we can simply always await. If the thing is not awaitable it will just return it's value:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#return_value

nover added a commit to nover/typeorm that referenced this issue Jun 30, 2023
Rather than trying to detect wheter the passed in thing is a promise, we
can simply await it. The `await` operator is defined in such way that if the
thing is not awaitable we simply get the value back

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#return_value
nover added a commit to nover/typeorm that referenced this issue Jul 3, 2023
Rather than trying to detect wheter the passed in thing is a promise, we
can simply await it. The `await` operator is defined in such way that if the
thing is not awaitable we simply get the value back

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#return_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant