cfworker icon indicating copy to clipboard operation
cfworker copied to clipboard

Support for KV Binding

Open equt opened this issue 4 years ago • 4 comments

The #54 PR seems to add support for local KV testing and KV items uploading, while I didn't find a way setting KV bindings (like in wrangler.toml) using @cfworker/dev. It would be great to have such a feature.

Also, lots of thanks for maintaining these great packages. Packages like dev and web just work like a charm.

equt avatar Jul 01 '21 09:07 equt

hey @equt - there's a --kv flag you can use to bind a json file to a kv namespace. Apologies, there's no docs for this yet. Here's how it works:

Assuming you have the following files:

/abc/xyz/GREETINGS.json

[
  { "key": "hello", "value": "world", "base64": false }
]

/worker.js

addEventListener('fetch', async e => {
  const body = await GREETINGS.get('hello', 'text');
  e.respondWith(new Response(body));
});

Then the following command should work:

cfworker run worker.js --kv abc/xyz/GREETINGS.json

More info:

  1. cfworker uses the basename of the json file as the KV namespace.

  2. You can use the --kv parameter multiple times if you need to bind multiple namespaces:

    cfworker run worker.js --kv namspace1.json --kv namespace2.json
    
  3. Changes to the namespace by the worker script via the put and delete methods are not persisted to the file.

  4. The --watch parameter will also cause the --kv files to be watched. *soon- there's a bug: #137

  5. The base64 property isn't required in the JSON, it's included in case you need to seed the namespace with some binary data.

  6. The runtime implementation your worker will be interacting with is here, most features are implemented, including expiration however the list method is not. Could be added.

jdanyow avatar Jul 12 '21 01:07 jdanyow

That was brilliant, and I find it way better than creating a preview_id. Also, the missing list method doesn't bother me since I barely use it.

But there seems to have no way of binding KV namespaces environment in production for now (like what wrangler does). I've done some quick research on Cloudflare API, maybe it's because wrangler used some private APIs?

equt avatar Jul 13 '21 08:07 equt

But there seems to have no way of binding KV namespaces environment in production for now (like what wrangler does). I've done some quick research on Cloudflare API, maybe it's because wrangler used some private APIs?

@equt is this to say while debugging locally with cfworker run you'd like to connect to production KV? Could probably build an alternate implementation of MemoryKVNamespace which uses the kv rest api. https://github.com/cfworker/cfworker/blob/main/packages/dev/src/cloudflare-api.js#L468

jdanyow avatar Jul 13 '21 18:07 jdanyow

Sorry for the unclear, it's to say that wrangler supports binding, so every time after deployment, I could see the namespaces automatically available to that specific worker in the Cloudflare dashboard like following.

worker settings

However, using the dev package's deployment command will lose the bindings, and break the worker. This is a pretty fundamental functionality, perhaps I just missed something in the documentation.

equt avatar Jul 14 '21 01:07 equt