-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
multiple-primary-keys-one-to-many.ts
134 lines (113 loc) · 4.82 KB
/
multiple-primary-keys-one-to-many.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { expect } from "chai"
import "reflect-metadata"
import { DataSource } from "../../../../../src"
import {
closeTestingConnections,
createTestingConnections,
} from "../../../../utils/test-utils"
import { User } from "./entity/User"
import { Setting } from "./entity/Setting"
/**
* Using OneToMany relation with composed primary key should not error and work correctly
*/
describe("relations > multiple-primary-keys > one-to-many", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [User, Setting],
schemaCreate: true,
dropSchema: true,
})),
)
after(() => closeTestingConnections(connections))
function insertSimpleTestData(connection: DataSource) {
const userRepo = connection.getRepository(User)
// const settingRepo = connection.getRepository(Setting);
const user = new User(1, "FooGuy")
const settingA = new Setting(1, "A", "foo")
const settingB = new Setting(1, "B", "bar")
user.settings = [settingA, settingB]
return userRepo.save(user)
}
it("should correctly insert relation items", () =>
Promise.all(
connections.map(async (connection) => {
const userEntity = await insertSimpleTestData(connection)
const persistedSettings = await connection
.getRepository(Setting)
.find()
expect(persistedSettings!).not.to.be.undefined
expect(persistedSettings.length).to.equal(2)
expect(persistedSettings[0].assetId).to.equal(userEntity.id)
expect(persistedSettings[1].assetId).to.equal(userEntity.id)
}),
))
it("should correctly load relation items", () =>
Promise.all(
connections.map(async (connection) => {
await insertSimpleTestData(connection)
const [user] = await connection.getRepository(User).find({
relations: ["settings"],
// relationLoadStrategy: "join"
})
expect(user!).not.to.be.undefined
expect(user!.settings).to.be.an("array")
expect(user!.settings!.length).to.equal(2)
}),
))
it("should correctly update relation items", () =>
Promise.all(
connections.map(async (connection) => {
await insertSimpleTestData(connection)
const userRepo = connection.getRepository(User)
await userRepo.save([
{
id: 1,
settings: [
{ id: 1, name: "A", value: "foobar" },
{ id: 1, name: "B", value: "testvalue" },
],
},
])
const [user] = await connection
.getRepository(User)
.find({ relations: ["settings"] })
// check the saved items have correctly updated value
expect(user!).not.to.be.undefined
expect(user!.settings).to.be.an("array")
expect(user!.settings!.length).to.equal(2)
user!.settings.forEach((setting) => {
if (setting.name === "A")
expect(setting.value).to.equal("foobar")
else expect(setting.value).to.equal("testvalue")
})
// make sure only 2 entries are in db, initial ones should have been updated
const settings = await connection.getRepository(Setting).find()
expect(settings).to.be.an("array")
expect(settings!.length).to.equal(2)
}),
))
it("should correctly delete relation items", () =>
Promise.all(
connections.map(async (connection) => {
await insertSimpleTestData(connection)
const userRepo = connection.getRepository(User)
await userRepo.save({
id: 1,
settings: [],
})
const [user] = await connection.getRepository(User).find({
relations: ["settings"],
})
// check that no relational items are found
expect(user!).not.to.be.null
expect(user!.settings).to.be.an("array")
expect(user!.settings!.length).to.equal(0)
// check there are no orphane relational items
const settings = await connection.getRepository(Setting).find()
expect(settings).to.be.an("array")
expect(settings!.length).to.equal(0)
}),
))
})