Hello guys, have you been in a situation that looks like you are making tons of requests during data fetching that you shouldn’t be making(unnecessary)? Not only does this increase your database traffic and load but it also slows down data fetching in a situation where lots of clients or users are making lots of requests.
Also do note that the DataLoader utility does not replace shared application-level cache. so the caching feature is only available on the current operating server.
What the DataLoader does is that it only serves as a data loading and caching mechanism that prevents you from loading the same information from your database twice in a single request.
So let’s say in an area of your application you obtained this record from your database:-
Let us call this object “USER”. Also do note that this explanation is based on the context of a single request. So after obtaining this user by the index, and we want to obtain the gender of the same user at another location of our application, instead of making another request to your database to obtain the info we can just push the initial user information into the data loader and obtain the gender of the same user without making a round trip to the database thereby reducing latency or database traffic
So let us get into how the database loader works. The database loader works by using a batch loading function that accepts an array of keys and returns a Promise which resolves to an array of values that are cached in it.
So let us take a look at an example:-
- run “npm init -y ” to set up your package.json file
- We would need the dataloader library, so run “npm i dataloader”
- In this example, I would be using a JSON object as a database but would use a timer to create a mockup request
- Create a file called db.js and add this to it:-
- Next, create your index.js file
- And add this to call in your data loader module and JSON database
Before we proceed this is what a basic batch function looks like:-
- also do note that the length of values returned must be equal to the number of keys sent to the batch function. For responses not found, try to substitute it with an identifier denoting that the response for a certain key is vacant. For instance, I chose to use ‘not found in DB’ in case the value of a certain key isn’t found.
My batch function was designed to mimic a request made to a DB, due to that, I had to introduce a timeout that resolves the values from the DB when ready. Add this block of code:-
- I set a timeout of 5 seconds so that you can verify the benefit associated with using the data loader.
- Next would be a function that creates an object with a user key that returns a Dataloader taking the batchFunction as an argument.
- Add this line:-
Next, let’s call the function to return the object. Add this: –
Finally, let us test our data loader. Since the data loader returns a promise. we would have to wait for it to resolve.
The loadMany method below allows you to input multiple keys while the load method only allows a single key
So add this asynchronous block of code to it.
Once you run the code you would see that for the next request for user(1) data would be instant rather than making another round trip.
You can check out the docs here to see available APIs.
And here is the repo to the example. You can leave a comment if you have any corrections or contributions to make. Thanks