-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
71 lines (65 loc) · 1.71 KB
/
main.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
import {
getRedisClient,
bulkInsert,
bulkRead,
bulk,
} from "./db.js"
import * as readline from 'node:readline'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const userInput = async (prompt) => {
return new Promise(resolve => rl.question(prompt, resolve))
}
const printHelp = () => {
console.log("Node Redis Playground")
console.log("Commands:")
console.log(" help")
console.log(" exit")
console.log(" bulk-insert <count>")
console.log(" bulk-read <count>")
console.log(" bulk <count>")
console.log(" <command> <args>")
console.log("Examples:")
console.log(" set foo bar")
console.log(" get foo")
console.log(" del foo")
}
const playground = async (redis) => {
while (true) {
const line = await userInput("> ")
if (line === "exit") {
process.exit()
} else if (line === "help") {
printHelp()
} else if (line === "") {
continue
} else {
const args = line.split(" ").filter((arg) => arg !== "")
const command = args[0]
const commandArgs = args.slice(1)
if (command === "bulk-insert") {
await bulkInsert(parseInt(commandArgs[0]))
} else if (command === "bulk-read") {
await bulkRead(parseInt(commandArgs[0]))
} else if (command === "bulk") {
await bulk(parseInt(commandArgs[0]))
} else {
try {
console.log(await redis[command](...commandArgs))
} catch (error) {
console.log("Error: " + error.message)
}
}
}
}
}
async function main(){
const redis = await getRedisClient()
console.log("Connected")
console.log("Type 'help' for commands")
await playground(redis)
}
await main()
process.exit()