typeorm-extension: Bug: OptionsError: The database options could not be located/loaded.

Versions

  • Node: v18.17.1
  • OS: MacOS Sonoma 14.2

Reproduction

When executing db:create/drop, it gives the following error:

OptionsError: The database options could not be located/loaded.
    at OptionsError.notFound
Additional Details

DataSource file:

config();

const configService = new ConfigService();

export const dataSourceOptions: DataSourceOptions & SeederOptions = {
    type: 'mysql',
    host: configService.getOrThrow('DB_HOST'),
    port: configService.getOrThrow('DB_PORT'),
    database: configService.getOrThrow('DB_DATABASE'),
    username: configService.getOrThrow('DB_USERNAME'),
    password: configService.getOrThrow('DB_PASSWORD'),
    entities: ['dist/**/*.entity{.ts,.js}'],
    migrations: ['dist/db/migrations/*.js'],
    migrationsTableName: 'migrations',
};

export default new DataSource(dataSourceOptions);

Steps to reproduce

npm run db:create

What is Expected?

To locate the datasource file correctly

What is actually happening?

Its throwing the following error: OptionsError: The database options could not be located/loaded. at OptionsError.notFound

It runs when i change the following in the “findDataSource” function:

if (info) {
          let fileExports = await locter.load(info);
          if (isPromise(fileExports)) {
              fileExports = await fileExports;
          }
          if (typeorm.InstanceChecker.isDataSource(fileExports)) {
              return fileExports;
          }
          const defaultExport = locter.getModuleExport(fileExports);
          if (isPromise(defaultExport.value)) {
              defaultExport.value = await defaultExport.value;
          }


          // Added .default to the defaultExport.value 
          if (defaultExport && typeorm.InstanceChecker.isDataSource(defaultExport.value.default)) {
              return defaultExport.value.default;
          }


          if (locter.isObject(fileExports)) {
              const keys = Object.keys(fileExports);
              for(let j = 0; j < keys.length; j++){
                  let value = fileExports[keys[j]];
                  if (isPromise(value)) {
                      value = await value;
                  }
                  if (typeorm.InstanceChecker.isDataSource(value)) {
                      return value;
                  }
              }
          }
      }

About this issue

  • Original URL
  • State: closed
  • Created 5 months ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I can confirm that v3.4.0 has fixed it for me.

Hi @tada5hi , I haven’t tested version 3.4.0-beta.1 but I want to add some information here, which might help.

We faced a similar issue recently when running typeorm-extension cli commands and trying to provide the datasource.

So we were getting a weird error that ‘TypeORMError: No connection options were found in any orm configuration files.’ and this error was happening with typeorm-extension.

After much investigation we found out that typeorm can see the datasource when we do:


export default dataSource;

And typeorm-extension can see the datasource when we do:


module.exports = dataSource;

So we ended up providing one datasource for typeorm and another one for typeorm-extension, because funny thing is that typeorm doesn’t work with module.exports, it throws :


Error: Given data source file must contain export of a DataSource instance

    at Function.loadDataSource (/node_modules/typeorm/commands/CommandUtils.js:39:19)

    at async Object.handler (/node_modules/typeorm/commands/MigrationRunCommand.js:41:26)

I hope this helps the situation.

We suspect that it has to do with how the dataSource is nested when we use es module, and findDataSource needs to adjust a little bit to handle this case. When we use commonjs module there is no wrapper object around it so it can read the dataSource properly.

It would be great if you could have a look at 3.4.0-beta.1 and check if the problem persists. In theory typeorm-extension should handle esm and cjs as well. You have an esm project right ? Do you execute the compiled js file or do you use ts-node ?