Consider this code running on Deno Deploy with KV enabled:
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?.name);What will be printed in 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?.name);
Think about what kv.get returns and how to access the stored value.
The kv.get method returns an object with a value property containing the stored data. Accessing result.value?.name safely retrieves the name Alice.
You want to delete the key ["session", "abc"] from Deno KV. Which code snippet will do this correctly?
Check the official Deno KV API method names.
The correct method to delete a key is delete. Other options are invalid method names and will cause errors.
count after this transaction?Given this code snippet using a Deno KV transaction:
const kv = await Deno.openKv(); await kv.set(["counter"], 0); const tx = kv.atomic(); const current = await tx.get(["counter"]); await tx.set(["counter"], (current.value ?? 0) + 1); await tx.commit(); const result = await kv.get(["counter"]); const count = result.value;
What is the value of count after running this code once?
const kv = await Deno.openKv(); await kv.set(["counter"], 0); const tx = kv.atomic(); const current = await tx.get(["counter"]); await tx.set(["counter"], (current.value ?? 0) + 1); await tx.commit(); const result = await kv.get(["counter"]); const count = result.value;
Think about how the transaction increments the counter.
The transaction reads the current value (0), adds 1, sets it, and commits. So the final value is 1.
Look at this code snippet:
const kv = await Deno.openKv();
await kv.set("user:1", { name: "Bob" });It throws a runtime error. What is the cause?
const kv = await Deno.openKv(); await kv.set("user:1", { name: "Bob" });
Check the expected key format for Deno KV.
Deno KV keys must be tuples (arrays) of strings or numbers. Passing a single string causes a type error.
Consider Deno KV's consistency guarantees. Which of these statements is correct?
Think about how atomic transactions affect consistency in distributed KV stores.
Deno KV guarantees strong consistency within atomic transactions. Outside transactions, reads may be eventually consistent due to distributed nature.
