Cleanup video join in entries

This commit is contained in:
Zoe Roux
2025-03-09 01:39:41 +01:00
parent c161d680e3
commit fc60fcc7c4
2 changed files with 23 additions and 10 deletions
+15 -10
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,