Skip to content

Commit 5843ac4

Browse files
committed
fix(query-typeorm): Use qb directly when adding additional fields
1 parent ba4e7d0 commit 5843ac4

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

jest.e2e.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ module.exports = {
33
preset: './jest.preset.ts',
44
globals: {
55
'ts-jest': {
6-
tsconfig: './examples/tsconfig.spec.json'
6+
tsconfig: process.cwd() + '/examples/tsconfig.spec.json'
77
}
88
},
99
testEnvironment: 'node',
1010
transform: {
1111
'^.+\\.[tj]s$': 'ts-jest'
1212
},
1313
moduleFileExtensions: ['ts', 'js', 'html'],
14-
testMatch: ['**/e2e/**/*.spec.ts'],
15-
14+
testMatch: ['**/examples/**/e2e/**/*.spec.ts'],
1615
setupFilesAfterEnv: ['jest-extended'],
1716
snapshotSerializers: ['jest-snapshot-serializer-raw/always'],
18-
19-
coverageDirectory: '../coverage/examples'
17+
coverageDirectory: './coverage/examples'
2018
};

packages/query-typeorm/src/query/relation-query.builder.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,18 @@ export class RelationQueryBuilder<Entity, Relation> {
264264
}).join(' AND ');
265265

266266
const whereParams = {};
267-
const andSelect = [];
268267
const whereCondition = primaryColumns.map((column) => {
269268
const paramName = this.getParamName(aliasName);
270269

271270
whereParams[paramName] = entities.map((entity) => column.getEntityValue(entity));
272271

273272
// Also select the columns, so we can use them to map later
274-
andSelect.push(`${aliasName}.${column.propertyPath}`);
273+
qb.addSelect(`${aliasName}.${column.propertyPath}`)
274+
275275
return `${aliasName}.${column.propertyPath} IN (:...${paramName})`;
276276
}).join(' AND ');
277277

278278
return qb.leftJoin(relation.entityMetadata.target as Class<unknown>, aliasName, joinCondition)
279-
.addSelect(andSelect)
280279
.andWhere(whereCondition, whereParams);
281280
},
282281

@@ -394,14 +393,14 @@ export class RelationQueryBuilder<Entity, Relation> {
394393

395394
batchSelect: (qb, entities: Entity[]) => {
396395
const params = {};
397-
const andSelect = [];
398396

399397
const sql = relation.joinColumns.map((column) => {
400398
const paramName = this.getParamName(column.propertyName);
401399
params[paramName] = entities.map((entity) => column.referencedColumn!.getEntityValue(entity));
402400

403401
// We also want to select the field, so we can map them back in the mapper
404-
andSelect.push(`${joinAlias}.${column.propertyName} AS ${joinAlias}_${column.propertyName}`);
402+
qb.addSelect(`${joinAlias}.${column.propertyName}`, `${joinAlias}_${column.propertyName}`)
403+
405404
return `${joinAlias}.${column.propertyName} IN (:...${paramName})`;
406405
}).join(' AND ');
407406

@@ -411,7 +410,6 @@ export class RelationQueryBuilder<Entity, Relation> {
411410

412411
return qb.innerJoin(join.target, join.alias, conditions.join(' AND '));
413412
}, qb)
414-
.addSelect(andSelect)
415413
.andWhere(sql, params);
416414
},
417415

@@ -469,10 +467,9 @@ export class RelationQueryBuilder<Entity, Relation> {
469467
// First filter the raw relations with the PK of the entity, then filter the relations
470468
// with the PK of the raw relation
471469
return lodashFilter(rawRelations, rawFilter).reduce((entityRelations, rawRelation) => {
472-
const filter = relation.inverseRelation.joinColumns.reduce((columns, column) => ({
470+
const filter = this.getRelationPrimaryKeysPropertyNameAndColumnsName().reduce((columns, column) => ({
473471
...columns,
474-
475-
[column.referencedColumn.propertyName]: rawRelation[`${joinAlias}_${column.propertyName}`]
472+
[column.propertyName]: rawRelation[column.columnName]
476473
}), {} as Partial<Entity>);
477474

478475
return entityRelations.concat(lodashFilter(relations, filter) as any);
@@ -481,14 +478,14 @@ export class RelationQueryBuilder<Entity, Relation> {
481478

482479
batchSelect: (qb, entities: Entity[]) => {
483480
const params = {};
484-
const andSelect = [];
485481

486482
const sql = relation.inverseRelation!.inverseJoinColumns.map((column) => {
487483
const paramName = this.getParamName(column.propertyName);
488484
params[paramName] = entities.map((entity) => column.referencedColumn!.getEntityValue(entity));
489485

490486
// We also want to select the field, so we can map them back in the mapper
491-
andSelect.push(`${joinAlias}.${column.propertyName} AS ${joinAlias}_${column.propertyName}`);
487+
qb.addSelect(`${joinAlias}.${column.propertyName}`, `${joinAlias}_${column.propertyName}`)
488+
492489
return `${joinAlias}.${column.propertyName} IN (:...${paramName})`;
493490
}).join(' AND ');
494491

@@ -498,7 +495,6 @@ export class RelationQueryBuilder<Entity, Relation> {
498495

499496
return qb.innerJoin(join.target, join.alias, conditions.join(' AND '));
500497
}, qb)
501-
.addSelect(andSelect)
502498
.andWhere(sql, params);
503499
},
504500

0 commit comments

Comments
 (0)