NodeJS 出现错误“MissingDriverError:驱动程序错误:使用TypeORM生成迁移时给定了“undefined”,

wlsrxk51  于 2023-01-08  发布在  Node.js
关注(0)|答案(3)|浏览(437)

我正在尝试生成迁移。我已经将这些行添加到package.json中

"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js  --config src/config/configuration.ts",
 "migration:generate":"npm run typeorm migration:generate -- -n"

我有一个configuration. ts文件,而不是ormconfig.json,它看起来像这样。

import { join } from 'path';

export default () => {
  const host = process.env.DB_HOST;
  const port = process.env.DB_PORT;
  const username = process.env.DB_USERNAME;
  const password = process.env.DB_PASSWORD;
  const database = process.env.DB_DATABASE;

  return {
    database: {
      type: 'mariadb',
      host,
      port,
      username,
      password,
      database,
      entities: ['dist/**/*.entity{.ts,.js}'],
      synchronize: false,
      migrations: ['/src/migration/**/*.ts'],
      keepConnectionAlive: true,
      charset: 'utf8mb4',
      cli: {
        migrationsDir: '/src/migrations',
      },
    },
  };
};

运行命令时

npm run migration:generate -- softDeleteUser

有这个错误

> user-service@0.0.1 migration:generate
> npm run typeorm migration:generate -- -n "softDeleteUser"

> user-service@0.0.1 typeorm
> node --require ts-node/register ./node_modules/typeorm/cli.js  --config src/config/configuration.ts "migration:generate" "-n" "softDeleteUser"

Error during migration generation:
MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "oracle", "postgres", "sqlite", "better-sqlite3", "sqljs", "react-native", "aurora-data-api", "aurora-data-api-pg".
at new MissingDriverError (/Users/akash/Documents/Projects/pulp-backend/user-service/src/error/MissingDriverError.ts:8:9)
    at DriverFactory.create (/Users/akash/Documents/Projects/pulp-backend/user-service/src/driver/DriverFactory.ts:67:23)
    at new Connection (/Users/akash/Documents/Projects/pulp-backend/user-service/src/connection/Connection.ts:128:43)
    at ConnectionManager.create (/Users/akash/Documents/Projects/pulp-backend/user-service/src/connection/ConnectionManager.ts:64:28)
    at Object.<anonymous> (/Users/akash/Documents/Projects/pulp-backend/user-service/src/index.ts:230:35)
    at step (/Users/akash/Documents/Projects/pulp-backend/user-service/node_modules/tslib/tslib.js:141:27)
    at Object.next (/Users/akash/Documents/Projects/pulp-backend/user-service/node_modules/tslib/tslib.js:122:57)
    at /Users/akash/Documents/Projects/pulp-backend/user-service/node_modules/tslib/tslib.js:115:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/Users/akash/Documents/Projects/pulp-backend/user-service/node_modules/tslib/tslib.js:111:16)

有什么建议吗?

ctehm74n

ctehm74n1#

您是否尝试导出配置,例如:

export = ORMConfig;
hm2xizp9

hm2xizp92#

我也遇到过同样的错误,所以我试着按照文档上说的去做。我把我的数据库配置导入到app.module imports里面,如下所示:

`import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
      entities: [],
      synchronize: true,
    }),
  ],
})

但不幸的是,这不是原因。真实的的原因是在我的实体文件中,我在实体表中放置了列。我有:

@Column({
    type: 'float',
    precision: 6,
    scale: 2,
  })
  price: number;

而不是:

@Column({
    type: 'decimal',
    precision: 6,
    scale: 2,
  })
  price: number;

因为只有小数才具有精度和刻度属性所有变体都在此处描述:https://typeorm.io/entities#column-options

gjmwrych

gjmwrych3#

这在我的情况下工作:-

*inside app.module.ts*
    imports: [
    ConfigModule.forRoot({isGlobal: true}),
    TypeOrmModule.forRootAsync({
    useClass: DatabaseConnectionService //DatabaseConnectionService is 
    //the  file containing the connection code
    }) ,
    UserModule
    ]

相关问题