Registry / database / redis-dataloader

redis-dataloader

JSON →
library1.0.2jsnpmunverified

Redis Dataloader wraps Facebook's DataLoader to add Redis as a distributed caching layer, enabling sharing of cache across processes/servers. Current stable version 1.0.2 (last released Nov 2019, low maintenance). Requires both Redis client (node_redis or ioredis) and DataLoader itself. Key differentiator from standard DataLoader: cross-process caching via Redis, TTL support, and async clear/prime methods. Designed for GraphQL backends needing horizontal scalability.

npm install redis-dataloader
INSTALL
IMPORT
SIG · REDIS-DATALOADER
R
redis-dataloader
databasejavascriptv1.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RedisDataLoader
import RedisDataLoader from 'redis-dataloader'
const RedisDataLoader = require('redis-dataloader')
Package exports a factory function; ESM import is default import. CommonJS require works too.
redis
import redis from 'redis'; const client = redis.createClient()
import { RedisClient } from 'redis'
The redis package exports a function, not a class. Use default import.
DataLoader
import DataLoader from 'dataloader'
import { DataLoader } from 'dataloader'
DataLoader uses a default export.

Complete setup: create Redis client, factory-import RedisDataLoader, create base DataLoader, then RedisDataLoader instance with prefix, batch loader, and options including TTL (expire). Demonstrates load, clear (async), and local-only clear operations.

import { createClient } from 'redis'; import DataLoader from 'dataloader'; import RedisDataLoader from 'redis-dataloader'; // Initialize Redis client (only one needed, use node_redis or ioredis) const redisClient = createClient({ url: process.env.REDIS_URL ?? 'redis://localhost:6379' }); await redisClient.connect(); // Factory function: pass redis client const RedisDataLoaderFactory = RedisDataLoader({ redis: redisClient }); // Batch function (simulate DB fetch) const batchFn = async (keys) => keys.map(k => ({ id: k, name: `Item ${k}` })); // Create a standard DataLoader with cache disabled const baseLoader = new DataLoader(batchFn, { cache: false }); // Create RedisDataLoader with prefix 'myapp', base loader, and options const loader = new RedisDataLoaderFactory('myapp', baseLoader, { cache: true, expire: 3600 }); // Load a value (first fetches from Redis, then from batchFn, caches both locally and in Redis) const result = await loader.load(5); console.log(result); // { id: 5, name: 'Item 5' } // Clear a specific key from local and Redis cache await loader.clear(5); // Clear only local cache (not Redis) loader.clearLocal(5); // Clear all local cache loader.clearAllLocal(); await redisClient.disconnect();
Debug
Known issues
breakingclear() returns a Promise (unlike Facebook DataLoader's synchronous clear).
fix
Always await the result of clear(): await loader.clear(key);
affects: >=0.0.0
breakingclearAll() is not available. Redis does not support efficient key-scanning for prefix-based delete.
fix
Use a Redis SCAN or FLUSHDB manually, or implement custom batch clear.
affects: >=0.0.0
breakingprime() always overwrites cache (unlike DataLoader which skips if key exists). Returns a Promise.
fix
Use prime() only when you intend to force-update the cache; otherwise avoid.
affects: >=0.0.0
gotchaRedis-stored values must be JSON or null. Non-serializable types (e.g., Date) need custom serialize/deserialize options.
fix
Provide serialize/deserialize in options to handle Date or other types.
affects: >=0.0.0
gotchaMust disable cache on the inner DataLoader (pass { cache: false }) to avoid double caching conflicts.
fix
Always pass { cache: false } to the base DataLoader.
affects: >=0.0.0
Errors
Common errors & fixes
RedisDataLoader is not a constructor
Calling require('redis-dataloader') directly without invoking the factory function with a redis client.
fix
Use const RedisDataLoader = require('redis-dataloader')({ redis: redisClient });
ERR Error: clearAll is not a function
The RedisDataLoader instance does not have clearAll method.
fix
Clear all keys with a Redis scan + del, or use clearAllLocal() for local cache only.
TypeError: loader.load is not a function
Factory function not invoked properly, resulting in undefined or wrong object.
fix
Ensure RedisDataLoader is called with { redis: client }, then construct new loader with prefix, baseLoader, options.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
dataloaderrequiredCore batching/caching logic from Facebook's DataLoader
redisrequiredRedis client for caching; can use ioredis instead
Agent activity
10 hits · last 30 days
node
8
Meta
1
Resources
redis-dataloader — npm install redis-dataloader · libregistry