-
Notifications
You must be signed in to change notification settings - Fork 54
/
Mlab.js
118 lines (95 loc) · 2.77 KB
/
Mlab.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
// 如果你要使用Mlab 而不是 Restheart 的话, 请把以下的KEY改成你自己的KEY
var mlabKey = "WULdZqaqxqikzrGePcLC5uWBeyvGZqBp";
var baseUrl = "https://api.mlab.com/api/1/";
var dbUrl = baseUrl + "databases/mongo/";
var collectionUrl = dbUrl + "collections/";
var mlab = {};
if (Const.useMlab) {
mongo = mlab;
}
function debugMLab(){
debug();
}
function convertRestHeartParasToMlabParas(query) {
if (!query){
return query;
}
query = query.replace("filter=", "q=");
query = query.replace("sort=", "s=");
query = query.replace("pagesize=", "l=");
// query = query.replace("page=", "sk=");
// Pagination is not supported yet
// We don't have pagination feature so far
return query;
}
mlab.insert = function(collection, data) {
var db = apendAPI(collectionUrl + collection);
var option = getMlabInsertOption(data);
return UrlFetchApp.fetch(db, option);
};
// Warning, put will override the whole collectoin, put empty array data will clear the whole collection
mlab.replace = function(collection, query, data) {
query = convertRestHeartParasToMlabParas(query);
var db = apendAPI(collectionUrl + collection);
if (query){
db += "&" + query;
}
var option = getMlabPutOption(data);
var url = encodeURI(db);
return UrlFetchApp.fetch(url, option);
};
mlab.get = function(collection, query) {
query = convertRestHeartParasToMlabParas(query);
var db = apendAPI(collectionUrl + collection);
if (query){
db += "&" + query;
}
var option = getMlabGetOption();
var response = UrlFetchApp.fetch(encodeURI(db), option);
var object = JSON.parse(response);
return object;
};
mlab.setOne = function(urlWithId, data) {
if (urlWithId && urlWithId.indexOf("/") > 0) {
return this.replace(urlWithId, null, data);
}
throw "Please provide document id";
}
mlab.setMany = function(collection, query, data) {
if (query && query.indexOf("filter=") == 0) {
return this.replace(collection, query, []);
}
throw "query cannot be null, it will replace the whole collection";
};
mlab.set = mlab.setMany;
mlab.remove = function(collection, query) {
if (query && query.indexOf("filter=") == 0) {
return this.replace(collection, query, []);
}
throw "query cannot be null, it will clear the whole collection";
}
function apendAPI(url){
return url + "?apiKey=" + mlabKey;
}
function getMlabInsertOption(data){
var option = {
"method": "post",
'contentType': 'application/json',
"payload": JSON.stringify(data)
};
return option;
}
function getMlabPutOption(data){
var option = {
"method": "put",
'contentType': 'application/json',
"payload": JSON.stringify(data)
};
return option;
}
function getMlabGetOption(){
var option = {
"method": "get",
};
return option;
}