/
mongo.js
77 lines (71 loc) · 2.16 KB
/
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
/*eslint no-console: ["error", { allow: ["log"] }] */
// dependencies --------------------------------------------------
import {MongoClient} from 'mongodb';
import config from '../config';
import async from 'async';
export default {
/**
* DB
*
* A storage location for a the database instance.
* Used to access the native mongo api.
*/
dbs: {
crn: null,
scitran: null
},
/**
* Collections
*
* A list of all mongo collections and a simplified
* interface for accessing them. Collections start
* out null an are initialized after mongo connects.
*/
collections: {
crn: {
blacklist: null,
validationQueue: null,
jobs: null,
jobDefinitions: null,
tickets: null,
userPreferences: null,
notifications: null,
counters: null,
logs: null
},
scitran: {
projects: null,
project_snapshots: null
}
},
/**
* Connect
*
* Makes a connection to mongodbs and creates an accessible
* reference to the dbs and collections
* takes optional callback (using callback to kick of server side job polling)
*/
connect(callback) {
async.each(Object.keys(this.dbs), (dbName, cb) => {
MongoClient.connect(config.mongo.url + dbName, (err, db) => {
if (err) {
console.log(err);
process.exit();
} else {
this.dbs[dbName] = db;
for (let collectionName in this.collections[dbName]) {
if (this.collections[dbName][collectionName] === null) {
this.collections[dbName][collectionName] = this.dbs[dbName].collection(collectionName);
}
}
console.log(dbName, ' - db connected');
}
cb();
});
}, () => {
if(callback && typeof callback === 'function') {
callback();
}
});
}
};