Giter Club home page Giter Club logo

Comments (4)

marcosfreitas avatar marcosfreitas commented on June 26, 2024 3

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",

from typeorm-seeding.

smartcrash avatar smartcrash commented on June 26, 2024

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

from typeorm-seeding.

shadowgroundz avatar shadowgroundz commented on June 26, 2024

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

from typeorm-seeding.

marcosfreitas avatar marcosfreitas commented on June 26, 2024

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"
    }
  }
}

from typeorm-seeding.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.