-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (37 loc) · 1.06 KB
/
index.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
'use strict';
const EventEmitter = require('events').EventEmitter;
const MongoClient = require('mongodb').MongoClient;
const inherits = require('util').inherits;
const MongoWrapper = function MongoWrapper(uri) {
EventEmitter.call(this);
MongoClient.connect(uri, (err, database) => {
if (err) { throw err; }
this.db = database;
this.api = {
users: database.collection('api.users'),
};
[
'arrangementer',
'bilder',
'grupper',
'lister',
'områder',
'steder',
'turer',
].forEach((type) => {
this[type] = database.collection(type);
});
this.emit('ready');
});
return this;
};
inherits(MongoWrapper, EventEmitter);
if (process.env.MONGO_URI) {
module.exports = new MongoWrapper(process.env.MONGO_URI);
} else {
const addr = process.env.MONGO_PORT_27017_TCP_ADDR || 'mongo';
const port = process.env.MONGO_PORT_27017_TCP_PORT || '27017';
const db = 'test';
module.exports = new MongoWrapper(`mongodb://${addr}:${port}/${db}`);
}
module.exports.MongoWrapper = MongoWrapper;