Building Cancellable PostgreSQL Streams
Building a Cancellable PostgreSQL Streaming Capstone
Hello, stream capstone builder! 🧠In the previous lesson, you streamed fixture bytes through a fresh gzip transform to the native HTTP response and verified the encoded representation by decompressing it. This capstone preserves that byte-mode gzip boundary, but replaces the fixture-side source with an ordered PostgreSQL cursor and adds ownership, cancellation, persistence, and cleanup around the full operation.
The completed backend exposes two related endpoints:
The first streams NDJSON observations. The second returns JSON metadata about stream operations. Metadata is deliberately separate: it must never become another line in the streamed NDJSON body.
The completed request flow is:
Operation ownership
Preserve the Stream Boundaries
backend/src/domain/observation.ts defines the object contracts shared by the database cursor and existing transforms.
- A cursor row must provide
id,temperatureC, andsiteso it matchesObservation. TemperatureObservationretains the original observation fields and addstemperatureF.- These are JavaScript object chunks, not serialized JSON or compressed bytes.
backend/src/streams/observation-filter.ts keeps filtering in object mode.
- Both stream sides use object mode, so accepted rows remain objects.
- Calling
callback()without a chunk intentionally drops a row. - The route validates query values before passing them into this filter.
backend/src/streams/temperature-transform.ts maps accepted observations while preserving object mode.
toFixed(1)returns a string, soNumber(...)restores a numeric value.satisfies TemperatureObservationchecks the emitted shape without changing the runtime object.- Filtering happens before this mapper, so rejected rows never receive a Fahrenheit value.
