Add entriesCount & availableCount to seasons

This commit is contained in:
Zoe Roux
2025-07-14 00:47:00 +02:00
parent 287b5350ff
commit aea2535cc6
8 changed files with 1943 additions and 3 deletions
+4
View File
@@ -24,6 +24,8 @@ const seasonFilters: FilterDef = {
seasonNumber: { column: seasons.seasonNumber, type: "int" },
startAir: { column: seasons.startAir, type: "date" },
endAir: { column: seasons.endAir, type: "date" },
entriesCount: { column: seasons.entriesCount, type: "int" },
availableCount: { column: seasons.availableCount, type: "int" },
};
const seasonSort = Sort(
@@ -31,6 +33,8 @@ const seasonSort = Sort(
seasonNumber: seasons.seasonNumber,
startAir: seasons.startAir,
endAir: seasons.endAir,
entriesCount: seasons.entriesCount,
availableCount: seasons.availableCount,
nextRefresh: seasons.nextRefresh,
},
{
+37 -2
View File
@@ -9,7 +9,13 @@ import {
sql,
} from "drizzle-orm";
import { db, type Transaction } from "~/db";
import { entries, entryVideoJoin, shows, showTranslations } from "~/db/schema";
import {
entries,
entryVideoJoin,
seasons,
shows,
showTranslations,
} from "~/db/schema";
import { conflictUpdateAllExcept, sqlarr } from "~/db/utils";
import type { SeedCollection } from "~/models/collections";
import type { SeedMovie } from "~/models/movie";
@@ -151,7 +157,7 @@ export async function updateAvailableCount(
updateEntryCount = false,
) {
const showPkQ = Array.isArray(showPks) ? sqlarr(showPks) : showPks;
return await tx
await tx
.update(shows)
.set({
availableCount: sql`${db
@@ -179,6 +185,35 @@ export async function updateAvailableCount(
}),
})
.where(eq(shows.pk, sql`any(${showPkQ})`));
await tx
.update(seasons)
.set({
availableCount: sql`${db
.select({ count: count() })
.from(entries)
.where(
and(
eq(entries.showPk, seasons.showPk),
eq(entries.seasonNumber, seasons.seasonNumber),
ne(entries.kind, "extra"),
exists(
db
.select()
.from(entryVideoJoin)
.where(eq(entryVideoJoin.entryPk, entries.pk)),
),
),
)}`,
...(updateEntryCount && {
entriesCount: sql`${db
.select({ count: count() })
.from(entries)
.where(
and(eq(entries.showPk, seasons.showPk), ne(entries.kind, "extra")),
)}`,
}),
})
.where(eq(seasons.showPk, sql`any(${showPkQ})`));
}
export async function updateAvailableSince(
+9 -1
View File
@@ -122,7 +122,15 @@ export const seedSerie = async (
);
if ("status" in show) return show;
const retSeasons = await insertSeasons(show, seasons);
const retSeasons = await insertSeasons(
show,
seasons.map((s) => ({
...s,
entriesCount: entries.filter(
(x) => x.kind === "episode" && x.seasonNumber === s.seasonNumber,
).length,
})),
);
const retEntries = await insertEntries(show, entries);
const retExtras = await insertEntries(
show,