Consider this code snippet using Deno KV:
const kv = await Deno.openKv();
await kv.set(["user", "123"], { name: "Alice", age: 30 });
const result = await kv.get(["user", "123"]);
console.log(result.value);What will be logged to the console?
const kv = await Deno.openKv(); await kv.set(["user", "123"], { name: "Alice", age: 30 }); const result = await kv.get(["user", "123"]); console.log(result.value);
Remember that kv.get() returns an object with a value property containing the stored data.
The kv.get() method returns an object with a value property holding the stored value. Since we stored { name: "Alice", age: 30 } under the key ["user", "123"], result.value will be that object.
Given this code:
const kv = await Deno.openKv();
await kv.set(["counter"], 1);
await kv.set(["counter"], 2);
const { value: count } = await kv.get(["counter"]);
console.log(count);What will be printed?
const kv = await Deno.openKv(); await kv.set(["counter"], 1); await kv.set(["counter"], 2); const { value: count } = await kv.get(["counter"]); console.log(count);
Think about what happens when you set the same key twice.
Setting the same key twice overwrites the previous value. The last set value is 2, so count will be 2.
Deno KV keys must be arrays of strings or numbers. Which of these key usages will cause a runtime error?
const kv = await Deno.openKv(); await kv.set(KEY, "value");
Remember that keys must be arrays, not strings.
Deno KV keys must be arrays of strings or numbers. Using a plain string like "user:123" as a key is invalid and will cause a runtime error.
Look at this code:
const kv = await Deno.openKv();
await kv.set("user", { name: "Bob" });
const result = await kv.get("user");
console.log(result.value);Why does it throw a TypeError?
const kv = await Deno.openKv(); await kv.set("user", { name: "Bob" }); const result = await kv.get("user"); console.log(result.value);
Check the type of the key argument in set and get.
Deno KV requires keys to be arrays of strings or numbers. Passing a string directly causes a TypeError.
Which statement about Deno KV transactions is true?
Think about atomicity and concurrency control in key-value stores.
Deno KV supports transactions that group multiple operations atomically. They can be retried automatically if conflicts occur, ensuring consistency.
