git clone https://github.com/yuki-takei/tsed-typeorm-undefined-repository.git
cd tsed-typeorm-undefined-repository
yarn
src/index.ts and switch server modules
import {Server} from "./server-works-well";
// import {Server} from "./server-not-work1"; // without name property
// import {Server} from "./server-not-work2"; // without $onReady() process
yarn dev
http://localhost:8080/hellosrc/server-works-well.tsUserRepository instance will be initialized correctly and you will see the message User count is 0src/server-not-work1.tsUserRepository instance will not be initialized.Cannot read property 'count' of undefinedsrc/server-not-work2.tsUserRepository instance will not be initialized.Cannot read property 'count' of undefinedUserRepository instance will be initialized correctly even without name property in ConnectionOptions or without this.injector.get<TypeORMService>(TypeORMService); process.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
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 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.