BatchLoader
BatchLoader is a batching utility for data fetching layer to reduce requests round trips, inspired by Facebook's DataLoader, written in TypeScript.
BatchLoader is a simplified version of Facebook's DataLoader and can be used with any database such as MongoDB or with GraphQL.
Comparison to DataLoader
+ written in TypeScript
+ Further reduces data fetching requests by filtering out duplicate keys
+ Similar api as DataLoader
+ Smaller in size (0 dependencies)
+ MappedBatchLoader can be used to compose a new loader using existing loaders.
- Removed caching functionalities. Leave caching to better cache libraries.
It is a very simple batcher that only does batching, and it does it very well.
Getting Started
First, install BatchLoader using npm.
npm install --save batchloader
or with Yarn,
yarn add batchloader
Note: BatchLoader assumes a JavaScript environment with global ES6 Promise
, available in all supported versions of Node.js.
Batching
Create loaders by providing a batch loading function and key transformation function (used for finding duplicate keys).
import { BatchLoader } from 'batchloader';
const userLoader = new BatchLoader(
(_ids: ObjectId[]) => User.getByIds(_ids),
(_id: ObjectId) => _id.toString(),
100
);
const user1 = await userLoader.load(id1);
const [user1, user2] = await userLoader.loadMany([id1, id2]);
const [user1, user1, user1] = await userLoader.loadMany([id1, id1, id1]);
const [user1, user2, user3, user2, user1] = await Promise.all([
userLoader.load(id1),
userLoader.load(id2),
userLoader.load(id3),
userLoader.load(id2),
userLoader.load(id1),
]);
Batch Function
A batch loading function must be of the following type:
(keys: Key[]) => Value[] | Promise<Value[]>
Constraints
- keys.length === values.length
- keys[i] => values[i]
- keys.length > 0
KeyToUniqueId Function
A function must return string value given a key:
(key: Key) => string
If key is not uniquely identifiable, simply pass null
instead. This will disable filtering out duplicate keys, and still work the same way.
const loader = new BatchLoader(
(keys: Key[]) => loadValues(keys),
null
);
const v1 = await loader.load(k1);
const [v1, v2, v1] = await loader.loadMany([k1, k2, k1]);
MappedBatchLoader
You can map a loader to create another loader.
import { MappedBatchLoader } from 'batchloader';
const getUsername = (user) => user && user.username;
const usernameLoader = userLoader.mapLoader(getUsername);
const usernameLoader = new MappedBatchLoader(userLoader, getUsername);
const username = await usernameLoader.load(userId);
const [username1, username2] = await usernameLoader.loadMany([userId1, userId2]);
const [user1, username1] = await Promise.all([
userLoader.load(id1),
usernameLoader.load(id1),
])
const anotherMappedLoader = usernameLoader.mapLoader(mapFn);
const anotherMappedLoader = new MappedBatchLoader(usernameLoader, mapFn);
Caching
Unlike DataLoader, BatchLoader does not do any caching.
This is intentional, because you may want to use your favorite cache library that is best suited for your own use case.
You can add caching ability easily like so:
let userCache = {};
const cachedUserLoader = new BatchLoader(
async (ids) => {
const uncachedIds = ids.filter(id => !userCache[id]);
const users = await getUsersByIds(uncachedIds);
uncachedIds.forEach((id, i) => { userCache[id] = users[i]; });
return ids.map(id => userCache[id]);
},
...
);
delete userCache[id1];
userCache[id2] = user2;
userCache = {};
Choose whatever caching library you like and simply add it like above.