doug-martin / nestjs-query

Easy CRUD for GraphQL.
https://doug-martin.github.io/nestjs-query
MIT License
820 stars 142 forks source link

Support for TypeORM 0.3 #1549

Closed abidon closed 1 month ago

abidon commented 2 years ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today we used patch-package to patch @nestjs-query/query-typeorm@0.30.0 for the project we are working on. This patch made our codebase work with TypeORM 0.3.


Apparently, with TypeORM 0.3, this.repo.manager.getRepository(relationMeta.type).target return a string instead of the entity class in some cases.

// (1) in TodoEntity.ts
@ManyToOne(() => ProjectEntity)
project: ProjectEntity;

// (2) in ProjectEntity.ts
@OneToMany('TodoEntity', (todo) => todo.project
todos: TodoEntity[]
  1. When specifying a typeFunction, repository.target equals the ProjectEntity class
  2. ⚠️ When specifying a string, repository.target is the 'TodoEntity' string, not class

This behaviour was introduced in TypeORM 0.3 and breaks nestjs-query, while TypeORM 0.2 always returned the entity class (at least in our use cases).


Here is the diff that solved our problem:

diff --git a/node_modules/@nestjs-query/query-typeorm/dist/src/services/relation-query.service.js b/node_modules/@nestjs-query/query-typeorm/dist/src/services/relation-query.service.js
index 0d46f16..8d06fee 100644
--- a/node_modules/@nestjs-query/query-typeorm/dist/src/services/relation-query.service.js
+++ b/node_modules/@nestjs-query/query-typeorm/dist/src/services/relation-query.service.js
@@ -245,7 +245,7 @@ class RelationQueryService {
     getRelationEntity(relationName) {
         const relationMeta = this.getRelationMeta(relationName);
         if (typeof relationMeta.type === 'string') {
-            return this.repo.manager.getRepository(relationMeta.type).target;
+            return this.repo.manager.connection.entityMetadatas.find(em => em.targetName == relationMeta.type).target;
         }
         return relationMeta.type;
     }

This issue body was partially generated by patch-package.

TriPSs commented 2 years ago

TypeORM v3 support is now released in the alpha version of my fork together with all the fixes from here

henry-huynh-3508 commented 1 year ago

TypeORM v3 support is now released in the alpha version of my fork together with all the fixes from here

Thanks TriPSs for keeping fixing the project bugs. The project owner should have transferred things over if he no longer continues to maintain this project, what a shame that it's a good project.

dzcpy commented 1 year ago

Any updates?