-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
repository-find-options.ts
338 lines (293 loc) · 11.7 KB
/
repository-find-options.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import "reflect-metadata"
import { expect } from "chai"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../../utils/test-utils"
import { DataSource } from "../../../../src/data-source/DataSource"
import { User } from "./entity/User"
import { Category } from "./entity/Category"
import { Post } from "./entity/Post"
import { Photo } from "./entity/Photo"
import sinon from "sinon"
import { FileLogger } from "../../../../src"
import { promisify } from "util"
import { readFile, unlink } from "fs"
describe("repository > find options", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))
it("should load relations", () =>
Promise.all(
connections.map(async (connection) => {
const user = new User()
user.name = "Alex Messer"
await connection.manager.save(user)
const category = new Category()
category.name = "Boys"
await connection.manager.save(category)
const post = new Post()
post.title = "About Alex Messer"
post.author = user
post.categories = [category]
await connection.manager.save(post)
const [loadedPost] = await connection.getRepository(Post).find({
relations: ["author", "categories"],
})
expect(loadedPost).to.be.eql({
id: 1,
title: "About Alex Messer",
author: {
id: 1,
name: "Alex Messer",
},
categories: [
{
id: 1,
name: "Boys",
},
],
})
}),
))
it("should execute select query inside transaction", () =>
Promise.all(
connections.map(async (connection) => {
const user = new User()
user.name = "Alex Messer"
await connection.manager.save(user)
const queryRunner = await connection.createQueryRunner()
const startTransactionFn = sinon.spy(
queryRunner,
"startTransaction",
)
const commitTransactionFn = sinon.spy(
queryRunner,
"commitTransaction",
)
expect(startTransactionFn.called).to.be.false
expect(commitTransactionFn.called).to.be.false
await connection
.createEntityManager(queryRunner)
.getRepository(User)
.findOne({
where: {
id: 1,
},
transaction: true,
})
expect(startTransactionFn.calledOnce).to.be.true
expect(commitTransactionFn.calledOnce).to.be.true
await queryRunner.release()
}),
))
it("should select specific columns", () =>
Promise.all(
connections.map(async (connection) => {
const category = new Category()
category.name = "Bears"
await connection.manager.save(category)
const categories = [category]
const photos = []
for (let i = 1; i < 10; i++) {
const photo = new Photo()
photo.name = `Me and Bears ${i}`
photo.description = `I am near bears ${i}`
photo.filename = `photo-with-bears-${i}.jpg`
photo.views = 10
photo.isPublished = false
photo.categories = categories
photos.push(photo)
await connection.manager.save(photo)
}
const loadedPhoto = await connection
.getRepository(Photo)
.findOne({
select: ["name"],
where: {
id: 5,
},
})
const loadedPhotos1 = await connection
.getRepository(Photo)
.find({
select: ["filename", "views"],
})
const loadedPhotos2 = await connection
.getRepository(Photo)
.find({
select: ["id", "name", "description"],
relations: ["categories"],
})
// const loadedPhotos3 = await connection.getRepository(Photo).createQueryBuilder("photo")
// .select(["photo.name", "photo.description"])
// .addSelect(["category.name"])
// .leftJoin("photo.categories", "category")
// .getMany();
expect(loadedPhoto).to.be.eql({
name: "Me and Bears 5",
})
expect(loadedPhotos1).to.have.deep.members(
photos.map((photo) => ({
filename: photo.filename,
views: photo.views,
})),
)
expect(loadedPhotos2).to.have.deep.members(
photos.map((photo) => ({
id: photo.id,
name: photo.name,
description: photo.description,
categories,
})),
)
// expect(loadedPhotos3).to.have.deep.members(photos.map(photo => ({
// name: photo.name,
// description: photo.description,
// categories: categories.map(category => ({
// name: category.name,
// })),
// })));
}),
))
it("should select by given conditions", () =>
Promise.all(
connections.map(async (connection) => {
const category1 = new Category()
category1.name = "Bears"
await connection.manager.save(category1)
const category2 = new Category()
category2.name = "Dogs"
await connection.manager.save(category2)
const category3 = new Category()
category3.name = "Cats"
await connection.manager.save(category3)
const loadedCategories1 = await connection
.getRepository(Category)
.find({
where: {
name: "Bears",
},
})
expect(loadedCategories1).to.be.eql([
{
id: 1,
name: "Bears",
},
])
const loadedCategories2 = await connection
.getRepository(Category)
.find({
where: [
{
name: "Bears",
},
{
name: "Cats",
},
],
})
expect(loadedCategories2).to.be.eql([
{
id: 1,
name: "Bears",
},
{
id: 3,
name: "Cats",
},
])
}),
))
})
describe("repository > find options > comment", () => {
let connections: DataSource[]
const logPath = "find_comment_test.log"
before(async () => {
// TODO: would be nice to be able to do this in memory with some kind of
// test logger that buffers messages.
const logger = new FileLogger(["query"], { logPath })
connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
createLogger: () => logger,
})
})
beforeEach(() => reloadTestingDatabases(connections))
after(async () => {
await closeTestingConnections(connections)
await promisify(unlink)(logPath)
})
it("repository should insert comment", () =>
Promise.all(
connections.map(async (connection) => {
await connection
.getRepository(User)
.find({ comment: "This is a query comment." })
const logs = await promisify(readFile)(logPath)
const lines = logs.toString().split("\n")
const lastLine = lines[lines.length - 2] // last line is blank after newline
// remove timestamp and prefix
const sql = lastLine.replace(/^.*\[QUERY\]\: /, "")
expect(sql).to.match(/^\/\* This is a query comment. \*\//)
}),
))
})
describe("repository > find options > cache", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
cache: true,
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))
it("repository should cache results properly", () =>
Promise.all(
connections.map(async (connection) => {
// first prepare data - insert users
const user1 = new User()
user1.name = "Harry"
await connection.manager.save(user1)
const user2 = new User()
user2.name = "Ron"
await connection.manager.save(user2)
const user3 = new User()
user3.name = "Hermione"
await connection.manager.save(user3)
// select for the first time with caching enabled
const users1 = await connection
.getRepository(User)
.find({ cache: true })
expect(users1.length).to.be.equal(3)
// insert new entity
const user4 = new User()
user4.name = "Ginny"
await connection.manager.save(user4)
// without cache it must return really how many there entities are
const users2 = await connection.getRepository(User).find()
expect(users2.length).to.be.equal(4)
// but with cache enabled it must not return newly inserted entity since cache is not expired yet
const users3 = await connection
.getRepository(User)
.find({ cache: true })
expect(users3.length).to.be.equal(3)
// give some time for cache to expire
await sleep(1000)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection
.getRepository(User)
.find({ cache: true })
expect(users4.length).to.be.equal(4)
}),
))
})