Fix union import: use two queries with JS deduplication

drizzle-orm 0.36.x does not export a standalone union() function.
Replace with two awaited queries merged via a Map (dedup by id),
then sorted by createdAt descending in JS.

https://claude.ai/code/session_01E3ugKTfatkEc5TcDGXvHiW
This commit is contained in:
Claude 2026-02-20 18:26:28 +00:00
parent f98d8d2e39
commit 32f972fc76
No known key found for this signature in database

View file

@ -1,4 +1,4 @@
import { eq, desc, and, union } from "drizzle-orm";
import { eq, desc, and } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
@ -82,22 +82,26 @@ export async function findLeaguesWithActiveSeasonsByUserId(
};
// Leagues where user has a team in the current season
const leaguesWithTeam = db
const leaguesWithTeam = await db
.select(leagueFields)
.from(schema.leagues)
.innerJoin(schema.teams, eq(schema.teams.seasonId, schema.leagues.currentSeasonId))
.where(eq(schema.teams.ownerId, userId));
// Leagues where user is a commissioner (with or without a team)
const leaguesAsCommissioner = db
const leaguesAsCommissioner = await db
.select(leagueFields)
.from(schema.leagues)
.innerJoin(schema.commissioners, eq(schema.commissioners.leagueId, schema.leagues.id))
.where(eq(schema.commissioners.userId, userId));
// Union deduplicates results in case user is both a commissioner and has a team
return await union(leaguesWithTeam, leaguesAsCommissioner).orderBy(
desc(schema.leagues.createdAt)
// Deduplicate by id in case user is both a commissioner and has a team, then sort
const leagueMap = new Map<string, League>();
for (const league of [...leaguesWithTeam, ...leaguesAsCommissioner]) {
leagueMap.set(league.id, league);
}
return [...leagueMap.values()].sort(
(a, b) => b.createdAt.getTime() - a.createdAt.getTime()
);
}