Update available count of shows when inserting videos

This commit is contained in:
Zoe Roux 2025-05-03 16:07:06 +02:00
parent e26bc931f5
commit 205dda652a
No known key found for this signature in database
3 changed files with 18 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { and, count, eq, exists, ne, sql } from "drizzle-orm"; import { type SQLWrapper, and, count, eq, exists, ne, sql } from "drizzle-orm";
import { type Transaction, db } from "~/db"; import { type Transaction, db } from "~/db";
import { entries, entryVideoJoin, showTranslations, shows } from "~/db/schema"; import { entries, entryVideoJoin, showTranslations, shows } from "~/db/schema";
import { conflictUpdateAllExcept, sqlarr } from "~/db/utils"; import { conflictUpdateAllExcept, sqlarr } from "~/db/utils";
@ -138,9 +138,10 @@ async function insertBaseShow(tx: Transaction, show: Show) {
export async function updateAvailableCount( export async function updateAvailableCount(
tx: Transaction, tx: Transaction,
showPks: number[], showPks: number[] | SQLWrapper,
updateEntryCount = true, updateEntryCount = false,
) { ) {
const showPkQ = Array.isArray(showPks) ? sqlarr(showPks) : showPks;
return await tx return await tx
.update(shows) .update(shows)
.set({ .set({
@ -168,5 +169,5 @@ export async function updateAvailableCount(
)}`, )}`,
}), }),
}) })
.where(eq(shows.pk, sql`any(${sqlarr(showPks)})`)); .where(eq(shows.pk, sql`any(${showPkQ})`));
} }

View File

@ -8,6 +8,7 @@ import {
isUniqueConstraint, isUniqueConstraint,
jsonbBuildObject, jsonbBuildObject,
jsonbObjectAgg, jsonbObjectAgg,
sqlarr,
values, values,
} from "~/db/utils"; } from "~/db/utils";
import { KError } from "~/models/error"; import { KError } from "~/models/error";
@ -343,6 +344,17 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
}, },
{} as Record<number, { slug: string }[]>, {} as Record<number, { slug: string }[]>,
); );
await updateAvailableCount(
tx,
tx
.selectDistinct({ pk: entries.showPk })
.from(entries)
.where(
eq(entries.pk, sql`any(${sqlarr(ret.map((x) => x.entryPk))})`),
),
);
return error( return error(
201, 201,
vids.map((x) => ({ vids.map((x) => ({

View File

@ -4,7 +4,7 @@ import type { SelectResultField } from "drizzle-orm/query-builders/select.types"
export const buildRelations = < export const buildRelations = <
R extends string, R extends string,
P extends object, P extends object,
Rel extends Record<R, (languages: P) => Subquery>, Rel extends Record<R, (params: P) => Subquery>,
>( >(
enabled: R[], enabled: R[],
relations: Rel, relations: Rel,