📖 Scenario: You want to create a simple to-do list app that saves tasks using Deno's built-in key-value store called Deno KV. This app will let you add tasks and mark them as done, storing all data persistently.
🎯 Goal: Build a small Deno script that stores to-do tasks in Deno KV, adds new tasks, and retrieves all tasks to show their status.
📋 What You'll Learn
Create a Deno KV instance
Set a configuration variable for the task prefix key
Add a new task to the KV store with a unique key
Retrieve and list all tasks stored in KV
💡 Why This Matters
🌍 Real World
Deno KV is useful for small to medium apps needing fast, simple persistent storage without external databases.
💼 Career
Understanding Deno KV helps backend developers build lightweight services and APIs with built-in storage, improving efficiency and reducing dependencies.
Progress0 / 4 steps
1
Create a Deno KV instance
Write a line of code to create a Deno KV instance and assign it to a variable called kv using await Deno.openKv().
Deno
Hint
Use await Deno.openKv() to create the KV instance and assign it to kv.
2
Set a task key prefix
Create a constant variable called TASK_PREFIX and set it to the string "task" to use as a prefix for task keys.
Deno
Hint
Use const TASK_PREFIX = "task"; to define the prefix.
3
Add a new task to KV
Write an async function called addTask that takes a taskId and description. Inside, use kv.set to store the task with key [TASK_PREFIX, taskId] and value as an object with description and done: false.
Deno
Hint
Define addTask as async and use await kv.set([TASK_PREFIX, taskId], { description, done: false }).
4
Retrieve and list all tasks
Write an async function called listTasks that uses kv.list with prefix [TASK_PREFIX] to get all tasks. Use a for await loop to iterate over entries and log each task's taskId and description.
Deno
Hint
Use for await to loop over kv.list({ prefix: [TASK_PREFIX] }) and extract taskId and description from each entry.
Practice
(1/5)
1. What is the correct way to store a value in Deno KV?
easy
A. Use kv.set(["key"], value) to save data.
B. Use kv.save("key", value) to save data.
C. Use kv.put("key", value) to save data.
D. Use kv.store(["key"], value) to save data.
Solution
Step 1: Understand Deno KV key format
Deno KV uses arrays as keys, so keys must be passed as arrays like ["key"].
Step 2: Identify the correct method to save data
The method to save data is set, so kv.set(["key"], value) is correct.
Final Answer:
Use kv.set(["key"], value) to save data. -> Option A
Quick Check:
Key as array + set method = Use kv.set(["key"], value) to save data. [OK]
Hint: Remember: keys are arrays and method is set [OK]
Common Mistakes:
Using string keys instead of array keys
Using wrong method names like save or put
Passing key as a single string instead of array
2. Which of the following is the correct syntax to read a value from Deno KV?
easy
A. const result = kv.get(["key"]);
B. const result = await kv.get("key");
C. const result = await kv.get(["key"]);
D. const result = kv.get("key");
Solution
Step 1: Recognize key format for get method
Deno KV requires keys as arrays, so the key must be passed as ["key"].
Step 2: Confirm asynchronous usage of get
The get method is asynchronous, so it must be awaited.
Final Answer:
const result = await kv.get(["key"]); -> Option C
Quick Check:
Await + array key + get method = const result = await kv.get(["key"]); [OK]
A. The key in set should be an array, not a string.
B. The get method should not be awaited.
C. The value "active" must be an object, not a string.
D. The console.log should print status directly, not status.value.
Solution
Step 1: Check key format in set method
The set method requires the key to be an array, but here it is a string "session".
Step 2: Confirm correct key format for get method
The get method uses the key as an array ["session"], which is correct.
Final Answer:
The key in set should be an array, not a string. -> Option A
Quick Check:
Key must be array in set and get [OK]
Hint: Keys must always be arrays in set and get [OK]
Common Mistakes:
Using string keys instead of array keys
Not awaiting async methods
Misunderstanding value access from get result
5. You want to store user preferences with keys like ["user", userId, "prefs"] and values as objects. Which code correctly updates preferences for userId 42 without overwriting other keys?