blob: ab00957dcb099b069c7ffb3313e1ad4eb6e0aeb2 (
plain)
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
|
//EJDB console
var path = require("path");
var EJDB = require("ejdb");
var cdb = null; //current DB desc
repl = require("repl").start({
prompt : "ejc> ",
input : process.stdin,
output : process.stdout,
terminal : true,
useColors : true
});
//console.log("repl.context=" + repl.context);
var dbctl = {
open : function(dbpath) {
if (dbpath == null) {
return error("No file path specified");
}
if (cdb) {
return error("Database already opened: " + cdb.dbpath);
}
dbpath = path.resolve(dbpath);
cdb = {
dbpath : dbpath,
jb : EJDB.open(dbpath)
};
syncdbctx();
return dbstatus(cdb);
},
status : function() {
return dbstatus(cdb);
},
close : function() {
if (!cdb || !cdb.jb) {
return error("Database already closed");
}
try {
cdb.jb.close();
} finally {
cdb = null;
}
syncdbctx();
}
};
repl.on("exit", function() {
dbctl.close();
console.log("Bye!");
});
function dbstatus(cdb) {
if (cdb) {
return cdb.jb.getDBMeta();
} else {
return {};
}
}
function syncdbctx() {
var db = {};
repl.resetContext();
if (cdb && cdb.jb) {
db.__proto__ = cdb.jb;
db["close"] = dbctl.close.bind(dbctl);
db["status"] = dbctl.status.bind(dbctl);
repl.context.db = db;
} else {
db.__proto__ = dbctl;
repl.context.db = db;
}
}
function error(msg) {
return "ERROR: " + msg;
}
syncdbctx();
|