-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_mongo.js
120 lines (106 loc) · 3.18 KB
/
db_mongo.js
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
require('dotenv').config();
const mongodb = require('mongodb');
const mongodb_credential = process.env.AOZORA_MONGODB_CREDENTIAL || '';
const mongodb_host = process.env.AOZORA_MONGODB_HOST || 'localhost';
const mongodb_port = process.env.AOZORA_MONGODB_PORT || '27017';
const mongodb_replica_set = process.env.AOZORA_MONGODB_REPLICA_SET;
const mongo_url = `mongodb://${mongodb_credential}${mongodb_host}:${mongodb_port}/aozora`;
class DB {
connect() {
const options = {
useNewUrlParser: true,
useUnifiedTopology: true
};
if (mongodb_replica_set) {
options.ssl = true;
options.replicaSet = mongodb_replica_set;
options.authMechanism = 'SCRAM-SHA-1';
options.authSource = 'admin';
}
// console.log(mongo_url, options);
return (mongodb.MongoClient).connect(mongo_url, options).then((client) => {
this.db = client.db();
});
}
_find_one(collection, query, options) {
options = options || {};
if (Array.isArray(options.fields)) {
options.projection = Object.assign({
_id: 0
}, ...(options.fields || []).map(e => ({
[e]: 1
})));
} else {
options.projection = options.fields || {};
options.projection._id = 0;
}
delete options.fields;
return collection.findOne(query, options);
}
_find_item(collection, query, options) {
options = options || {};
if (Array.isArray(options.fields)) {
options.projection = Object.assign({
_id: 0
}, ...(options.fields || []).map(e => ({
[e]: 1
})));
} else {
options.projection = options.fields || {};
options.projection._id = 0;
}
delete options.fields;
return collection.find(query, options);
}
find_one_book(book_id, options) {
return this._find_one(this.db.collection('books'), {
book_id: book_id
}, options);
}
find_books(query, options) {
return this._find_item(this.db.collection('books'), query, options).toArray();
}
find_one_person(person_id, options) {
return this._find_one(this.db.collection('persons'), {
person_id: person_id
}, options);
}
find_persons(query, options) {
return this._find_item(this.db.collection('persons'), query, options).toArray();
}
find_one_worker(worker_id, options) {
return this._find_one(this.db.collection('workers'), {
id: worker_id
}, options);
}
find_workers(query, options) {
return this._find_item(this.db.collection('workers'), query, options).toArray();
}
find_ranking(query, options) {
const collection = 'ranking_' + query.type;
delete query.type;
options = {
projection: {
year_month: 0
},
sort: {
access: -1
}
};
const book_options = {
book_title: 1,
authors: 1
};
return this._find_item(this.db.collection(collection), query, options).toArray()
.then((a) => {
return Promise.all(a.map((e) => {
return this.find_one_book(e.book_id, book_options).then((book) => {
e.title = book.title;
e.authors = book.authors.map((a) => a.last_name + ' ' + a.first_name);
return e;
});
}));
});
}
}
exports.DB = DB;