Skip to content

feat: Add async iterable support for paginating query results#564

Open
anatolzak wants to merge 3 commits intotywalch:masterfrom
anatolzak:add-async-iterable-pagination
Open

feat: Add async iterable support for paginating query results#564
anatolzak wants to merge 3 commits intotywalch:masterfrom
anatolzak:add-async-iterable-pagination

Conversation

@anatolzak
Copy link
Contributor

@anatolzak anatolzak commented Mar 21, 2026

closes #563

Summary

  • Introduces AsyncIterable support on .go() for query, scan, and collection operations, enabling for await...of iteration over pages of DynamoDB results
  • Each iteration yields a single page with its own { data, cursor }, allowing streaming processing, early termination via break, and lower memory usage for large result sets
  • Fully backward-compatible — .go() still works as a Promise via .then()/await; async iteration is opt-in
  • important: .go() execution is now lazy — the request to DynamoDB is deferred until the result is consumed via await, .then(), or for await...of

How it works

A new ElectroQueryResult class wraps query execution. It implements both PromiseLike (so await entity.query.go() works exactly as before) and AsyncIterable (so for await (const page of entity.query.go()) yields individual pages). Internally, executeQuery was refactored from collecting all pages into a single array to an async generator that yields each page as it arrives from DynamoDB.

Example

for await (const page of MallStores.query.leases({ mallId }).go({ pages: "all" })) {
  console.log(page.data);   // items for this page
  console.log(page.cursor);  // cursor for this page
  if (done) break;           // stop pagination early
}

@netlify
Copy link

netlify bot commented Mar 21, 2026

Deploy Preview for electrodb-dev ready!

Name Link
🔨 Latest commit fdd333a
🔍 Latest deploy log https://app.netlify.com/projects/electrodb-dev/deploys/69beee0fb982210008e7a05e
😎 Deploy Preview https://deploy-preview-564--electrodb-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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.

Add async iterable support for paginating query, scan, and collection results

1 participant