Skip to main content

What is DataLoader?

DataLoader is a utility built into Orionjs MongoDB collections that helps solve the N+1 query problem and optimize database access patterns. It provides:
  • Batching: Combines multiple individual requests into a single database query
  • Caching: Avoids duplicate queries by caching results for the duration of a request
  • Consistent API: Simple methods for various loading patterns

When to Use DataLoader

DataLoader is particularly useful in these scenarios:
  1. GraphQL Resolvers: Where many child resolvers may request the same data
  2. Nested API Endpoints: When processing lists of related data
  3. Heavy Read Operations: For optimizing repeated reads on the same dataset

Available DataLoader Methods

Orionjs MongoDB collections include four main DataLoader methods:

loadById

Loads a single document by its ID with DataLoader caching and batching.

loadOne

Loads a single document by any field with DataLoader caching and batching.

loadMany

Loads multiple documents by field values with DataLoader batching.

loadData

The most flexible DataLoader method that powers the other methods.

How DataLoader Works

Behind the scenes, Orionjs uses the Facebook DataLoader library to implement efficient data loading:
  1. When you call a DataLoader method, it registers your request in a batch
  2. After a short timeout (default: 5ms), all batched requests are combined into a single MongoDB query
  3. Results are distributed to the appropriate requesters
  4. Results are cached in memory for the duration of the current execution context

Best Practices

  • Use loadById for ID-based queries: It’s optimized for the common case of loading by ID
  • Batch related queries: Group related data loading operations close together in your code
  • Use appropriate timeouts: Adjust the timeout parameter based on your application’s needs
  • Add projections: Use the project parameter to request only the fields you need
  • Be careful with mutations: After updating a document, the cached version may be stale

Example: Solving N+1 Query Problem

Performance Considerations

  • DataLoader adds a small overhead for the batching timeout
  • For single queries that won’t be repeated, use regular MongoDB operations
  • The caching benefits are most pronounced in request-scoped operations
  • DataLoader’s cache is cleared between requests, preventing stale data