Skip to main content
Chapter 5 pushed a live trickle of events to observers. This chapter moves a large amount of data at once: a CSV of links, sent to one function as a stream. A stream is a value in a call’s payload. The call carries a small reference, and the SDK sends the bytes over a separate pipe while the function reads them, so the file never has to fit in one JSON message. You’ll give this its own bulk-importer worker so the link worker stays focused on single links.

Add the worker

Uncomment the Ch. 6 block in worker-compose.yaml:
worker-compose.yaml

Import a CSV from a stream

The bulk-importer worker exposes one function that receives the CSV as an InputStream, reads it line by line, and calls link::create from the link worker for each row. Create bulk-importer/src/index.ts:
bulk-importer/src/index.ts
Restart Compose so it picks up the change:

See it work

With the engine running, let’s bulk-load some links.
Unlike previous chapters this section and Chapter 7 require that you have node and npm installed locally. This is because we’re now creating client side code that runs outside of workers.

Upload a CSV

The uploader is a small standalone script that calls bulk-importer::import_csv with the CSV in its payload. InputStream.from(csv) marks the CSV as a stream: the SDK creates a pipe for it, puts a reference in the payload, and sends the bytes while the importer reads them. The call resolves with the importer’s answer. The script is not a worker, so channel-client/ holds it, outside worker-compose.yaml:
Write this into channel-client/import-links.js:
import-links.js
The SDK prints a few [iii] / [OTel] connection lines around that object; the { imported, skipped } line is the importer’s answer. Run it again and it reports { imported: 0, skipped: 2 }: the codes are already taken, so link::create throws its is already taken error for each row and the importer skips those rows instead of aborting the batch. The check reads link::create’s own message, which the chapter controls, not the database’s wording. To upload a file from disk instead, pass createReadStream("links.csv") from node:fs as csv: a Node Readable in a payload is a stream too. See Streaming for the other ways to send, return, and forward streams. Both new links resolve immediately:

Conclusion

Linkly can now ingest a file’s worth of links in a single streamed upload through a dedicated bulk-importer worker. Next, in Ch. 7: Bring in the browser, you turn a browser tab into a worker that creates links and subscribes to the live click stream.