forked from jschirrmacher/netvis-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDB.js
51 lines (47 loc) · 1.53 KB
/
MongoDB.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
/*eslint-env node*/
module.exports = function ({logger}) {
const MongoClient = require('mongodb').MongoClient
return function (mongoURL, dbName) {
return new Promise((resolve, reject) => {
MongoClient.connect(mongoURL, {useNewUrlParser: true}, function (err, client) {
if (err) {
reject(err)
return
}
const db = client.db(dbName)
logger.info(`Successfully logged into MongoDB '${dbName}'`)
db.listCollections().toArray((err, docs) => {
if (err) {
reject(err)
}
const map = Object.assign({}, ...docs.map(collection => ({
[collection.name]: ({
find(where, fields) {
return new Promise((resolve, reject) => {
db.collection(collection.name).find(where, fields).toArray((err, docs) => {
if (err) {
reject(err)
}
resolve(docs)
})
})
},
findWithCursor(where, fields) {
return new Promise((resolve, reject) => {
db.collection(collection.name).find(where, fields, (err, resultCursor) => {
if (err) {
reject(err)
}
resolve(resultCursor)
})
})
}
})
})
))
resolve(map)
})
})
})
}
}