Add helper function for entries' translations

This commit is contained in:
Zoe Roux 2025-07-19 15:26:48 +02:00
parent 616c7140d3
commit 8c8a974054
No known key found for this signature in database
3 changed files with 23 additions and 44 deletions

View File

@ -141,6 +141,17 @@ export const entryVideosQ = db
.leftJoin(videos, eq(videos.pk, entryVideoJoin.videoPk))
.as("videos");
export const getEntryTransQ = (languages: string[]) => {
return db
.selectDistinctOn([entryTranslations.pk])
.from(entryTranslations)
.orderBy(
entryTranslations.pk,
sql`array_position(${sqlarr(languages)}, ${entryTranslations.language})`,
)
.as("entry_t");
};
export const mapProgress = ({ aliased }: { aliased: boolean }) => {
const { time, percent, playedDate, videoId } = getColumns(entryProgressQ);
const ret = {
@ -174,15 +185,7 @@ export async function getEntries({
userId: string;
progressQ?: typeof entryProgressQ;
}): Promise<(Entry | Extra)[]> {
const transQ = db
.selectDistinctOn([entryTranslations.pk])
.from(entryTranslations)
.orderBy(
entryTranslations.pk,
sql`array_position(${sqlarr(languages)}, ${entryTranslations.language})`,
)
.as("t");
const { pk, name, ...transCol } = getColumns(transQ);
const transQ = getEntryTransQ(languages);
const {
kind,
@ -196,7 +199,7 @@ export async function getEntries({
return await db
.select({
...entryCol,
...transCol,
...getColumns(transQ),
videos: entryVideosQ.videos,
progress: mapProgress({ aliased: true }),
// specials don't have an `episodeNumber` but a `number` field.
@ -212,7 +215,7 @@ export async function getEntries({
order: sql<number>`${order}`,
seasonNumber: sql<number>`${seasonNumber}`,
episodeNumber: sql<number>`${episodeNumber}`,
name: sql<string>`${name}`,
name: sql<string>`${transQ.name}`,
})
.from(entries)
.innerJoin(transQ, eq(entries.pk, transQ.pk))

View File

@ -22,6 +22,7 @@ import {
entryFilters,
entryProgressQ,
entryVideosQ,
getEntryTransQ,
mapProgress,
} from "../entries";
@ -73,16 +74,7 @@ export const nextup = new Elysia({ tags: ["profiles"] })
jwt: { sub },
}) => {
const langs = processLanguages(languages);
const transQ = db
.selectDistinctOn([entryTranslations.pk])
.from(entryTranslations)
.orderBy(
entryTranslations.pk,
sql`array_position(${sqlarr(langs)}, ${entryTranslations.language})`,
)
.as("t");
const { pk, name, ...transCol } = getColumns(transQ);
const transQ = getEntryTransQ(langs);
const {
externalId,
@ -97,7 +89,7 @@ export const nextup = new Elysia({ tags: ["profiles"] })
const items = await db
.select({
...entryCol,
...transCol,
...getColumns(transQ),
videos: entryVideosQ.videos,
progress: mapProgress({ aliased: true }),
// specials don't have an `episodeNumber` but a `number` field.

View File

@ -36,7 +36,7 @@ import {
} from "~/models/utils";
import type { EmbeddedVideo } from "~/models/video";
import { WatchlistStatus } from "~/models/watchlist";
import { entryProgressQ, entryVideosQ, mapProgress } from "../entries";
import { entryProgressQ, entryVideosQ, getEntryTransQ, mapProgress } from "../entries";
export const watchStatusQ = db
.select({
@ -147,7 +147,7 @@ const showRelations = {
).as("json"),
})
.from(studios)
.leftJoin(studioTransQ, eq(studios.pk, studioTransQ.pk))
.innerJoin(studioTransQ, eq(studios.pk, studioTransQ.pk))
.where(
exists(
db
@ -185,21 +185,13 @@ const showRelations = {
.as("videos");
},
firstEntry: ({ languages }: { languages: string[] }) => {
const transQ = db
.selectDistinctOn([entryTranslations.pk])
.from(entryTranslations)
.orderBy(
entryTranslations.pk,
sql`array_position(${sqlarr(languages)}, ${entryTranslations.language})`,
)
.as("t");
const { pk, ...transCol } = getColumns(transQ);
const transQ = getEntryTransQ(languages);
return db
.select({
firstEntry: jsonbBuildObject<Entry>({
...getColumns(entries),
...transCol,
...getColumns(transQ),
number: entries.episodeNumber,
videos: entryVideosQ.videos,
progress: mapProgress({ aliased: false }),
@ -217,21 +209,13 @@ const showRelations = {
.as("firstEntry");
},
nextEntry: ({ languages }: { languages: string[] }) => {
const transQ = db
.selectDistinctOn([entryTranslations.pk])
.from(entryTranslations)
.orderBy(
entryTranslations.pk,
sql`array_position(${sqlarr(languages)}, ${entryTranslations.language})`,
)
.as("t");
const { pk, ...transCol } = getColumns(transQ);
const transQ = getEntryTransQ(languages);
return db
.select({
nextEntry: jsonbBuildObject<Entry>({
...getColumns(entries),
...transCol,
...getColumns(transQ),
number: entries.episodeNumber,
videos: entryVideosQ.videos,
progress: mapProgress({ aliased: false }),