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:
parent
f98d8d2e39
commit
32f972fc76
1 changed files with 10 additions and 6 deletions
|
|
@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue