From 6767a0993b571763e4ff480a3a99ec35cc209aab Mon Sep 17 00:00:00 2001 From: Shinsina Date: Mon, 22 Apr 2024 13:25:18 -0500 Subject: [PATCH 1/3] Include appropriate foreign key relationships --- db/config.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/db/config.ts b/db/config.ts index 0bfa004..2197b9b 100644 --- a/db/config.ts +++ b/db/config.ts @@ -124,7 +124,7 @@ const Season = defineTable({ season_short_name: column.text(), season_year: column.number(), send_to_open_practice: column.boolean(), - series_id: column.boolean(), + series_id: column.number(), short_parade_lap: column.boolean(), start_date: column.date(), start_on_qual_tire: column.boolean(), @@ -137,12 +137,12 @@ const Season = defineTable({ const Standing = defineTable({ columns: { id: column.text({ primaryKey: true }), - car_class_id: column.text(), + car_class_id: column.number({ references: () => CarClass.columns.car_class_id }), division: column.number(), overall_rank: column.number(), rank: column.number(), - cust_id: column.number(), - display_name: column.text({}), + cust_id: column.number({ references: () => User.columns.cust_id }), + display_name: column.text(), club_id: column.number(), license: column.json(), helmet: column.json(), @@ -163,7 +163,7 @@ const Standing = defineTable({ week_dropped: column.boolean(), country_code: column.text(), country: column.text(), - season_id: column.text(), + season_id: column.number({ references: () => PastSeason.columns.season_id }), season_name: column.text(), division_rank: column.number(), }, @@ -206,7 +206,7 @@ const Subsession = defineTable({ race_summary: column.json(), race_week_num: column.number(), // results_restricted: column.boolean(), - season_id: column.number(), + season_id: column.number({ references: () => PastSeason.columns.season_id }), season_name: column.text(), season_quarter: column.number(), season_short_name: column.text(), @@ -227,7 +227,8 @@ const Subsession = defineTable({ const SubsessionSessionResults = defineTable({ columns: { - subsession_id: column.number(), + id: column.text({ primaryKey: true }), + subsession_id: column.number({ references: () => Subsession.columns.subsession_id }), simsession_number: column.number(), simsession_type: column.number(), simsession_type_name: column.text(), @@ -257,7 +258,7 @@ const SubsessionSessionResults = defineTable({ qual_lap_time: column.number(), starting_position: column.number(), starting_position_in_class: column.number(), - car_class_id: column.number(), + car_class_id: column.number({ references: () => CarClass.columns.car_class_id }), car_class_name: column.text(), car_class_short_name: column.text(), club_id: column.number(), @@ -283,7 +284,7 @@ const SubsessionSessionResults = defineTable({ weight_penalty_kg: column.number(), league_points: column.number(), league_agg_points: column.number(), - car_id: column.number(), + car_id: column.number({ references: () => Car.columns.car_id }), car_name: column.text(), aggregate_champ_points: column.number(), livery: column.json(), From d216df27f3df96be5862c01ddad145eb778fc5d9 Mon Sep 17 00:00:00 2001 From: Shinsina Date: Mon, 22 Apr 2024 13:28:06 -0500 Subject: [PATCH 2/3] Insert PastSeasons prior to Standings, add ids to Result tables --- db/seed.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/db/seed.ts b/db/seed.ts index 413c97b..afe1f80 100644 --- a/db/seed.ts +++ b/db/seed.ts @@ -116,32 +116,40 @@ export default async function seed() { }) ); console.log("Seeded Seasons!"); + const pastSeasonsCollection = mongoDb.collection("pastseasons"); + const pastSeasons = await paginatedQuery(pastSeasonsCollection); + console.log("Seeding Past Seasons..."); + await db.delete(PastSeason); + await db.insert(PastSeason).values( + pastSeasons.map((pastSeason: any) => { + const { _id, ...rest } = pastSeason; + return rest; + }) + ); + console.log("Past Seasons Seeded!"); const standingsCollection = mongoDb.collection("standings"); const standings = await paginatedQuery(standingsCollection); console.log("Seeding Standings..."); await db.delete(Standing); await db.insert(Standing).values( standings.map((standing: any) => { - const { _id: id, season_driver_data, ...rest } = standing; + const { + _id: id, + car_class_id, + season_driver_data, + season_id, + ...rest + } = standing; return { id, + car_class_id: Number(car_class_id), + season_id: Number(season_id), ...season_driver_data, ...rest, }; }) ); console.log("Standings Seeded!"); - const pastSeasonsCollection = mongoDb.collection("pastseasons"); - const pastSeasons = await paginatedQuery(pastSeasonsCollection); - console.log("Seeding Past Seasons..."); - await db.delete(PastSeason); - await db.insert(PastSeason).values( - pastSeasons.map((pastSeason: any) => { - const { _id, ...rest } = pastSeason; - return rest; - }) - ); - console.log("Past Seasons Seeded!"); const subsessionsCollection = mongoDb.collection("subsessions"); // @ts-ignore const subsessions: Array = await paginatedQuery( @@ -165,6 +173,7 @@ export default async function seed() { subsession_id: _id, ...result, ...rest, + id: `${_id}_${rest.simsession_name}_${result.cust_id}`, }; if (rest.simsession_type_name.match("Practice")) { object.allPracticeResults.push(resultWithSession); From d2ee813f891ff2cb57f1104dfd722dee3f8a21b9 Mon Sep 17 00:00:00 2001 From: Shinsina Date: Mon, 22 Apr 2024 15:38:29 -0500 Subject: [PATCH 3/3] Utilize innerJoins where plausible on pages --- src/lib/layouts/subsessions.astro | 127 +++++++++--------- .../car-class/[carClassId]/index.astro | 28 ++-- .../[carClassId]/season-summary.astro | 31 ++--- .../standings/by-car-class/[carClassId].astro | 2 +- .../user/[id]/standings/by-year/[year].astro | 15 +-- .../user/[id]/standings/by-year/index.astro | 18 +-- .../by-car-class/[carClassId].astro | 23 +--- .../subsessions/by-track/[trackSlug].astro | 51 +++---- .../[id]/subsessions/by-track/index.astro | 11 +- .../[id]/subsessions/by-year/[year].astro | 45 +++---- .../user/[id]/subsessions/by-year/index.astro | 18 +-- src/pages/user/[id]/subsessions/index.astro | 23 +--- 12 files changed, 157 insertions(+), 235 deletions(-) diff --git a/src/lib/layouts/subsessions.astro b/src/lib/layouts/subsessions.astro index c32f3bc..73352bf 100644 --- a/src/lib/layouts/subsessions.astro +++ b/src/lib/layouts/subsessions.astro @@ -3,69 +3,76 @@ import handleResults from "$lib/utils/handle-session-results"; import fieldIdToLabelMap from "$lib/utils/field-id-to-label-map"; import lapTimeFields from "$lib/utils/lap-time-fields"; import parseLapTime from "$lib/utils/parse-lap-time"; -import type { Subsession } from "$lib/types"; +import type { SessionResult, Subsession as SubsessionType } from "$lib/types"; import DefaultLayout from "$lib/layouts/default.astro"; const { seoTitle, seoDescription, subsessions } = Astro.props; -const results = subsessions.map((subsession: Subsession) => { - const { - subsession_id, - track, - corners_per_lap, - allowed_licenses, - license_category, - userResult, - } = subsession; - // Current user in Race session; - const { - finish_position, - finish_position_in_class, - laps_lead, - laps_complete, - average_lap, - best_lap_time, - reason_out, - champ_points, - starting_position, - starting_position_in_class, - car_class_short_name, - division_name, - new_license_level, - new_cpi, - newi_rating, - incidents, - car_name, - aggregate_champ_points, - } = userResult; - const { group_name: License } = allowed_licenses.slice(0, 2).pop() || {}; +const results = subsessions.map( + ({ + Subsession, + SubsessionRaceResults, + }: { + Subsession: SubsessionType; + SubsessionRaceResults: SessionResult; + }) => { + const { + subsession_id, + track, + corners_per_lap, + allowed_licenses, + license_category, + } = Subsession; + // Current user in Race session; + const { + finish_position, + finish_position_in_class, + laps_lead, + laps_complete, + average_lap, + best_lap_time, + reason_out, + champ_points, + starting_position, + starting_position_in_class, + car_class_short_name, + division_name, + new_license_level, + new_cpi, + newi_rating, + incidents, + car_name, + aggregate_champ_points, + } = SubsessionRaceResults; + const { group_name: License } = allowed_licenses.slice(0, 2).pop() || {}; - return { - subsession_id, - Track: `${track.track_name} ${ - track.config_name !== "N/A" ? track.config_name : "" - }`, - Corners: corners_per_lap, - License, - license_category, - finish_position, - finish_position_in_class, - laps_lead, - laps_complete, - average_lap, - best_lap_time, - reason_out, - champ_points, - starting_position, - starting_position_in_class, - car_class_short_name, - division_name, - new_license_level, - new_cpi, - newi_rating, - incidents, - car_name, - aggregate_champ_points, - }; -}); + return { + subsession_id, + Track: `${track.track_name} ${ + track.config_name !== "N/A" ? track.config_name : "" + }`, + Corners: corners_per_lap, + License, + license_category, + finish_position, + finish_position_in_class, + laps_lead, + laps_complete, + average_lap, + best_lap_time, + reason_out, + champ_points, + starting_position, + starting_position_in_class, + car_class_short_name, + division_name, + new_license_level, + new_cpi, + newi_rating, + incidents, + car_name, + aggregate_champ_points, + }; + } +); const keysToDisplay = new Set(Object.keys(results[0] || {})); const { keysArray, handledResults } = handleResults({ keysToDisplay, results }); --- diff --git a/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/index.astro b/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/index.astro index b9d0ae3..a0817d4 100644 --- a/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/index.astro +++ b/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/index.astro @@ -32,31 +32,27 @@ const { id, seasonId, carClassId } = Astro.params; const allSeasonSubsessions = await db .select() .from(Subsession) - .where(eq(Subsession.season_id, Number(seasonId))); -const allSeasonRaceResultsForUser = await db - .select() - .from(SubsessionRaceResults) + .innerJoin( + SubsessionRaceResults, + eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id) + ) .where( and( + eq(Subsession.season_id, Number(seasonId)), eq(SubsessionRaceResults.cust_id, Number(id)), eq(SubsessionRaceResults.car_class_id, Number(carClassId)), sql`${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE", "N/A"]}` ) ); -const results = allSeasonSubsessions - .filter((v) => - allSeasonRaceResultsForUser - .map((w) => w.subsession_id) - .includes(v.subsession_id) - ) - .map((subsession) => { +const results = allSeasonSubsessions.map( + ({ Subsession, SubsessionRaceResults }) => { const { subsession_id, track, corners_per_lap, allowed_licenses, license_category, - } = subsession; + } = Subsession; // Current user in Race session; const { finish_position, @@ -77,10 +73,7 @@ const results = allSeasonSubsessions incidents, car_name, aggregate_champ_points, - } = - allSeasonRaceResultsForUser.find( - (v) => v.subsession_id === subsession_id - ) || {}; + } = SubsessionRaceResults; const { group_name: License } = Array.isArray(allowed_licenses) ? allowed_licenses.slice(0, 2).pop() : { group_name: "" }; @@ -113,7 +106,8 @@ const results = allSeasonSubsessions car_name, aggregate_champ_points, }; - }); + } +); const keysToDisplay = new Set(Object.keys(results[0])); const { keysArray, handledResults } = handleResults({ keysToDisplay, results }); const title = `Season information for season ID ${seasonId} - car class ID - ${carClassId} and user ID - ${id}`; diff --git a/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/season-summary.astro b/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/season-summary.astro index 7f215b2..2dd5cad 100644 --- a/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/season-summary.astro +++ b/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/season-summary.astro @@ -33,22 +33,19 @@ const { id, seasonId, carClassId } = Astro.params; const allSeasonSubsessions = await db .select() .from(Subsession) - .where(eq(Subsession.season_id, Number(seasonId))); -const allSeasonRaceResultsForUser = await db - .select() - .from(SubsessionRaceResults) + .innerJoin( + SubsessionRaceResults, + eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id) + ) .where( and( + eq(Subsession.season_id, Number(seasonId)), eq(SubsessionRaceResults.cust_id, Number(id)), eq(SubsessionRaceResults.car_class_id, Number(carClassId)), sql`${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE", "N/A"]}` ) ); -const subsessions = allSeasonSubsessions.filter((v) => - allSeasonRaceResultsForUser - .map((w) => w.subsession_id) - .includes(v.subsession_id) -); +const subsessions = allSeasonSubsessions; const [season] = await db .select() .from(Standing) @@ -74,15 +71,15 @@ const resultsPerWeek: Array<{ }> = []; trackScheduleMap.forEach((value) => { const respectiveSubsessions = subsessions.filter( - (subsession) => - Number(subsession.race_week_num) === Number(value.race_week_num) + ({ Subsession }) => + Number(Subsession.race_week_num) === Number(value.race_week_num) + ); + const userResults = respectiveSubsessions.map( + ({ Subsession, SubsessionRaceResults }) => ({ + subsession: Subsession, + userResult: SubsessionRaceResults, + }) ); - const userResults = respectiveSubsessions.map((subsession) => ({ - subsession, - userResult: allSeasonRaceResultsForUser.find( - (v) => v.subsession_id === subsession.subsession_id - ), - })); if (userResults.length > 1) { const bestUserResult = userResults .sort( diff --git a/src/pages/user/[id]/standings/by-car-class/[carClassId].astro b/src/pages/user/[id]/standings/by-car-class/[carClassId].astro index 77df197..9fce6ae 100644 --- a/src/pages/user/[id]/standings/by-car-class/[carClassId].astro +++ b/src/pages/user/[id]/standings/by-car-class/[carClassId].astro @@ -32,7 +32,7 @@ const standingsResults = await db .where( and( eq(Standing.cust_id, Number(id)), - eq(Standing.car_class_id, String(carClassId)) + eq(Standing.car_class_id, Number(carClassId)) ) ) .orderBy(desc(Standing.season_id)); diff --git a/src/pages/user/[id]/standings/by-year/[year].astro b/src/pages/user/[id]/standings/by-year/[year].astro index 1e548b0..3f857c0 100644 --- a/src/pages/user/[id]/standings/by-year/[year].astro +++ b/src/pages/user/[id]/standings/by-year/[year].astro @@ -5,7 +5,7 @@ import { eq, desc, like, - sql, + asc, Standing, SubsessionRaceResults, Subsession, @@ -15,16 +15,15 @@ import userIds from "$lib/utils/user-ids"; export async function getStaticPaths() { const mappings = await Promise.all( userIds.map(async (userId) => { - const subsessions = await db - .selectDistinct({ subsession_id: SubsessionRaceResults.subsession_id }) - .from(SubsessionRaceResults) - .where(eq(SubsessionRaceResults.cust_id, Number(userId))); const years = await db .selectDistinct({ season_year: Subsession.season_year }) .from(Subsession) - .where( - sql`${Subsession.subsession_id} IN ${subsessions.map((subsession) => subsession.subsession_id)}` - ); + .innerJoin( + SubsessionRaceResults, + eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id) + ) + .where(eq(SubsessionRaceResults.cust_id, Number(userId))) + .orderBy(asc(Subsession.season_year)); return { userId, years }; }) ); diff --git a/src/pages/user/[id]/standings/by-year/index.astro b/src/pages/user/[id]/standings/by-year/index.astro index 0d75f93..b2fd653 100644 --- a/src/pages/user/[id]/standings/by-year/index.astro +++ b/src/pages/user/[id]/standings/by-year/index.astro @@ -2,7 +2,7 @@ import { db, eq, - sql, + asc, SubsessionRaceResults, Subsession, } from "astro:db"; @@ -12,16 +12,12 @@ import { return userIds.map((id) => ({ params: { id } })); } const { id } = Astro.params; - const subsessions = await db - .selectDistinct({ subsession_id: SubsessionRaceResults.subsession_id }) - .from(SubsessionRaceResults) - .where(eq(SubsessionRaceResults.cust_id, Number(id))); - const years = await db - .selectDistinct({ season_year: Subsession.season_year }) - .from(Subsession) - .where( - sql`${Subsession.subsession_id} IN ${subsessions.map((subsession) => subsession.subsession_id)}` - ); + const years = await db + .selectDistinct({ season_year: Subsession.season_year }) + .from(Subsession) + .innerJoin(SubsessionRaceResults, eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id)) + .where(eq(SubsessionRaceResults.cust_id, Number(id))) + .orderBy(asc(Subsession.season_year)); const title = `Standings By Year - ${id}`; const description = `Standings by year page for user ID: ${id}`; --- diff --git a/src/pages/user/[id]/subsessions/by-car-class/[carClassId].astro b/src/pages/user/[id]/subsessions/by-car-class/[carClassId].astro index 2bfe840..b55622b 100644 --- a/src/pages/user/[id]/subsessions/by-car-class/[carClassId].astro +++ b/src/pages/user/[id]/subsessions/by-car-class/[carClassId].astro @@ -35,34 +35,21 @@ export async function getStaticPaths() { const { id, carClassId } = Astro.params; const seoTitle = `Subsessions list for user ID - ${id} and car class ID - ${carClassId}`; const seoDescription = seoTitle; -const subsessionRaceResults = await db +const subsessions = await db .select() .from(SubsessionRaceResults) + .innerJoin( + Subsession, + eq(SubsessionRaceResults.subsession_id, Subsession.subsession_id) + ) .where( and( eq(SubsessionRaceResults.cust_id, Number(id)), eq(SubsessionRaceResults.car_class_id, Number(carClassId)), sql`${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE", "N/A"]}` ) - ); -const subsessionsWithoutResult = await db - .select() - .from(Subsession) - .where( - sql`${Subsession.subsession_id} IN ${subsessionRaceResults.map((subsession) => subsession.subsession_id)}` ) .orderBy(desc(Subsession.subsession_id)); -const subsessions = subsessionsWithoutResult.map((subsessionWithoutResult) => { - const { subsession_id } = subsessionWithoutResult; - const userResult = subsessionRaceResults.find( - (subsessionRaceResult) => - subsessionRaceResult.subsession_id === subsession_id - ); - return { - ...subsessionWithoutResult, - userResult, - }; -}); --- diff --git a/src/pages/user/[id]/subsessions/by-track/[trackSlug].astro b/src/pages/user/[id]/subsessions/by-track/[trackSlug].astro index 3076151..3fecdf5 100644 --- a/src/pages/user/[id]/subsessions/by-track/[trackSlug].astro +++ b/src/pages/user/[id]/subsessions/by-track/[trackSlug].astro @@ -16,16 +16,14 @@ import generateTrackMap from "$lib/utils/generate-track-map"; export async function getStaticPaths() { const mappings = await Promise.all( userIds.map(async (userId) => { - const subsessions = await db - .selectDistinct({ subsession_id: SubsessionRaceResults.subsession_id }) - .from(SubsessionRaceResults) - .where(eq(SubsessionRaceResults.cust_id, Number(userId))); const tracks = await db .selectDistinct({ track: Subsession.track }) .from(Subsession) - .where( - sql`${Subsession.subsession_id} IN ${subsessions.map((subsession) => subsession.subsession_id)}` - ); + .innerJoin( + SubsessionRaceResults, + eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id) + ) + .where(eq(SubsessionRaceResults.cust_id, Number(userId))); return { userId, tracks }; }) ); @@ -52,50 +50,31 @@ export async function getStaticPaths() { const { id, trackSlug } = Astro.params; const seoTitle = `Subsessions list for user ID - ${id} and track - ${trackSlug}`; const seoDescription = seoTitle; -const blah = await db - .selectDistinct({ subsession_id: SubsessionRaceResults.subsession_id }) - .from(SubsessionRaceResults) - .where(eq(SubsessionRaceResults.cust_id, Number(id))); const tracksFromDb: Array<{ track: unknown | Track }> = await db .selectDistinct({ track: Subsession.track }) .from(Subsession) - .where( - sql`${Subsession.subsession_id} IN ${blah.map((subsession) => subsession.subsession_id)}` + .innerJoin( + SubsessionRaceResults, + eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id) ); const tracks = tracksFromDb as Array<{ track: Track }>; const generatedTrackMap = generateTrackMap({ tracks }); const { trackName, configName } = generatedTrackMap.get(trackSlug) || {}; -const subsessionsWithoutResult = await db +const subsessions = await db .select() .from(Subsession) + .innerJoin( + SubsessionRaceResults, + eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id) + ) .where( and( like(Subsession.track, `%${trackName}%`), - like(Subsession.track, `%${configName}%`) + like(Subsession.track, `%${configName}%`), + sql`${SubsessionRaceResults.cust_id} IS ${id} AND ${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE", "N/A"]}` ) ) .orderBy(desc(Subsession.subsession_id)); -const subsessionRaceResults = await db - .select() - .from(SubsessionRaceResults) - .where( - // @ts-ignore - sql`${SubsessionRaceResults.cust_id} IS ${id} AND ${SubsessionRaceResults.subsession_id} IN ${subsessionsWithoutResult.filter((subsession) => subsession.track?.track_name === trackName && subsession.track?.config_name === configName).map((subsessionWithoutResult) => subsessionWithoutResult.subsession_id)} AND ${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE"]}` - ); - -const subsessions = subsessionsWithoutResult - .map((subsessionWithoutResult) => { - const { subsession_id } = subsessionWithoutResult; - const userResult = subsessionRaceResults.find( - (subsessionRaceResult) => - subsessionRaceResult.subsession_id === subsession_id - ); - return { - ...subsessionWithoutResult, - userResult, - }; - }) - .filter((v) => v.userResult); --- diff --git a/src/pages/user/[id]/subsessions/by-track/index.astro b/src/pages/user/[id]/subsessions/by-track/index.astro index fe55b5c..7d575d3 100644 --- a/src/pages/user/[id]/subsessions/by-track/index.astro +++ b/src/pages/user/[id]/subsessions/by-track/index.astro @@ -1,5 +1,5 @@ --- -import { db, eq, sql, SubsessionRaceResults, Subsession } from "astro:db"; +import { db, eq, SubsessionRaceResults, Subsession } from "astro:db"; import DefaultLayout from "$lib/layouts/default.astro"; import userIds from "$lib/utils/user-ids"; import type { Track } from "$lib/types"; @@ -8,16 +8,11 @@ export function getStaticPaths() { return userIds.map((id) => ({ params: { id } })); } const { id } = Astro.params; -const subsessions = await db - .selectDistinct({ subsession_id: SubsessionRaceResults.subsession_id }) - .from(SubsessionRaceResults) - .where(eq(SubsessionRaceResults.cust_id, Number(id))); const tracksFromDb = await db .selectDistinct({ track: Subsession.track }) .from(Subsession) - .where( - sql`${Subsession.subsession_id} IN ${subsessions.map((subsession) => subsession.subsession_id)}` - ); + .innerJoin(SubsessionRaceResults, eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id)) + .where(eq(SubsessionRaceResults.cust_id, Number(id))); const tracks = tracksFromDb as Array<{ track: Track }> const trackMap = generateTrackMap({ tracks }); const title = `Subsessions By Track - ${id}`; diff --git a/src/pages/user/[id]/subsessions/by-year/[year].astro b/src/pages/user/[id]/subsessions/by-year/[year].astro index 2784474..deba855 100644 --- a/src/pages/user/[id]/subsessions/by-year/[year].astro +++ b/src/pages/user/[id]/subsessions/by-year/[year].astro @@ -3,7 +3,7 @@ import { db, desc, eq, - and, + asc, sql, Subsession, SubsessionRaceResults, @@ -13,16 +13,15 @@ import userIds from "$lib/utils/user-ids"; export async function getStaticPaths() { const mappings = await Promise.all( userIds.map(async (userId) => { - const subsessions = await db - .selectDistinct({ subsession_id: SubsessionRaceResults.subsession_id }) - .from(SubsessionRaceResults) - .where(eq(SubsessionRaceResults.cust_id, Number(userId))); const years = await db .selectDistinct({ season_year: Subsession.season_year }) .from(Subsession) - .where( - sql`${Subsession.subsession_id} IN ${subsessions.map((subsession) => subsession.subsession_id)}` - ); + .innerJoin( + SubsessionRaceResults, + eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id) + ) + .where(eq(SubsessionRaceResults.cust_id, Number(userId))) + .orderBy(asc(Subsession.season_year)); return { userId, years }; }) ); @@ -38,31 +37,17 @@ export async function getStaticPaths() { const { id, year } = Astro.params; const seoTitle = `Subsessions list for user ID - ${id} and year - ${year}`; const seoDescription = seoTitle; -const subsessionsWithoutResult = await db - .select() - .from(Subsession) - .where(eq(Subsession.season_year, Number(year))) - .orderBy(desc(Subsession.subsession_id)); -const subsessionRaceResults = await db +const subsessions = await db .select() .from(SubsessionRaceResults) + .innerJoin( + Subsession, + eq(SubsessionRaceResults.subsession_id, Subsession.subsession_id) + ) .where( - sql`${SubsessionRaceResults.cust_id} IS ${id} AND ${SubsessionRaceResults.subsession_id} IN ${subsessionsWithoutResult.map((subsessionWithoutResult) => subsessionWithoutResult.subsession_id)} AND ${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE"]}` - ); - -const subsessions = subsessionsWithoutResult - .map((subsessionWithoutResult) => { - const { subsession_id } = subsessionWithoutResult; - const userResult = subsessionRaceResults.find( - (subsessionRaceResult) => - subsessionRaceResult.subsession_id === subsession_id - ); - return { - ...subsessionWithoutResult, - userResult, - }; - }) - .filter((v) => v.userResult); + sql`${SubsessionRaceResults.cust_id} IS ${id} AND ${Subsession.season_year} IS ${Number(year)} AND ${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE", "N/A"]}` + ) + .orderBy(desc(Subsession.subsession_id)); --- diff --git a/src/pages/user/[id]/subsessions/by-year/index.astro b/src/pages/user/[id]/subsessions/by-year/index.astro index da76b90..10a1e24 100644 --- a/src/pages/user/[id]/subsessions/by-year/index.astro +++ b/src/pages/user/[id]/subsessions/by-year/index.astro @@ -1,8 +1,8 @@ --- import { + asc, db, eq, - sql, SubsessionRaceResults, Subsession, } from "astro:db"; @@ -12,16 +12,12 @@ import { return userIds.map((id) => ({ params: { id } })); } const { id } = Astro.params; - const subsessions = await db - .selectDistinct({ subsession_id: SubsessionRaceResults.subsession_id }) - .from(SubsessionRaceResults) - .where(eq(SubsessionRaceResults.cust_id, Number(id))); - const years = await db - .selectDistinct({ season_year: Subsession.season_year }) - .from(Subsession) - .where( - sql`${Subsession.subsession_id} IN ${subsessions.map((subsession) => subsession.subsession_id)}` - ); + const years = await db + .selectDistinct({ season_year: Subsession.season_year }) + .from(Subsession) + .innerJoin(SubsessionRaceResults, eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id)) + .where(eq(SubsessionRaceResults.cust_id, Number(id))) + .orderBy(asc(Subsession.season_year)); const title = `Subsessions By Year - ${id}`; const description = `Subsessions by year page for user ID: ${id}`; --- diff --git a/src/pages/user/[id]/subsessions/index.astro b/src/pages/user/[id]/subsessions/index.astro index 28fb7fe..cc43cdc 100644 --- a/src/pages/user/[id]/subsessions/index.astro +++ b/src/pages/user/[id]/subsessions/index.astro @@ -16,33 +16,20 @@ export function getStaticPaths() { const { id } = Astro.params; const seoTitle = `Subsessions - ${id}`; const seoDescription = `Subsessions page for user ID: ${id}`; -const subsessionRaceResults = await db +const subsessions = await db .select() .from(SubsessionRaceResults) + .innerJoin( + Subsession, + eq(SubsessionRaceResults.subsession_id, Subsession.subsession_id) + ) .where( and( eq(SubsessionRaceResults.cust_id, Number(id)), sql`${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE", "N/A"]}` ) - ); -const subsessionsWithoutResult = await db - .select() - .from(Subsession) - .where( - sql`${Subsession.subsession_id} IN ${subsessionRaceResults.map((subsession) => subsession.subsession_id)}` ) .orderBy(desc(Subsession.subsession_id)); -const subsessions = subsessionsWithoutResult.map((subsessionWithoutResult) => { - const { subsession_id } = subsessionWithoutResult; - const userResult = subsessionRaceResults.find( - (subsessionRaceResult) => - subsessionRaceResult.subsession_id === subsession_id - ); - return { - ...subsessionWithoutResult, - userResult, - }; -}); ---