> For the complete documentation index, see [llms.txt](https://www.health-samurai.io/docs/interbox/llms.txt).
> Use it to discover all available pages before guessing URLs.

---
# Chaining pipelines with bridges

A pipeline has one store, so work in two stages is two pipelines joined by a
bridge: the first ends in `.sender(intoPipeline("b"))`, and the second starts
with `.source(fromPipeline("a"))`.

```ts
import { env } from "@health-samurai/interbox";
import { aidbox, fhirItems, skipUnchanged } from "@health-samurai/interbox/aidbox";
import { fromPipeline, intoPipeline, pipeline, postgresStore, webhookSource } from "@health-samurai/interbox/chain";
import type { Resource } from "@health-samurai/interbox/fhir";

pipeline("bulk-files")
  .source(webhookSource({ path: "/bulk/files" }))
  .store(postgresStore({ codec: "text" }))
  .step((files) => files.flatMap((file) => file.split("\n").filter((line) => line.trim() !== "")))
  .sender(intoPipeline("bulk-resources"));

pipeline("bulk-resources")
  .source(fromPipeline<string>("bulk-files"))
  .store(postgresStore({ codec: "text" }))
  .step((lines) => lines.map((line): Resource => JSON.parse(line)))
  .step(fhirItems())
  .step(skipUnchanged())
  .sender(aidbox({ url: env("AIDBOX_URL"), auth: { kind: "bearer", token: env("AIDBOX_TOKEN") } }));
```

Here an NDJSON file of FHIR resources is kept whole in the first store, and
each line becomes a unit of the second pipeline, so a bad line fails and is
retried on its own.

- **Each item of the first pipeline becomes a unit of the second.** When a unit
  of `bulk-files` finishes its steps, its items are written into the store of
  `bulk-resources` as new `received` units, and the unit of `bulk-files`
  becomes `sent`. Each item's value must fit the second store's codec.
  If one does not, the first pipeline's unit ends at `error` with
  `engine/codec_mismatch`.
- **The second pipeline has no steps before its store.** Nothing could run
  them: the bridge writes straight into the store, so the builder after
  `fromPipeline` offers only `.store()` and `.webhook()`.
- **The new units start clean, with a link back.** They carry no attributes of
  the first unit, but keep its event time (`order_at`), the id of the unit that
  created them, and its correlation id, which names the unit the work started
  from.
- **Both ends must agree.** Loading fails unless `intoPipeline("b")` in `a` is
  matched by `fromPipeline("a")` in `b`. The row type written in
  `fromPipeline<Row>` is not checked against what the first pipeline sends; the
  codec is checked when the units are written.
- **Size.** A unit may fan out into hundreds of thousands of items. Its steps
  hold them all in memory until they end, and the take must finish within its
  [deadline](../concepts/systems.md#the-takes-deadline). A step after
  the store of a `read: "delivery"` source streams its outputs instead.
- **Bridge publication waits for older committed work in the first pipeline.** Later outputs may
  be staged until earlier mappings finish. This covers pending work, not late
  arrivals or already delivered versions. See [ordering limits](../concepts/stages.md#senders).

See [Correlation and lineage](../concepts/units-and-state.md#correlation-and-lineage) for the ids a bridge carries from unit to unit.
