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

No repository for "XXXRepository" was found. Looks like this entity is not registered in current "default" connection? #70

Closed
freefred81 opened this issue Aug 22, 2019 · 15 comments

Comments

@freefred81
Copy link

Hi ... I'm trying to use TypeORM-Linq with my Nest.js project.
But it give to me this error on startup:

[Nest] 1208 - 2019-08-22 9:07 AM [ExceptionHandler] No repository for "OpportunityRepository" was found. Looks like this entity is not registered in current "default" connection? +32ms
RepositoryNotFoundError: No repository for "OpportunityRepository" was found. Looks like this entity is not registered in current "default" connection?
at new RepositoryNotFoundError (/Users/fscamuzzi/Documents/Temp/nestproject/src/error/RepositoryNotFoundError.ts:10:9)
at EntityManager.getRepository (/Users/fscamuzzi/Documents/Temp/nestproject/src/entity-manager/EntityManager.ts:1008:19)
at Connection.getRepository (/Users/fscamuzzi/Documents/Temp/nestproject/src/connection/Connection.ts:342:29)
at getRepository (/Users/fscamuzzi/Documents/Temp/nestproject/node_modules/@nestjs/typeorm/dist/typeorm.providers.js:13:26)
at InstanceWrapper.useFactory [as metatype] (/Users/fscamuzzi/Documents/Temp/nestproject/node_modules/@nestjs/typeorm/dist/typeorm.providers.js:22:20)
at Injector.instantiateClass (/Users/fscamuzzi/Documents/Temp/nestproject/node_modules/@nestjs/core/injector/injector.js:279:55)
at callback (/Users/fscamuzzi/Documents/Temp/nestproject/node_modules/@nestjs/core/injector/injector.js:74:41)
at process._tickCallback (internal/process/next_tick.js:68:7)
Waiting for the debugger to disconnect...

If i remove TypeOrm-linq everything works as expected ...

Can you help me? your prject for a guy like me who came from a strong C# background is a Joy!!! ...

my repo :

https://github.com/freefred81/nestproject

@freefred81
Copy link
Author

freefred81 commented Aug 22, 2019

I have change a little bit my code ..and now it works .. BUT only the GetAll() ... if i try a where clause it stay stucked .. i have updated the repo ... thnx!

@freefred81
Copy link
Author

Hi .. any news?

@IRCraziestTaxi
Copy link
Owner

Sorry I have been off the radar. Work has been bonkers lately.

I will take a look at the repo and see if I can spot the problem.

@IRCraziestTaxi
Copy link
Owner

@freefred81 Your usage of

let query =  await this.opportunityRepository.getAll()
    .where(xx => xx.isdeleted)
    .isFalse();

looks correct. Could you clarify what you mean by

if i try a where clause it stay stucked

? Do you get an error or does the async never return? Hopefully you can get that info to me quicker than it would take me to get a database set up and all that so I can try it myself.

@freefred81
Copy link
Author

freefred81 commented Aug 24, 2019

Hi ...!!! ... First of all REALLY REALLY thnx for your time and support .... !! .. i know you maybe work hard .. so really thnx for your time !! ... I'vee update my repo with a new folder (database) inside there's a scripts folder where you can find 2 scripts (.sql) to create the Table Opportunity e insert one record inside it ... So you can debug it and you'll se what happen .... if you try to call the GET method of Opportunity route with :

http://localhost:3000/opportunity

it hit the method in the controller

 @Get()
  async findAll(@Query() query: QueryParams): Promise<OpportunityDto[]> {
    const resultEntities = await this.opportunityService.getAll();
    return resultEntities;
  }

It calls the getAll() method in the opportunityService:

getAll = async (): Promise<OpportunityDto[]> => {

const result = await this.opportunityRepository.getAll();
return result.map(xx => xx.ToDto());

};

and it call the getAll() method from the OpportunityRepository (which extends your LinqRepository)

@Injectable()
// @EntityRepository(Opportunity)
export class OpportunityRepository extends LinqRepository<Opportunity> implements IOpportunityRepository {

    public constructor() {
        super(Opportunity);
    }

}

Here everything works as expected ...

but if you try to call other API in of the OpportunityController with

http://localhost:3000/opportunity/not/deleted?skip=0&take=10

it call the method of controller

  @Get('not/deleted')
  async findAllNotDeleted(@Query() query: QueryParams): Promise<OpportunityDto[]> {
    const resultEntities = await this.opportunityService.findNotDeleted(query);
    return resultEntities;
  }

it calls the findNotDeleted(..) method in the opportunityService

 findNotDeleted = async (params: QueryParams): Promise<OpportunityDto[]> => {

    let query = this.opportunityRepository.getAll()
      .where(xx => xx.isdeleted)
      .isFalse();

    const countNotDeleted = await query.count();

    // Set paging parameters on the query.
    query =  query
      .skip(params.skip)
      .take(params.tale);

    const result = await query;
    return result.map(xx => xx.ToDto());

  }

BUT here when it reach the let query = this.opportunityRepository.getAll() .where(xx => xx.isdeleted) .isFalse(); in the debug tab (see screenshot above of my vscode debug tab) it hit the DB with a FULL select * frorm Opportunity (no where(...) ) and it's also strange cause it launch the query BEFORE the await ... and another strange things .. even if it launch the wrong query (and it's logged in the debug tab) .. it get stuck and

IT NEVER GOES TO THE NEXT BREAK POINT

(line 36)

const countNotDeleted = await query.count();

AND NO RESULT IS RETURNED ...

image

@IRCraziestTaxi
Copy link
Owner

How are you debugging? Can you share your launch.json? I haven't really used nest or nodemon and am having quite a time trying to figure out how to debug with it.

@freefred81
Copy link
Author

freefred81 commented Aug 26, 2019

Really sorry for delay ...

try to install nodemon globally with

npm install -g nodemon

then inside the project (once you've

open it with vs code try the auto attach settings and sei it to ON

then strat the npm scipts start:debug

Vs code will start in debug mode (and will hitting yours break point)

image

Let me know if it works or not!
realy thnx!!

@IRCraziestTaxi
Copy link
Owner

I don't know what's happening here. :/ The debug console spat out some SQL junk then the debugger detached. Now I cannot debug because I get Starting inspector on 127.0.0.1:9229 failed: address already in use.

When I just run npm start in a cmd console, I get the original error: No repository for "OpportunityRepository" was found. Looks like this entity is not registered in current "default" connection?.

I also had to fix several typescript errors to get the TS to compile before I could run the app at all. I don't see any commits to pull or another branch to check out... are you sure you have all your changes checked in?

@freefred81
Copy link
Author

mmm looks strange .. i don'n have any error.. try to disable the tslint vscode extensions (it's too much strict).

then have you set auto attach to ON in the vscode?

@IRCraziestTaxi
Copy link
Owner

IRCraziestTaxi commented Aug 27, 2019

After restarting so I could attempt debugging again, I took exact notes of my actions and the results I received.

  1. Auto Attach: On in VS Code

  2. NPM Scripts > start:debug

  3. Debugger breaks (although no breakpoint is set) at line 1 of main.ts (which is weird, but doesn't appear to hurt anything as far as I can tell)

  4. Continue debugging

  5. After several seconds, the Debug Console displays the following:

query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = current_schema() AND "table_name" = 'migrations'
PlatformTools.ts:220
query: SELECT * FROM "migrations" "migrations"
PlatformTools.ts:220
  1. Debugger immediately detaches after above output

  2. Attempt to view the source of PlatformTools.ts by clicking the link to PlatformTools.ts:220 in the Debug Console output

  3. Receive the following error:

Unable to open 'c:\github\nestproject\src\platform\PlatformTools.ts': Unable to resolve the resource without a debug session.
  1. NPM Scripts > start:debug again

  2. Terminal (not Debug Console) displays the following:

(Note the original No repository error)

> Executing task: npm run start:debug <


> project@0.0.1 start:debug C:\github\nestproject
> nodemon --config nodemon-debug.json

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: C:\github\nestproject\src/**/*
[nodemon] starting `node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts`
Debugger listening on ws://127.0.0.1:9229/8673e28e-6c3a-434e-a5d6-8881aad3430f
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
[Nest] 9140   - 08/27/2019, 1:53 PM   [NestFactory] Starting Nest application...
[Nest] 9140   - 08/27/2019, 1:53 PM   [InstanceLoader] AppModule dependencies initialized +143ms
[Nest] 9140   - 08/27/2019, 1:53 PM   [InstanceLoader] TypeOrmModule dependencies initialized +1ms
query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = current_schema() AND "table_name" = 'migrations'
query: SELECT * FROM "migrations" "migrations"
[Nest] 9140   - 08/27/2019, 1:53 PM   [InstanceLoader] TypeOrmCoreModule dependencies initialized +742ms
[Nest] 9140   - 08/27/2019, 1:53 PM   [ExceptionHandler] No repository for "OpportunityRepository" was found. Looks like this entity is not registered in current "default" connection? +36ms
RepositoryNotFoundError: No repository for "OpportunityRepository" was found. Looks like this entity is not registered in current "default" connection?
    at new RepositoryNotFoundError (C:\github\nestproject\src\error\RepositoryNotFoundError.ts:10:9)
    at EntityManager.getRepository (C:\github\nestproject\src\entity-manager\EntityManager.ts:1008:19)
    at Connection.getRepository (C:\github\nestproject\src\connection\Connection.ts:342:29)
    at getRepository (C:\github\nestproject\node_modules\@nestjs\typeorm\dist\typeorm.providers.js:13:26)
    at InstanceWrapper.useFactory [as metatype] (C:\github\nestproject\node_modules\@nestjs\typeorm\dist\typeorm.providers.js:22:20)
    at Injector.instantiateClass (C:\github\nestproject\node_modules\@nestjs\core\injector\injector.js:279:55)
    at callback (C:\github\nestproject\node_modules\@nestjs\core\injector\injector.js:74:41)
    at process._tickCallback (internal/process/next_tick.js:68:7)
Waiting for the debugger to disconnect...
[nodemon] app crashed - waiting for file changes before starting...
  1. Also receive the following notification in VS Code:
The task 'npm: start:debug (nestproject)' is already active.

With the following options available:

Terminate Task

Restart Task
  1. Click Restart Task

  2. Terminal displays the following:

> Executing task: npm run start:debug <


> project@0.0.1 start:debug C:\github\nestproject
> nodemon --config nodemon-debug.json

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: C:\github\nestproject\src/**/*
[nodemon] starting `node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts`
Starting inspector on 127.0.0.1:9229 failed: address already in use
[nodemon] app crashed - waiting for file changes before starting...

HOWEVER, debugger starts anyway (again, strange, but doesn't appear to hurt anything)

  1. Same results as steps 3 - 8.

  2. NPM Scripts > start:debug again

  3. Receive the same notification from step 11.

  4. Click Restart Task again.

  5. Same Terminal output from step 13 BUT the debugger does not start this time.

  6. Repeat steps 15 - 18 perpetually.

So the Starting inspector on 127.0.0.1:9229 failed: address already in use error is extremely annoying, but the thing that is killing me is the result from steps 5 - 6. I have no idea why this PlatformTools.ts file that apparently only exists while debugging is running those queries and why the debugger detaches immediately afterward.

I'll keep looking when I have time, but without being able to run the app, there's not much I can do.

@IRCraziestTaxi
Copy link
Owner

Ah, ok... I had a suspicion of something and confirmed it.

The reason the debugger detaches is because the app crashes with the original No repository error.

At the same time the debugger stops, the Terminal displays the error.

> Executing task: npm run start:debug <


> project@0.0.1 start:debug C:\github\nestproject
> nodemon --config nodemon-debug.json

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: C:\github\nestproject\src/**/*
[nodemon] starting `node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts`
Debugger listening on ws://127.0.0.1:9229/1bdcf50e-d842-471d-9c84-6828d3c591a4
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
[Nest] 16948   - 08/27/2019, 2:37 PM   [NestFactory] Starting Nest application...
[Nest] 16948   - 08/27/2019, 2:37 PM   [InstanceLoader] AppModule dependencies initialized +135ms
[Nest] 16948   - 08/27/2019, 2:37 PM   [InstanceLoader] TypeOrmModule dependencies initialized +1ms
query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = current_schema() AND "table_name" = 'migrations'
query: SELECT * FROM "migrations" "migrations"
[Nest] 16948   - 08/27/2019, 2:37 PM   [InstanceLoader] TypeOrmCoreModule dependencies initialized +722ms
[Nest] 16948   - 08/27/2019, 2:37 PM   [ExceptionHandler] No repository for "OpportunityRepository" was found. Looks like this entity is not registered in current "default" connection? +37ms
RepositoryNotFoundError: No repository for "OpportunityRepository" was found. Looks like this entity is not registered in current "default" connection?
    at new RepositoryNotFoundError (C:\github\nestproject\src\error\RepositoryNotFoundError.ts:10:9)
    at EntityManager.getRepository (C:\github\nestproject\src\entity-manager\EntityManager.ts:1008:19)
    at Connection.getRepository (C:\github\nestproject\src\connection\Connection.ts:342:29)
    at getRepository (C:\github\nestproject\node_modules\@nestjs\typeorm\dist\typeorm.providers.js:13:26)
    at InstanceWrapper.useFactory [as metatype] (C:\github\nestproject\node_modules\@nestjs\typeorm\dist\typeorm.providers.js:22:20)
    at Injector.instantiateClass (C:\github\nestproject\node_modules\@nestjs\core\injector\injector.js:279:55)
    at callback (C:\github\nestproject\node_modules\@nestjs\core\injector\injector.js:74:41)
    at process._tickCallback (internal/process/next_tick.js:68:7)
Waiting for the debugger to di[nodemon] app crashed - waiting for file changes before starting...

@IRCraziestTaxi
Copy link
Owner

IRCraziestTaxi commented Aug 27, 2019

Ok, I can't believe I missed this before, but here is what is happening.

I didn't realize the error said that no repository was found for OpportunityRepository, not for Opportunity.

Since OpportunityRepository is not an entity, that's why I was getting that error and the app was crashing.

So, sure enough, in opportunity.module.ts, the imported entities included OpportunityRepository:

imports: [TypeOrmModule.forFeature([Opportunity, OpportunityRepository])],

But since OpportunityRepository is not an entity, it should be:

imports: [TypeOrmModule.forFeature([Opportunity])],

So I made that change and then could run and debug the app.

However, what I find now is that OpportunityRepository is not an instance of a class that extends LinqRepository as it is defined in opportunity.repository.ts, but for some reason, rather an instance of TypeORM's own Repository, which has no getAll() method, which is why the service calls are crashing and sending an empty result.

I'm not sure why your OpportunityRepository does not extend LinqRepository, but my guess is that Nest is simply injecting an instance of the TypeORM Repository for the entity instead of paying any mind to the fact that you defined it as extending LinqRepository.

My first suggestion was going to replace the following in opportunity.module.ts:

providers: [OpportunityService],

with:

providers: [
    {
        provide: OpportunityService,
        useFactory: () => new OpportunityService(new OpportunityRepository())
    }
]

just to see if that would force the correct repository to be used (although it should not be necessary), but oddly enough, that throws the following error:

No repository for "Opportunity" was found. Looks like this entity is not registered in current "default" connection?

Where now the actual Opportunity entity is indeed not registered for some reason despite being present in:

imports: [TypeOrmModule.forFeature([Opportunity])],

I am truly, truly perplexed at what the hell Nest is doing.

@IRCraziestTaxi
Copy link
Owner

Ok, after some digging, I found this @nestjs/typeorm issue. Go through that; in particular, this comment looks promising.

I won't have time to look into this further for a while, but maybe that will get you closer to the solution.

At any rate, I believe it is clear at this point that this is not a typeorm-linq-repository issue but a Nest issue, so I'm going to close this for now, but feel free to continue the discussion.

@freefred81
Copy link
Author

Hi !! ... IT WORKS!!!!! .... really really thnx for your time and patiente ... it works as expected!! .... you' re the number ONE!!!

@IRCraziestTaxi
Copy link
Owner

That's great! I'm glad it's resolved! :)

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

No branches or pull requests

2 participants