44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
|
|
import { eq } from "drizzle-orm";
|
||
|
|
import { database } from "~/database/context";
|
||
|
|
import * as schema from "~/database/schema";
|
||
|
|
|
||
|
|
export type CanonicalSurfaceElo = typeof schema.participantSurfaceElos.$inferSelect;
|
||
|
|
export type NewCanonicalSurfaceElo = typeof schema.participantSurfaceElos.$inferInsert;
|
||
|
|
|
||
|
|
export async function upsertCanonicalSurfaceElo(
|
||
|
|
data: NewCanonicalSurfaceElo
|
||
|
|
): Promise<CanonicalSurfaceElo> {
|
||
|
|
if (!data.participantId) {
|
||
|
|
throw new Error("participantId is required");
|
||
|
|
}
|
||
|
|
|
||
|
|
const db = database();
|
||
|
|
const [result] = await db
|
||
|
|
.insert(schema.participantSurfaceElos)
|
||
|
|
.values(data)
|
||
|
|
.onConflictDoUpdate({
|
||
|
|
target: [schema.participantSurfaceElos.participantId],
|
||
|
|
set: {
|
||
|
|
worldRanking: data.worldRanking,
|
||
|
|
eloHard: data.eloHard,
|
||
|
|
eloClay: data.eloClay,
|
||
|
|
eloGrass: data.eloGrass,
|
||
|
|
updatedAt: new Date(),
|
||
|
|
},
|
||
|
|
})
|
||
|
|
.returning();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getCanonicalSurfaceElo(
|
||
|
|
participantId: string
|
||
|
|
): Promise<CanonicalSurfaceElo | null> {
|
||
|
|
const db = database();
|
||
|
|
const results = await db
|
||
|
|
.select()
|
||
|
|
.from(schema.participantSurfaceElos)
|
||
|
|
.where(eq(schema.participantSurfaceElos.participantId, participantId))
|
||
|
|
.limit(1);
|
||
|
|
return results[0] ?? null;
|
||
|
|
}
|