This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
commands.db.js
executable file
·112 lines (97 loc) · 3.15 KB
/
commands.db.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
var showDatabase = function(db, _selected) {
if (!db) { db = window.db; }
if (!db) { this.error("Could not find database."); return; }
var dbname = db.getName();
var item = jsconsole.createItem();
var selected = "";
if (_selected) { selected = " selected"; }
item.addTitle("Database " + dbname + selected);
item.addAction("select", function(val) { selectDatabase(val); }, dbname);
item.addAction("collections", function(val) { selectDatabase(dbname, true); show("collections"); });
item.addAction("repair", function(val) { selectDatabase(val, true); repairDatabase(); }, dbname);
item.addAction("drop", function(val) { selectDatabase(val, true); dropDatabase(); }, dbname);
jsconsole.addItem(item);
}
var showCollections = function(db) {
if (!db) { db = window.db; }
if (!db) { this.error("Could not find database."); return; }
var dbname = db.getName();
db.getCollections(function(collections) {
var item = jsconsole.createItem();
item.addTitle("Collections in " + db.getName());
for (var i = 0; i < collections.length; i++) {
item.addAction("select", function(val) { selectCollection(db.getName(), val); }, collections[i]);
item.addText(" " + collections[i]);
item.addBR();
}
item.addBR();
item.addAction("create", function(val) { selectDatabase(db.getName(), true), createCollection(); });
jsconsole.addItem(item);
});
}
var selectCollection = function(dbname, collname, _auto) {
var item = jsconsole.createItem();
if (_auto && window.db && window.db.getName() == dbname && window.coll && window.coll.getName() == collname) {
return;
}
window.mongo.setDB(dbname);
var db = mongo.getCurrentDatabase();
db.setCollection(collname);
showCollection(null, true);
}
var execute = function(code, scope) {
var db = window.db;
if (!db) {
this.error("No database selected.");
return;
}
db.execute(function(result) {
var item = jsconsole.createItem();
item.addTitle("Executed");
item.addVariable(result);
jsconsole.addItem(item);
}, code, scope);
}
var dropDatabase = function(silent) {
var db = window.db;
if (!db) {
this.error("No database selected.");
return;
}
if (silent || confirm("Really drop database " + db.getName() + "?")) {
db.dropDatabase(function(result) {
var item = jsconsole.createItem();
item.addTitle("Database drop done");
item.addVariable(result);
jsconsole.addItem(item);
});
}
}
var repairDatabase = function() {
var db = window.db;
if (!db) {
this.error("No database selected.");
return;
}
db.repair(function(result) {
var item = jsconsole.createItem();
item.addTitle("Database repair done");
item.addVariable(result);
jsconsole.addItem(item);
});
}
var createCollection = function(name) {
var db = window.db;
if (!db) {
this.error("No database selected.");
return;
}
if (!name) {
name = prompt("Enter collection name");
if (!name) {
this.error("Canceled.");
return;
}
}
selectCollection(db.getName(), name);
}