References table
This commit is contained in:
@@ -11,11 +11,13 @@ export async function GET() {
|
||||
const enka = await fetch("https://enka.network/api/uid/888888888/?info", {
|
||||
cache: "force-cache",
|
||||
headers: { "User-Agent": "Buzz Event Platform(Health Checker)" },
|
||||
signal: AbortSignal.timeout(5000)
|
||||
})
|
||||
.then((r) => r.ok)
|
||||
.catch(() => false);
|
||||
const amber = await fetch("https://gi.yatta.moe/api/v2/en/avatar", {
|
||||
cache: "force-cache",
|
||||
signal: AbortSignal.timeout(5000)
|
||||
})
|
||||
.then((r) => r.ok)
|
||||
.catch(() => false);
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ import { defineConfig } from "drizzle-kit";
|
||||
|
||||
export default defineConfig({
|
||||
out: "./drizzle",
|
||||
schema: "./lib/db/schema.ts",
|
||||
schema: ["./lib/db/schema.ts", "./lib/db/references.ts"],
|
||||
schemaFilter: ["public", "artifact", "tierlist"],
|
||||
dialect: "postgresql",
|
||||
dbCredentials: {
|
||||
|
||||
+1
-1
@@ -270,7 +270,7 @@ export async function cdnify(
|
||||
const [{ id }] = await tx
|
||||
.insert(cdn)
|
||||
.values({
|
||||
name: name || data instanceof File ? (data as File).name : null,
|
||||
name: name || (data instanceof File ? (data as File).name : null),
|
||||
data: Buffer.from(await data.arrayBuffer()),
|
||||
size: `${data.size}`,
|
||||
type: data.type,
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ export const auth = betterAuth({
|
||||
"http://nyx:3000",
|
||||
"http://astral:3000",
|
||||
"http://localhost:3000",
|
||||
"https://dev3000.dgnr.us",
|
||||
"https://dev3000.dgnr.us"
|
||||
],
|
||||
database: drizzleAdapter(db, {
|
||||
provider: "pg", // or "mysql", "sqlite"
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { getTableColumns, isNotNull, isTable, sql } from "drizzle-orm";
|
||||
import { getTableConfig, pgView } from "drizzle-orm/pg-core";
|
||||
import * as Schema from "./schema";
|
||||
|
||||
const tables = Object.values(Schema)
|
||||
.filter((t) => isTable(t))
|
||||
.flatMap((table) => {
|
||||
const config = getTableConfig(table);
|
||||
const references = config.foreignKeys.map((fk) => fk.reference());
|
||||
const cdnColumns = config.columns
|
||||
.filter((col) => {
|
||||
// Check if column references cdn.id
|
||||
const fk = references.find((fk) => fk.columns.some((c) => c === col));
|
||||
return (
|
||||
fk?.foreignTable === Schema.cdn &&
|
||||
fk?.foreignColumns?.[0] === Schema.cdn.id
|
||||
);
|
||||
})
|
||||
.map((col) => col.name);
|
||||
|
||||
return cdnColumns.map((colName) => ({
|
||||
table,
|
||||
column: colName,
|
||||
}));
|
||||
});
|
||||
|
||||
// console.log(tables.map((t) => ({ column: t.column, table: getTableConfig(t.table).name })));
|
||||
|
||||
// @ts-expect-error
|
||||
export const cdnReferences = pgView("cdn_references").as((qb) => {
|
||||
const queries = tables.reduce((prev, { table, column }) => {
|
||||
const cols = getTableColumns(table);
|
||||
const query = qb
|
||||
.select({
|
||||
cdn: sql<string>`${cols[column as keyof typeof cols]}`.as("cdn"),
|
||||
id: cols["id" as keyof typeof cols],
|
||||
column: sql<string>`${column}`.as("column"),
|
||||
table: sql<string>`${getTableConfig(table).name}`.as("table"),
|
||||
})
|
||||
.from(table)
|
||||
.where(isNotNull(cols[column as keyof typeof cols]));
|
||||
// @ts-expect-error
|
||||
if (prev) return prev.unionAll(query);
|
||||
return query;
|
||||
}, null);
|
||||
return queries;
|
||||
});
|
||||
Executable → Regular
Reference in New Issue
Block a user