@philiprehberger/ts-async-batcher
v0.1.4
Published
Automatic batching and deduplication for async operations
Readme
@philiprehberger/ts-async-batcher
Automatic batching and deduplication for async operations
Installation
npm install @philiprehberger/ts-async-batcherUsage
import { createBatcher } from '@philiprehberger/ts-async-batcher';
const userLoader = createBatcher(async (ids: string[]) => {
// Called once with all collected IDs
return db.users.findMany({ where: { id: { in: ids } } });
});
// These are automatically batched into a single call
const user1 = await userLoader.load('user-1');
const user2 = await userLoader.load('user-2');
// Load multiple at once
const users = await userLoader.loadMany(['user-3', 'user-4']);Options
const loader = createBatcher(batchFn, {
maxBatchSize: 100, // Flush when batch reaches this size (default: 100)
windowMs: 10, // Batch window in milliseconds (default: 10)
});How It Works
load(key)adds the key to an internal queue and returns a promise- After
windowMsor whenmaxBatchSizeis reached, the queue is flushed - Duplicate keys within a batch window are deduplicated — the batch function receives unique keys only
- Results are matched back to callers by index (batch function must return results in the same order as keys)
API
| Export | Description |
|--------|-------------|
| createBatcher(batchFn, options?) | Create a new batcher instance |
Batcher<K, V>
| Method | Description |
|--------|-------------|
| load(key) | Load a single value, batched automatically |
| loadMany(keys) | Load multiple values, returns Promise<V[]> |
BatcherOptions
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| maxBatchSize | number | 100 | Max keys per batch |
| windowMs | number | 10 | Batch collection window in ms |
Development
npm install
npm run build
npm testLicense
MIT
