You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm in minute 11:36:00 of the tutorial, and I noticed that findById was deprecated when implementing the createUserLoader function. I was able to get around it by doing the following:
As you can see I used findBy instead of findByIds and the In function provided by typeorm. I tried using the same approach for the createUpdootLoader, but I noticed that it did not work since I needed to search for both the userId and the postId to retrieve the corresponding upvotes. Was anyone able to implement that correctly? I am unsure how to use findBy to search multiple fields with the keys since I cannot seem to separate the key fields by using keys.userId and keys.postId. This is my current code:
Hey @csandoval18 , here's how I was able to solve it, though I'm not 100% sure it's the right approach.
Basically, I map over the list of key-value objects to extract the userIds and postIds into separate arrays. Then apply them as separate search criteria in the findBy function. After that, I generate an updootToIdMap with the key as a string of format: <postId>|<userId> and the value as a returned updoot. Finally, I return the array of updoots in the same order the initial object array idObjArr was provided.
Hope this helps!
importDataLoaderfrom"dataloader";import{In}from"typeorm";import{Updoot}from"../entities/Updoot";// A data loader takes a list of keys (in this instance, a list of objects of keys),// and returns a list of corresponding entities - updoots | null in the same order as the given keys.// Data loaders batch and cache database requests to avoid the N + 1 problem of// fetching N requests for a single db queryexportconstcreateUpdootLoader=()=>newDataLoader<{userId: number,postId: number},Updoot|null>(async(idObjArr)=>{constuserIds=idObjArr.map(obj=>obj.userId)constpostIds=idObjArr.map(obj=>obj.postId)constupdoots=awaitUpdoot.findBy({userId: In(userIds),postId: In(postIds)})constupdootToIdMap: Record<string,Updoot>={}updoots.forEach(updoot=>{updootToIdMap[`${updoot.postId}|${updoot.userId}`]=updoot})returnidObjArr.map(obj=>{returnupdootToIdMap[`${obj.postId}|${obj.userId}`];})})
This is the SQL that was generated when the loader is executed.
I'm in minute 11:36:00 of the tutorial, and I noticed that findById was deprecated when implementing the createUserLoader function. I was able to get around it by doing the following:
As you can see I used
findBy
instead offindByIds
and theIn
function provided by typeorm. I tried using the same approach for the createUpdootLoader, but I noticed that it did not work since I needed to search for both theuserId
and thepostId
to retrieve the corresponding upvotes. Was anyone able to implement that correctly? I am unsure how to usefindBy
to search multiple fields with the keys since I cannot seem to separate the key fields by usingkeys.userId
andkeys.postId
. This is my current code:The text was updated successfully, but these errors were encountered: