2
0

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でキューを実装【Deno Deploy】

Last updated at Posted at 2023-05-11
# プッシュ
curl https://kvq.deno.dev/mykey -d '[{"a":"b"},"foo",null,42]'

# プル
curl https://kvq.deno.dev/mykey
{"a":"b"}
curl https://kvq.deno.dev/mykey
foo
curl https://kvq.deno.dev/mykey
null
curl https://kvq.deno.dev/mykey
42
curl https://kvq.deno.dev/mykey
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";

const kv = await Deno.openKv();

serve(async (req: Request) => {
    const pathname = new URL(req.url).pathname
    if (req.method === 'GET') {
        for await (const { key, value } of kv.list({ prefix: [pathname] }, { limit: 1 })) {
            kv.delete(key)
            return new Response(typeof value === 'object' ? JSON.stringify(value) : value, { headers: { "Access-Control-Allow-Origin": "*" } })
        }
    } else if (req.method === 'POST') {
        const now = Date.now()
        const entries = await req.json()
        const id = Math.random()
        for(var i=0; i<entries.length; i++) {
            kv.set([pathname, now, i, id], entries[i])
        }
        return new Response('201 Created', { status: 201, headers: { "Access-Control-Allow-Origin": "*" } })
    }
    return new Response('', { status: 404, headers: { "Access-Control-Allow-Origin": "*" } })
});
2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?