Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new option for bulk gets in KV #3628

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

teresalves
Copy link

@teresalves teresalves commented Feb 28, 2025

Creating a new part in the binding for new bulk gets, along with tests for regular gets and the bulk gets
We consider we have a POST endpoint in SGW and we send the keys in a json format as such:

{
   "keys": ["key1", "key2"]
}

However, we decided that the binding here will only be a passthrough and everything will processed in the endpoint. On top of that, since everything is processed in the binding and comes as a single response, we don't need extra logic to process metadata, we just need to make sure that the binding responds what we what. Because of these two things, we added two more parameters in the request body:

{
   "keys": ["key1", "key2"],
   "type": "text",
   "withMetadata": true
}

We added tests for:
Regular gets:

  • 400s
  • 500s
  • 200s with text, json, stream and arrayBuffer

Bulk Gets:

  • 500s
  • 200s with text and json, as these will be the only supported types for now.

Note that we do not test for 400s because we will not have them. If none of the keys exist, we return a 200 but with empty values. Example:

{
   "key1": "",
   "key2": "",
 }

@teresalves teresalves requested review from a team as code owners February 28, 2025 01:15
@teresalves teresalves requested review from jasnell and mar-cf February 28, 2025 01:15
Copy link

github-actions bot commented Feb 28, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@teresalves teresalves marked this pull request as draft February 28, 2025 01:15
@teresalves teresalves force-pushed the talves/bulk-gets-for-kv branch from 6e63cdc to 44f5025 Compare February 28, 2025 01:27
@teresalves
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Feb 28, 2025
@teresalves
Copy link
Author

recheck

Copy link
Contributor

@sesteves sesteves left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! Left some comments with suggestions to improve tests.

const decoder = new TextDecoder(); // UTF-8 by default
let r = "";
while (true) {
const { done, value } = await reader.read();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - any particular reason for doing this in stream, instead of just doing request.arrayBuffer()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried some other ways and it was not reading the stream properly. Both here and in the type "stream" test below.

} catch ({ name, message }){
// assert(message.includes("invalid")) // this message is not processed, should it?
assert.ok(true);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about adding the following test cases:

  1. a key is not found
  2. the array of keys is empty (or some of the key names are empty strings)
  3. fetching the maximum number of keys that we will allow

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. added
  2. added but I am returning an empty object and not throwing an error in that case - confirming that this is the intended behaviour and getting back to you
  3. have not added that one since that will be validated in the endpoint and not here. Should it be in both? I get it so, but wondering if it might be redundant

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed 2 to throw a 400

}
// requested type is invalid for bulk get
try{
var response = await env.KV.get(["key-not-json", "key2"], "arrayBuffer");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this even get compiled?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sending the type that is invalid here. But the format itself is acceptable, so it compiles but throws an error.

@teresalves teresalves force-pushed the talves/bulk-gets-for-kv branch from b44d460 to ff830c5 Compare February 28, 2025 14:02
@teresalves teresalves force-pushed the talves/bulk-gets-for-kv branch from ff830c5 to 39f063b Compare February 28, 2025 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants