-
Notifications
You must be signed in to change notification settings - Fork 4
/
mongo.js
42 lines (37 loc) · 1.3 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
const fs = require("fs");
const { MongoClient, ObjectId } = require('mongodb');
const NoSqlCrudAdapter = require("./nosql-crud-adapter");
const SurveyStorage = require("./survey-storage");
const readFileSync = filename => fs.readFileSync(filename).toString("utf8");
const dbConfig = {
host: process.env.DATABASE_HOST || "localhost",
port: process.env.DATABASE_PORT || 27017,
database: process.env.DATABASE_DB,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD
? readFileSync(process.env.DATABASE_PASSWORD)
: null
};
const url = `mongodb://${dbConfig.user}:${dbConfig.password}@${dbConfig.host}:${dbConfig.port}/`;
const client = new MongoClient(url);
function MongoStorage() {
function dbConnectFunction (dbCallback) {
client.connect()
.then(() => {
const db = client.db(dbConfig.database);
dbCallback(db, () => {
if (!!process.env.DATABASE_LOG) {
console.log(arguments[0]);
console.log(arguments[1]);
}
client.close();
});
})
.catch(() => {
console.error(JSON.stringify(arguments));
});
}
const dbQueryAdapter = new NoSqlCrudAdapter(dbConnectFunction, () => new ObjectId().toString());
return new SurveyStorage(dbQueryAdapter);
}
module.exports = MongoStorage;