-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-ranger.js
613 lines (501 loc) · 16 KB
/
mongo-ranger.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
const blessed = require("blessed");
const assert = require("assert");
const MongoClient = require("mongodb").MongoClient;
const components = require("./components");
const util = require("./util");
const browser = require("./browser");
let focused = 0;
let client, db; // mongo connection
let screen, logger, input, cols; // all Blessed components
const DOC_LIMIT = 64;
async function main(options) {
const uri = options.port ? `${options.host}:${options.port}` : options.host;
console.log(`Connecting to ${uri}`);
client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
appname: "mongo-ranger"
});
await client.connect();
const admin = client.db("test").admin();
const dbs = await admin.listDatabases();
screen = blessed.screen({
title: "mongo-ranger",
smartCSR: true,
dockBorders: true
});
input = components.input();
const search = cb => {
input.clearValue();
input.setLabel("{cyan-fg}{bold}Search{/}");
screen.render();
input.readInput((val, err) => {
cb(val, err);
input.clear();
});
};
// this should be customizable
// janky arbitrary values
cols = [
components.column({
width: "18%",
level: 0,
search
}),
components.column({
left: "16%",
width: "36%",
level: 1,
search
}),
components.column({
left: "49%",
right: 0,
level: 2,
search
})
];
const numCols = cols.length;
// list of databases is only fetched once
// admin/local are problematic and cause bugs, esp. for atlas free tier
cols[0].setKeys(
dbs.databases
.map(db => db.name)
.filter(name => !["admin", "local"].includes(name))
);
cols[0].setItems(cols[0].keys);
// initialize listeners for each column
cols.forEach((col, index) => {
screen.append(col);
// move up/down or select item
col.key(
["j", "k", "up", "down"],
util.crashOnError(screen, () => applySelection(index))
);
col.on("focus", () => {
setTimeout(
// on focus, selected element doesn't always update right away, so using this timeout 0
util.crashOnError(screen, () => applySelection(index)),
0
);
});
// when we change levels, shift the columns accordingly
col.key(
["l", "right", "enter"],
util.crashOnError(screen, async () => {
if (focused === numCols - 2 && browser.canAdvance()) {
shiftRight();
} else if (focused < numCols - 1) {
// can change focused column without needing to shift
cols[++focused].focus();
} else if (cols[focused].level > util.levels.DOCUMENT_BASE) {
// can't move forward any more -- start edit mode
await promptEdit();
}
})
);
col.key(["h", "left"], () => {
if (focused === 1 && cols[0].level > 0) {
shiftLeft();
} else if (focused > 0) {
// can change focused column without needing to shift
cols[--focused].focus();
}
});
});
screen.append(input);
// Handle debug mode
if (options.debug) {
logger = components.logger();
screen.append(logger);
} else {
logger = {
// no-op all prints
log: () => {}
};
}
// Handle query request
screen.key([":"], util.crashOnError(screen, promptQuery));
// Handle add/insert request
screen.key(["i"], util.crashOnError(screen, promptInsert));
// Handle delete request
screen.key(["d"], util.crashOnError(screen, deleteSelected));
// Handle refresh request
screen.key(["r", "f5"], util.crashOnError(screen, reloadCollection));
// Quit q or Control-C.
screen.key(["q", "C-c"], () => {
client.close();
return process.exit(0);
});
cols[focused].focus();
screen.render();
}
/**
* Apply the selected item at cols[index], and make the appropriate
* database calls to populate the column(s) to the right. Re-renders the UI.
*
* @param {Number} index
*/
async function applySelection(index) {
const col = cols[index];
const nextCol = cols[index + 1]; // undefined for last column
const numCols = cols.length;
const selectedKey = col.getKey(col.selected);
logger.log("Selected: " + util.stringify(selectedKey));
if (selectedKey === undefined) {
return screen.render(); // list was empty
}
if (col.level === util.levels.DATABASE) {
// A selection on the DATABASE level loads the COLLECTION level
assert(index === 0);
db = client.db(selectedKey);
const collections = await db.listCollections().toArray();
nextCol.setKeys(collections.map(coll => coll.name));
nextCol.setItems(nextCol.keys); // no formatting
browser.clear();
} else if (col.level === util.levels.COLLECTION) {
// A selection on the COLLECTION level loads the DOCUMENT_BASE level
assert(index <= 1);
const collection = col.getKey(col.selected);
if (collection === browser.collection) {
// no need to requery, this collection already loaded
return;
}
if (input.getLabelText() === "Query") {
input.clear(); // clear out any stale queries
}
await applyQuery({});
} else if (col.level >= util.levels.DOCUMENT_BASE) {
if (!nextCol) return screen.render();
// content of the document/sub-document the user selected
const content = browser.traverse(col.level, selectedKey);
setColumnContents(nextCol, content);
}
for (let i = index + 2; i < numCols; i++) {
// clear out old columns if necessary
cols[i].setItems([]);
}
if (browser.cursor.length) {
logger.log(`Cursor: ${util.stringify(browser.cursor)}`);
}
screen.render();
}
// set the contents of a column to display the specified javascript object, with pretty printing
// accepts true columns and virtual (not visible) columns
function setColumnContents(col, content) {
if (Array.isArray(content)) {
col.setItems(content.map(util.stringify));
col.setKeys(Array.from(content.keys())); // arr of indices
} else if (util.isObject(content)) {
//q && Object.keys(content).length) {
col.setKeys(Object.keys(content));
col.setItems(
col.keys.map(k => `{bold}${k}:{/} ${util.stringify(content[k])}`)
);
} else {
col.setKeys([JSON.stringify(content)]); // plain/unformatted
col.setItems([util.stringify(content)]);
}
}
// shift columns when user moves to the right
function shiftRight() {
const numCols = cols.length;
util.saveColumn(cols[0]); // save this before it is removed from the screen
for (let i = 0; i < numCols - 1; i++) {
cols[i].copyFrom(cols[i + 1]);
}
// need to populate this
cols[numCols - 1].setItems([]);
cols[numCols - 1].moveLevel(1);
cols[focused].focus(); // trigger reload data
screen.render();
}
// shift columns when user moves to the left
function shiftLeft() {
const numCols = cols.length;
for (let i = numCols - 1; i > 0; i--) {
cols[i].copyFrom(cols[i - 1]);
}
util.loadColumn(cols[0], cols[0].level - 1);
cols[focused].focus(); // trigger reload data
screen.render();
}
/**
* Apply a user-inputted query to the currently-selected collection
* @param {Object} query
*/
async function applyQuery(queryObj) {
const col = cols[focused];
const nextCol = cols[focused + 1];
assert(col.level === util.levels.COLLECTION);
assert(!!nextCol);
browser.query = queryObj;
const collection = col.getKey(col.selected);
logger.log(`Querying "${util.stringify(queryObj)}" on db.${collection}`);
let docs;
try {
docs = await db
.collection(collection)
.find(queryObj)
.limit(DOC_LIMIT)
.toArray();
} catch (e) {
input.setError(e.toString());
return screen.render();
}
logger.log(`Found ${docs.length} results`);
browser.load(collection, docs);
setColumnContents(nextCol, docs);
screen.render();
}
// used to query a collection (prompts user for input, then calls applyQuery)
async function promptQuery() {
const col = cols[focused];
input.setLabel("{green-fg}{bold}Query{/}");
if (col.level !== util.levels.COLLECTION) {
input.setError(
"Must select a collection in order to query!" +
(col.level === util.levels.DOCUMENT_BASE ? " (Go back a level)" : "")
);
return screen.render();
}
input.clearValue();
screen.render();
const val = await input.readObject();
if (!val) return screen.render();
await applyQuery(val);
}
// called when user pushes i, to insert a new document/field
async function promptInsert() {
const col = cols[focused];
if (col.level > util.levels.DOCUMENT_BASE) {
return await promptAddField(); // add field rather than insert doc
}
if (col.level < util.levels.DOCUMENT_BASE) {
input.setError("Adding new db/collections is unsupported");
return screen.render();
}
logger.log("Inserting document into db." + browser.collection);
input.setLabel("{blue-fg}{bold}Insert new document{/}");
input.clearValue();
screen.render();
const valObj = await input.readObject();
input.clear();
if (valObj === null) return screen.render();
try {
await db.collection(browser.collection).insertOne(valObj);
} catch (e) {
input.setError(e.toString());
return screen.render();
}
propogateInsert(valObj);
screen.render();
}
// Adder used to add a new field to a document
async function promptAddField() {
const col = cols[focused];
if (col.level <= util.levels.DOCUMENT_BASE) {
return; // can't add a field to this low a level
}
const doc = browser.get(util.levels.DOCUMENT_BASE + 1);
const prop = browser.cursor
.slice(1, col.level - util.levels.DOCUMENT_BASE)
.join("."); // property to be updated
const content = browser.get(col.level); // array/obj to insert to
assert(!!doc._id);
if (Array.isArray(content)) {
logger.log("Append to " + util.stringify(prop));
input.setLabel("{blue-fg}{bold}Add to array{/}");
input.clearValue();
screen.render();
const valObj = await input.readObject();
input.clear();
if (valObj === null) return screen.render();
const res = await dbUpdate(doc, { $push: { [prop]: valObj } });
if (!res) return;
propogateUpdate(res);
screen.render();
} else if (util.isObject(content)) {
logger.log("Add to " + util.stringify(prop));
input.setLabel("{blue-fg}{bold}Add new field{/}");
input.clearValue();
screen.render();
const key = await input.readString();
if (!key) return screen.render();
input.setLabel(`{blue-fg}{bold}Specify value for "${key}"{/}`);
input.clearValue();
screen.render();
const valObj = await input.readObject();
input.clear();
if (valObj === null) return screen.render();
let pathToKey = `${prop}${prop ? "." : ""}${key}`;
const res = await dbUpdate(doc, { $set: { [pathToKey]: valObj } });
if (!res) return;
propogateUpdate(res);
screen.render();
} else {
input.setError(
"Cannot append to field of type " + util.colorize(typeof content)
);
return screen.render();
}
}
// Editor used to edit existing fields
async function promptEdit() {
input.setLabel("{blue-fg}{bold}Edit{/}");
const content = browser.get();
logger.log("Editing: " + util.stringify(content));
input.setValue(JSON.stringify(content));
screen.render();
const valObj = await input.readObject();
input.clear();
if (valObj === null) return screen.render();
const doc = browser.get(util.levels.DOCUMENT_BASE + 1);
const prop = browser.cursor.slice(1).join("."); // property to be updated
logger.log(`Updating ${prop} to: ${util.stringify(valObj)}`);
assert(!!doc._id);
const res = await dbUpdate(doc, { $set: { [prop]: valObj } });
if (!res) return;
propogateUpdate(res);
screen.render();
}
async function deleteSelected() {
const col = cols[focused];
if (col.level < util.levels.DOCUMENT_BASE) {
input.setError("Deleting this is not yet supported");
return screen.render();
}
if (col.level === util.levels.DOCUMENT_BASE) {
const doc = browser.get();
logger.log(`Deleting ${doc._id} from db.${browser.collection}`);
try {
await db.collection(browser.collection).deleteOne({ _id: doc._id });
} catch (e) {
input.setError(e.toString());
return screen.render();
}
propogateDelete(doc);
screen.render();
} else {
if (focused === cols.length - 1) {
input.setError("To delete properties, go back a layer");
return screen.render();
}
const prop = browser.cursor.slice(1).join("."); // property to be deleted
const doc = browser.get(util.levels.DOCUMENT_BASE + 1);
logger.log(`Deleting ${prop} from document ${doc._id}`);
const fromArr = Array.isArray(browser.get(col.level));
let res;
if (fromArr) {
// this is so sketchy, why isn't there a simple way to remove by index?
const deleteKey = `__toDelete%${doc._id}%${col.selected}`;
const arr = prop.substr(0, prop.lastIndexOf("."));
await dbUpdate(doc, { $set: { [prop]: deleteKey } });
res = await dbUpdate(doc, { $pull: { [arr]: deleteKey } });
} else {
res = await dbUpdate(doc, { $unset: { [prop]: "" } });
}
if (!res) return;
propogateUpdate(res);
screen.render();
}
}
async function reloadCollection() {
// reload the docs in the browser
if (cols[focused].level === util.levels.DATABASE) {
cols[focused].focus(); // simply refocusing should trigger a refresh on this level
return;
}
// (until a more elegant solution is found)
// kick the user back out to the collection level
while (focused > 1) {
cols[--focused].focus();
}
while (cols[focused].level > util.levels.COLLECTION) {
shiftLeft();
}
await applyQuery(browser.query || {});
}
// update all columns to reflect an updated document
function propogateUpdate(doc) {
assert(!!doc);
// update data stored in the browser, but we have to update the UI separately
browser.update(doc);
// update cols from right to left
for (let i = focused; i >= 0; i--) {
const col = cols[i];
const content = browser.get(col.level);
setColumnContents(col, content);
}
const lowestVisibleLevel = cols[0].level;
for (
let level = lowestVisibleLevel - 1;
level >= util.levels.DOCUMENT_BASE;
level--
) {
// for deeply nested updates, we may need to update columns that are off the screen
const col = util.getVirtualColumn(level);
const content = browser.get(level);
setColumnContents(col, content);
}
if (focused === cols.length - 1 && browser.canAdvance()) {
shiftRight(); // we may have introduced a new layer during the edit
cols[--focused].focus();
} else if (
focused === cols.length - 2 &&
util.isEmpty(browser.get(cols[focused].level))
) {
shiftLeft();
cols[++focused].focus(); // if you deleted the last item in an object/array
} else {
cols[focused].focus();
}
}
// update all columns to reflect an inserted document
function propogateInsert(doc) {
assert(!!doc);
browser.insert(doc);
// update document layer
const col = cols[focused];
assert(col.level === util.levels.DOCUMENT_BASE);
const content = browser.get(col.level);
setColumnContents(col, content);
if (browser.docs.length === 1) {
// edge case: adding the first document
shiftRight();
cols[--focused].focus();
}
}
function propogateDelete(doc) {
assert(!!doc);
browser.delete(doc);
// update document layer
const col = cols[focused];
assert(col.level === util.levels.DOCUMENT_BASE);
const content = browser.get(col.level);
setColumnContents(col, content);
if (browser.docs.length === 0) {
// edge case: deleting the final document
shiftLeft();
}
// always refocus so last column reflects the delete
cols[focused].focus();
}
/**
* Wraps the monogdb driver findOneAndUpdate and handles errors
*
* @param {Object} doc document to be updated
* @param {Object} update
*/
function dbUpdate(doc, update) {
return db
.collection(browser.collection)
.findOneAndUpdate({ _id: doc._id }, update, { returnOriginal: false })
.then(e => e.value)
.catch(e => {
input.setError(e.toString());
screen.render();
return null;
});
}
module.exports = main;