feat(client): add queryStream for chunked query results - #55
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #36.
What the live server actually does
The open questions in the issue were answered against
community-latest(2.4.3) before committing to a shape.chunked=truewithAccept: application/jsonis neither one JSON value nor NDJSON. It is successive JSON arrays written back to back with no separator, each about 500 rows:An empty result is an empty body. Invalid SQL still returns HTTP 422 before any array is sent, so a mid-stream failure after HTTP 200 is a transport problem rather than an application error. The same NULL-omission and alphabetical key sorting as buffered JSON apply inside each array. Recorded in
docs/compatibility.md.What shipped
client.queryStream(statement, options?)returns an async generator of rows. It setschunked=true, parses each complete array as bytes arrive, and yields its elements so memory stays proportional to one server batch.query()is unchanged and still buffers withchunked=false.Partial results and cancellation
If the stream fails after some rows have already been yielded, the iterator throws and those rows stay consumed — the caller can see it is a partial result. Breaking out of the loop, or aborting
options.signal, cancels the underlying reader so the connection is not left half-read. Retries (when configured) apply only to failures before the response body starts; a mid-stream failure cannot be retried without re-running the statement.Tests
Unit tests cover split chunks, brackets inside strings, empty bodies, truncated and malformed streams, early consumer exit, and client wiring (
chunked=trueonly on the stream path). Integration tests write 1,200 points (enough to span multiple 500-row batches), assert every value arrives exactly once, and confirm empty results and bad SQL behave as above.