D
Deno

help

Problems with Deno KV

Mmostprodev8/16/2023
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. 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?
LLeokuma8/16/2023
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>

Looking for more? Join the community!

Recommended Posts
File differs only by case errorI'm getting a strange error from TypeScript. For some reason it thinks the case of my file name is Possible to have multiple http servers listening to different ports in the same module?I'm porting a service mesh platform we wrote for NodeJS over to Deno and it is based on the idea of How to have private state in embedded deno_core JsRuntime?I need to either keep a reference in rust to an object I then `delete globalThis.__context`, but I cIssues getting local environment running in VSCode and Visual Studio 2022I'm a windows user trying to contribute on an github issue, but I've run into a few blockers. Using Relative import path "ioredis" not prefixed with / or ./ or ../```ts // main.ts import Redis from "ioredis"; ``` ```json // deno.json { "imports": { "ioredisPixi.js breaks DenoI'm trying to import Pixi in a Fresh island: ``` import { Sprite, Stage } from "npm:@pixi/react@7.1Confused by deno vendor-related behaviorsI created a main.ts containing this code: ``` import * as thrift from "npm:thrift"; let connection =How to release node:zlib resourcesI have some testcases which use the `node:zlib` polyfill and I'm having a bit of trouble getting theDeno Repl ProgrammaticallyPython has a really nice not-well-known tool for creating a repl at any point in the code (kinda likDeno for SASS dependencies?I'm trying to understand how I would migrate from Jekyll and NPM to Lume and Deno. Specifically, I uHow can I send a number across a Deno.TcpConn as stream of bytes?I need to send a number across a Deno.TcpConn as 4 bytes, so it can be read by a server as an int32.Migrating `Deno.run` to `Deno.command` with `readlines`I had the following: ```ts browserProcess = Deno.run({ cmd: buildArgs, // eg chrome exe path, th