mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-03 13:44:33 -04:00
Fix subquery handling of translations
This commit is contained in:
parent
78e84cf960
commit
a37c4fe723
@ -2,34 +2,35 @@ import { Elysia, t } from "elysia";
|
|||||||
import { Movie } from "../models/movie";
|
import { Movie } from "../models/movie";
|
||||||
import { db } from "../db";
|
import { db } from "../db";
|
||||||
import { shows, showTranslations } from "../db/schema/shows";
|
import { shows, showTranslations } from "../db/schema/shows";
|
||||||
import { eq, and, sql, or, inArray, getTableColumns } from "drizzle-orm";
|
import { eq, and, sql, or, inArray } from "drizzle-orm";
|
||||||
|
import { getColumns } from "../db/schema/utils";
|
||||||
|
|
||||||
|
const translations = db
|
||||||
|
.selectDistinctOn([showTranslations.language])
|
||||||
|
.from(showTranslations)
|
||||||
|
.where(
|
||||||
|
or(
|
||||||
|
inArray(showTranslations.language, sql.placeholder("langs")),
|
||||||
|
eq(showTranslations.language, shows.originalLanguage),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.orderBy(
|
||||||
|
sql`array_position(${showTranslations.language}, ${sql.placeholder("langs")})`,
|
||||||
|
)
|
||||||
|
.as("t");
|
||||||
|
|
||||||
|
const { pk: _, kind, startAir, endAir, ...moviesCol } = getColumns(shows);
|
||||||
|
const { pk, language, ...translationsCol } = getColumns(translations);
|
||||||
|
|
||||||
const { pk: _, kind, startAir, endAir, ...moviesCol } = getTableColumns(shows);
|
|
||||||
const { pk, language, ...translationsCol } = getTableColumns(showTranslations);
|
|
||||||
|
|
||||||
const findMovie = db
|
const findMovie = db
|
||||||
.select({
|
.select({
|
||||||
...moviesCol,
|
...moviesCol,
|
||||||
airDate: startAir,
|
airDate: startAir,
|
||||||
...translationsCol,
|
translations: translationsCol,
|
||||||
})
|
})
|
||||||
.from(shows)
|
.from(shows)
|
||||||
.innerJoin(
|
.innerJoin(translations, eq(shows.pk, translations.pk))
|
||||||
db
|
|
||||||
.selectDistinctOn([showTranslations.language])
|
|
||||||
.from(showTranslations)
|
|
||||||
.where(
|
|
||||||
or(
|
|
||||||
inArray(showTranslations.language, sql.placeholder("langs")),
|
|
||||||
eq(showTranslations.language, shows.originalLanguage),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.orderBy(
|
|
||||||
sql`array_position(${showTranslations.language}, ${sql.placeholder("langs")})`,
|
|
||||||
)
|
|
||||||
.as("t"),
|
|
||||||
eq(shows.pk, showTranslations.pk),
|
|
||||||
)
|
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(shows.kind, "movie"),
|
eq(shows.kind, "movie"),
|
||||||
|
@ -1,4 +1,20 @@
|
|||||||
import { jsonb, pgSchema, varchar } from "drizzle-orm/pg-core";
|
import {
|
||||||
|
is,
|
||||||
|
type ColumnsSelection,
|
||||||
|
type Subquery,
|
||||||
|
Table,
|
||||||
|
View,
|
||||||
|
ViewBaseConfig,
|
||||||
|
} from "drizzle-orm";
|
||||||
|
import type { AnyMySqlSelect } from "drizzle-orm/mysql-core";
|
||||||
|
import {
|
||||||
|
type AnyPgSelect,
|
||||||
|
jsonb,
|
||||||
|
pgSchema,
|
||||||
|
varchar,
|
||||||
|
} from "drizzle-orm/pg-core";
|
||||||
|
import type { AnySQLiteSelect } from "drizzle-orm/sqlite-core";
|
||||||
|
import type { WithSubquery } from "drizzle-orm/subquery";
|
||||||
|
|
||||||
export const schema = pgSchema("kyoo");
|
export const schema = pgSchema("kyoo");
|
||||||
|
|
||||||
@ -20,3 +36,30 @@ export const externalid = () =>
|
|||||||
>()
|
>()
|
||||||
.notNull()
|
.notNull()
|
||||||
.default({});
|
.default({});
|
||||||
|
|
||||||
|
// https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts#L58
|
||||||
|
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
||||||
|
|
||||||
|
// See https://github.com/drizzle-team/drizzle-orm/pull/1789
|
||||||
|
type Select = AnyPgSelect | AnyMySqlSelect | AnySQLiteSelect;
|
||||||
|
type AnySelect = Simplify<Omit<Select, "where"> & Partial<Pick<Select, "where">>>;
|
||||||
|
export function getColumns<
|
||||||
|
T extends
|
||||||
|
| Table
|
||||||
|
| View
|
||||||
|
| Subquery<string, ColumnsSelection>
|
||||||
|
| WithSubquery<string, ColumnsSelection>
|
||||||
|
| AnySelect,
|
||||||
|
>(
|
||||||
|
table: T,
|
||||||
|
): T extends Table
|
||||||
|
? T["_"]["columns"]
|
||||||
|
: T extends View | Subquery | WithSubquery | AnySelect
|
||||||
|
? T["_"]["selectedFields"]
|
||||||
|
: never {
|
||||||
|
return is(table, Table)
|
||||||
|
? (table as any)[(Table as any).Symbol.Columns]
|
||||||
|
: is(table, View)
|
||||||
|
? (table as any)[ViewBaseConfig].selectedFields
|
||||||
|
: table._.selectedFields;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user