Tsed: [BUG] TypeORM repositories are not initialized in some situations

Created on 5 Apr 2021  Â·  12Comments  Â·  Source: tsedio/tsed

Information

  • Version: 6.38.2
  • Packages:

    • @tsed/typeorm

Example

  1. Build environment for testing
    git clone https://github.com/yuki-takei/tsed-typeorm-undefined-repository.git cd tsed-typeorm-undefined-repository yarn
  2. Modify src/index.ts and switch server modules

    • You can switch 3 kinds of servers. Activate one of them.

      import {Server} from "./server-works-well"; // import {Server} from "./server-not-work1"; // without name property // import {Server} from "./server-not-work2"; // without $onReady() process

  3. Launch server
    yarn dev
  4. Go to http://localhost:8080/hello

What happens?

  • src/server-works-well.ts

    • This works fine. UserRepository instance will be initialized correctly and you will see the message User count is 0

  • src/server-not-work1.ts

    • UserRepository instance will not be initialized.

    • The server returns 500 with the message Cannot read property 'count' of undefined

  • src/server-not-work2.ts

    • UserRepository instance will not be initialized.

    • The server returns 500 with the message Cannot read property 'count' of undefined

What is the expected result?

  • UserRepository instance will be initialized correctly even without name property in ConnectionOptions or without this.injector.get<TypeORMService>(TypeORMService); process.
bug

All 12 comments

Hello @yuki-takei
The major init issue is related to typeorm itself and not from Ts.ED. If the difference is over given options, I can’t help you. I’m not an expert of Typeorm connection issue :)

see you
Romain

The encoutered error mean the connection is not initialized correctly by typeorm.

Thank you for your comment.

Hmmm...

The official docs of TypeORM says:

If connection name is not given then it will be called "default".

in https://typeorm.io/#/connection-options/common-connection-options.

However, the following code with Ts.ED generates a connection named "0" when I didn't specify name property.
Is this an expected behavior of @tsed/typeorm?

  async $onReady(): Promise<void> {
    const typeormService = this.injector.get<TypeORMService>(TypeORMService);
    console.log(typeormService);

    const connection = typeormService?.connectionManager.get('0');
    console.log(connection);
  }

Furthermore, I think it is strange that UserRepository couldn't be retrieved without these code.

  async $onReady(): Promise<void> {
    this.injector.get<TypeORMService>(TypeORMService);
  }

Please see the defference between files src/server-works-well.ts and src/server-not-work2.ts.

Hoo ok.
I see. The problem are here:
https://github.com/TypedProject/tsed/blob/production/packages/typeorm/src/TypeORMModule.ts#L13

And here: https://github.com/TypedProject/tsed/blob/production/packages/typeorm/src/services/TypeORMService.ts#L22
Because, originally the @tsed/typeorm in Ts.ED v5 accept a Record Or an array.
Now it’s always an array of configuration:
https://github.com/TypedProject/tsed/blob/production/packages/typeorm/src/index.ts#L7

The code isn’t correct. I think the create should not have the first parameter (id) and just get only the connection option.

I’m not on my computer. You can make a PR if you have time. I can merge it from my mobile.

See you

Yes the second point it a strange behavior. I’ll investigate ASAP.

You can make a PR if you have time. I can merge it from my mobile.

OK, I'll try. Thanks.

@Romakita
Hi. I made #1334. Please review it and feedback.

Regards,

Hi @Romakita .

Is there any progress about this behavior?
Should we close this issue?

Honestly, I haven’t find a time to investigate one…

I sent you a PR. You've missed the import of typeorm. It's really important to keep it.

import {Configuration} from "@tsed/common";
import "@tsed/platform-express";
import "@tsed/typeorm"; // !!! IMPORTANT TO ADD THIS !!!

I added a warning on the documentation:
https://tsed.io/tutorials/typeorm.html#installation

See you
Romain

Please consider supporting

Please consider supporting the framework if it provides value to you or your company and this support was of help to you. Supporting the framework means, amongst other things, that such support will be available in the future.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

royibernthal picture royibernthal  Â·  3Comments

yangukmo picture yangukmo  Â·  6Comments

tomer-amir-vonage picture tomer-amir-vonage  Â·  7Comments

Romakita picture Romakita  Â·  5Comments

tobiasmuecksch picture tobiasmuecksch  Â·  6Comments