mostprodev
mostprodev11mo ago

Problems with Deno KV

import InputLoop from "https://deno.land/x/input@2.0.3/index.ts";

const kv = await Deno.openKv();
const input = new InputLoop();
let name;
let age;
let profession;
let id;

while (true) {
console.log("");
console.log("DENO KV DEMO");
console.log("> 0: Read Database");
console.log("> 1: Create/Edit User");
console.log("> 2: Delete User");
console.log("> 3: Exit");
const choice = await input.question("Input: ", false);
switch (choice) {
case "0":
for (const i in kv.list({ prefix: ["users"] })) {
console.log(i);
}
break;
case "1":
name = await input.question("Name: ", false);
if (!name) {
console.log("Invalid Input!");
break;
}
age = await input.question("Age: ", false);
profession = await input.question("Profession: ", false);
id = await input.question("Id: ", false);
await kv.set(["users", name], { age, profession, id });
break;
case "2":
name = await input.question("Name: ", false);
if (!name) {
console.log("Invalid Input!");
break;
}
kv.delete(["users", name]);
break;
case "3":
Deno.exit();
break;
default:
console.log("Invalid Input!");
}
}
import InputLoop from "https://deno.land/x/input@2.0.3/index.ts";

const kv = await Deno.openKv();
const input = new InputLoop();
let name;
let age;
let profession;
let id;

while (true) {
console.log("");
console.log("DENO KV DEMO");
console.log("> 0: Read Database");
console.log("> 1: Create/Edit User");
console.log("> 2: Delete User");
console.log("> 3: Exit");
const choice = await input.question("Input: ", false);
switch (choice) {
case "0":
for (const i in kv.list({ prefix: ["users"] })) {
console.log(i);
}
break;
case "1":
name = await input.question("Name: ", false);
if (!name) {
console.log("Invalid Input!");
break;
}
age = await input.question("Age: ", false);
profession = await input.question("Profession: ", false);
id = await input.question("Id: ", false);
await kv.set(["users", name], { age, profession, id });
break;
case "2":
name = await input.question("Name: ", false);
if (!name) {
console.log("Invalid Input!");
break;
}
kv.delete(["users", name]);
break;
case "3":
Deno.exit();
break;
default:
console.log("Invalid Input!");
}
}
This is just some code I made to test KV. However, it does seem to save anything.
2 Replies
mostprodev
mostprodev11mo ago
I somewhat fixed it.
case "0":
for await (const x of kv.list({ prefix: ["users"] })) {
console.log(x.key, x.value);
}
break;
case "0":
for await (const x of kv.list({ prefix: ["users"] })) {
console.log(x.key, x.value);
}
break;
But is there a way of writing a schema or something for better code? And how else can I improve the code?
Leokuma
Leokuma11mo ago
Deno kv itself intends to provide a raw, low level interface, at least by now. More ergonomic APIs built on top of kv have been coming from userland. You can search for kv in #showcase. People have been creating ORMs, autoindexes, schemas and stuff But IIRC, you can at least pass a type to kv.get<T>