Skip to content

findByIds Deprecated #61

@csandoval18

Description

@csandoval18

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:

export const createUserLoader = () =>
	new DataLoader<number, Users>(async (userIds) => {
		const users = await Users.findBy({ id: In(userIds as number[]) })
		const userIdToUser: Record<number, Users> = {}
		users.forEach((u) => {
			userIdToUser[u.id] = u
		})

		const sortedUsers = userIds.map((userId) => userIdToUser[userId])
		return sortedUsers
	})

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:

export const createUpdootLoader = () =>
	new DataLoader<{ postId: number; userId: number }, Updoot | null>(
		async (keys) => {
			console.log('keys:', keys)
			const updoots = await Updoot.findby({userId:  In(keys as any} )
			const updootIdsToUpdoot: Record<string, Upvotes> = {}
			updoots.forEach((updoot) => {
				upvoteIdsToUpvote[`${updoot.userId} | ${updoot.postId}`] = updoot
			})

			const sortedUpdoot = keys.map(
				(key) => updootIdsToUpdoot[`${key.userId} | ${key.postId}`],
			)
			return sortedUpdoots
		},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions