typeorm: CLI does not support async DataSource

Issue Description

Starting from the 0.3.0 version, CLI requires to specify the file with DataSource instance instead of ormconfig. When I am trying to export async created DataSource it fails

Expected Behavior

Command ran successfully

Actual Behavior

Command failed with error: Error: Given data source file must contain export of a DataSource instance

Steps to Reproduce

Have your DataSource defined in the file:

const buildDataSource = async () => {
  // Provided config-like functionality just for example, it will fail anyway with just async function
  const dbConfig = await config.get('database');

  return new DataSource({
    type: 'mysql',
    host: dbConfig.host,
    port: 3306,
    username: dbConfig.username,
    password: dbConfig.password,
    database: dbConfig.database,
    logging: true,
    entities: typeormEntities,
    migrations: [ './src/components/database/migrations/**/*.ts' ]
  })
};

export default buildDataSource();

This works though:

const buildDataSource = () => {
  return new DataSource({
    type: 'mysql',
    host: '127.0.0.1',
    port: 3306,
    username: 'test',
    password: 'test',
    database: 'test',
    logging: true,
    entities: typeormEntities,
    migrations: [ './src/components/database/migrations/**/*.ts' ]
  })
};

export default buildDataSource();

My Environment

Dependency Version
Operating System Ubuntu 20.04.4 LTS
Node.js version v16.13.0
Typescript version 4.6.3
TypeORM version 0.3.4

Relevant Database Driver(s)

I tested with mysql only but I do not think it is a driver problem at all

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql yes
nativescript no
oracle no
postgres no
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

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.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 17 (7 by maintainers)

Commits related to this issue

Most upvoted comments

A quick and very dirty hack which can be used until this gets resolved:

import { DataSource, DataSourceOptions, InstanceChecker } from 'typeorm';
import * as dotenv from 'dotenv';

dotenv.config();

function patchAsyncDataSourceSetup() {
    const oldIsDataSource = InstanceChecker.isDataSource;
    InstanceChecker.isDataSource = function(obj: unknown): obj is DataSource {
        if (obj instanceof Promise) {
            return true;
        }
        return oldIsDataSource(obj);
    }
}
patchAsyncDataSourceSetup();

const buildDataSource = async () => { ... }

export default buildDataSource();

Why doesn’t the TypeORM CLI use the datasource definition that’s already in Nest?

You can create a ormconfig.ts file like this that will allow that:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './src/app/app.module';
import { DataSource } from 'typeorm';

export default NestFactory.create(AppModule)
  .then((app) => app.get(DataSource))
  .then((dataSource) => Promise.all([dataSource, dataSource.destroy()]))
  .then(([dataSource]) => dataSource);

Hello again, sorry for the inconvenience. It was indeed a quite obvious solution. What I had to do was the following: even though I’m using Nest, this DataSource has no integration with it, and so this ConfigModule hook to await for the env to load would not work. I needed to install dotenv and dotenv expand manually, even though the ConfigModule already uses them under the hood. And so applying both of them manually, it finally worked. Even the factory became unnecessary.

import * as dotenv from 'dotenv';
import * as dotenvExpand from 'dotenv-expand';
import { DataSource } from 'typeorm';

dotenvExpand.expand(dotenv.config());

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

I’m leaving this here just in case someone passes through a similar situation. Sorry once again due to the obvious question, and best wishes to all.

—CORRECTION—

It is not necessary to install dotenv and dotenv-expand again. You can just import them as described above, however autocompletion of the module won’t work, probably because it’s a “sub-dependency”.

Thanks @Ginden, but still I don’t understand exactly the proper way to setup the async DataSource, sorry if it’s something kind of obvious. Do you think you could help me?

I’m using TypeORM with NestJS, so I imagined this setup would work

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();

When I execute a migration generation, it stops giving an error. However, it appears to simply run and do nothing.

PS C:\Workspaces\ws-nest\nest-lecture-project> yarn migration:generate src/database/migrations/create-user
yarn run v1.22.17
$ yarn typeorm migration:generate -d dist/database/data-source -p src/database/migrations/create-user
$ C:\Workspaces\ws-nest\nest-lecture-project\node_modules\.bin\typeorm migration:generate -d dist/database/data-source -p src/database/migrations/create-user
Done in 1.89s.

Would you be able to tell if I am in the right track? I’m also thinking about trying to use a static class, to be able to leverage a configuration namespace and partial registration through Nest, though I’m not sure if it’s possible in this case.