diff --git a/api/src/controllers/seed/movies.ts b/api/src/controllers/seed/movies.ts index 65557781..2c32f492 100644 --- a/api/src/controllers/seed/movies.ts +++ b/api/src/controllers/seed/movies.ts @@ -9,8 +9,7 @@ import { shows, videos, } from "~/db/schema"; -import { conflictUpdateAllExcept } from "~/db/schema/utils"; -import { KErrorT } from "~/models/error"; +import { conflictUpdateAllExcept } from "~/db/utils"; import type { SeedMovie } from "~/models/movie"; import { processOptImage } from "./images"; import { guessNextRefresh } from "./refresh"; diff --git a/api/src/db/schema/shows.ts b/api/src/db/schema/shows.ts index a8229ae3..dbb5703f 100644 --- a/api/src/db/schema/shows.ts +++ b/api/src/db/schema/shows.ts @@ -1,4 +1,4 @@ -import { relations, SQL, sql } from "drizzle-orm"; +import { relations, type SQL, sql } from "drizzle-orm"; import { check, date, diff --git a/api/src/db/schema/utils.ts b/api/src/db/schema/utils.ts index 2b969cb0..a3126902 100644 --- a/api/src/db/schema/utils.ts +++ b/api/src/db/schema/utils.ts @@ -1,26 +1,4 @@ -import { - type ColumnsSelection, - type SQL, - type Subquery, - Table, - View, - ViewBaseConfig, - getTableColumns, - is, - sql, -} from "drizzle-orm"; -import type { CasingCache } from "drizzle-orm/casing"; -import type { AnyMySqlSelect } from "drizzle-orm/mysql-core"; -import { - type AnyPgSelect, - customType, - jsonb, - pgSchema, - varchar, -} from "drizzle-orm/pg-core"; -import type { AnySQLiteSelect } from "drizzle-orm/sqlite-core"; -import type { WithSubquery } from "drizzle-orm/subquery"; -import { db } from ".."; +import { customType, jsonb, pgSchema, varchar } from "drizzle-orm/pg-core"; export const schema = pgSchema("kyoo"); @@ -29,61 +7,6 @@ export const language = () => varchar({ length: 255 }); export const image = () => jsonb().$type<{ id: string; source: string; blurhash: string }>(); -// https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts#L58 -type Simplify = { [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 & Partial> ->; -export function getColumns< - T extends - | Table - | View - | Subquery - | WithSubquery - | 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; -} - -// See https://github.com/drizzle-team/drizzle-orm/issues/1728 -export function conflictUpdateAllExcept< - T extends Table, - E extends (keyof T["_"]["columns"])[], ->(table: T, except: E) { - const columns = getTableColumns(table); - const updateColumns = Object.entries(columns).filter( - ([col]) => !except.includes(col), - ); - - return updateColumns.reduce( - (acc, [colName, col]) => { - // @ts-expect-error: drizzle internal - const name = (db.dialect.casing as CasingCache).getColumnCasing(col); - acc[colName as keyof typeof acc] = sql.raw(`excluded."${name}"`); - return acc; - }, - {} as Omit, E[number]>, - ); -} - -// drizzle is bugged and doesn't allow js arrays to be used in raw sql. -export function sqlarr(array: unknown[]) { - return `{${array.map((item) => `"${item}"`).join(",")}}`; -} - // idk why they didn't implement this one export const tsvector = customType<{ data: string }>({ dataType() { diff --git a/api/src/db/utils.ts b/api/src/db/utils.ts new file mode 100644 index 00000000..48334a68 --- /dev/null +++ b/api/src/db/utils.ts @@ -0,0 +1,72 @@ +import { + is, + type ColumnsSelection, + type Subquery, + Table, + View, + ViewBaseConfig, + getTableColumns, + sql, + type SQL, +} from "drizzle-orm"; +import type { AnyPgSelect } from "drizzle-orm/pg-core"; +import type { AnyMySqlSelect } from "drizzle-orm/mysql-core"; +import type { AnySQLiteSelect } from "drizzle-orm/sqlite-core"; +import type { WithSubquery } from "drizzle-orm/subquery"; +import { db } from "./index"; +import type { CasingCache } from "drizzle-orm/casing"; + +// https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts#L58 +type Simplify = { [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 & Partial> +>; +export function getColumns< + T extends + | Table + | View + | Subquery + | WithSubquery + | 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; +} + +// See https://github.com/drizzle-team/drizzle-orm/issues/1728 +export function conflictUpdateAllExcept< + T extends Table, + E extends (keyof T["_"]["columns"])[], +>(table: T, except: E) { + const columns = getTableColumns(table); + const updateColumns = Object.entries(columns).filter( + ([col]) => !except.includes(col), + ); + + return updateColumns.reduce( + (acc, [colName, col]) => { + // @ts-expect-error: drizzle internal + const name = (db.dialect.casing as CasingCache).getColumnCasing(col); + acc[colName as keyof typeof acc] = sql.raw(`excluded."${name}"`); + return acc; + }, + {} as Omit, E[number]>, + ); +} + +// drizzle is bugged and doesn't allow js arrays to be used in raw sql. +export function sqlarr(array: unknown[]) { + return `{${array.map((item) => `"${item}"`).join(",")}}`; +}