Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database connection failed! Check your typeORM config file. MissingDriverError: Wrong driver: "undefined" given. #225

Open
svJariwala opened this issue Jun 6, 2022 · 4 comments

Comments

@svJariwala
Copy link

hello guys

When I do seed:run in my NestJS app I get the following error:

image

This is my seed:config:

image

This is my database.config.ts file

image

Originally posted by @svJariwala in #81 (comment)

@svJariwala svJariwala reopened this Jun 7, 2022
@smartcrash
Copy link

Hi svJariwala!

Do you have a ormconfig.json file on you project's root folder?
I think the problem is that typeorm-seeding is trying to read that file and never found it, that's why it tells you that "Wrong driver: undefined given".

Here is an example of how your ormconfig.json should look like:

{
   "type": "mysql",
   "host": "localhost",
   "port": 3306,
   "username": "test",
   "password": "test",
   "database": "test"
}

Here is the TypeOrm's confi file docs

@marcosfreitas
Copy link

I was facing this same problem. I solved this by importing the datasource.ts configurations at the ormconfig.ts.

import * as config from './datasource';
export = config.dataSource;

datasource.ts has all the connection options, and for me, it is used at the command line to inform which file is the datasource file:

package.json script:
"typeorm": "typeorm-ts-node-commonjs -d ./datasource.ts",

@shadowgroundz
Copy link

I was facing this same problem. I solved this by importing the datasource.ts configurations at the ormconfig.ts.

import * as config from './datasource';
export = config.dataSource;

datasource.ts has all the connection options, and for me, it is used at the command line to inform which file is the datasource file:

package.json script: "typeorm": "typeorm-ts-node-commonjs -d ./datasource.ts",

Where you place ormconfig.ts or datasource? And can you share your package.json script for seeding? Thank you

@marcosfreitas
Copy link

Hi, @shadowgroundz.

I placed my ormconfig.ts at the project root with the other files below.
PS.: I was wondering if something could be improved in this configuration declarations but I haven't much time to test other options.

  • /datasource.ts
import { DataSource, DataSourceOptions } from 'typeorm';
import { config } from 'dotenv';
import { SeederOptions } from 'typeorm-extension';

config();

// @bug configService isn't loading the environment variables
//const configService = new ConfigService();
//const databaseConfig: DatabaseConfig =
//  configService.get<DatabaseConfig>('database');

export const dataSource: DataSourceOptions & SeederOptions = {
  type: 'mysql',
  charset: 'utf8mb4_general_ci',
  host: process.env.DB_HOST,
  port: 3306,
  username: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.APP_NAME,
  entities: [__dirname + '/src/modules/**/*.entity.js'], // path to dist folder
  migrations: [__dirname + '/src/database/migrations/*.ts'],
  seeds: [__dirname + '/src/database/seeders/*.seeder.ts'],
  factories: [__dirname + '/src/database/factories/*.factory.ts'],
};

export default new DataSource(dataSource);
  • /ormconfig.ts
import * as config from './datasource';
export = config.dataSource;
  • /ormconfig-nest.ts
import { AppConfig } from '@app/configuration/contracts/app.config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import {
  TypeOrmModuleAsyncOptions,
  TypeOrmModuleOptions,
} from '@nestjs/typeorm';
import { DatabaseConfig } from 'src/configuration/contracts/database.config';

export const config: TypeOrmModuleAsyncOptions = {
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (config: ConfigService): TypeOrmModuleOptions => {
    const appConfig = config.get<AppConfig>('application');
    const databaseConfig: DatabaseConfig =
      config.get<DatabaseConfig>('database');

    const basePath =
      __dirname + (appConfig.environment === 'test' ? '/dist' : '');

    return {
      type: 'mysql',
      charset: 'utf8mb4_general_ci',
      host: databaseConfig.host,
      port: databaseConfig.port,
      username: databaseConfig.user,
      password: databaseConfig.password,
      database: databaseConfig.name,
      autoLoadEntities: true,
      entities: [basePath + '/src/modules/**/*.entity.js'],
      migrations: [basePath + '/src/database/migrations/*.ts'],
    };
  },
};

I have added these 3 scripts to my package.json:

    "typeorm": "typeorm-ts-node-commonjs -d ./datasource.ts",
    "db:create": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js db:create",
    "db:drop": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js db:drop",
    "db:seed": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed"

If I could be useful, here is my package.json:

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest --runInBand --detectOpenHandles",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "typeorm": "typeorm-ts-node-commonjs -d ./datasource.ts",
    "db:create": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js db:create",
    "db:drop": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js db:drop",
    "db:seed": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed"
  },
  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.2.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/typeorm": "^9.0.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "mysql2": "^2.3.3",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "sqlite3": "^5.0.11",
    "typeorm": "^0.3.7",
    "typeorm-extension": "^2.1.5"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "28.1.4",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "faker": "^6.6.6",
    "jest": "28.1.2",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.2.4",
    "ts-jest": "28.0.5",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "4.0.0",
    "typescript": "^4.3.5"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": ".",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node",
    "moduleNameMapper": {
      "^@app/(.*)$": "<rootDir>/src/$1",
      "^@configuration/(.*)$": "<rootDir>/src/configuration/$1",
      "^@database/(.*)$": "<rootDir>/src/database/$1",
      "^@modules/(.*)$": "<rootDir>/src/modules/$1",
      "^@shared/(.*)$": "<rootDir>/src/shared/$1",
      "^@test/(.*)$": "<rootDir>/test/$1"
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants