Saltar a contenido

dorm.contrib.streaming

Bytes-yielding streaming serialisers (JSON / JSONL / CSV). Memory-bounded; framework-agnostic — use with FastAPI's StreamingResponse, Starlette, aiohttp, etc.

API

dorm.contrib.streaming.stream_json(source: Any, *, chunk_size: int = 1000, mask_pii: bool = False) -> Iterator[bytes]

Yield bytes of a single [{...}, {...}] JSON array.

Suitable for clients that expect a JSON document; the encoded bytes are streamed comma-separated so memory stays flat.

Parameters:

Name Type Description Default
source Any

a :class:~dorm.QuerySet, a Manager, or any iterable of dict-/Model-shaped rows.

required
chunk_size int

rows per round-trip when source exposes iterator().

1000
mask_pii bool

when True, fields flagged pii=True on the source model are replaced with the masked sentinel before serialisation (uses :func:dorm.contrib.pii.mask_dict). Silently no-op for sources that don't carry a model (model attribute missing).

False

dorm.contrib.streaming.stream_jsonl(source: Any, *, chunk_size: int = 1000, mask_pii: bool = False) -> Iterator[bytes]

Yield newline-delimited JSON. One full record per line.

See :func:stream_json for the mask_pii semantics.

dorm.contrib.streaming.stream_csv(source: Any, *, chunk_size: int = 1000, columns: list[str] | None = None) -> Iterator[bytes]

Yield RFC-4180 CSV bytes. The header row is built from columns when provided, otherwise from the first row's keys.

Subsequent rows that lack a column emit an empty cell; extra keys are dropped (CSV's grid shape is non-negotiable).

dorm.contrib.streaming.stream_ndjson_pretty(source: Any, *, chunk_size: int = 1000, mask_pii: bool = False) -> Iterator[bytes]

Yield indented JSON records, one record per multi-line block.

Use for less-grade human inspection only — the format is not a valid JSONL document for downstream parsers.

dorm.contrib.streaming.astream_json(source: Any, *, chunk_size: int = 1000, mask_pii: bool = False) -> AsyncIterator[bytes] async

Async [{...}, {...}] JSON stream.

See :func:stream_json for the mask_pii semantics.

dorm.contrib.streaming.astream_jsonl(source: Any, *, chunk_size: int = 1000, mask_pii: bool = False) -> AsyncIterator[bytes] async

dorm.contrib.streaming.astream_csv(source: Any, *, chunk_size: int = 1000, columns: list[str] | None = None) -> AsyncIterator[bytes] async

Async CSV stream. See :func:stream_csv for the contract.