Skip to content

Commit

Permalink
fix(typeorm): Allow using string based typeorm relations
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Jun 19, 2020
1 parent c016867 commit 55c157d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class TestEntity {
@Column({ name: 'date_type' })
dateType!: Date;

@OneToMany(() => TestRelation, (tr) => tr.testEntity)
@OneToMany('TestRelation', 'testEntity')
testRelations?: TestRelation[];

@ManyToMany(() => TestRelation, (tr) => tr.manyTestEntities, { onDelete: 'CASCADE', nullable: false })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Repository,
SelectQueryBuilder,
UpdateQueryBuilder,
EntityManager,
} from 'typeorm';
import { SoftDeleteQueryBuilder } from 'typeorm/query-builder/SoftDeleteQueryBuilder';
import { TypeOrmQueryService, TypeOrmQueryServiceOpts } from '../../src';
Expand Down Expand Up @@ -101,7 +102,27 @@ describe('TypeOrmQueryService', (): void => {
const queryResult = await queryService.queryRelations(TestRelation, relationName, entity, query);
return expect(queryResult).toEqual(relations);
});

it('should look up the type when the relation.type is a string', async () => {
const entity = testEntities()[0];
const relations = testRelations(entity.testEntityPk);
const query: Query<TestRelation> = { filter: { relationName: { eq: 'name' } } };
const { queryService, mockRepo, mockRelationQueryBuilder } = createQueryService<TestEntity, TestRelation>();
const selectQueryBuilder: SelectQueryBuilder<TestRelation> = mock(SelectQueryBuilder);
const mockManger = mock(EntityManager);
const mockRelationRepo = mock<Repository<TestRelation>>();
// @ts-ignore
when(mockRepo.metadata).thenReturn({ relations: [{ propertyName: relationName, type: 'TestRelation' }] });
when(mockRepo.manager).thenReturn(instance(mockManger));
when(mockManger.getRepository('TestRelation')).thenReturn(instance(mockRelationRepo));
when(mockRelationRepo.target).thenReturn(TestRelation);
when(mockRelationQueryBuilder.select(objectContaining(entity), query)).thenReturn(instance(selectQueryBuilder));
when(selectQueryBuilder.getMany()).thenResolve(relations);
const queryResult = await queryService.queryRelations(TestRelation, relationName, entity, query);
return expect(queryResult).toEqual(relations);
});
});

describe('with multiple entities', () => {
it('call select and return the result', async () => {
const entities = testEntities();
Expand Down
3 changes: 3 additions & 0 deletions packages/query-typeorm/src/services/relation-query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ export abstract class RelationQueryService<Entity> {
if (!relationMeta) {
throw new Error(`Unable to find relation ${relationName} on ${this.EntityClass.name}`);
}
if (typeof relationMeta.type === 'string') {
return this.repo.manager.getRepository(relationMeta.type).target as Class<unknown>;
}
return relationMeta.type as Class<unknown>;
}
}

0 comments on commit 55c157d

Please sign in to comment.