-
Notifications
You must be signed in to change notification settings - Fork 1
/
database_manager.js
executable file
·78 lines (66 loc) · 2.21 KB
/
database_manager.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
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
var url = process.env.MONGO_CONNSTRING;
var databaseMode = true;
console.log(url);
class DatabaseManager {
constructor() {
// Connect to the db
var callback = (db) => {
this.database = db;
}
if (databaseMode) {
MongoClient.connect(url, function (err, db) {
if(err) { return console.dir(err); }
callback(db);
});
} else {
console.log("database mode is off")
}
}
getAll(callback) {
if (databaseMode) {
var collection = this.database.collection('wall');
collection.find().toArray(function(err, items) {
callback(items)
});
}
}
putDummyData() {
var doc1 = {'hello':'doc1'};
var doc2 = {'hello':'doc2'};
var lotsOfDocs = [{'hello':'doc3'}, {'hello':'doc4'}, doc1, doc2];
if (databaseMode) {
var collection = this.database.collection('test2');
this.database.createCollection('test2', {strict:true}, function(err, collection) {});
collection.insert(lotsOfDocs, {w:1}, function(err, result) {});
var collection = this.database.collection('test2');
}
}
update(key, record) {
if (databaseMode) {
var collection = this.database.collection('wall');
collection.update(key, {$set:record}, {upsert: true}, function(err, result) {
if(err) { return console.dir(err); }
});
}
}
}
module.exports = new DatabaseManager();
return;
//database test, run by removing return ^^:
var dbm = new DatabaseManager();
var callbackWith = (items) => {
console.log("items fetched:")
console.log(items)
return items;
}
setTimeout(() => {
var test_id = {key: "123"}
console.log("Running DBM tests:");
dbm.update(test_id, {key: "123", "fieldtoupdate": "field123" });
dbm.update(test_id , {key: "123", "fieldtoupdate": "field456" });
console.log("Update() Test completed.");
dbm.getAll(callbackWith);
console.log("GetAll Test completed.")
}, 5000);