postgresql MissingDriverError while using postgres

s3fp2yjn  于 2023-04-20  发布在  PostgreSQL
关注(0)|答案(1)|浏览(108)
    • 错误**迁移生成错误:MissingDriverError:错误的司机:"undefined"给出。支持的驱动程序有:"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)在新MissingDriverError(D:\Projects\Mark1\api2\src\error\MissingDriverError.ts:8:9)at DriverFactory. create(D:\Projects\Mark1\api2\src\driver\DriverFactory.ts:七十:23)在新连接(D:\Projects\Mark1\api2\src\connection\Connection.ts:一百二十二:43)在ConnectionManager. create(D:\Projects\Mark1\api2\src\connection\ConnectionManager.ts:六十一:28)在D:\Projects\Mark1\api2\src\globals.ts:七十七:35在步骤(D:\Projects\Mark1\api2\node_modules\tslib\tslib.js:一百四十三:27)在 www.example.com (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文件*
POSTGRES_HOST=localhost
POSTGRES_PORT=8080
POSTGRES_USER=postgres
POSTGRES_PASSWORD=saad2113
POSTGRES_DATABASE=mark1
PORT=3000
MODE=DEV
RUN_MIGRATIONS=true
8i9zcol2

8i9zcol21#

我试过这样的方法,对我很有效

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,
})

在这里,我添加了driver: "mysql"行,它解决了我对MissingDriverError的问题。
我希望这能帮上忙…

相关问题