Cleanup video join in entries

This commit is contained in:
Zoe Roux 2025-03-09 01:39:41 +01:00
parent c161d680e3
commit fc60fcc7c4
No known key found for this signature in database
2 changed files with 23 additions and 10 deletions

View File

@ -9,7 +9,13 @@ import {
shows,
videos,
} from "~/db/schema";
import { getColumns, sqlarr } from "~/db/utils";
import {
coalesce,
getColumns,
jsonbAgg,
jsonbBuildObject,
sqlarr,
} from "~/db/utils";
import {
Entry,
type EntryKind,
@ -107,17 +113,16 @@ async function getEntries({
const { guess, createdAt, updatedAt, ...videosCol } = getColumns(videos);
const videosQ = db
.select({ slug: entryVideoJoin.slug, ...videosCol })
.select({
videos: coalesce(
jsonbAgg(jsonbBuildObject({ slug: entryVideoJoin.slug, ...videosCol })),
sql`'[]'::jsonb`,
),
})
.from(entryVideoJoin)
.where(eq(entryVideoJoin.entryPk, entries.pk))
.leftJoin(videos, eq(videos.pk, entryVideoJoin.videoPk))
.as("videos");
const videosJ = db
.select({
videos: sql`coalesce(json_agg("videos"), '[]'::json)`.as("videos"),
})
.from(videosQ)
.as("videos_json");
const {
kind,
@ -132,7 +137,7 @@ async function getEntries({
.select({
...entryCol,
...transCol,
videos: videosJ.videos,
videos: videosQ.videos,
// specials don't have an `episodeNumber` but a `number` field.
number: episodeNumber,
@ -150,7 +155,7 @@ async function getEntries({
})
.from(entries)
.innerJoin(transQ, eq(entries.pk, transQ.pk))
.leftJoinLateral(videosJ, sql`true`)
.leftJoinLateral(videosQ, sql`true`)
.where(
and(
filter,

View File

@ -94,10 +94,18 @@ export function values(items: Record<string, unknown>[]) {
};
}
export const coalesce = (val: SQLWrapper, def: SQLWrapper) => {
return sql`coalesce(${val}, ${def})`;
};
export const jsonbObjectAgg = (key: SQLWrapper, value: SQLWrapper) => {
return sql`jsonb_object_agg(${sql.join([key, value], sql.raw(","))})`;
};
export const jsonbAgg = (val: SQLWrapper) => {
return sql`jsonb_agg(${val})`;
};
export const jsonbBuildObject = (select: Record<string, SQLWrapper>) => {
const query = sql.join(
Object.entries(select).flatMap(([k, v]) => {