@liorandb/db 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.js +279997 -0
- package/dist/cli/index.js +99 -91
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -8,44 +8,59 @@ const readline_1 = __importDefault(require("readline"));
|
|
|
8
8
|
const util_1 = __importDefault(require("util"));
|
|
9
9
|
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
|
10
10
|
const database_1 = require("../config/database");
|
|
11
|
-
/* --------------------------------
|
|
11
|
+
/* -------------------------------- INIT -------------------------------- */
|
|
12
|
+
console.clear();
|
|
12
13
|
console.log("🚀 LioranDB Interactive Shell");
|
|
13
14
|
console.log("Type: help to see commands\n");
|
|
14
15
|
const rl = readline_1.default.createInterface({
|
|
15
16
|
input: process.stdin,
|
|
16
17
|
output: process.stdout,
|
|
17
|
-
prompt: "liorandb> ",
|
|
18
18
|
historySize: 1000,
|
|
19
19
|
});
|
|
20
20
|
let currentDB = "default";
|
|
21
|
+
let currentCollection = null;
|
|
22
|
+
/* -------------------------------- PROMPT -------------------------------- */
|
|
23
|
+
function updatePrompt() {
|
|
24
|
+
let p = `liorandb:${currentDB}`;
|
|
25
|
+
if (currentCollection)
|
|
26
|
+
p += `.${currentCollection}`;
|
|
27
|
+
rl.setPrompt(`${p}> `);
|
|
28
|
+
}
|
|
21
29
|
/* -------------------------------- HELP -------------------------------- */
|
|
22
30
|
async function printHelp() {
|
|
23
31
|
console.log(`
|
|
24
|
-
Database:
|
|
32
|
+
📦 Database: ( current: ${currentDB} )
|
|
25
33
|
show dbs
|
|
26
34
|
use <dbname>
|
|
27
35
|
show collections
|
|
36
|
+
|
|
37
|
+
📁 Collection: ( current: ${currentCollection ?? "none"} )
|
|
38
|
+
use collection <name>
|
|
28
39
|
db.createCollection("<name>")
|
|
29
40
|
db.dropCollection("<name>")
|
|
30
41
|
db.renameCollection("<old>", "<new>")
|
|
31
42
|
|
|
32
|
-
CRUD:
|
|
33
|
-
db.<collection>.insert({...})
|
|
34
|
-
db.<collection>.insertMany([...])
|
|
43
|
+
🧠 CRUD:
|
|
35
44
|
db.<collection>.find({...})
|
|
36
|
-
db.<collection>.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
db.<collection>.insert({...})
|
|
46
|
+
|
|
47
|
+
When collection selected:
|
|
48
|
+
find({...})
|
|
49
|
+
findOne({...})
|
|
50
|
+
insert({...})
|
|
51
|
+
insertMany([...])
|
|
52
|
+
update({...filter},{...update})
|
|
53
|
+
updateMany({...filter},{...update})
|
|
54
|
+
delete({...})
|
|
55
|
+
deleteMany({...})
|
|
56
|
+
count({...})
|
|
42
57
|
|
|
43
|
-
User
|
|
58
|
+
👤 User:
|
|
44
59
|
user.create("username","password")
|
|
45
60
|
user.delete("username")
|
|
46
61
|
user.list()
|
|
47
62
|
|
|
48
|
-
System:
|
|
63
|
+
⚙ System:
|
|
49
64
|
clear
|
|
50
65
|
exit
|
|
51
66
|
`);
|
|
@@ -59,17 +74,17 @@ function safeParse(obj) {
|
|
|
59
74
|
return null;
|
|
60
75
|
}
|
|
61
76
|
}
|
|
62
|
-
/* -------------------------------- USER
|
|
77
|
+
/* -------------------------------- USER COMMANDS -------------------------------- */
|
|
63
78
|
async function handleUserCommand(cmd) {
|
|
64
79
|
const users = await (0, database_1.getAuthCollection)();
|
|
65
80
|
if (cmd.startsWith("user.create")) {
|
|
66
81
|
const args = cmd.match(/\("(.+?)","(.+?)"\)/);
|
|
67
82
|
if (!args)
|
|
68
|
-
return console.error("Invalid syntax");
|
|
83
|
+
return console.error("❌ Invalid syntax");
|
|
69
84
|
const [, username, password] = args;
|
|
70
85
|
const existing = await users.findOne({ username });
|
|
71
86
|
if (existing)
|
|
72
|
-
return console.error("User already exists");
|
|
87
|
+
return console.error("❌ User already exists");
|
|
73
88
|
const hashed = await bcryptjs_1.default.hash(password, 10);
|
|
74
89
|
await users.insertOne({
|
|
75
90
|
username,
|
|
@@ -81,10 +96,10 @@ async function handleUserCommand(cmd) {
|
|
|
81
96
|
if (cmd.startsWith("user.delete")) {
|
|
82
97
|
const args = cmd.match(/\("(.+?)"\)/);
|
|
83
98
|
if (!args)
|
|
84
|
-
return console.error("Invalid syntax");
|
|
99
|
+
return console.error("❌ Invalid syntax");
|
|
85
100
|
const [, username] = args;
|
|
86
101
|
const r = await users.deleteOne({ username });
|
|
87
|
-
return console.log(
|
|
102
|
+
return console.log(`✔ Deleted: ${r}`);
|
|
88
103
|
}
|
|
89
104
|
if (cmd === "user.list()") {
|
|
90
105
|
const list = await users.find({});
|
|
@@ -94,7 +109,48 @@ async function handleUserCommand(cmd) {
|
|
|
94
109
|
})));
|
|
95
110
|
}
|
|
96
111
|
}
|
|
97
|
-
/* --------------------------------
|
|
112
|
+
/* -------------------------------- CRUD EXEC -------------------------------- */
|
|
113
|
+
async function runCollectionCommand(colName, action) {
|
|
114
|
+
const db = await database_1.manager.db(currentDB);
|
|
115
|
+
const col = db.collection(colName);
|
|
116
|
+
if (action.startsWith("insertMany")) {
|
|
117
|
+
const data = JSON.parse(action.match(/^insertMany\((.*)\)$/)[1]);
|
|
118
|
+
return console.log(util_1.default.inspect(await col.insertMany(data), false, 10, true));
|
|
119
|
+
}
|
|
120
|
+
if (action.startsWith("insert")) {
|
|
121
|
+
const data = JSON.parse(action.match(/^insert\((.*)\)$/)[1]);
|
|
122
|
+
return console.log(util_1.default.inspect(await col.insertOne(data), false, 10, true));
|
|
123
|
+
}
|
|
124
|
+
if (action.startsWith("findOne")) {
|
|
125
|
+
const q = safeParse(action.slice(8));
|
|
126
|
+
return console.log(util_1.default.inspect(await col.findOne(q || {}), false, 10, true));
|
|
127
|
+
}
|
|
128
|
+
if (action.startsWith("find")) {
|
|
129
|
+
const q = safeParse(action.slice(4));
|
|
130
|
+
return console.log(util_1.default.inspect(await col.find(q || {}), false, 10, true));
|
|
131
|
+
}
|
|
132
|
+
if (action.startsWith("updateMany")) {
|
|
133
|
+
const args = safeParse(`[${action.match(/^updateMany\((.*)\)$/)[1]}]`);
|
|
134
|
+
return console.log(util_1.default.inspect(await col.updateMany(args[0], args[1]), false, 10, true));
|
|
135
|
+
}
|
|
136
|
+
if (action.startsWith("update")) {
|
|
137
|
+
const args = safeParse(`[${action.slice(6)}]`);
|
|
138
|
+
return console.log(util_1.default.inspect(await col.updateOne(args[0], args[1]), false, 10, true));
|
|
139
|
+
}
|
|
140
|
+
if (action.startsWith("deleteMany")) {
|
|
141
|
+
const q = safeParse(action.slice(10));
|
|
142
|
+
return console.log(`✔ Deleted: ${await col.deleteMany(q)}`);
|
|
143
|
+
}
|
|
144
|
+
if (action.startsWith("delete")) {
|
|
145
|
+
const q = safeParse(action.slice(6));
|
|
146
|
+
return console.log(`✔ Deleted: ${await col.deleteOne(q)}`);
|
|
147
|
+
}
|
|
148
|
+
if (action.startsWith("count")) {
|
|
149
|
+
const q = safeParse(action.slice(5));
|
|
150
|
+
return console.log(`✔ Count: ${await col.countDocuments(q)}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/* -------------------------------- MAIN HANDLER -------------------------------- */
|
|
98
154
|
async function handleCommand(input) {
|
|
99
155
|
const cmd = input.trim();
|
|
100
156
|
if (!cmd)
|
|
@@ -106,23 +162,26 @@ async function handleCommand(input) {
|
|
|
106
162
|
if (cmd === "help")
|
|
107
163
|
return printHelp();
|
|
108
164
|
if (cmd === "show dbs") {
|
|
109
|
-
|
|
110
|
-
|
|
165
|
+
return console.table(await database_1.manager.listDatabases());
|
|
166
|
+
}
|
|
167
|
+
if (cmd.startsWith("use collection ")) {
|
|
168
|
+
currentCollection = cmd.split(" ")[2];
|
|
169
|
+
return updatePrompt();
|
|
111
170
|
}
|
|
112
171
|
if (cmd.startsWith("use ")) {
|
|
113
172
|
currentDB = cmd.split(" ")[1];
|
|
173
|
+
currentCollection = null;
|
|
114
174
|
await database_1.manager.db(currentDB);
|
|
115
|
-
return
|
|
175
|
+
return updatePrompt();
|
|
116
176
|
}
|
|
117
177
|
if (cmd === "show collections") {
|
|
118
178
|
const db = await database_1.manager.db(currentDB);
|
|
119
|
-
|
|
120
|
-
return console.table(cols);
|
|
179
|
+
return console.table(await db.listCollections());
|
|
121
180
|
}
|
|
122
181
|
if (cmd.startsWith("db.createCollection")) {
|
|
123
182
|
const name = cmd.match(/\("(.+)"\)/)?.[1];
|
|
124
183
|
if (!name)
|
|
125
|
-
return console.error("Invalid syntax");
|
|
184
|
+
return console.error("❌ Invalid syntax");
|
|
126
185
|
const db = await database_1.manager.db(currentDB);
|
|
127
186
|
await db.createCollection(name);
|
|
128
187
|
return console.log("✔ Collection created");
|
|
@@ -130,7 +189,7 @@ async function handleCommand(input) {
|
|
|
130
189
|
if (cmd.startsWith("db.dropCollection")) {
|
|
131
190
|
const name = cmd.match(/\("(.+)"\)/)?.[1];
|
|
132
191
|
if (!name)
|
|
133
|
-
return console.error("Invalid syntax");
|
|
192
|
+
return console.error("❌ Invalid syntax");
|
|
134
193
|
const db = await database_1.manager.db(currentDB);
|
|
135
194
|
await db.deleteCollection(name);
|
|
136
195
|
return console.log("✔ Collection deleted");
|
|
@@ -138,85 +197,34 @@ async function handleCommand(input) {
|
|
|
138
197
|
if (cmd.startsWith("db.renameCollection")) {
|
|
139
198
|
const args = cmd.match(/\("(.+)",\s*"(.+)"\)/);
|
|
140
199
|
if (!args)
|
|
141
|
-
return console.error("Invalid syntax");
|
|
200
|
+
return console.error("❌ Invalid syntax");
|
|
142
201
|
const db = await database_1.manager.db(currentDB);
|
|
143
202
|
await db.renameCollection(args[1], args[2]);
|
|
144
203
|
return console.log("✔ Collection renamed");
|
|
145
204
|
}
|
|
146
|
-
|
|
147
|
-
if (cmd.startsWith("user.")) {
|
|
205
|
+
if (cmd.startsWith("user."))
|
|
148
206
|
return handleUserCommand(cmd);
|
|
149
|
-
}
|
|
150
|
-
/* -------- DATABASE CRUD -------- */
|
|
151
207
|
if (cmd.startsWith("db.")) {
|
|
152
208
|
const match = cmd.match(/^db\.([^.]+)\.(.+)$/);
|
|
153
209
|
if (!match)
|
|
154
|
-
return console.error("Invalid syntax");
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const r = await col.insertMany(data);
|
|
162
|
-
return console.log(util_1.default.inspect(r, false, 10, true));
|
|
163
|
-
}
|
|
164
|
-
if (action.startsWith("insert")) {
|
|
165
|
-
const match = action.match(/^insert\((.*)\)$/);
|
|
166
|
-
const data = JSON.parse(match[1]);
|
|
167
|
-
const r = await col.insertOne(data);
|
|
168
|
-
return console.log(util_1.default.inspect(r, false, 10, true));
|
|
169
|
-
}
|
|
170
|
-
if (action.startsWith("findOne")) {
|
|
171
|
-
const q = safeParse(action.slice(8));
|
|
172
|
-
const r = await col.findOne(q || {});
|
|
173
|
-
return console.log(util_1.default.inspect(r, false, 10, true));
|
|
174
|
-
}
|
|
175
|
-
if (action.startsWith("find")) {
|
|
176
|
-
const q = safeParse(action.slice(4));
|
|
177
|
-
const r = await col.find(q || {});
|
|
178
|
-
return console.log(util_1.default.inspect(r, false, 10, true));
|
|
179
|
-
}
|
|
180
|
-
if (action.startsWith("updateMany")) {
|
|
181
|
-
const match = action.match(/^updateMany\((.*)\)$/);
|
|
182
|
-
const args = safeParse(`[${match[1]}]`);
|
|
183
|
-
const r = await col.updateMany(args[0], args[1]);
|
|
184
|
-
return console.log(util_1.default.inspect(r, false, 10, true));
|
|
185
|
-
}
|
|
186
|
-
if (action.startsWith("update")) {
|
|
187
|
-
const args = safeParse(`[${action.slice(6)}]`);
|
|
188
|
-
const r = await col.updateOne(args[0], args[1]);
|
|
189
|
-
return console.log(util_1.default.inspect(r, false, 10, true));
|
|
190
|
-
}
|
|
191
|
-
if (action.startsWith("deleteMany")) {
|
|
192
|
-
const q = safeParse(action.slice(10));
|
|
193
|
-
const r = await col.deleteMany(q);
|
|
194
|
-
return console.log(`Deleted: ${r}`);
|
|
195
|
-
}
|
|
196
|
-
if (action.startsWith("delete")) {
|
|
197
|
-
const q = safeParse(action.slice(6));
|
|
198
|
-
const r = await col.deleteOne(q);
|
|
199
|
-
return console.log(`Deleted: ${r}`);
|
|
200
|
-
}
|
|
201
|
-
if (action.startsWith("count")) {
|
|
202
|
-
const q = safeParse(action.slice(5));
|
|
203
|
-
const r = await col.countDocuments(q);
|
|
204
|
-
return console.log(`Count: ${r}`);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
console.log("Unknown command. Type: help");
|
|
210
|
+
return console.error("❌ Invalid syntax");
|
|
211
|
+
return runCollectionCommand(match[1], match[2]);
|
|
212
|
+
}
|
|
213
|
+
if (currentCollection) {
|
|
214
|
+
return runCollectionCommand(currentCollection, cmd);
|
|
215
|
+
}
|
|
216
|
+
console.log("❓ Unknown command. Type: help");
|
|
208
217
|
}
|
|
209
|
-
/* -------------------------------- START
|
|
218
|
+
/* -------------------------------- START -------------------------------- */
|
|
219
|
+
updatePrompt();
|
|
210
220
|
rl.prompt();
|
|
211
221
|
rl.on("line", async (line) => {
|
|
212
222
|
try {
|
|
213
223
|
await handleCommand(line);
|
|
214
224
|
}
|
|
215
225
|
catch (err) {
|
|
216
|
-
console.error("Error:", err);
|
|
226
|
+
console.error("❌ Error:", err);
|
|
217
227
|
}
|
|
218
228
|
rl.prompt();
|
|
219
229
|
});
|
|
220
|
-
rl.on("close", () =>
|
|
221
|
-
process.exit(0);
|
|
222
|
-
});
|
|
230
|
+
rl.on("close", () => process.exit(0));
|