Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: RelationIdLoader has access to queryPlanner when wrapped in tran…
…saction (#9990) fix issue - createQueryBuilder in repository loses queryplanner when wrapped in txn upsteam until RelationIdLoader Closes: #9988
- Loading branch information
Showing
5 changed files
with
140 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Entity, ManyToMany, PrimaryGeneratedColumn } from "../../../../src" | ||
import { Product } from "./product" | ||
|
||
@Entity({ name: "category" }) | ||
export class Category { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@ManyToMany(() => Product, (product) => product.categories) | ||
products: Product[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
Entity, | ||
JoinTable, | ||
ManyToMany, | ||
PrimaryGeneratedColumn, | ||
} from "../../../../src" | ||
import { Category } from "./category" | ||
|
||
@Entity({ name: "product" }) | ||
export class Product { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@ManyToMany(() => Category, (category) => category.products) | ||
@JoinTable({ name: "product_category" }) | ||
categories: Category[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import "reflect-metadata" | ||
import { | ||
createTestingConnections as createTestingDatasources, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src/data-source/DataSource" | ||
import { expect } from "chai" | ||
import { Product } from "./entity/product" | ||
import { Category } from "./entity/category" | ||
|
||
describe("github issues > #9988 RelationIdLoader reuses the same queryplanner within a transaction", () => { | ||
let dataSources: DataSource[] | ||
|
||
before( | ||
async () => | ||
(dataSources = await createTestingDatasources({ | ||
entities: [Product, Category], | ||
enabledDrivers: ["postgres"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})), | ||
) | ||
|
||
beforeEach(() => reloadTestingDatabases(dataSources)) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("custom repository querybuilders within transactions returns relations for getOne() and getMany", async () => { | ||
await Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
const manager = dataSource.manager | ||
|
||
// Setup seed | ||
const categoryRepo = manager.getRepository(Category) | ||
const categoryOne = categoryRepo.create({ id: 1 }) | ||
const categoryTwo = categoryRepo.create({ id: 2 }) | ||
const productOneId = 1 | ||
const productTwoId = 2 | ||
await categoryRepo.save(categoryOne) | ||
await categoryRepo.save(categoryTwo) | ||
const options = (id: number) => ({ | ||
relationLoadStrategy: "query", | ||
where: { id: productOneId }, | ||
relations: { categories: true }, | ||
}) | ||
|
||
// Create a custom repository that uses a query builder without query planner | ||
// For both methods, relationLoadStrategy is set to "query", where the bug lies. | ||
const productRepo = dataSource.getRepository(Product).extend({ | ||
async getOne(): Promise<Product> { | ||
return this.createQueryBuilder("product") | ||
.setFindOptions(options(productOneId)) | ||
.getOne() | ||
}, | ||
|
||
async getMany(): Promise<Product[]> { | ||
return this.createQueryBuilder("product") | ||
.setFindOptions(options(productTwoId)) | ||
.getMany() | ||
}, | ||
}) | ||
|
||
// Creates a transaction that is shared across all the queries | ||
const getOneProduct = await manager.transaction( | ||
async (txnManager) => { | ||
const customProductRepo = | ||
txnManager.withRepository(productRepo) | ||
const product = customProductRepo.create({ | ||
id: productOneId, | ||
categories: [{ id: categoryOne.id }], | ||
}) | ||
|
||
await customProductRepo.save(product) | ||
return await customProductRepo.getOne() | ||
}, | ||
) | ||
|
||
expect(getOneProduct.categories.length).to.be.eql(1) | ||
|
||
const getManyProduct = await manager.transaction( | ||
async (txnManager) => { | ||
const customProductRepo = | ||
txnManager.withRepository(productRepo) | ||
const product = customProductRepo.create({ | ||
id: productTwoId, | ||
categories: [{ id: categoryOne.id }], | ||
}) | ||
|
||
await customProductRepo.save(product) | ||
return await customProductRepo.getMany() | ||
}, | ||
) | ||
|
||
expect(getManyProduct[0].categories.length).to.be.eql(1) | ||
}), | ||
) | ||
}) | ||
}) |