-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBridge.js
More file actions
260 lines (197 loc) · 6.76 KB
/
DataBridge.js
File metadata and controls
260 lines (197 loc) · 6.76 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
(function _DataBridge(){ // Encapsulate scope
// The js version of the DataBridge object. Pass object parameters as {host:host,data:data,user:user,pass:pass}
function DataBridge(obj) {
if (!(obj=jsonToObj(obj))) { return; }
var _this = this,
host = obj.host || 'local', // Defaults to whatever the preferred context may be. Could be 'localhost', './', 'http://...'.
data = obj.data || 'default', // Typically a database name, but could also be any number of other options.
user = obj.user || 'default', // The user authentication identifier to be used to reference access privilages, if applicable.
pass = obj.pass || 'default', // The authentication password used for the user, if applicable.
type = obj.type || 'mongo', // If specifying a database type. Would typically be inferred from other data?
db = getDB(), // Authenticate and instantiate correct database type.
_api = { // API functions.
'get': function(key,val) {
db.api({cmd:'get',key:key,val:val});
},
'set': function(key,val) {
db.api({cmd:'set',key:key,val:val});
},
'add': function(key,val) {
db.api({cmd:'add',key:key,val:val});
},
'rem': function(key,val) {
db.api({cmd:'rem',key:key,val:val});
},
'new': function(key,val) {
db.api({cmd:'new',key:key,val:val});
},
'del': function(key,val) {
db.api({cmd:'del',key:key,val:val});
},
'exe': function(key,val) {
db.api({cmd:'exe',key:key,val:val});
}
};//{_api}
// Initialize the DataBridge object.
(function initDataBridge(){
_this.api = apiCall;
for (var cmd in _api) { _this[cmd] = _api[cmd]; }
})();//(initDataBridge)()
// Makes the actual API call to the db abstraction interface.
function apiCall(callObj){
return db.api(callObj);
}
// Resolves authentication and instantiation of the correct type of db interface.
function getDB() {
if (!type) { getDbType(); }
if (type=='mongo') { return new MongoDb(); }
// Identifies db type if not specified.
function getDbType() {
// Fix. Do something functional to infer correct db type...
return type='mongo';
}//getDbType()
}//getDB()
// Instantiates a persistent authenticated mongodb connection to a specific database for a particular user.
function MongoDb() {
var _this = this,
_api = { // API functions.
'get': function() {
},
'set': function() {
},
'add': function() {
},
'rem': function() {
},
'new': function() {
},
'del': function() {
},
'exe': function() {
}
},//{_api}
db; // Overrides local reference to db var, which in this case now represents the actual mongodb instance used within this object.
// Initialize the db connection using the 'global' authentication variables.
(function initMongoDb(){
// Fix. Initialize mongodb connection using these items:
// db = mongo(host,data,user,pass);
_this.api = apiCall;
for (var cmd in _api) { _this[cmd] = _api[cmd]; }
})();//(initMongoDb)()
function apiCall(callObj) {
console.log('MongoDb.api() called: ',callObj);
}
}//MongoDb
}//DataBridge
// ******* Utility Functions: ************************************************************* //
// Performs basic checks for JSON structure and returns the extracted object if applicable, returns the original string otherwise.
function jsonToObj(str) {
if (!is(str,'str')) { return str; }
var b = str.charAt(0),
e = str.charAt(str.length-1),
obj;
// Fix. Complete these checks:
// if (!(b=='{'&&e=='}')&&!(b=='['&&e==']')) { return; }
if (b=='[' && e==']') {
obj = JSON.parse('{"tmp":'+str+'}');
if (obj && obj.tmp) { return obj.tmp; }
return; // flagError('Invalid JSON str', str);
} else if (b=='{' && e=='}') {
obj = JSON.parse(str);
} else {
return; // flagError('Invalid JSON str', str);
}
return is(obj,'obj') ? obj : str;
}
// Shorthand for type comparisons. If typ is set, will return TRUE or FALSE against that type, or will check against undefined if typ is not passed.
function is(val,typ) {
if (!typ) { return (typeof val != 'undefined'); }
if (typ=='fnc'||typ=='function') { return (typeof val == 'function'); }
if (typ=='str'||typ=='string') { return (typeof val == 'string'); }
if (typ=='obj'||typ=='object') { return (val === Object(val)); }
if (typ=='arr'||typ=='array') {
if (Array.isArray) { return Array.isArray(val); }
return Object.prototype.toString.call(val) == '[object Array]';
}
}//is()
// Export DataBridge object.
module.exports = DataBridge;
/* Taken from wiki, etc:
// Instantiate a new DataBridge object using the local language constructs:
db = new DataBridge({
'host': 'host_uri', // Defaults to whatever the preferred context may be. Could be 'localhost', './', 'http://...'.
'data': 'data_source', // Typically a database name, but could also be any number of other options.
'user': 'user', // The user authentication identifier to be used to reference access privilages, if applicable.
'pass': 'pass' // The authentication password used for the user, if applicable.
});
// Which obviously could actually look like this:
db = new DataBridge({host:host,data:data,user:user,pass:pass}); // Or whatever...
// Instantiate a new DataBridge object using a JSON string:
db = new DataBridge('{
"host" : "host_uri",
"data" : "data_source",
"user" : "user",
"pass" : "pass"
}');
// Which obviously could actually look like this:
db = new DataBridge(loginString); // Or whatever...
=== Access: ===
// Obtain data from an authenticated DataBridge object instance. Compare to the to the following SQL statement:
SELECT "field1", "field2", "field3", "field4"
FROM "table"
WHERE "field1"="filterVal",
"field2" >= "filterVal",
"field3" < "filterVal",
"field4" != "filterVal"
ORDER BY "field1", "field2", "field3", "field4";
// Typical standard usage.
db.get('{
"table": {
"field1": "filterVal",
"field2": ">=filterVal",
"field3": "<filterVal",
"field4": "!filterVal"
}
}');
// Implicit Join:
db.get('{
"Table1": {
"Field1" : "Val1"
},
"Table2": [
"joinField1",
"joinField1"
]
}');
// Ordered key-val set:
{[
"key1":"val1",
"anotherKey":"keyVal"
]}
// ==> Becomes:
[
{"key1":"val1"},
{"anotherKey":"keyVal"}
]
{
"_index_" : [
"key1",
"anotherKey"
],
"key1":"val1",
"anotherKey":"keyVal"
}
* student: {
* time: timestamp
* loc: {lng:, lat:},
* ip: 0.0.0.0,
* }
*
* db.student.aggregate([
* {$project:{ time: 1, loc: 1, ip: 0 }}, // somthing like this
* {$group: blah blah}
* ]);
*
*
*/
})();//(DataBridge)()