tada5hi/typeorm-extension

Bug: having trouble to link my seeder using the direct path with .env

billalouaali opened this issue ยท 4 comments

Versions

  • Node: 16.19.1
  • OS: MacOS Ventura

Steps to reproduce

Declare your seeder path using .env TYPEORM_SEEDING_SEEDS=src/database/seeders/**/*{.ts,.js} instead of TYPEORM_SEEDING_SEEDS=src/database/seeds/**/*{.ts,.js} (we replaced /seeds to /seeders)

What is Expected?

The library is supposed to identify the seeder using the path

What is actually happening?

The seeder file doesn't react

Additional details

Importing the seeder class or adding the path directly in the seeds key of SeederOptions works fine

You have a reproduction repository here

Thank you for the reproduction repo. I praise that ๐Ÿ˜Š .
The problem here seems to be that the .env file is not loaded by the library. Currently it is assumed that the env variable is set by the user or system environment or via CLI with cross-env for example.
But probably it is also a good idea to support file env loading from the root directory.

@billalouaali an alternative to this would be to define either the glob pattern path to the seeder or the seeder constructor in the datasource options as follows:

data-source.ts

export const dataSourceOptions: DataSourceOptions & SeederOptions = {
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '',
  database: 'seeder',
  extra: {
    'charset': 'utf8mb4_unicode_ci'
  },
  logging: true,
  entities: [
    __dirname + '/**/*.entity{.ts,.js}'
  ],
  seeds: ['src/database/seeders/**/*{.ts,.js}'], // or  [CreateDays]
  synchronize: true
}

export const dataSource = new DataSource(dataSourceOptions);

Thank you for the reproduction repo. I praise that ๐Ÿ˜Š . The problem here seems to be that the .env file is not loaded by the library. Currently it is assumed that the env variable is set by the user or system environment or via CLI with cross-env for example. But probably it is also a good idea to support file env loading from the root directory.

yes as you said, the .env is not loaded, i was thinking that the path was the issue, but in reality even if you don't specify a .env or a seeds key in SeederOptions, and you keep the exact path for you directories (src/database/seeds/**/*{.ts,.js}) it works, maybe the library look for a specific path if no seeds path are linked in ?

Yes, the library uses the fallback path in case no environment variable is set: src/database/seeds/**/*{.ts,.js})