シンプルなインタフェースでKVS使いたいときに活用ください
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
const kv = await Deno.openKv()
class localStorage {
static async getItem(key) {
const { value } = await kv.get([key])
return value
}
static async setItem(key, value) {
await kv.set([key], value)
}
static async removeItem(key) {
await kv.delete([key])
}
static async clear() {
for await (const { key } of kv.list({ prefix: [] })) await kv.delete(key)
}
}
serve(async (req: Request) => {
await localStorage.setItem('abc', 'xyz')
console.log(await localStorage.getItem('abc'))
await localStorage.removeItem('abc')
console.log(await localStorage.getItem('abc'))
await localStorage.setItem('abc', 'opq')
console.log(await localStorage.getItem('abc'))
await localStorage.clear()
console.log(await localStorage.getItem('abc'))
return new Response("Hello World")
});