3

Errors Error during migration generation: MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-data-api", "aurora-data-api-pg", "better-sqlite3", "capacitor", "cockroachdb", "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "nativescript", "oracle", "postgres", "react-native", "sap", "sqlite", "sqljs". at MissingDriverError.TypeORMError [as constructor] (D:\Projects\Mark1\api2\src\error\TypeORMError.ts:7:9) at new MissingDriverError (D:\Projects\Mark1\api2\src\error\MissingDriverError.ts:8:9) at DriverFactory.create (D:\Projects\Mark1\api2\src\driver\DriverFactory.ts:70:23) at new Connection (D:\Projects\Mark1\api2\src\connection\Connection.ts:122:43) at ConnectionManager.create (D:\Projects\Mark1\api2\src\connection\ConnectionManager.ts:61:28) at D:\Projects\Mark1\api2\src\globals.ts:77:35 at step (D:\Projects\Mark1\api2\node_modules\tslib\tslib.js:143:27) at Object.next (D:\Projects\Mark1\api2\node_modules\tslib\tslib.js:124:57) at D:\Projects\Mark1\api2\node_modules\tslib\tslib.js:117:75 at new Promise ()

ormConfig.ts

import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { join } from 'path';

require('dotenv').config();

class ConfigService {

  constructor(private env: { [k: string]: string | undefined }) { }

  private getValue(key: string, throwOnMissing = true): string {
    const value = this.env[key];
    if (!value && throwOnMissing) {
      throw new Error(`config error - missing env.${key}`);
    }

    return value;
  }

  public ensureValues(keys: string[]) {
    keys.forEach(k => this.getValue(k, true));
    return this;
  }

  public getPort() {
    return this.getValue('PORT', true);
  }

  public isProduction() {
    const mode = this.getValue('MODE', false);
    return mode != 'DEV';
  }

  public getTypeOrmConfig(): TypeOrmModuleOptions {
    return {
      type: 'postgres',
      host: this.getValue('POSTGRES_HOST'),
      port: parseInt(this.getValue('POSTGRES_PORT')),
      username: this.getValue('POSTGRES_USER'),
      password: this.getValue('POSTGRES_PASSWORD'),
      database: this.getValue('POSTGRES_DATABASE'),
      entities: [join(__dirname, '**', '*.entity.{ts,js}')],
      migrationsTableName: 'migration',
      migrations: ['src/migration/*.ts'],
      cli: {
        migrationsDir: 'src/migration',
      },
    };
  }

}

const configService = new ConfigService(process.env)
  .ensureValues([
    'POSTGRES_HOST',
    'POSTGRES_PORT',
    'POSTGRES_USER',
    'POSTGRES_PASSWORD',
    'POSTGRES_DATABASE'
  ]);

export = configService ;

appmodule.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import configService from 'ormConfig';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [TypeOrmModule.forRoot(configService.getTypeOrmConfig())],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {

}

.env file

POSTGRES_HOST=localhost
POSTGRES_PORT=8080
POSTGRES_USER=postgres
POSTGRES_PASSWORD=saad2113
POSTGRES_DATABASE=mark1
PORT=3000
MODE=DEV
RUN_MIGRATIONS=true
Saad Bin Zia
  • 133
  • 1
  • 2
  • 6

1 Answers1

0

I tried something like this and it worked for me

TypeOrmModule.forRoot({
      type: "mysql",
      driver: "mysql",
      host: "localhost",
      port: 3306,
      username: 'Your username',
      password: 'Your password',
      database: 'Your database',
      entities: ['/**/*.entity{.ts,.js}'],
      synchronize: true,
})

In this I added the line driver: "mysql", which solved my issue for MissingDriverError.

I hope this helps...

Narayen S
  • 1
  • 3