const scoreLoader = () => new DataLoader(async (scoreIds) => { const scores = await Score.find({ _id: { $in: scoreIds } }).exec() const scoresById = keyBy(scores, '_id') return scoreIds.map((scoreId) => scoresById[scoreId]) })
function groupByParents(array, options) { var arrayByID = keyBy(array, options.customID); return array.reduce(function(prev, item) { var parentID = property.get(item, options.parentProperty); if (!parentID || !arrayByID.hasOwnProperty(parentID)) { parentID = options.rootID; } if (parentID && prev.hasOwnProperty(parentID)) { prev[parentID].push(item); return prev; } prev[parentID] = [item]; return prev; }, {}); }
/** * Gets the all the player deltas (gains), for every period. */ async function getPlayerDeltas(playerId: number) { const initial = await InitialValues.findOne({ where: { playerId } }); const latest = await snapshotService.findLatest(playerId); const periodDeltas = await Promise.all( PERIODS.map(async period => { const deltas = await getPlayerPeriodDeltas(playerId, period, latest, initial); return { period, deltas }; }) ); // Turn an array of deltas, into an object, using the period as a key, // then include only the deltas array in the final object, not the period fields return mapValues(keyBy(periodDeltas, 'period'), p => p.deltas); }
const mapChanges = (changes, side, toValue) => { if (!changes.length) { return []; } const computeLineNumber = side === 'old' ? computeOldLineNumber : computeNewLineNumber; const changesByLineNumber = keyBy(changes, computeLineNumber); const maxLineNumber = computeLineNumber(last(changes)); return Array.from({length: maxLineNumber}).map((value, i) => toValue(changesByLineNumber[i + 1])); }
/** * Finds all player snapshots, grouped by period. * * Ex: * { * day: [...], * week: [...], * etc * } */ async function getAllGrouped(playerId) { if (!playerId) { throw new BadRequestError(`Invalid player id.`); } const partials = await Promise.all( PERIODS.map(async period => { const list = await getAllInPeriod(playerId, period); return { period, snapshots: list }; }) ); // Turn an array of snapshots, into an object, using the period as a key, // then include only the snapshots array in the final object, not the period fields return mapValues(keyBy(partials, 'period'), p => p.snapshots); }
/** * Given a list of groups, it will fetch the member count of each, * and inserts a "memberCount" field in every group object. */ async function attachMembersCount(groups) { /** * Will return a members count for every group, with the format: * [ {groupId: 35, count: "4"}, {groupId: 41, count: "31"} ] */ const membersCount = await Membership.findAll({ where: { groupId: groups.map(g => g.id) }, attributes: ['groupId', [Sequelize.fn('COUNT', Sequelize.col('groupId')), 'count']], group: ['groupId'] }); /** * Convert the counts fetched above, into a key:value format: * { 35: 4, 41: 31 } */ const countMap = mapValues( keyBy( membersCount.map((c: any) => ({ groupId: c.groupId, count: parseInt(c.toJSON().count, 10) })), c => c.groupId ), (c: any) => c.count ); return groups.map(g => ({ ...g, memberCount: countMap[g.id] || 0 })); }
const memberMap = keyBy(members, 'id'); const memberIds = members.map(m => m.id);
keyBy( participantCount.map((c: any) => ({ competitionId: c.competitionId,
const finalsLeaderboardLoader = () => new DataLoader(async (leaderboardIds) => { const leaderboards = await FinalsLeaderboard.find({ _id: { $in: leaderboardIds } }).exec() const leaderboardsById = keyBy(leaderboards, '_id') return leaderboardIds.map( (leaderboardId) => leaderboardsById[leaderboardId] ) })
const qualifiersLeaderboardLoader = () => new DataLoader(async (leaderboardIds) => { const leaderboards = await QualifiersLeaderboard.find({ _id: { $in: leaderboardIds } }).exec() const leaderboardsById = keyBy(leaderboards, '_id') return leaderboardIds.map( (leaderboardId) => leaderboardsById[leaderboardId] ) })
const adminLoader = () => new DataLoader(async (adminIds) => { const admins = await Admin.find({ _id: { $in: adminIds } }).exec() const adminsById = keyBy(admins, '_id') return adminIds.map((adminId) => adminsById[adminId]) })
const competitionLoader = () => new DataLoader(async (competitionIds) => { const competitions = await Competition.find({ _id: { $in: competitionIds } }).exec() const competitionsById = keyBy(competitions, '_id') return competitionIds.map( (competitionId) => competitionsById[competitionId] ) })
const venueLoader = () => new DataLoader(async (venueIds) => { const venues = await Venue.find({ _id: { $in: venueIds } }).exec() const venuesById = keyBy(venues, '_id') return venueIds.map((venueId) => venuesById[venueId]) })
const workoutLoader = () => new DataLoader(async (workoutIds) => { const workouts = await Workout.find({ _id: { $in: workoutIds } }).exec() const workoutsById = keyBy(workouts, '_id') return workoutIds.map((workoutId) => workoutsById[workoutId]) })
const athleteLoader = () => new DataLoader(async (athleteIds) => { const athletes = await Athlete.find({ _id: { $in: athleteIds } }).exec() const athletesById = keyBy(athletes, '_id') return athleteIds.map((athleteId) => athletesById[athleteId]) })