tada5hi/typeorm-extension

Bug: CLI not recognizing data-source files if they have a different name

Closed this issue · 6 comments

Versions

  • Node: 8.12.2
  • OS: Ubuntu 20.04

Reproduction

Steps to reproduce

I'm using NestJs.

Configure your package.json like this:

"seed": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed -- -d src/database/database.config.ts"

Add a database.config.ts under src/database in your project with the following code:

import { DataSource, DataSourceOptions } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { config } from 'dotenv';
import { SeederOptions } from 'typeorm-extension';

config();

const configService = new ConfigService();

const options: DataSourceOptions & SeederOptions = {
  type: 'mysql',
  host: configService.get<string>('MYSQL_HOST'),
  port: configService.get<number>('MYSQL_PORT'),
  username: configService.get<string>('MYSQL_USER'),
  password: configService.get<string>('MYSQL_PASSWORD'),
  database: configService.get<string>('MYSQL_DB_NAME'),
  entities: ['dist/**/*.entity{.ts,.js}'],
  migrations: ['dist/migrations/**/*{.ts,.js}'],
  timezone: 'Z',
  logging: true,
  migrationsRun: true,
  synchronize: false,
  ssl: {
    rejectUnauthorized: false,
  },
  seeds: ['src/database/seeds/**/*{.ts,.js}'],
  factories: ['src/database/factories/**/*{.ts,.js}'],
};

export const AppDataSource = new DataSource(options);

Now, if you run the seed script the following error will occur:

TypeORMError: No connection options were found in any orm configuration files.
    at ConnectionOptionsReader.all (/home/jph/zenbit/HiveIn_be/node_modules/src/connection/ConnectionOptionsReader.ts:46:19)
    at async ConnectionOptionsReader.get (/home/jph/zenbit/HiveIn_be/node_modules/src/connection/ConnectionOptionsReader.ts:58:28)

What is Expected?

It would be expected to connect with the database, if I change the file to data-source.ts it will find the connection file.

What is actually happening?

Can't run the CLI script if I have a configuration file with a name that isn't data-source.ts

Can you try if the following command is working for you?

"seed": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed -- -r src/database -d database.config.ts"

@tada5hi Unfortunately not, same error

> ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed -- -r src/database -d database.config.ts

typeorm-extension seed

Populate the database with an initial data set or generated data by a factory.

TypeORMError: No connection options were found in any orm configuration files.
    at ConnectionOptionsReader.all (~/zenbit/HiveIn_be/node_modules/src/connection/ConnectionOptionsReader.ts:46:19)
    at async ConnectionOptionsReader.get (~zenbit/HiveIn_be/node_modules/src/connection/ConnectionOptionsReader.ts:58:28)

@jphinning damm, i forgot to mention to remove the .ts extension in the command, sorry 🙈
The library automatically checks for .ts and .js extension. But the library should definitly check if an extension is already present in the data-source name, which is not the case at the moment.

"seed": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed -- -r src/database -d database.config"

If this still don't work i will dig deeper into it.
Thank you for your patients.

Greetings
Peter

@tada5hi Hi. I have the same trouble.
image
image
image

@tada5hi Didn't work as well, same error, unfortunately. Strange because according to the scr code in the module.ts file your suggestion should work 🤔

 const fileNames : string[] = [
        'data-source',
    ];

 if (
        context.fileName &&
        context.fileName !== 'data-source'
    ) {
        fileNames.unshift(context.fileName);
    }

 for (let i = 0; i < fileNames.length; i++) {
        const info = await locateFile(`${fileNames[i]}.{js,ts}`, {
            path: paths,
            ignore: ['**/*.d.ts'],
        });
    }

When I tried to reconstruct your problems and didn't encounter the same problem, I looked at your command again and found out that you use double hyphen -- in the command. When i also added the hyphens in the command, i encountered the same problem.

@jphinning:

"seed": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed -r src/database -d database.config"

@Spartaques

"seed": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed -r src/common/typeorm -d data.source"