Git Product home page Git Product logo

Comments (1)

Adeniyii avatar Adeniyii commented on July 21, 2024 1

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!

import DataLoader from "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 query
export const createUpdootLoader = () => new DataLoader<{userId: number, postId: number}, Updoot | null>(async (idObjArr) => {
	const userIds = idObjArr.map(obj => obj.userId)
	const postIds = idObjArr.map(obj => obj.postId)
	const updoots = await Updoot.findBy({userId: In(userIds), postId: In(postIds)})

	const updootToIdMap: Record<string, Updoot> = {}
	updoots.forEach(updoot => {
		updootToIdMap[`${updoot.postId}|${updoot.userId}`] = updoot
	})

	return idObjArr.map(obj => {
		return updootToIdMap[`${obj.postId}|${obj.userId}`];
	})
})

This is the SQL that was generated when the loader is executed.

dootsql

from lireddit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.