1.0.3 • Published 5 years ago

cf-kv-crud v1.0.3

Weekly downloads
4
License
ISC
Repository
github
Last release
5 years ago

Cloudflare KV CRUD Service

An CRUD abstraction on Cloudflare KV for dealing with objects

This module is very naive and should only be used for small scale apps.

Install

yarn add cf-kv-crud

Usage

Ensure you've created a namespace and binding.

Create a types file with the following contents:

types.ts

import { KVNamespace } from '@cloudflare/workers-types'

declare global {
  // Whatever variable you bound your namespace to
  const myKvNamespaceBinding: KVNamespace
}

main.ts

import { Router, getErrorPageHTML } from '8track'
import { KVCrudService } from 'cf-kv-crud'

const router = new Router()
const kvService

interface User {
  // Objects going into the CRUD service must have an ID
  id: string
  name: string
}

router.get`/api/users`.handle(async ctx => {
  const usersService = new KVCrudService<User>({
    kv: myKvNamespaceBinding,
    objectPrefix: 'users',
  })

  const users = await usersService.list()

  return ctx.json(users)
})

addEventListener('fetch', e => {
  const res = router.getResponseForRequest(e.request).catch(
    error =>
      new Response(getErrorPageHTML(e.request, error), {
        status: 500,
        headers: {
          'Content-Type': 'text/html',
        },
      }),
  )

  e.respondWith(res as any)
})

API