TypeORM cannot use cli with configuration

17.6k Views Asked by At

I have my (typescript) Project (Its a discord bot if that is important), which contains a "ormconfig.json" file. This file contains my configuration for TypeORM, see as follows:

{
    "type": "mysql",
    "host": "<IP of my server>",
    "port": 3306,
    "username": "<MySQL User>",
    "password": "<MySQL Password>",
    "database": "<existing MySQL database>",
    "synchronize": true,
    "logging": false,
    "entities": [
        "src/entities/*.ts"
    ],
    "migrations": [
        "src/migration/*.ts"
    ],
    "subscribers": [
        "src/subscriber/*.ts"
    ],
    "cli": {
        "entitiesDir": "src/entities/",
        "migrationsDir": "src/migration/",
        "subscribersDir": "src/subscriber/"
    }
}

Now, if I run anything on my actual server, the configuration in general works. Entities get loaded correctly and I can use them as intended. Now I made some changes in an entity, which means I would have to do a migration. Trying to generate the migration made the actual problem appear.

I tried running the following command:

npm run typeorm migration:show -- -d .\ormconfig.json

I expected this to load the config, connect to the server, check the database and give me output. Yet when it loads, it simply throws an error, saying

Given data source file must contain export of a DataSource instance

I also tried doing it over javascript, yet when I export a "DataSource"-Instance, i receive a similar error:

Invalid file path given: "C:\temp\Bot\src\database.ts". File must contain a TypeScript / JavaScript code and export a DataSource instance.

Following also my package.json file, for the dependencies of the project.

{
  "name": "Bot",
  "version": "1.1.1",
  "description": "desc",
  "main": "index.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "serve": "npx ts-node src/index.ts",
    "start": "npx ts-node src/index.ts",
    "typeorm": "npx --require ts-node/register ./node_modules/typeorm/cli.js",
  },
  "author": "PlayWolfYT",
  "license": "ISC",
  "dependencies": {
    "@discordjs/builders": "^0.12.0",
    "@discordjs/rest": "^0.3.0",
    "@types/nodemailer": "^6.4.4",
    "dayjs": "^1.10.8",
    "discord-api-types": "^0.27.0",
    "discord.js": "^13.6.0",
    "mysql2": "^2.3.3",
    "nodemailer": "^6.7.2",
    "nodemon": "2.0.15",
    "reflect-metadata": "^0.1.13",
    "ts-node": "^10.5.0",
    "typeorm": "^0.3.3",
    "typescript": "^4.6.2"
  },
  "devDependencies": {
    "@types/node": "^17.0.21",
    "@discordjs/builders": "^0.12.0",
    "@discordjs/rest": "^0.3.0"
  }
}
3

There are 3 best solutions below

0
Luis Vinicius M. On

So, you are using one of the newer versions of Typeorm, in which they dropped support to ormconfig.{json,xml} file. You'll have to put your database connection options in a {ts,js} file using a Data Source and export it.

Two options to work with your problem:

I can help you setup through editing this answer if needed, just comment it below.

1
Edovanie On

Create a file named "data-source.ts"

import { DataSource } from 'typeorm';

export const AppDataSource = new DataSource({
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'user',
  password: 'pass',
  database: 'db_name',
  entities: [],
  migrations: [],
  subscribers: [],
});

then run

npm run typeorm migration:show -- -d "path/data-source.ts"

0
Hifounder On
// ./data-source.ts
import { DataSource } from "typeorm"
import { Table } from "./entity/Table"

export default new DataSource({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "user",
    password: "password",
    database: "db",
    synchronize: true,
    logging: false,
    entities: [Table],
    migrations: [],
    subscribers: [],
})

if use mysql piz npm package use mysql2

[email protected] if use TypeScript connection:
npx typeorm-ts-node-commonjs schema:sync -d ./data-source.ts

if play sample:
npx typeorm init --database mysql && npm uninstall mysql && npm install mysql2