forked from Azure-Samples/nodejs-appsvc-cosmosdb-bottleneck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseOperations.js
99 lines (93 loc) · 3.55 KB
/
databaseOperations.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var MongoClient = require("mongodb").MongoClient;
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('connectionData.json', 'utf8'));
var DbConnection = require('./db');
var connectionString = "mongodb://account:[email protected]:10255/?ssl=true";
if(process.env.NODE_ENV == "production"){
var connectionString = process.env.CONNECTION_STRING;
var stringSplit1 = connectionString.split("://")[1];
var stringSplit2 = stringSplit1.split('@');
var userNamePassword = stringSplit2[0];
userNamePassword = userNamePassword.split(':');
var userName = userNamePassword[0];
var password = userNamePassword[1];
var databaseName = obj.databaseName;
var collectionName = obj.collectionName;
connectionString = ("mongodb://" + encodeURIComponent(userName) + ":" + encodeURIComponent(password) + "@" + stringSplit2[1] + (stringSplit2.length >= 3 ? ("@" + stringSplit2[2] + "@") : ""));
}
else{
MongoClient = {
connect: function(connectionString, options, callback){
var client = {
db: function(databaseName){
var testDB = {
collection: function(collectionName){
var testCollection = {
count: function(callback){
callback(null, "unittest");
},
insertMany: function(items, callback){
callback(null, "success");
}
}
return testCollection;
}
}
return testDB;
},
close: function(){
}
}
callback(null, client);
}
};
}
module.exports = {
queryCount: function (callback, errorCallback, retry = 2) {
DbConnection.Get()
.then((mongoClient) => {
// Find some documents
mongoClient.count(function (err, count) {
if (err != null) {
if(retry > 0) {
setTimeout(() => {
queryCount(callback, errorCallback, retry-1);
}, (3 - retry) * 600);
return;
} else {
errorCallback(err)
}
} else {
console.log(`Found ${count} records`);
callback(count);
}
});
})
},
addRecord: function (pageName, callback, errorCallback, retry = 2) {
DbConnection.Get()
.then((mongoClient) => {
var milliseconds = (new Date).getTime().toString();
var itemBody = {
"id": milliseconds,
"page": pageName
};
console.log("Connected correctly to server");
// Insert some documents
mongoClient.insertMany([itemBody], function (err, result) {
if (err != null) {
if(retry > 0) {
setTimeout(() => {
addRecord(pageName, callback, errorCallback, retry-1);
}, (3 - retry) * 600);
return;
} else {
errorCallback(err)
}
} else {
callback();
}
});
})
}
}