-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
issue-9988.ts
103 lines (90 loc) · 4.13 KB
/
issue-9988.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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"
import { FindManyOptions } from "../../../src"
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 },
relations: { categories: true },
} as FindManyOptions<Product>)
// 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() {
return this.createQueryBuilder("product")
.setFindOptions(options(productOneId))
.getOne()
},
async getMany() {
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 },
{ id: categoryTwo.id },
],
})
await customProductRepo.save(product)
return await customProductRepo.getMany()
},
)
expect(getManyProduct[0].categories.length).to.be.eql(2)
}),
)
})
})