typeorm: ConnectionOptions does not allow variable assigned to type

src/db.ts(14,39): error TS2352: Type '{ type: string; host: string; port: number; username: string; password: string; database: string;...' cannot be converted to type 'ConnectionOptions'.
  Type '{ type: string; host: string; port: number; username: string; password: string; database: string;...' is not comparable to type 'CordovaConnectionOptions'.
    Types of property 'type' are incompatible.
      Type 'string' is not comparable to type '"cordova"'.

Where I am trying to pass in the connection options. ie:

const opts: ConnectionOptions = {
   type: wConfig.type,
   host: wConfig.host,
   ...

FWIW, I am actually trying to create a Postgres connection in this case. Not sure why it chose to report cordova as the type The Postgres config isn’t directly exposed, either

my workaround:

const opts: ConnectionOptions = {
   type: 'postgres',
   host: wConfig.host,
   ...
};
(opts.type as any) = wConfig.type;

I’m not using any specific features of postgres, so any db connection as configured should be fine, and would/should not require code modification.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Guys, just use the database you are really using, its pointless to set a type from the configuration because each type has its own specifics, you cannot practically really have it dynamic.

There are lots of use cases for wanting a variable for the database type … for tools, or testing, or for that matter apps that need to connect to more than one type of database are pretty common.

I recognize this issue isn’t exactly a show-stopper, but it got in my way too, and I had to write some ugly code to work around it.

And dismissing the request as “pointless” is offensive. 😦

This is what it worked for me in typescript import {ConnectionOptions, DatabaseType} from ‘typeorm’;

import {ConnectionOptions, DatabaseType} from 'typeorm';

const postgresDatabase: DatabaseType = 'postgres';
const baseConfig = {
    host: DB_HOST,
    port: DB_PORT,
    password: DB_PASSWORD,
    database: DB_NAME,
    synchronize: false,
    logging: false,
    type: postgresDatabase,
    entities: [ConcertsEntitySchema]
};

The Issue is marked as Closed but seems to be persistent

@ typeorm 0.2.15

const database: ConnectionOptions = { type: process.env.TYPEORM_CONNECTION as DatabaseType, host: process.env.TYPEORM_HOST || ‘localhost’, port: Number(process.env.TYPEORM_PORT) || 3306, name: ‘default’, username: process.env.TYPEORM_USERNAME, password: process.env.TYPEORM_PASSWORD, database: process.env.TYPEORM_DATABASE, synchronize: Boolean(process.env.TYPEORM_SYNCHRONIZE) || true, logging: false, entityPrefix: process.env.TYPEORM_ENTITY_PREFIX, connectTimeout: 30000, entities: [ OAuth2Token, User, Role, Permission, Client, ], }; `

Throws error:

error TS2322: Type ‘{ type: DatabaseType; host: string; port: number; name: string; username: string; password: string; database: string; synchronize: true; logging: false; entityPrefix: string; connectTimeout: number; entities: (typeof Permission | … 3 more … | typeof OAuth2Token)[]; }’ is not assignable to type ‘ConnectionOptions’. Type ‘{ type: DatabaseType; host: string; port: number; name: string; username: string; password: string; database: string; synchronize: true; logging: false; entityPrefix: string; connectTimeout: number; entities: (typeof Permission | … 3 more … | typeof OAuth2Token)[]; }’ is not assignable to type ‘MysqlConnectionOptions’. Types of property ‘type’ are incompatible. Type ‘DatabaseType’ is not assignable to type ‘“mysql” | “mariadb”’. Type ‘“postgres”’ is not assignable to type ‘“mysql” | “mariadb”’.

15 const database: ConnectionOptions = {

where this issue happened too and the solution was add type DataSourceOptions on return. The solved code:

useFactory: (configService: ConfigService) => {
    return (
        {
            retryAttempts: 5,
            retryDelay: 1000,
            keepConnectionAlive: true,
            type: configService.DB_TYPE,
            host: configService.DB_HOST,
            port: configService.DB_PORT,
            username: configService.DB_USER,
            password: configService.DB_PASS,
            database: configService.DB_DATABASE,
            entities: [__dirname + '/**/*.entity{.ts,.js}'],
            synchronize: configService.DB_SYNC,
        } as DataSourceOptions
    )
}),

for some reason typeorm understand ‘mysql | postgres | sqlite…’ as custom types so if you inject string it will break

This one works for me but it’s ugly. You can add more types in switch section.

      useFactory: (configService: ConfigService) => {
        let type;
        switch (configService.DB_TYPE) {
          case "mysql": type = "mysql"; break;
          case "postgres": type = "postgres"; break;
          default: type = "postgres";
        }
        return ({
          retryAttempts: 5,
          retryDelay: 1000,
          keepConnectionAlive: true,
          type,
          host: configService.DB_HOST,
          port: configService.DB_PORT,
          username: configService.DB_USER,
          password: configService.DB_PASS,
          database: configService.DB_DATABASE,
          entities: [__dirname + '/**/*.entity{.ts,.js}'],
          synchronize: configService.DB_SYNC,
        })
      },

type: 'postgres' as const should also work.

yeah, I cant add it to external, you can use full path for now. Also you can do:

   type: wConfig.type as "postgres"

but it does not make sense because if you have a type stored inside a variable then it suppose to be other then postgres too