-
Notifications
You must be signed in to change notification settings - Fork 52
/
Mongo.js
183 lines (162 loc) · 4.74 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
function debugMongo(){
debug();
}
var mongo;
var restheart = {};
function request(db, option) {
try {
option.muteHttpExceptions = true;
var response = UrlFetchApp.fetch(db, option);
var headers = response.getAllHeaders().toSource();
if (response) {
if (response.getResponseCode() == 404) {
if (content.message) {
var regex = /Collection '([A-z0-9_-]+)' does not exist/;
var found = content.message.match(regex);
if (found.length > 0) {
var collectionName = found[1];
mongo.replace(collectionName, {"desc": "created by bot"});
}
}
}
// var contentText = response.getContentText();
// var content = {};
// if (contentText) {
// content = JSON.parse(contentText);
// }
// var log = {
// type: "restheart-response",
// // headers: JSON.parse(headers.substring(1, headers.length - 1)),
// status: response.getResponseCode(),
// headers: headers.substring(1, headers.length - 1),
// content: content
// };
// mongo.insert(Const.logsColl, log);
}
return response;
} catch (e) {
if (e) {
var exceptionLog = {
type: "error",
error: e
};
mongo.insert(Const.logsColl, exceptionLog);
}
return null;
}
}
restheart.insert = function(collection, data) {
var db =Const.restheartUrl + collection;
var option = getInsertOption(data);
var response = UrlFetchApp.fetch(db, option);
return response;
};
function testRestHeart(){
// var data = {test: 'test'};
// restheart.insert("Const.testColl", data);
// var data = {test: 'test replace'};
// restheart.replace("Const.testColl/5c75be5e294610d3cbaa39f9", null, data);
// var data = {test: 'test get'};
// restheart.get("Const.testColl/5c75be5e294610d3cbaa39f9", null, data);
//
// var data = {test: 'test set'};
// restheart.set("Const.testColl/5c75be5e294610d3cbaa39f9", null, data);
var data = {"$addToSet":{"array" : "bar2"}};
restheart.setOne("Const.testColl/5c75be5e294610d3cbaa39f9", data);
}
restheart.replace = function(collection, query, data) {
var db =Const.restheartUrl + collection;
if (query){
db += "?" + query;
}
var option = getPutOption(data);
var url = encodeURI(db);
return request(url, option);
};
restheart.setOne = function(urlWithId, data) {
var db =Const.restheartUrl + urlWithId;
var option = getPatchOption(data);
option.muteHttpExceptions = true;
var url = encodeURI(db);
return request(url, option);
}
restheart.setMany = function(collection, query, data) {
var db =Const.restheartUrl + collection;
if (query){
db += "/*?" + query;
}
var option = getPatchOption(data);
var url = encodeURI(db);
return request(url, option);
};
restheart.set = restheart.setMany;
restheart.remove = function(collection, query) {
var db =Const.restheartUrl + collection;
if (query){
db += "/*?" + query;
}
var option = getDeleteOption();
var url = encodeURI(db);
return request(url, option);
}
restheart.get = function(collection, query) {
var db =Const.restheartUrl + collection;
if (query){
db += "?" + query;
}
var option = getGetOption();
var url = encodeURI(db);
var response = request(url, option);
var object = JSON.parse(response);
if (object._embedded) {
var result = object._embedded;
return result;
}
return object;
};
if (!Const.useMlab){
mongo = restheart;
}
function getInsertOption(data){
var option = {
"method": "post",
'contentType': 'application/json',
'headers': {"Authorization": "Basic " + Utilities.base64Encode(Const.restheartUser + ":" + Const.restheartPass)},
"muteHttpExceptions": true,
"payload": JSON.stringify(data)
}
return option;
}
function getDeleteOption(){
var option = {
"method": "delete",
'contentType': 'application/json',
'headers': {"Authorization": "Basic " + Utilities.base64Encode(Const.restheartUser + ":" + Const.restheartPass)},
}
return option;
}
function getPutOption(data){
var option = {
"method": "put",
'contentType': 'application/json',
'headers': {"Authorization": "Basic " + Utilities.base64Encode(Const.restheartUser + ":" + Const.restheartPass)},
"payload": JSON.stringify(data)
}
return option;
}
function getPatchOption(data){
var option = {
"method": "patch",
'contentType': 'application/json',
'headers': {"Authorization": "Basic " + Utilities.base64Encode(Const.restheartUser + ":" + Const.restheartPass)},
"payload": JSON.stringify(data)
}
return option;
}
function getGetOption(){
var option = {
"method": "get",
'headers': {"Authorization": "Basic " + Utilities.base64Encode(Const.restheartUser + ":" + Const.restheartPass)},
}
return option;
}