sendou.ink/app/features/tournament/TournamentRepository.server.ts
Kalle c8ea75ebb6
SQL solution migration to Kysely + getting rid of routes folder (#1530)
* Kysely initial

* Badges initial

* Badge routes migrated

* Badges migrated

* Calendar work

* Fix one type problem

* Calendar work

* findResultsByUserId work

* Calendar reworking finished

* PlusSuggestions work

* Migrated suggestions

* Builds progress

* Migrated builds

* Admin migrated

* Migrate articles

* User search

* Faster getUser

* Selectable/insertable as global

* Refresh prod db script + patronTier index

* identifierToUserId

* updateProfile

* findByIdentifier

* More indexes

* User upsert

* upsertLite

* findAllPlusMembers

* updateResultHighlights

* updateMany

* User finished migration

* Fix types

* Fix PlusVotingResult typing

* PlusVotingRepository WIP

* Migrated resultsByMonthYear

* Migrated plusVotes (done with db. related migrations)

* Plus code to features folder

* Fix TODOs

* Export

* Fix range

* Migrate some user pages

* Move rest user routes

* Move /play

* Map list generator

* Front page

* Move map list generation logic

* Move plus voting logic

* Info

* API

* Adjust TODOs

* theme

* Auth

* Remove TODO
2023-11-04 13:15:36 +02:00

56 lines
1.7 KiB
TypeScript

import { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/sqlite";
import invariant from "tiny-invariant";
import { db } from "~/db/sql";
import { COMMON_USER_FIELDS } from "~/utils/kysely.server";
import type { Unwrapped } from "~/utils/types";
export type FindById = NonNullable<Unwrapped<typeof findById>>;
export async function findById(id: number) {
const row = await db
.selectFrom("Tournament")
.innerJoin("CalendarEvent", "Tournament.id", "CalendarEvent.tournamentId")
// TODO: it does not support multiple dates
.innerJoin(
"CalendarEventDate",
"CalendarEvent.id",
"CalendarEventDate.eventId",
)
.select(({ eb }) => [
"Tournament.id",
"Tournament.mapPickingStyle",
"Tournament.format",
"Tournament.showMapListGenerator",
"CalendarEvent.id as eventId",
"CalendarEvent.name",
"CalendarEvent.description",
"CalendarEvent.bracketUrl",
"CalendarEventDate.startTime",
jsonObjectFrom(
eb
.selectFrom("User")
.whereRef("CalendarEvent.authorId", "=", "User.id")
.select(COMMON_USER_FIELDS),
).as("author"),
jsonArrayFrom(
eb
.selectFrom("MapPoolMap")
.whereRef(
"MapPoolMap.tieBreakerCalendarEventId",
"=",
"CalendarEvent.id",
)
.select(["MapPoolMap.stageId", "MapPoolMap.mode"]),
).as("tieBreakerMapPool"),
])
.where("Tournament.id", "=", id)
.executeTakeFirst();
if (!row) return null;
// TODO: can be made better when $narrowNotNull lands
const author = row.author;
invariant(author, "Tournament author is missing");
return { ...row, author };
}