Skip to content

Commit

Permalink
fix(query-typeorm): Use qb directly when adding additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed May 16, 2022
1 parent ba4e7d0 commit 5843ac4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
8 changes: 3 additions & 5 deletions jest.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ module.exports = {
preset: './jest.preset.ts',
globals: {
'ts-jest': {
tsconfig: './examples/tsconfig.spec.json'
tsconfig: process.cwd() + '/examples/tsconfig.spec.json'
}
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': 'ts-jest'
},
moduleFileExtensions: ['ts', 'js', 'html'],
testMatch: ['**/e2e/**/*.spec.ts'],

testMatch: ['**/examples/**/e2e/**/*.spec.ts'],
setupFilesAfterEnv: ['jest-extended'],
snapshotSerializers: ['jest-snapshot-serializer-raw/always'],

coverageDirectory: '../coverage/examples'
coverageDirectory: './coverage/examples'
};
20 changes: 8 additions & 12 deletions packages/query-typeorm/src/query/relation-query.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,18 @@ export class RelationQueryBuilder<Entity, Relation> {
}).join(' AND ');

const whereParams = {};
const andSelect = [];
const whereCondition = primaryColumns.map((column) => {
const paramName = this.getParamName(aliasName);

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

// Also select the columns, so we can use them to map later
andSelect.push(`${aliasName}.${column.propertyPath}`);
qb.addSelect(`${aliasName}.${column.propertyPath}`)

return `${aliasName}.${column.propertyPath} IN (:...${paramName})`;
}).join(' AND ');

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

Expand Down Expand Up @@ -394,14 +393,14 @@ export class RelationQueryBuilder<Entity, Relation> {

batchSelect: (qb, entities: Entity[]) => {
const params = {};
const andSelect = [];

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

// We also want to select the field, so we can map them back in the mapper
andSelect.push(`${joinAlias}.${column.propertyName} AS ${joinAlias}_${column.propertyName}`);
qb.addSelect(`${joinAlias}.${column.propertyName}`, `${joinAlias}_${column.propertyName}`)

return `${joinAlias}.${column.propertyName} IN (:...${paramName})`;
}).join(' AND ');

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

return qb.innerJoin(join.target, join.alias, conditions.join(' AND '));
}, qb)
.addSelect(andSelect)
.andWhere(sql, params);
},

Expand Down Expand Up @@ -469,10 +467,9 @@ export class RelationQueryBuilder<Entity, Relation> {
// First filter the raw relations with the PK of the entity, then filter the relations
// with the PK of the raw relation
return lodashFilter(rawRelations, rawFilter).reduce((entityRelations, rawRelation) => {
const filter = relation.inverseRelation.joinColumns.reduce((columns, column) => ({
const filter = this.getRelationPrimaryKeysPropertyNameAndColumnsName().reduce((columns, column) => ({
...columns,

[column.referencedColumn.propertyName]: rawRelation[`${joinAlias}_${column.propertyName}`]
[column.propertyName]: rawRelation[column.columnName]
}), {} as Partial<Entity>);

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

batchSelect: (qb, entities: Entity[]) => {
const params = {};
const andSelect = [];

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

// We also want to select the field, so we can map them back in the mapper
andSelect.push(`${joinAlias}.${column.propertyName} AS ${joinAlias}_${column.propertyName}`);
qb.addSelect(`${joinAlias}.${column.propertyName}`, `${joinAlias}_${column.propertyName}`)

return `${joinAlias}.${column.propertyName} IN (:...${paramName})`;
}).join(' AND ');

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

return qb.innerJoin(join.target, join.alias, conditions.join(' AND '));
}, qb)
.addSelect(andSelect)
.andWhere(sql, params);
},

Expand Down

0 comments on commit 5843ac4

Please sign in to comment.