Skip to content

Commit f2150bd

Browse files
committed
refactor(query-builder): address SonarQube findings
- Add readonly to RelationIdLoader.loadEagerRelations - Extract applyEagerRelations helper in RelationLoader to eliminate code duplication across the four load methods - Use optional chaining instead of non-null assertions in tests
1 parent bc8d306 commit f2150bd

3 files changed

Lines changed: 46 additions & 72 deletions

File tree

‎src/query-builder/RelationIdLoader.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class RelationIdLoader {
1717
constructor(
1818
private dataSource: DataSource,
1919
protected queryRunner?: QueryRunner | undefined,
20-
private loadEagerRelations?: boolean,
20+
private readonly loadEagerRelations?: boolean,
2121
) {}
2222

2323
// -------------------------------------------------------------------------

‎src/query-builder/RelationLoader.ts‎

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,7 @@ export class RelationLoader {
187187
qb.where(condition)
188188
}
189189

190-
if (loadEagerRelations !== false) {
191-
if (qb.expressionMap.relationLoadStrategy === "query") {
192-
qb.concatRelationMetadata(
193-
...qb.expressionMap.mainAlias!.metadata.eagerRelations,
194-
)
195-
} else {
196-
FindOptionsUtils.joinEagerRelations(
197-
qb,
198-
qb.alias,
199-
qb.expressionMap.mainAlias!.metadata,
200-
)
201-
}
202-
}
190+
this.applyEagerRelations(qb, loadEagerRelations)
203191

204192
return qb.getMany()
205193
// return qb.getOne(); todo: fix all usages
@@ -285,19 +273,7 @@ export class RelationLoader {
285273
qb.where(condition)
286274
}
287275

288-
if (loadEagerRelations !== false) {
289-
if (qb.expressionMap.relationLoadStrategy === "query") {
290-
qb.concatRelationMetadata(
291-
...qb.expressionMap.mainAlias!.metadata.eagerRelations,
292-
)
293-
} else {
294-
FindOptionsUtils.joinEagerRelations(
295-
qb,
296-
qb.alias,
297-
qb.expressionMap.mainAlias!.metadata,
298-
)
299-
}
300-
}
276+
this.applyEagerRelations(qb, loadEagerRelations)
301277

302278
return qb.getMany()
303279
// return relation.isOneToMany ? qb.getMany() : qb.getOne(); todo: fix all usages
@@ -367,19 +343,7 @@ export class RelationLoader {
367343
),
368344
).setParameters(parameters)
369345

370-
if (loadEagerRelations !== false) {
371-
if (qb.expressionMap.relationLoadStrategy === "query") {
372-
qb.concatRelationMetadata(
373-
...qb.expressionMap.mainAlias!.metadata.eagerRelations,
374-
)
375-
} else {
376-
FindOptionsUtils.joinEagerRelations(
377-
qb,
378-
qb.alias,
379-
qb.expressionMap.mainAlias!.metadata,
380-
)
381-
}
382-
}
346+
this.applyEagerRelations(qb, loadEagerRelations)
383347

384348
return qb.getMany()
385349
}
@@ -449,23 +413,36 @@ export class RelationLoader {
449413
),
450414
).setParameters(parameters)
451415

452-
if (loadEagerRelations !== false) {
453-
if (qb.expressionMap.relationLoadStrategy === "query") {
454-
qb.concatRelationMetadata(
455-
...qb.expressionMap.mainAlias!.metadata.eagerRelations,
456-
)
457-
} else {
458-
FindOptionsUtils.joinEagerRelations(
459-
qb,
460-
qb.alias,
461-
qb.expressionMap.mainAlias!.metadata,
462-
)
463-
}
464-
}
416+
this.applyEagerRelations(qb, loadEagerRelations)
465417

466418
return qb.getMany()
467419
}
468420

421+
/**
422+
* Applies eager relation loading to the given query builder based on the
423+
* configured relation load strategy.
424+
* @param qb
425+
* @param loadEagerRelations
426+
*/
427+
private applyEagerRelations(
428+
qb: SelectQueryBuilder<any>,
429+
loadEagerRelations?: boolean,
430+
): void {
431+
if (loadEagerRelations === false) return
432+
433+
if (qb.expressionMap.relationLoadStrategy === "query") {
434+
qb.concatRelationMetadata(
435+
...qb.expressionMap.mainAlias!.metadata.eagerRelations,
436+
)
437+
} else {
438+
FindOptionsUtils.joinEagerRelations(
439+
qb,
440+
qb.alias,
441+
qb.expressionMap.mainAlias!.metadata,
442+
)
443+
}
444+
}
445+
469446
/**
470447
* Wraps given entity and creates getters/setters for its given relation
471448
* to be able to lazily load data when accessing this relation.

‎test/functional/relations/load-strategy/relation-load-strategy-query.test.ts‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ describe("relations > load-strategy > query", () => {
8888
})
8989

9090
expect(result).to.not.be.null
91-
expect(result!.name).to.equal("author1")
92-
expect(result!.books).to.be.an("array")
93-
expect(result!.books).to.have.length(3)
91+
expect(result?.name).to.equal("author1")
92+
expect(result?.books).to.be.an("array")
93+
expect(result?.books).to.have.length(3)
9494

95-
const titles = result!.books
95+
const titles = result?.books
9696
.map((b) => b.title)
9797
.sort((a, b) => a.localeCompare(b))
9898
expect(titles).to.deep.equal(["book1", "book2", "book3"])
@@ -113,10 +113,10 @@ describe("relations > load-strategy > query", () => {
113113
})
114114

115115
expect(result).to.not.be.null
116-
for (const book of result!.books) {
116+
for (const book of result?.books ?? []) {
117117
expect(book.comments).to.be.an("array")
118118
expect(book.comments).to.have.length(2)
119-
for (const comment of book.comments!) {
119+
for (const comment of book.comments ?? []) {
120120
expect(comment.text).to.include(book.title)
121121
}
122122
}
@@ -147,7 +147,6 @@ describe("relations > load-strategy > query", () => {
147147
expect(review.book).to.not.be.undefined
148148
expect(review.book).to.not.be.null
149149
expect(review.book.title).to.equal("book1")
150-
// nested eager: Book -> Comments
151150
expect(review.book.comments).to.be.an("array")
152151
}
153152
}),
@@ -264,8 +263,8 @@ describe("relations > load-strategy > query", () => {
264263
})
265264

266265
expect(result).to.not.be.null
267-
expect(result!.books).to.be.an("array")
268-
expect(result!.books).to.have.length(0)
266+
expect(result?.books).to.be.an("array")
267+
expect(result?.books).to.have.length(0)
269268
}),
270269
))
271270

@@ -292,12 +291,12 @@ describe("relations > load-strategy > query", () => {
292291
})
293292

294293
expect(result).to.not.be.null
295-
const loadedBook = result!.books.find(
294+
const loadedBook = result?.books.find(
296295
(b) => b.id === book.id,
297296
)
298297
expect(loadedBook).to.not.be.undefined
299-
expect(loadedBook!.comments).to.be.an("array")
300-
expect(loadedBook!.comments).to.have.length(0)
298+
expect(loadedBook?.comments).to.be.an("array")
299+
expect(loadedBook?.comments).to.have.length(0)
301300
}),
302301
))
303302
})
@@ -323,7 +322,6 @@ describe("relations > load-strategy > query", () => {
323322
expect(results[1].books).to.be.an("array")
324323
expect(results[1].books).to.have.length(2)
325324

326-
// nested eager relations load for all results
327325
for (const author of results) {
328326
for (const book of author.books) {
329327
expect(book.comments).to.be.an("array")
@@ -375,7 +373,6 @@ describe("relations > load-strategy > query", () => {
375373
]),
376374
)
377375

378-
// no relationLoadStrategy specified — uses DataSource default
379376
const results = await authorRepository.find({
380377
order: { name: "ASC" },
381378
})
@@ -406,8 +403,8 @@ describe("relations > load-strategy > query", () => {
406403
})
407404

408405
expect(result).to.not.be.null
409-
expect(result!.name).to.equal("author1")
410-
expect(result!.books).to.be.undefined
406+
expect(result?.name).to.equal("author1")
407+
expect(result?.books).to.be.undefined
411408
}),
412409
))
413410

@@ -425,10 +422,10 @@ describe("relations > load-strategy > query", () => {
425422
})
426423

427424
expect(result).to.not.be.null
428-
expect(result!.books).to.be.an("array")
429-
expect(result!.books).to.have.length(3)
425+
expect(result?.books).to.be.an("array")
426+
expect(result?.books).to.have.length(3)
430427

431-
for (const book of result!.books) {
428+
for (const book of result?.books ?? []) {
432429
expect(book.comments).to.be.undefined
433430
}
434431
}),

0 commit comments

Comments
 (0)