Skip to content

Conversation

@tranhl
Copy link

@tranhl tranhl commented May 17, 2025

This PR adds a "Batching" page to the "Examples" section on the documentation website.


### Multiple Tables & Entities

### Handling `BatchGetItem` Limits
Copy link
Author

Choose a reason for hiding this comment

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

The plan for this section is to have examples for handling partial responses via UnprocessedKeys and batch size edge cases (i.e. chunking requests to 100 items per request).

Copy link
Collaborator

@ThomasAribart ThomasAribart May 20, 2025

Choose a reason for hiding this comment

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

I think it's better to sprinkle limits inside the examples, or at least give a warning (with :::caution admonitions for instance) that points to that section. Otherwise some devs might just overlook it.

const pokemonIds = ['bulbasaur', 'charmander', 'squirtle']

// 👇 Map data into a collection of `BatchGetRequest` actions
const requests = pokemonIds.map((pokemonId) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nitpick but you can remove the bracket (and simply use pokemonId as key):

const requests = pokemonIds.map((pokemonId) =>
  PokemonEntity.build(BatchGetRequest).key({ pokemonId })
)

const { Responses } = await execute(command)

// 👇 Extract your data from the responses
const [pokemon] = Responses
Copy link
Collaborator

@ThomasAribart ThomasAribart May 20, 2025

Choose a reason for hiding this comment

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

Maybe this will be clearer?

// 👇 `Responses` is an array of size 1 as we only sent one `BatchGetCommand`
const [pokemons] = Responses

for (const pokemon of pokemons) {
  // ...do something with pokemon here
  // (note that pokemon might be `undefined`)
}

Copy link
Author

Choose a reason for hiding this comment

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

Yeah sure! I do think using .map() to iterate would translate better to real-world use cases though as internally we haven't come across a scenario where using a for of loop has been needed. I'm not too precious on syntax though, thoughts?

@ThomasAribart
Copy link
Collaborator

@tranhl Awesome! Did some minor feedbacks but it looks good to me, thanks a lot!

return pokemon.filter(item => item !== undefined)
```

### Single Table, Multiple Entities
Copy link
Author

Choose a reason for hiding this comment

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

@ThomasAribart It turns out we don't have a use case internally where we need to retrieve multiple entities in batch so I'm coming up with an example myself. Since we're limited to one BatchGetCommand per table, I tried passing multiple types of BatchGetRequests into a BatchGetCommand but ran into a ts(2859) Excessive complexity comparing types error. Any recommendations on how to best approach this use case?

Copy link
Author

Choose a reason for hiding this comment

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

I also wonder whether this use case is even worth including in the documentation 🤔? Batch retrieving multiple entities from a single table screams code smell to me in the context of single table design -- I'm struggling to think of a scenario where this would be useful/valid. Perhaps we should remove it?


### Multiple Tables & Entities

### Handling `BatchGetItem` Edge Cases
Copy link
Author

Choose a reason for hiding this comment

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

@ThomasAribart I've fleshed out the "Handling BatchGetItem Edge Cases" section, let me know what you think!

Comment on lines +123 to +125
#### Handling Empty Input Keys

TODO
Copy link
Author

Choose a reason for hiding this comment

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

Still thinking about how to best write this section. Also considering rolling it into the previous section as we've handled this internally with a wrapper function, but I'm not 100% happy with the solution.

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