nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Option to not initialize DataSource in createDataSourceFactory

Junikorn opened this issue · comments

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 have to create nestjs app using @nestjs/typeorm without connecting to database during app creation

Describe the solution you'd like

An option in TypeOrmModuleOptions to leave DataSource.initialize to be run manually

Teachability, documentation, adoption, migration strategy

No migration startegy necessary, this feature should be opt-in.

Simplified solution:
/lib/typeorm-core.module.ts
screenshot

Same in line 241. Of course this would require interface changes and such. Option name up for discussion. Users should be made aware of usage consequences in the documentation.

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

My current usecase is generating openapi documentation using @nestjs/swagger DocumentBuilder without a need to have a running database instance or mocking DataSource.
I also imagine that some applications might need to start even without connection to DB and report connection status using healthcheck endpoints instead of awaiting DB connection before exposing API (see this StackOverflow question)

Can you retrieve a repository of a non-initialized data source? This wasn't doable in previous versions of typeorm (with createConnection), hence the question

It seems you can

describe('DataSource', () => {
  it('should allow to retrieve repository before init', () => {
    const ds = new DataSource({
      type: 'postgres',
      host: 'localhost',
      port: 5434,
      username: 'postgres',
      password: 'password',
      database: 'postgres',
      entities: [Code],
    });
    const repository = ds.getRepository(Code);
    expect(repository).toEqual(expect.any(Repository));
  });
});

This test is passing for me

Sounds good. Would you like to create a PR for this issue then?

I think I can

I added PR for this issue

Let's track this here #1766