0
0
NestJSframework~10 mins

DataLoader integration in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - DataLoader integration
Request received
Create DataLoader instance
Batch multiple requests
Send batched query to database
Receive batched results
Distribute results to individual requests
Return responses
Request complete
DataLoader batches multiple data requests into one query, then splits results back to each request.
Execution Sample
NestJS
const loader = new DataLoader(keys => batchLoadFn(keys));
const p1 = loader.load('id1');
const p2 = loader.load('id2');
const result1 = await p1;
const result2 = await p2;
// batchLoadFn called once with ['id1', 'id2']
This code creates a DataLoader, loads two keys, and batches them into one database call.
Execution Table
StepActionInput KeysBatch Function Called?Batch Function InputBatch Function OutputIndividual Result Returned
1Create DataLoader instance-No---
2Call loader.load('id1')['id1']No--Promise pending
3Call loader.load('id2')['id1','id2']No--Promise pending
4End of event loop tick triggers batch['id1','id2']Yes['id1','id2'][result1, result2]-
5Resolve promise for 'id1'-No--result1
6Resolve promise for 'id2'-No--result2
7All requests resolved-No---
💡 Batch function called once with all keys; individual promises resolved with corresponding results.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6Final
loader.pendingKeys[]['id1']['id1','id2'][][][]
loader.promises{}{id1: Promise}{id1: Promise, id2: Promise}{id1: Resolved, id2: Resolved}{id1: Resolved, id2: Resolved}{}
Key Moments - 2 Insights
Why does DataLoader wait before calling the batch function?
DataLoader waits until the end of the event loop tick to collect all load calls (see steps 2 and 3) so it can batch them together in one call (step 4).
How does DataLoader know which result belongs to which request?
The batch function returns results in the same order as the input keys (step 4), so DataLoader matches each result to the corresponding promise (steps 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the batch function called?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Check the 'Batch Function Called?' column in the execution table.
According to the variable tracker, what is the state of loader.pendingKeys after step 3?
A[]
B['id1']
C['id1','id2']
D['id2']
💡 Hint
Look at the 'loader.pendingKeys' row and the 'After Step 3' column.
If you call loader.load('id3') before the batch function runs, what happens to the batch input?
ABatch input remains ['id1','id2']
BBatch input includes ['id1','id2','id3']
CBatch input is only ['id3']
DBatch function is called twice
💡 Hint
DataLoader batches all keys requested in the same event loop tick (see concept flow).
Concept Snapshot
DataLoader integration in NestJS:
- Create a DataLoader instance with a batch function.
- Calls to load() queue keys during the event loop tick.
- Batch function runs once per tick with all keys.
- Results returned in order, matched to requests.
- Improves performance by reducing duplicate queries.
Full Transcript
DataLoader integration batches multiple data requests into a single database query. When a request comes in, a DataLoader instance is created. Calls to loader.load() queue keys but do not immediately call the batch function. At the end of the event loop tick, DataLoader calls the batch function once with all queued keys. The batch function returns results in the same order as keys. DataLoader then resolves each individual promise with its corresponding result. This reduces duplicate database queries and improves performance in NestJS applications.