-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
mongo.service.ts
72 lines (60 loc) · 2.04 KB
/
mongo.service.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
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { MongoRepository } from 'typeorm';
import { Fortune } from './fortune.entity';
import { World } from './world.entity';
@Injectable()
export class MongoService {
constructor(
@InjectRepository(World)
private readonly worldRepository: MongoRepository<World>,
@InjectRepository(Fortune)
private readonly fortuneRepository: MongoRepository<Fortune>,
) {}
singleQuery() {
const rand = Math.floor(Math.random() * 10000) + 1;
return this.worldRepository.findOne({ id: rand });
}
async multiQueries(queries: string) {
const number = Math.min(Math.max(parseInt(queries) || 1, 1), 500);
const promisesArray = [];
for (let i = 0; i < number; i++) {
promisesArray.push(this.singleQuery());
}
const worlds = await Promise.all(promisesArray);
return worlds;
}
async fortunes() {
const allFortunes = await this.fortuneRepository.find();
allFortunes.push({
id: 0,
message: 'Additional fortune added at request time.',
});
allFortunes.sort((a, b) => (a.message < b.message ? -1 : 1));
return { fortunes: allFortunes };
}
async updates(queries) {
const number = Math.min(Math.max(parseInt(queries) || 1, 1), 500);
const worlds = [];
const bulk = this.worldRepository.initializeUnorderedBulkOp();
for (let i = 0; i < number; i++) {
const worldToUpdate = await this.singleQuery();
worldToUpdate.randomNumber = Math.floor(Math.random() * 10000) + 1;
worlds.push(worldToUpdate);
bulk
.find({ id: worldToUpdate.id })
.update({ $set: { randomNumber: worldToUpdate.randomNumber } });
}
await bulk.execute();
return worlds;
}
async cachedWorlds(count) {
const number = Math.min(Math.max(parseInt(count) || 1, 1), 500);
const promisesArray = [];
for (let i = 0; i < number; i++) {
promisesArray.push(this.singleQuery());
}
const worlds = await Promise.all(promisesArray);
return worlds;
}
}