-
Notifications
You must be signed in to change notification settings - Fork 0
/
mangrove.js
584 lines (558 loc) · 22.8 KB
/
mangrove.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
(function (root, factory) {
var clientReadFileGenerator = function(request){
return function(name, cb){
request(name, function(err, req, data){
cb(err, data);
});
};
};
if (typeof define === 'function' && define.amd) {
define([
'indexed-set',
'sift',
'where-parser',
'strangler',
'browser-request',
'async-arrays'
], function(a, b, c, request){
return factory(a, b, c, {readFile:clientReadFileGenerator(request)});
});
}else if (typeof module === 'object' && module.exports){
module.exports = factory(
require('indexed-set'),
require('sift'),
require('where-parser'),
require('strangler'),
require('fs'),
require('async-arrays')
);
}else{
root.returnExports = factory(
root.IndexedSet,
root.Sift,
root.WhereParser,
root.Strangler,
{readFile:clientReadFileGenerator(root.request)},
root.AsyncArrays
);
}
}(this, function(Indexed, Sift, WhereParser, strings, fs, arrays){
//var jobs = [];
//var active = 0;
var whereParser = new WhereParser();
function computeSetWhere(set, where){
var results = set;
var fullSet = results.clone();
var previousConjunction;
where.forEach(function(part, whereIndex){
if(Array.isArray(part)){
//support subenvironments through recursive call with clone
}else{
switch(part.type){
case 'expression':
if(previousConjunction){
if(!results[previousConjunction.value]){
throw new Error(
'conjunction not supported: '+
previousConjunction.value
);
}
results = results.clone();
var predicate = results.with(part.key, part.operator, part.value);
results[previousConjunction.value](predicate);
previousConjunction = undefined;
}else{
results = results.clone();
results.with(
part.key,
part.operator,
part.value
);
}
break;
case 'conjunction':
previousConjunction = part;
break;
}
}
});
return results;
}
function DataService(options){
this.options = options || {};
this.tables = {};
this.jobs = [];
this.links = this.options.collections || [];
this.active = 0;
var ob = this;
if(this.options.file){
this.active++;
fs.readFile(this.options.file, function(err, body){
var data = JSON.parse(body);
if(data){
Object.keys(data).forEach(function(collectionName){
ob.tables[collectionName] = new Indexed.Collection(
data[collectionName],
'id'
);
});
ob.completed();
}
});
}
if(this.options.data){
var data = this.options.data;
Object.keys(data).forEach(function(collectionName){
ob.tables[collectionName] = new Indexed.Collection(
data[collectionName],
'id'
);
});
}
//options.lazy
}
DataService.prototype.ready = function(cb){
if((!this.jobs) || this.active === 0) return cb();
else return this.jobs.push(cb);
}
DataService.prototype.completed = function(){
this.active--;
if(this.active === 0){
var actions = this.jobs;
this.jobs = false;
actions.forEach(function(cb){
cb();
})
}
}
DataService.prototype.upstream = function(emitter, opts){
var options = opts || {};
var upstreamOp = function(operation, collectionName, query, callback){
var id = 'todo_switch_to_uuids'+Math.floor(Math.random()*1000000);
emitter.once('mangrove-results', {id:{$eq:id}}, function(response){
let data = arguments[arguments.length-1];
callback(
(data.err && new Error('Upstream Error: '+data.err)),
data.results
);
});
emitter.send('mangrove-'+operation, {
id: id,
name: collectionName,
query: query
});
};
//todo: proactively fetch marked collections
if(options.collections){
this.links = this.links.concat(options.collections);
}
this.upstream = function(collectionName){
return {
find : function(query, cb){
return upstreamOp('find', collectionName, query, cb);
},
insert : function(item, cb){
return upstreamOp('insert', collectionName, item, cb);
},
update : function(updates, where, cb){
return upstreamOp('update', collectionName, {updates, where}, cb);
},
'delete' : function(where, cb){
return upstreamOp('delete', collectionName, where, cb);
}
}
}
this.links.forEach((link)=>{
if(this.upstream && link.remote && !link.driver){
//if we don't have a driver, it's remote and there's an upstream use that:
link.driver = new (DataService.adapt(this.upstream))();
}
});
}
DataService.prototype.downstream = function(emitter, outboundEmitter){
var out = outboundEmitter || emitter;
emitter.on('mangrove-find', (event, command)=>{
var handler = (err, set)=>{
out.emit('mangrove-results', {
results : set.toArray(),
error: (err && err.message),
id: command.id
});
}
if(typeof command.query === 'string'){
this.sql(command.query, handler);
}else{
this.query(command.name).find(command.query, handler);
}
});
emitter.on('mangrove-update', (event, command)=>{
var handler = (err, set)=>{
out.emit('mangrove-results', {
error: (err && err.message),
id: command.id
});
}
if(typeof command.query === 'string'){
this.sql(command.query, handler);
}else{
this.query(command.name).update(command.query, handler);
}
});
emitter.on('mangrove-delete', (event, command)=>{
var handler = (err, set)=>{
out.emit('mangrove-results', {
error: (err && err.message),
id: command.id
});
}
if(typeof command.query === 'string'){
this.sql(command.query, handler);
}else{
this.query(command.name).delete(command.query, handler);
}
});
emitter.on('mangrove-insert', (event, command)=>{
var handler = (err, set)=>{
out.emit('mangrove-results', {
error: (err && err.message),
id: command.id
});
}
if(typeof command.query === 'string'){
this.sql(command.query, handler);
}else{
this.query(command.name).insert(command.query, handler);
}
});
}
DataService.prototype.populate = function(link, query, cb){
//todo: actually populate
//cases:
// name
// name + source
// name + remote
if(
(
link.remote ||
false //todo: name/name + source && empty
) && link.driver
){
if(!this.tables[link.name]){
this.tables[link.name] = new Indexed.Collection([], link.key || 'id');
}
link.driver.loadCollection(this.tables[link.name], link.name, {}, ()=>{
cb();
});
}
}
DataService.prototype.prepopulate = function(cb){
arrays.forEachEmission((this.links || []), (link, index, done)=>{
this.populate(link, {}, ()=>{ done() });
}, ()=>{
cb()
});
}
DataService.prototype.collection = function(name, noError){
if(!this.tables[name] && !noError){
throw new Error('unknown collection: '+name);
}
return this.tables[name];
}
DataService.prototype.toJSON = function(formatted){
var result = {};
var ob = this
Object.keys(this.tables).forEach(function(tableName){
result[tableName] = (new Indexed.Set(ob.tables[tableName])).toArray();
});
return !!formatted?JSON.stringify(result):JSON.stringify(result, undefined, ' ');
}
DataService.prototype.query = function(query, callback){
switch(typeof query){
case 'string':
if(query.indexOf(' ') === -1 && query.indexOf("\n") === -1){
//it's a single world
var collection = this.collection(query, true);
if(!collection){
collection = new Indexed.Collection([], 'id' );
this.tables[query] = collection;
var link = this.links.find(function(link){
return link.name === query.name;
});
if(link){
this.populate(link, query, ()=>{
this.query(query, callback);
});
return;
}
}
var set = new Indexed.Set(collection);
return {
find : function(query, cb){
if(typeof query === 'function' && !cb){
return query(undefined, set);
}
set.sift(query, function(){
return cb?cb(undefined, set):undefined;
});
},
update : function(updates, where, cb){
set.sift(where, function(){
set.forEach(function(item){
Object.keys(updates).forEach(function(key){
item[key] = updates[key];
});
});
if(cb) cb();
});
},
insert : function(item, cb){
if(Array.isArray(item)){
item.forEach(function(itm){
set.push(itm);
});
}else set.push(item);
if(cb) cb();
},
'delete' : function(where, cb){
set.sift(where, function(){
set.forEach(function(item){
var id = item[set.primaryKey];
var pos = set.ordering.indexOf(id);
if(pos === -1 && !err) err = new Error('Item not found in set:'+id);
collection.index[id] = false;
set.ordering.splice(pos, 1);
});
if(cb) cb();
});
}
}
}else this.sql(query, callback);
break;
}
}
var needsRemoteFetch = function(ob, collectionName){
if(ob.links){
var link = ob.links.find(function(link){
if(link.name === collectionName) return true;
return false;
});
if(link && link.remote) return link;
else return false;
}else return false;
}
var handleRemoteFetch = function(ob, collectionName, collection, callback){
var link;
if((!collection) && (link = needsRemoteFetch(ob, collectionName))){
ob.upstream(collectionName).find('select * from '+collectionName, function(err, results){
callback(err, results);
});
return true;
}
return false;
}
DataService.prototype.sql = function(query, callback){
var ob = this;
this.ready(function(){
var match;
match = query.match(/select (.*) from (.*) where (.*)/i)
if(match){
var returns = match[1];
var collections = match[2].split(',');
var collection = ob.collection(collections[0]);
var link;
if(handleRemoteFetch(ob, collections[0], collection, callback)) return;
var where = whereParser.parse(match[3]);
if(ob.log) ob.log('select', collections, where);
//todo: handle specific returns
var results = computeSetWhere(new Indexed.Set(collection), where);
if(ob.log) ob.log('select-result', results.toArray());
if(collections.length > 1) throw new Exception('multi-collection selection unavailable');
callback(undefined, results);
}else{
//if there's no complex selection, let's test a simple one
match = query.match(/select (.*) from (.*)/i)
if(match){
var returns = match[1].trim();
var collections = match[2].split(',');
if(ob.log) ob.log('select', collections, match[0]);
if(collections.length > 1) throw new Exception('multi-collection selection unavailable');
var collection = ob.collection(collections[0], true);
if(handleRemoteFetch(ob, collections[0], collection, callback)) return;
if(!collection) throw new Error('Collection not Loaded');
var result = new Indexed.Set(collection);
if(ob.log) ob.log('select-result', result.toArray());
if(returns == '*'){
callback(undefined, result);
}else{
returns = returns.split(',').map(function(str){return str.trim()});
var trimmedSet = [];
result.forEach(function(item){
var copy = {};
returns.forEach(function(ret){
if(item[ret] !== undefined) copy[ret] = item[ret];
});
trimmedSet.push(copy);
});
trimmedSet.toArray = function(){ return trimmedSet; };
callback(undefined, trimmedSet);
}
}
}
match = query.match(/insert into (.*) \((.*)\) values \((.*)\)/i)
if(match){
var collections = match[1].split(',').map(function(str){ return str.trim()});
if(collections.length > 1) throw new Exception('you can only insert into a single collection');
var collection = ob.collection(collections[0]);
if(!collection) throw new Exception('collection not found: '+collections[0]);
var columns = match[2].split(',').map(function(str){ return str.trim()});
var valuesSets = match[3].split(/\) *, *\(/).map(function(set){
return eval('['+set+']'); //yes, this is crazy dangerous and needs to be replaced by a parser
});
if(ob.log) ob.log('insert', collections, columns, valuesSets);
var fullSet = new Indexed.Set(collection);
valuesSets.forEach(function(set){
var newItem = {};
columns.forEach(function(columnName, index){
newItem[columnName] = set[index];
});
fullSet.push(newItem);
});
callback(undefined);
}else{
match = query.match(/insert into (.*) values (.*)/i)
if(match){
throw new Error('you must define column names in order to insert. While defining by column order is legal SQL, it would be ambiguous given the arbitrary object insertion and the lack of consistent ordering in js objects. Manually defining a field order may be supported in the future in order to work around this. File a bug!');
}
}
match = query.match(/update (.*) set (.*) where (.*)/i)
if(match){
var values = strings.splitHonoringQuotes(match[2], ',').map(function(str){
var clean = str.trim();
var pos = clean.indexOf('=');
var field = clean.substring(0, pos).trim();
var value = eval(clean.substring(pos+1).trim());
return {
name : field,
value : value
};
});
var collections = match[1].split(',');
var collection = ob.collection(collections[0]);
var where = whereParser.parse(match[3]);
if(ob.log) ob.log('update', collections, values, where);
//todo: handle specific returns
var results = computeSetWhere(new Indexed.Set(collection), where);
if(collections.length > 1) throw new Exception('multi-collection updates not supported');
results.forEach(function(item, index){
values.forEach(function(set){
item[set.name] = typeof set.value == 'function'?set.value(item):set.value;
});
});
callback(undefined);
}
match = query.match(/delete from (.*) where (.*)/i)
if(match){
var collections = match[1].split(',');
var collection = ob.collection(collections[0]);
var where = whereParser.parse(match[2]);
var err;
if(ob.log) ob.log('delete', collections, where);
var results = computeSetWhere(new Indexed.Set(collection), where);
var deleting = results.toArray();
results.forEach(function(item){
var id = item[results.primaryKey];
var pos = results.ordering.indexOf(id);
if(pos === -1 && !err) err = new Error('Item not found in set:'+id);
collection.index[id] = false;
results.ordering.splice(pos, 1);
});
if(ob.log) ob.log('delete-results', deleting, results.ordering, results.root.index);
if(err) return callback(err);
callback(undefined);
}
match = query.match(/create (.*)/)
if(match){
var collections = match[1].split(',');
if(ob.log) ob.log('create', collections, where);
var collectionObject = new Indexed.Collection([], 'id' );
ob.tables[collections[0]] = collectionObject;
if(callback) callback(undefined);
}
return Error('no matches found!');
});
}
DataService.prototype.inquire = function(query){ //promise-based
//todo: support mongo query documents through this interface
var ob = this;
if(query.indexOf(' ') === -1 && query.indexOf("\n") === -1){
var cbInterface = this.query(query);
return {
find : function(document){
var promise = new Promise(function(resolve, reject){
cbInterface.find(document, function(err, results){
if(err) return reject(err);
else return resolve(results);
});
});
return promise;
},
update : function(updates, where){
var promise = new Promise(function(resolve, reject){
cbInterface.find(updates, where, function(err, results){
if(err) return reject(err);
else return resolve(results);
});
});
return promise;
},
insert : function(item){
var promise = new Promise(function(resolve, reject){
cbInterface.insert(item, function(err, results){
if(err) return reject(err);
else return resolve(results);
});
});
return promise;
},
'delete' : function(where){
var promise = new Promise(function(resolve, reject){
cbInterface.delete(where, function(err, results){
if(err) return reject(err);
else return resolve(results);
});
});
return promise;
}
}
}else{
var promise = new Promise(function(resolve, reject){
ob.query(query, function(err, results){
if(err) return reject(err);
else return resolve(results);
});
});
return promise;
}
}
DataService.adapt = function(fudi){
var Adapter = function(){
}
Adapter.prototype.load = function(name, options, handler, cb){
fudi(name).find('select * from '+name, function(err, results){
results.forEach((item)=>{
handler(item);
});
setTimeout(()=>{
cb();
})
})
}
Adapter.prototype.loadCollection = function(collection, name, options, cb){
this.load(name, options, function(item){
collection.index[item[collection.primaryKey]] = item;
}, cb);
}
return Adapter;
}
return DataService;
}));