clone

a daily shikaku puzzle for phones: one seeded puzzle per date, solved by dragging rectangles

main
rect.run / .github / skills / deno-deploy / references / DENO_KV.md
DENO_KV.md 3.3K rendered
1# Deno KV23## What is Deno KV?45Deno KV is a key-value database built into Deno. On Deno Deploy, it's a fast, globally distributed store that requires no setup or configuration.67## Quick Start89```typescript10// Open the KV store (auto-connects on Deno Deploy)11const kv = await Deno.openKv();1213// Store a value14await kv.set(["users", "alice"], { name: "Alice", email: "alice@example.com" });1516// Retrieve a value17const result = await kv.get(["users", "alice"]);18console.log(result.value); // { name: "Alice", email: "alice@example.com" }1920// Delete a value21await kv.delete(["users", "alice"]);22```2324## Keys2526Keys are arrays of "key parts" that form a hierarchy:2728```typescript29// Simple key30["settings"]3132// Hierarchical keys33["users", "alice"]34["users", "bob"]35["posts", "2024", "01", "my-post"]36```3738Key parts can be strings, numbers, booleans, Uint8Array, or bigints.3940## Basic Operations4142### Get4344```typescript45const result = await kv.get<User>(["users", "alice"]);46if (result.value) {47 console.log(result.value.name);48}49```5051### Set5253```typescript54await kv.set(["users", "alice"], { name: "Alice" });5556// With expiration (in milliseconds)57await kv.set(["sessions", sessionId], data, { expireIn: 3600000 }); // 1 hour58```5960### Delete6162```typescript63await kv.delete(["users", "alice"]);64```6566### List6768```typescript69// List all users70const users = kv.list<User>({ prefix: ["users"] });71for await (const entry of users) {72 console.log(entry.key, entry.value);73}7475// With limit76const firstTen = kv.list<User>({ prefix: ["users"] }, { limit: 10 });77```7879## Atomic Transactions8081Perform multiple operations atomically (all succeed or all fail):8283```typescript84// Transfer credits between users85const alice = await kv.get<number>(["credits", "alice"]);86const bob = await kv.get<number>(["credits", "bob"]);8788const result = await kv.atomic()89 .check(alice) // Ensure alice hasn't changed90 .check(bob) // Ensure bob hasn't changed91 .set(["credits", "alice"], alice.value! - 100)92 .set(["credits", "bob"], bob.value! + 100)93 .commit();9495if (!result.ok) {96 console.log("Transaction failed - data was modified");97}98```99100## Example: Request Counter101102```typescript103const kv = await Deno.openKv();104105Deno.serve(async () => {106 // Increment counter atomically107 const key = ["requests"];108109 let result = { ok: false };110 while (!result.ok) {111 const current = await kv.get<number>(key);112 const newCount = (current.value ?? 0) + 1;113114 result = await kv.atomic()115 .check(current)116 .set(key, newCount)117 .commit();118 }119120 const count = (await kv.get<number>(key)).value;121 return new Response(`Requests: ${count}`);122});123```124125## Data Location126127On Deno Deploy, KV data is replicated across at least three data centers in Northern Virginia (us-east-4). Cross-region replication is not currently available.128129## Local Development130131When running locally with `deno run`, KV data is stored in memory by default. For persistent local storage:132133```typescript134// Persist to a local file135const kv = await Deno.openKv("./my-database.sqlite");136```137138## Important Notes139140- **Deletion is permanent** - Deleting a KV instance removes all data with no recovery141- **Back up important data** before deleting instances142- **No cross-region replication** yet - data lives in us-east-4143144## Documentation145146- Deno KV on Deploy: https://docs.deno.com/deploy/reference/deno_kv/147- Deno KV API: https://docs.deno.com/api/deno/~/Deno.Kv