Skip to content

Commit d5093fa

Browse files
author
Gabriel Montalvo
committed
adds main content
1 parent aafe6ca commit d5093fa

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

README.md

Whitespace-only changes.

index.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/* TODO
2+
View, update or delete a single document
3+
Run database and collection-level commands
4+
*/
5+
6+
'use strict';
7+
8+
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
9+
var xmlHttp;
10+
var httpGET = function(url) {
11+
xmlHttp = new XMLHttpRequest();
12+
xmlHttp.open('GET', url, false);
13+
xmlHttp.send(null);
14+
return JSON.parse(xmlHttp.responseText);
15+
};
16+
17+
var httpPOST = function(url, request) {
18+
xmlHttp = new XMLHttpRequest();
19+
xmlHttp.open('POST', url, false);
20+
xmlHttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
21+
xmlHttp.send(JSON.stringify(request));
22+
return JSON.parse(xmlHttp.responseText);
23+
};
24+
25+
var httpPUT= function(url, request) {
26+
xmlHttp = new XMLHttpRequest();
27+
xmlHttp.open('PUT', url, false);
28+
xmlHttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
29+
xmlHttp.send(JSON.stringify(request));
30+
return JSON.parse(xmlHttp.responseText);
31+
};
32+
33+
var httpDELETE= function(url, request) {
34+
xmlHttp = new XMLHttpRequest();
35+
xmlHttp.open('DELETE', url, false);
36+
xmlHttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
37+
xmlHttp.send(JSON.stringify(request));
38+
return JSON.parse(xmlHttp.responseText);
39+
};
40+
41+
var BASEURL = 'https://api.mongolab.com/api/1/';
42+
43+
var MongoLab = function (apiKey) {
44+
if (!(this instanceof MongoLab)) {
45+
return new MongoLab(apiKey);
46+
}
47+
48+
this.APIKEY = apiKey;
49+
50+
var KEY_CHECK_URL = httpGET(BASEURL + 'databases?apiKey=' + this.APIKEY);
51+
if (KEY_CHECK_URL.message === 'Please provide a valid API key.') {
52+
throw new Error('Invalid API key');
53+
}
54+
};
55+
56+
(function () {
57+
58+
this.listDatabases = function (cb) {
59+
var res = httpGET(BASEURL + 'databases?apiKey=' + this.APIKEY);
60+
cb(null, res);
61+
};
62+
63+
this.listCollections = function (database, cb) {
64+
if ((typeof database !== 'string') || (database === undefined)) {
65+
throw new Error('database name is required');
66+
}
67+
68+
var res = httpGET(BASEURL + 'databases/' + database + '/collections?apiKey=' + this.APIKEY);
69+
cb(null, res);
70+
};
71+
72+
this.listDocuments = function (options, cb) {
73+
var database = options.database || null;
74+
var collectionName = options.collectionName || null;
75+
var OPTIONAL_PARAMS = {
76+
q: options.query || null,
77+
c: options.resultCount || null,
78+
f: options.setOfFields || null,
79+
fo: options.findOne || null,
80+
s: options.sortOrder || null,
81+
sk: options. resultsToSkip || null,
82+
l: options.limit || null
83+
};
84+
85+
if (database == null || collectionName == null) {
86+
cb(new Error('invalid options'), null);
87+
} else {
88+
var res = httpGET(BASEURL + 'databases/' + database + '/collections/' + collectionName + '?apiKey=' + this.APIKEY +
89+
(OPTIONAL_PARAMS.q ? '&q=' + OPTIONAL_PARAMS.q : '') + (OPTIONAL_PARAMS.c ? '&c=' + OPTIONAL_PARAMS.c : '') +
90+
(OPTIONAL_PARAMS.f ? '&f=' + OPTIONAL_PARAMS.f : '') + (OPTIONAL_PARAMS.fo ? '&fo=' + OPTIONAL_PARAMS.fo : '') +
91+
(OPTIONAL_PARAMS.s ? '&s=' + OPTIONAL_PARAMS.s : '') + (OPTIONAL_PARAMS.sk ? '&sk=' + OPTIONAL_PARAMS.sk : '') +
92+
(OPTIONAL_PARAMS.l ? '&l=' + OPTIONAL_PARAMS.l : ''));
93+
94+
cb(null, res);
95+
}
96+
};
97+
98+
this.insertDocuments = function (options, cb) {
99+
var database = options.database || null;
100+
var collectionName = options.collectionName || null;
101+
var documents = options.documents || null;
102+
103+
if (database == null || collectionName == null || documents == null) {
104+
cb(new Error('invalid options'), null);
105+
} else {
106+
var res = httpPOST(BASEURL + 'databases/' + database + '/collections/' + collectionName + '?apiKey=' + this.APIKEY, documents);
107+
108+
cb(null, res);
109+
}
110+
};
111+
112+
this.updateDocuments = function (options, cb) {
113+
var database = options.database || null;
114+
var collectionName = options.collectionName || null;
115+
var data = { "$set" : options.data } || null;
116+
var OPTIONAL_PARAMS = {
117+
q: options.query || null,
118+
m: options.allDocuments || null,
119+
u: options.upsert || null
120+
};
121+
122+
if (database == null || collectionName == null || data == null) {
123+
cb(new Error('invalid options'), null);
124+
} else {
125+
var res = httpPUT(BASEURL + 'databases/' + database + '/collections/' + collectionName + '?apiKey=' + this.APIKEY +
126+
(OPTIONAL_PARAMS.q ? '&q=' + OPTIONAL_PARAMS.q : '') + (OPTIONAL_PARAMS.m ? '&m=' + OPTIONAL_PARAMS.m : '') +
127+
(OPTIONAL_PARAMS.u ? '&u=' + OPTIONAL_PARAMS.u : ''), data);
128+
129+
cb(null, res);
130+
}
131+
};
132+
133+
this.deleteDocuments = function (options, cb) {
134+
var database = options.database || null;
135+
var collectionName = options.collectionName || null;
136+
var documents = options.documents || [];
137+
var OPTIONAL_PARAMS = {
138+
q: options.query || null
139+
};
140+
141+
if (database == null || collectionName == null) {
142+
cb(new Error('invalid options'), null);
143+
} else {
144+
var res = httpPUT(BASEURL + 'databases/' + database + '/collections/' + collectionName + '?apiKey=' + this.APIKEY +
145+
(OPTIONAL_PARAMS.q ? '&q=' + OPTIONAL_PARAMS.q : ''), documents);
146+
147+
cb(null, res);
148+
}
149+
};
150+
151+
}).call(MongoLab.prototype);
152+
153+
module.exports = MongoLab;
154+

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "mongolab-api",
3+
"version": "1.0.0",
4+
"description": "A NodeJS wrapper for MongoLab’s Data API",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/gmontalvoriv/mongolab-api.git"
12+
},
13+
"keywords": [
14+
"mongodb",
15+
"mongolab",
16+
"api"
17+
],
18+
"author": "Gabriel Montalvo",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/gmontalvoriv/mongolab-api/issues"
22+
},
23+
"homepage": "https://github.com/gmontalvoriv/mongolab-api#readme",
24+
"dependencies": {
25+
"xmlhttprequest": "^1.8.0"
26+
}
27+
}

0 commit comments

Comments
 (0)