Registry / database / dldr
library1.0.1jsnpmunverified

dldr (pronounced "dataloader") is a minimalist JavaScript utility, currently at version 0.0.10, designed for efficiently batching and caching operations. It is particularly useful in data fetching scenarios, such as optimizing queries within GraphQL resolvers. The library distinguishes itself by its extremely small footprint (367B gzipped) and its use of `queueMicrotask` to schedule and execute batched load functions within the current event loop tick. This mechanism ensures that multiple requests for the same or different keys, made in quick succession within the same microtask queue, are consolidated into a single call to the underlying data fetching function. dldr offers both a basic batching mechanism and an extended version accessible via `dldr/cache` that incorporates an in-memory `Map`-based cache, preventing redundant data fetches for previously loaded keys. Its primary goal is to improve performance by reducing the number of requests to databases or APIs, positioning it as a lightweight alternative to more feature-rich dataloading solutions. While in early development, its API is straightforward, centered around `load` functions that accept an array of keys and return corresponding results.

npm install dldr
INSTALL
IMPORT
SIG · DLDR
D
dldr
databasejavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

load
import { load } from 'dldr';
const load = require('dldr').load;
This is the primary named export for batching operations. It processes all queued calls within the current microtask.
load
import { load } from 'dldr/cache';
const load = require('dldr/cache').load;
This variant of the `load` function includes an in-memory cache. It accepts an optional `Map` instance for caching results by key, preventing redundant fetches for already loaded data.

This example demonstrates how to use `dldr` to batch multiple data fetching calls into a single underlying function execution and shows the `load.bind` pattern for convenience.

import { load } from 'dldr'; // Mock a simple database interaction const mockDb = { posts: new Map([ ['123', { id: '123', name: 'Post One' }], ['456', { id: '456', name: 'Post Two' }], ['789', { id: '789', name: 'Post Three' }] ]), // Simulates a database call that takes an array of keys execute: async (query: string, keys: string[]): Promise<Array<{ id: string, name: string }>> => { console.log(`[DB] Executing query for keys: ${keys.join(', ')}`); // Simulate network delay await new Promise(resolve => setTimeout(resolve, 50)); return keys.map(key => mockDb.posts.get(key) || { id: key, name: 'Unknown Post' }); } }; // Define the core data loading function that dldr will batch const getPosts = async (keys: string[]): Promise<Array<{ id: string, name: string }>> => { // In a real application, this would be a single database call // that fetches multiple records based on the provided keys. return mockDb.execute('SELECT id, name FROM posts WHERE id IN (?)', keys); }; async function main() { console.log('Demonstrating batching with dldr:\n'); // Request multiple posts concurrently. dldr will batch these into a single call to `getPosts`. const post123Promise = load(getPosts, '123'); const post456Promise = load(getPosts, '456'); // Even if requested again, it's still part of the same batch operation if in the same tick. const post123AgainPromise = load(getPosts, '123'); // Add another request later in the same event loop tick (e.g., from another resolver) // This will still be part of the initial batch await Promise.resolve(); // Ensures other microtasks run, still within the same tick conceptually const post789Promise = load(getPosts, '789'); const loadedPosts = await Promise.all([ post123Promise, post123AgainPromise, post456Promise, post789Promise ]); console.log('\nResults from batched load:'); console.log(loadedPosts); console.log('\nDemonstrating `load.bind` for convenience:'); const loadPost = load.bind(null, getPosts); const boundPostPromise = loadPost('123'); const anotherBoundPostPromise = loadPost('456'); const boundLoadedPosts = await Promise.all([boundPostPromise, anotherBoundPostPromise]); console.log(boundLoadedPosts); } main().catch(console.error);
Debug
Known issues
gotchadldr batches operations within the current microtask queue. Requests made across different event loop ticks (e.g., separated by `setTimeout` or `setImmediate` calls without an intervening microtask) will not be batched together, leading to multiple calls to your underlying `loadFn`.
fix
Ensure all batchable `load` calls are initiated within the same synchronous execution context or within the same microtask queue phase.
affects: >=0.0.1
breakingAs dldr is in an early development stage (version 0.0.10), its API surface may undergo changes without adhering to strict semantic versioning. Early minor or patch releases could potentially introduce breaking changes.
fix
Pin to exact versions (`"dldr": "0.0.10"`) and carefully review the GitHub repository for changes when upgrading, particularly if new versions are released.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: loadFn is not a function
The first argument passed to `load` was not a function, or was null/undefined.
fix
Ensure the first argument to `load` (your data fetching function) is a valid, callable function that accepts an array of keys.
TypeError: Cannot read properties of undefined (reading 'bind')
Attempting to use `load.bind` when `load` itself is not correctly imported or is undefined.
fix
Verify that `import { load } from 'dldr';` or `import { load } from 'dldr/cache';` is correctly specified and executed before calling `load.bind`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
dldr — npm install dldr · libregistry