Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Add kv_namespaces in metadata #215

Merged
merged 1 commit into from
Jul 3, 2019

Conversation

xtuc
Copy link
Member

@xtuc xtuc commented Jun 5, 2019

Fixes #92

@xtuc xtuc requested a review from steveklabnik June 10, 2019 13:58
@xtuc
Copy link
Member Author

xtuc commented Jun 10, 2019

I'm not super happy but It kinda works, you need to specifiy the namespace_id in your wrangler.toml.

# ...

[[kv-namespaces]]
local_binding = "local_binding"
name = "name"
namespace_id = "abc"

@kristianfreeman
Copy link
Contributor

kristianfreeman commented Jun 14, 2019

Re: the namespace_id comment - I agree that having to specify it inside of wrangler.toml isn't great, but if users are creating the bindings from inside of a Wrangler-enabled project, can we write it to wrangler.toml for them on initial creation?

There may be situations where they'll need to go look up the namespace manually, but I imagine the average workflow will take place (hopefully!) in Wrangler, so we should know the namespace_id of a KV store since we're creating it for them during the publishing process.

(BTW - I am super motivated to help make this work land; it's a HUGE pain right now to have to keep the editor UI open right now to re-bind a KV namespace after each publish)

@steveklabnik
Copy link
Contributor

What is currently holding this PR back from landing?

@xtuc
Copy link
Member Author

xtuc commented Jun 20, 2019

@steveklabnik it would require a couple of rebases but I'm happy to see this landing. If you need it I can continue to work on it.

@steveklabnik
Copy link
Contributor

It'd be nice to get it landed so that we can continue the work on KV support overall; if you've got the time that would be great.

@ashleygwilliams ashleygwilliams added this to the 1.1.0 milestone Jun 20, 2019
@ashleygwilliams ashleygwilliams changed the title Add kv_namespaces in metadata [WIP] Add kv_namespaces in metadata Jun 20, 2019
@ashleymichal
Copy link
Contributor

I think what I'm missing to be able to judge this PR is the README section on how to configure your namespaces in the wrangler.toml; @xtuc touched on it in his comment but what would my wrangler.toml look like if I had two or more namespaces exposed to my worker?

@steveklabnik
Copy link
Contributor

@ashleymichal it currently looks like this:

kv-namespaces = ["STATIC_CONTENT"]

if you had two

kv-namespaces = ["STATIC_CONTENT", "FOOBAR"]

@ashleymichal
Copy link
Contributor

@steveklabnik based on the earlier comment from @xtuc here, that does not appear to be the case in the context of this PR; the user is required to specify not only the name of the binding as it should appear in the Worker, but the "title" and id of the namespace, in order to both create the namespace (if it does not already exist), and specify the binding in the metadata.

The question still stands, as once we provide any interface to users, it becomes difficult to change. In order to land this, we need to care about a few things:

  1. Providing enough information to allow Wrangler to provision a KV namespace on behalf of a user OR requiring the user to provision the namespace before integrating with their Worker project.
  2. Providing enough information for Wrangler to generate a KV Binding entry for the metadata part of the upload form. This can either be provided by the user manually (asking them to enter their namespace id manually as shown in @xtuc 's comment), or it can be retrieved by Wrangler behind the scenes with a query to the KV API based on the namespace title.
  3. Providing configuration of the relationship between the binding variable name and the namespace title. This can be done as it stands currently (by assuming they are one and the same and requiring they adhere to JavaScript variable name standards), or it can be done more manually as specified in @xtuc 's comment.

I think 1. is well handled here and lays a decent enough foundation for later improvements and features.

In the case of 2 and 3, I think it would be short-sighted to go with the seemingly "simpler" approach of requiring the user to only provide the namespace title. It is much easier in the long term to make those three connections explicitly and provide "sane defaults" later than to do the reverse. We should therefore take the time to consider what the entries should look like in the wrangler.toml now.

@ashleygwilliams
Copy link
Contributor

ashleygwilliams commented Jun 21, 2019

config wise- re multiple kv name spaces i'd probably do something like this... keying on "title".. i could also see us keying on local binding. i personally find the existence of both to be redundant (open to hear the motivation and have my mind changed, of course).

# Cargo.toml

[kv-namespaces]
title1 = { local_binding: "thing", id : "aksdhjfkahg;a"}

# or,  its equivalent

[kv-namespaces.title1]
local_binding = "thing"
id = "aksdhjfkahg;a"

collapsing the "local_binding" and "title" (assuming they obey the property with the most constraints constraints...) we could do this instead:

[kv-namespaces]
title1 = "aksdhjfkahg;a"
title2 = "<id string>"

this is preferrable IMO because it now looks like a list of dependencies! which is nice.

@ashleygwilliams
Copy link
Contributor

ashleygwilliams commented Jun 21, 2019

it should also be noted this is only for the webpack project type, and we have 3 types... choosing to release it for a single type should be a deliberate decision.

@steveklabnik
Copy link
Contributor

steveklabnik commented Jun 21, 2019 via email

@ashleygwilliams ashleygwilliams removed this from the 1.1.0 milestone Jun 24, 2019
@xtuc
Copy link
Member Author

xtuc commented Jun 25, 2019

Maybe we could iterate on this PR, to get something useful I would:

  • remove the auto creation of namespaces (because we don't store the id). The user/we should do it from the UI.
  • let the namespace_id, name and binding name be in the wrangler.toml

This will unlock our deployement and provide a base used for future iterations. We shouldn't document it for now tho. What do you think?

@cwndrws
Copy link

cwndrws commented Jun 27, 2019

I agree that the KV config should be in wrangler.toml. I like the idea of requiring all three values: (title, id, and binding) like @ashleymichal suggested. I think requiring title be a valid JS symbol would bite us eventually.

@xtuc xtuc force-pushed the feat-add-kv_namespaces-bindings branch from 55042eb to 0c3fef2 Compare July 1, 2019 08:19
@xtuc xtuc marked this pull request as ready for review July 1, 2019 08:19
@xtuc xtuc force-pushed the feat-add-kv_namespaces-bindings branch from 0c3fef2 to 34a6b22 Compare July 1, 2019 08:25
@xtuc
Copy link
Member Author

xtuc commented Jul 1, 2019

I made an update, mainly based on #215 (comment).

@cwndrws comments:

I agree that the KV config should be in wrangler.toml. I like the idea of requiring all three values: (title, id, and binding) like @ashleymichal suggested.

The kv-namespace configuration is only possible in wrangler.toml; I removed the auto-creation feature (for now).

The config looks like the following:

[[kv-namespaces]]
name = "KV_NAMESPACE_A"
id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

I'm not sure we should ask the user for the namespace name because we won't use it, the API doesn't support it. It would be more confusing if these names get out-of-sync and it would still work, until we add a check which would be a breaking change.

I think requiring title be a valid JS symbol would bite us eventually.

I like that idea actually, I updated #90 and we can discuss there.


Merge #285 first.

@cwndrws
Copy link

cwndrws commented Jul 1, 2019

@xtuc

I'm not sure we should ask the user for the namespace name because we won't use it, the API doesn't support it. It would be more confusing if these names get out-of-sync and it would still work, until we add a check which would be a breaking change.

I agree with not using the namespace name.

Your example config:

[[kv-namespaces]]
name = "KV_NAMESPACE_A"
id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

doesn't contain a binding symbol. Should it be:

[[kv-namespaces]]
name = "KV_NAMESPACE_A"
binding = "KV_NAMESPACE_A"
id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

Or something similar?

@xtuc
Copy link
Member Author

xtuc commented Jul 1, 2019

@cwndrws the name is the binding, I'm happy to rename it if it's confusing to you.

@xtuc

This comment has been minimized.

@ashleymichal
Copy link
Contributor

ashleymichal commented Jul 1, 2019

upon further reflection, i actually do think that long term this is the right way to specify these namespaces. the wrangler.toml for a workers project should regard kv namespaces as an external dependency, and the configuration of that dependency is a separate issue.

@cwndrws the name is the binding, I'm happy to rename it if it's confusing to you.

i too am struggling with the correct key for name but unless we can find something sufficiently descriptive we may need to rely on documentation. binding may be fine; varName is also maybe a thing?

@xtuc xtuc force-pushed the feat-add-kv_namespaces-bindings branch 4 times, most recently from 75a189b to dc90d8f Compare July 2, 2019 09:30
@xtuc xtuc changed the title [WIP] Add kv_namespaces in metadata Add kv_namespaces in metadata Jul 2, 2019
Allow the user to declare kv-namespaces in the configuration and writes
the binding to the worker metadata. Adds a new binding type `kv_namespace`.

Extends the Wrangler configuration to add:
    - `kv-namespaces`: Bind kv namespaces to your worker; an array of:
         - `binding`: name that will be used to bind the kv-namespace to your
            script, in JavaScript.
         - `id`: Identifer of the namespace.
For example:
```toml
[[kv-namespaces]]
binding = "KV_NAMESPACE_A"
id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

[[kv-namespaces]]
binding = "KV_NAMESPACE_B"
id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
```

The documentation (README.md) has been updated with this configuration change.

Fixes #92
@xtuc xtuc force-pushed the feat-add-kv_namespaces-bindings branch from dc90d8f to d7fbd78 Compare July 2, 2019 09:30
@xtuc
Copy link
Member Author

xtuc commented Jul 2, 2019

I noticed that when the namespace id is wrong, the publishing API returns a unclear error (generic 400, workers.api.error.internal_server); it would be great to change it.

@ashleymichal I did the updated.

This was referenced Jul 2, 2019
@xtuc xtuc changed the base branch from master to feat-better-kv July 2, 2019 17:09
@xtuc
Copy link
Member Author

xtuc commented Jul 3, 2019

Merging into feat-better-kv

@xtuc xtuc merged commit 35f7d0e into feat-better-kv Jul 3, 2019
@delete-merged-branch delete-merged-branch bot deleted the feat-add-kv_namespaces-bindings branch July 3, 2019 13:21
@juancampuzano
Copy link

juancampuzano commented Jul 15, 2019

Is it possible to make kv-namespaces in a production project that uses wrangler or is it a feature in development?

Currently I do not see the documentation of this in the master.

Screen Shot 2019-07-15 at 11 51 38 AM

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants