cloudflare / serverless-cloudflare-workers

Serverless provider plugin for Cloudflare Workers
BSD 3-Clause "New" or "Revised" License
175 stars 37 forks source link

KV Namespace Binding clarification #40

Closed cwndrws closed 1 year ago

cwndrws commented 5 years ago

It's not clear that when binding a KV namespace, the name is the value that's required as opposed to the ID. This compounded with the fact that if you give it a namespace name that doesn't exist, it silently creates a new namespace with that name. This led to confusion as the KV API was returning different data than my worker and not until I went into the UI did I realize that there were multiple namespace created, one with the ID of the other as its name.

saibotsivad commented 4 years ago

This was unclear to me as well, even reading your description 😅 so here's an example for anyone else confused.

If you have this in your YAML file:

functions:
  demo:
    name: my-demo
    script: handler
    resources:
      kv:
        - variable: CREDENTIALS
          namespace: FOO_CREDENTIALS

You will want this in your JS:

export async function handleRequest() {
    const credentials =await CREDENTIALS.get('example-secret')
cwndrws commented 4 years ago

The thing I was attempting to call out is that the namespace should be the name of the namespace and not the ID, which was not clear to me.