-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
custom-repository.test.ts
61 lines (57 loc) · 2.21 KB
/
custom-repository.test.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
import "../../utils/test-setup"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { User } from "./entity/User"
import { DataSource } from "../../../src"
import { expect } from "chai"
describe("custom repository", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
entities: [User],
})),
)
beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))
it("withRepository must work properly in transactions", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const CustomRepository = dataSource.getRepository(User).extend({
findOneByName(name: string) {
return this.findOneBy({ name })
},
})
// check if custom repository function works
await CustomRepository.save({ name: "Timber Saw" })
const user = await CustomRepository.findOneByName("Timber Saw")
expect(user).to.be.eql({
id: 1,
name: "Timber Saw",
})
// now check it in the transaction
await dataSource.manager.transaction(
async (transactionalManager) => {
const transactionalCustomRepository =
await transactionalManager.withRepository(
CustomRepository,
)
await transactionalCustomRepository.save({
name: "Natures Prophet",
})
const user =
await transactionalCustomRepository.findOneByName(
"Natures Prophet",
)
expect(user).to.be.eql({
id: 2,
name: "Natures Prophet",
})
},
)
}),
))
})