3

Hello i have a problem in my app.ts when loading my settings from my ormconfig.ts for the typeorm function to create the connection i got this error on my app:

No overload matches this call.
  Overload 1 of 3, '(name: string): Promise<Connection>', gave the following error.
    Argument of type 'typeof import("d:/EmasaTi_StockControl/src/shared/infra/http/ormconfig")' is not assignable to parameter of type 'string'.
  Overload 2 of 3, '(options: ConnectionOptions): Promise<Connection>', gave the following error.
    Argument of type 'typeof import("d:/EmasaTi_StockControl/src/shared/infra/http/ormconfig")' is not assignable to parameter of type 'ConnectionOptions'.
      Type 'typeof import("d:/EmasaTi_StockControl/src/shared/infra/http/ormconfig")' is missing the following properties from type 'ExpoConnectionOptions': type, database, driverts(2769)
Peek Problem (Alt+F8)
No quick fixes available

code:

import 'dotenv/config';
import { createConnection } from 'typeorm';
import App from './app';
import validateEnv from '@utils/validateEnv';
import * as config from './ormconfig';
validateEnv();

(async () => {
  try {
    const connection = await createConnection(config);
    await connection.runMigrations();
  } catch (error) {
    console.log('Error while connecting to the database', error);
    return error;
  }
  const app = new App(
  );
  app.listen();
})();

my config file:

import { ConnectionOptions } from 'typeorm';

const rootDir = process.env.NODE_ENV === 'development' ? 'src' : 'build/src';
export const config: ConnectionOptions = {
  type: 'postgres',
  host: process.env.DB_HOST,
  port: Number(process.env.DB_PORT),
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  entities: [rootDir + '/entities/**/*.{js,ts}'],
  migrations: [rootDir + '/migrations/*.{js,ts}'],
  subscribers: [rootDir + '/subscribers/**/*.{js,ts}'],
  cli: {
    entitiesDir: `${rootDir}/entities`,
    migrationsDir: `${rootDir}/migration`,
    subscribersDir: `${rootDir}/subscriber`,
  },
  synchronize: false,
  logging: true
};

module.exports = config;

I can't imagine how I could solve this problem if someone can help me I am grateful

Ming
  • 1,349
  • 4
  • 13
  • 36
  • If you set the config object inline at `createConnection({...})` without import it works? – noam steiner Jul 21 '20 at 14:23
  • See please my answer there. I wrote example working config for postgres, may be help you: https://stackoverflow.com/questions/63678216/nestjs-setup-typeorm-connection-with-env-and-nestjs-config/63879524#63879524 – V.Tur Sep 22 '20 at 04:14

1 Answers1

3

I am not sure what the perfect solution is, but I found a way to work around it.

  1. Remove "type": "mysql" from your tsconfig.json or other config file as the case may be.
  2. Import your config as usual. Example: import config from './ormconfig.json'.
  3. Use the config variable like so : createConnection({type: "postgres", ...config}).

I think that for some reasons, the type key is not being parsed appropriately.

Cheers.

Olu Udeh
  • 1,031
  • 1
  • 10
  • 20