-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
131 lines (114 loc) · 3.05 KB
/
database.js
File metadata and controls
131 lines (114 loc) · 3.05 KB
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
const {MongoClient} = require('mongodb');
const {URL} = require('./const');
const {log} = console;
class DataBase {
_collection = null;
_isSetupCollection = false;
constructor(dataBase) {
this._dataBase = dataBase;
this._shouldSetupCollection();
this.createIndex({name: 1});
this.createIndex({sex: 1});
}
static _connect() {
return MongoClient.connect(URL).then((client) => client.db('myApp')).
catch((e) => {
console.error('Failedto connect to MongoDB', e);
process.exit();
});
}
static createDataBase() {
const db = (async () => await this._connect())();
return new DataBase(db);
}
setupCollection() {
const promise = new Promise((resolve) => {
const collection = (async () => (await this._dataBase).collection('users'))();
resolve(collection);
});
this._collection = promise.then((collection) => collection);
this._isSetupCollection = true;
return this._collection;
}
_shouldSetupCollection() {
if(!this.isSetupCollection) {
this.setupCollection();
}
}
async addData (name, dateOfBirth, sex) {
(await this._collection).insertOne({name: name, dateOfBirth: new Date(dateOfBirth), sex: sex});
}
async addDataArraay (array) {
(await this._collection).insertMany(array);
}
async createIndex(config) {
return (await this._collection).createIndex(config);
}
getData () {
const promise = (async () => (await this._collection).find())();
promise.then((cursor) => cursor.toArray()).then((data) => log(data));
}
async getUniqueValues () {
const promise = (async () => (await this._collection).aggregate([
{
$project: {
_id: 1,
name: 1,
dateOfBirth: 1,
sex: 1,
sortData: {
$concat: [
'$name',
{$substr: ['$dateOfBirth', 0, -1]},
]
},
ageDay: {
$dateDiff: {
startDate: '$dateOfBirth',
endDate: new Date(),
unit: 'day',
},
},
},
},
{
$group: {
_id: '$sortData',
users: {
$first: {
_id: '$_id',
name: '$name',
dateOfBirth: '$dateOfBirth',
sex: '$sex',
ageDay: '$ageDay'
},
},
}
},
{
$project: {
_id: '$users._id',
name: '$users.name',
dateOfBirth: '$users.dateOfBirth',
sex: '$users.sex',
age: {
$trunc: {
$divide: ['$users.ageDay', 365],
}
}
},
},
{$sort: {'name': 1}},
]))();
promise.then((cursor) => cursor.toArray()).then((data) => log(data));
}
async getFMale() {
return (await this._collection).find({name: /^F\w+/i, sex: 'male'}).toArray();
}
async getRunTime() {
const startTime = new Date();
await this.getData();
log(`time: ${(new Date() - startTime) / 1000}s`)
}
}
module.exports = DataBase;