batch
batch(options?: { size?: number; flushInterval?: Duration }): RouteBuilder<Current>
Process exchanges in batches instead of one at a time. Useful for bulk operations like database inserts or API batch requests.
craft()
.id('bulk-processor')
.batch({ size: 50, flushInterval: 5000 })
.from(timer({ interval: 1000 }))
.to(saveToDB)
Options:
size- Maximum exchanges per batch (default: 100)flushInterval- Maximum wait before flushing a partial batch (default: 5000ms)
Linting: route-level positioning
Use the ESLint rule @routecraft/routecraft/batch-before-from to ensure batch() is placed before .from(). See Linting Rules.
Incompatible with synchronous sources
The batch() operation only works with asynchronous message sources like timer(). It cannot be used with direct() sources because direct endpoints are synchronous and blocking -- each sender waits for the consumer to fully process a message before the next can be sent, preventing message accumulation.
It also cannot be combined with a route whose http() source declares respond: a batched message waits in the buffer instead of counting as in-flight work, so a graceful shutdown would discard it after the responder had already answered the sender. The pair is refused with RC5003 at subscribe.
If you need to combine multiple messages from split branches, use the aggregate() operation instead.