Introduction
Deno KV lets you save and get data quickly using simple keys. It works like a small notebook where you write and read notes easily.
Deno KV lets you save and get data quickly using simple keys. It works like a small notebook where you write and read notes easily.
const kv = await Deno.openKv(); // To set a value await kv.set(["key"], value); // To get a value const result = await kv.get(["key"]); console.log(result.value);
Keys are arrays to allow grouping, like folders and files.
Values can be any JSON-serializable data.
await kv.set(["username"], "alice"); const user = await kv.get(["username"]); console.log(user.value);
await kv.set(["settings", "theme"], "dark"); const theme = await kv.get(["settings", "theme"]); console.log(theme.value);
await kv.set(["counter"], 5); const count = await kv.get(["counter"]); console.log(count.value);
This program saves a user's name and age in Deno KV, then reads and prints them.
async function demoKv() { const kv = await Deno.openKv(); // Save a user's name await kv.set(["user", "name"], "Bob"); // Save user's age await kv.set(["user", "age"], 30); // Get and print the name const nameResult = await kv.get(["user", "name"]); console.log(`Name: ${nameResult.value}`); // Get and print the age const ageResult = await kv.get(["user", "age"]); console.log(`Age: ${ageResult.value}`); } demoKv();
Always await the set and get calls because they work with promises.
If a key does not exist, get() returns an object with value undefined.
Keys are arrays to help organize data in groups.
Deno KV stores data using keys that are arrays.
You use set to save and get to read data.
It is simple and good for small, quick data storage needs.
set, so kv.set(["key"], value) is correct.kv.set(["key"], value) to save data. -> Option Akv.set(["key"], value) to save data. [OK]get method is asynchronous, so it must be awaited.const kv = await Deno.openKv(); await kv.set(["user", "name"], "Alice"); const result = await kv.get(["user", "name"]); console.log(result.value);
get method fetches the stored value, which is "Alice".const kv = await Deno.openKv();
await kv.set("session", "active");
const status = await kv.get(["session"]);
console.log(status.value);set method requires the key to be an array, but here it is a string "session".get method uses the key as an array ["session"], which is correct.const kv = await Deno.openKv();
const userId = 42;
const newPrefs = { theme: "dark" };
// Which code below is correct?
Pass the test and this pattern moves unknown