Skip to content

Commit

Permalink
Add reload button, optimize requerying
Browse files Browse the repository at this point in the history
Fixes #6
Fixes #12
  • Loading branch information
cory2067 committed Aug 1, 2019
1 parent 106b7fa commit 62e4906
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const assert = require("assert");
*/
const browser = {
collection: null,
query: "",
docs: [],
cursor: [],

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down
31 changes: 31 additions & 0 deletions mongo-ranger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`);

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 62e4906

Please sign in to comment.