Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support injecting providers into dataSourceFactory #1294

Open
1 task done
brandon-leapyear opened this issue Jun 3, 2022 · 2 comments
Open
1 task done

Support injecting providers into dataSourceFactory #1294

brandon-leapyear opened this issue Jun 3, 2022 · 2 comments
Labels

Comments

@brandon-leapyear
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I would like to provide my own DataSource, but I need to inject my ConfigService so that I can do things in my DataSource depending on configuration.

Describe the solution you'd like

The most straightforward solution is to provide a dataSourceFactoryInject option.

A bigger change (which I think makes more sense) would be to remove dataSourceFactory and add

dataSourceProviderModify: (provider) => provider
entityManagerProviderModify: (provider) => provider

which passes in the default provider, and the user can modify whatever they want, e.g.

dataSourceProviderModify: (provider) => ({
  ...provider,
  useFactory: (typeOrmOptions, configService) => {
    return new MyDataSource(typeOrmOptions, configService)
  },
  inject: [...provider.inject, ConfigService],
})

Teachability, documentation, adoption, migration strategy

See solution.

What is the motivation / use case for changing the behavior?

See problem.

@khanh-le-otsv
Copy link

I would like to suggest this feature as well.

@tafaust
Copy link

tafaust commented Aug 20, 2024

I need this feature as well. My current workaround (not an actual workaround) is to

const configService = new ConfigService();
// use the configService in DataSource creation

Moreover, if one uses the dataSourceFactory callback of type TypeOrmDataSourceFactory = (options?: DataSourceOptions) => Promise<DataSource>; there is the chance to create the DataSourceOptions here from the provided ConfigService in case of undefined.

Here is an example of a dataSourceFactory implementation outlining why this feature is urgently needed:

export const dataSourceFactory: TypeOrmDataSourceFactory = async (
  options?: DataSourceOptions,
) => {
  const configService = new ConfigService();

  return new DataSource(options ?? {
    type: 'mysql',
    host: configService.get('HOST'),
    port: +configService.get('PORT'),
    username: configService.get('USERNAME'),
    password: configService.get('PASSWORD'),
    database: configService.get('DATABASE'),
    // and so on ...
  }).initialize();
};

But we could also use provider to enhance DataSource creation in other ways. I create a pg-mem connection for instance when serving or unit testing my backends locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants