2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Deno KVでlocalStorageを実装する【Deno Deploy】

Posted at

シンプルなインタフェースで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")
});

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?