From 62e4906358a96c2a1f06a17c865d7ab42d03ef9e Mon Sep 17 00:00:00 2001 From: Cory Lynch Date: Thu, 1 Aug 2019 16:12:36 -0400 Subject: [PATCH] Add reload button, optimize requerying Fixes #6 Fixes #12 --- README.md | 1 + browser.js | 1 + index.js | 3 ++- mongo-ranger.js | 31 +++++++++++++++++++++++++++++++ package.json | 5 ++++- 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 87a16a9..4dd7dc6 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ Controls - Move forward on an individual field to edit - Push i to insert a new document or field - Push d to delete a document or field + - Push r to reload the current collection Options diff --git a/browser.js b/browser.js index d4d205b..9975369 100644 --- a/browser.js +++ b/browser.js @@ -9,6 +9,7 @@ const assert = require("assert"); */ const browser = { collection: null, + query: "", docs: [], cursor: [], diff --git a/index.js b/index.js index 44c9336..98ef5e4 100755 --- a/index.js +++ b/index.js @@ -51,7 +51,8 @@ const usage = [ "- Use : to type a MongoDB query for the selected collection", "- Move forward on an individual field to edit", "- Push i to insert a new document or field", - "- Push d to delete a document or field" + "- Push d to delete a document or field", + "- Push r to reload the current collection" ] }, { diff --git a/mongo-ranger.js b/mongo-ranger.js index fd989da..276dc12 100644 --- a/mongo-ranger.js +++ b/mongo-ranger.js @@ -147,6 +147,9 @@ async function main(options) { // 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(); @@ -191,6 +194,12 @@ async function applySelection(index) { } 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 @@ -275,6 +284,7 @@ async function applyQuery(queryObj) { 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}`); @@ -485,6 +495,27 @@ async function deleteSelected() { } } +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); diff --git a/package.json b/package.json index b70d6bf..d2a59f5 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,18 @@ { "name": "mongo-ranger", - "version": "1.0.1", + "version": "1.1.0", "description": "A MongoDB data browser for the console", "main": "index.js", "bin": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", + "build": "sh build.sh", "start": "node index.js" }, "author": "Cory Lynch", "license": "MIT", + "repository": "github:cory2067/mongo-ranger", + "homepage": "https://github.com/cory2067/mongo-ranger", "dependencies": { "blessed": "^0.1.81", "command-line-args": "^5.1.1",