mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Fix schema utils to make drizzle-kit work
This commit is contained in:
parent
a51de86c95
commit
988b705a30
@ -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";
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { relations, SQL, sql } from "drizzle-orm";
|
||||
import { relations, type SQL, sql } from "drizzle-orm";
|
||||
import {
|
||||
check,
|
||||
date,
|
||||
|
@ -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<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;
|
||||
}
|
||||
|
||||
// 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<Record<keyof T["_"]["columns"], SQL>, 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() {
|
||||
|
72
api/src/db/utils.ts
Normal file
72
api/src/db/utils.ts
Normal file
@ -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<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;
|
||||
}
|
||||
|
||||
// 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<Record<keyof T["_"]["columns"], SQL>, 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(",")}}`;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user