diff --git a/api/src/controllers/movies.ts b/api/src/controllers/movies.ts index 024f9bb0..21a4f3e6 100644 --- a/api/src/controllers/movies.ts +++ b/api/src/controllers/movies.ts @@ -1,6 +1,6 @@ import { type SQL, and, eq, exists, sql } from "drizzle-orm"; import { Elysia, t } from "elysia"; -import { entries, entryVideoJoint, showTranslations, shows } from "~/db/schema"; +import { entries, entryVideoJoin, showTranslations, shows } from "~/db/schema"; import { getColumns, sqlarr } from "~/db/utils"; import { KError } from "~/models/error"; import { bubble } from "~/models/examples"; @@ -81,8 +81,8 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] }) exists( db .select() - .from(entryVideoJoint) - .where(eq(entries.pk, entryVideoJoint.entry)), + .from(entryVideoJoin) + .where(eq(entries.pk, entryVideoJoin.entry)), ), ), ), @@ -258,8 +258,8 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] }) exists( db .select() - .from(entryVideoJoint) - .where(eq(entries.pk, entryVideoJoint.entry)), + .from(entryVideoJoin) + .where(eq(entries.pk, entryVideoJoin.entry)), ), ) .as("video"); diff --git a/api/src/controllers/seed/insert/entries.ts b/api/src/controllers/seed/insert/entries.ts index 3cc0c84c..1a39d8c2 100644 --- a/api/src/controllers/seed/insert/entries.ts +++ b/api/src/controllers/seed/insert/entries.ts @@ -3,7 +3,7 @@ import { db } from "~/db"; import { entries, entryTranslations, - entryVideoJoint as entryVideoJoin, + entryVideoJoin, videos, } from "~/db/schema"; import { conflictUpdateAllExcept, values } from "~/db/utils"; diff --git a/api/src/db/schema/entries.ts b/api/src/db/schema/entries.ts index 64380470..b95f55d7 100644 --- a/api/src/db/schema/entries.ts +++ b/api/src/db/schema/entries.ts @@ -1,4 +1,4 @@ -import { sql } from "drizzle-orm"; +import { relations, sql } from "drizzle-orm"; import { check, date, @@ -14,6 +14,7 @@ import { } from "drizzle-orm/pg-core"; import { shows } from "./shows"; import { image, language, schema } from "./utils"; +import { entryVideoJoin } from "./videos"; export const entryType = schema.enum("entry_type", [ "unknown", @@ -92,3 +93,16 @@ export const entryTranslations = schema.table( }, (t) => [primaryKey({ columns: [t.pk, t.language] })], ); + +export const entryRelations = relations(entries, ({ many }) => ({ + translations: many(entryTranslations, { relationName: "entryTranslations" }), + evj: many(entryVideoJoin, { relationName: "evj_entry" }), +})); + +export const entryTrRelations = relations(entryTranslations, ({ one }) => ({ + entry: one(entries, { + relationName: "entryTranslations", + fields: [entryTranslations.pk], + references: [entries.pk], + }), +})); diff --git a/api/src/db/schema/videos.ts b/api/src/db/schema/videos.ts index c233b0bc..2464629f 100644 --- a/api/src/db/schema/videos.ts +++ b/api/src/db/schema/videos.ts @@ -1,4 +1,4 @@ -import { sql } from "drizzle-orm"; +import { relations, sql } from "drizzle-orm"; import { check, integer, @@ -33,7 +33,7 @@ export const videos = schema.table( ], ); -export const entryVideoJoint = schema.table( +export const entryVideoJoin = schema.table( "entry_video_jointure", { entry: integer() @@ -46,3 +46,22 @@ export const entryVideoJoint = schema.table( }, (t) => [primaryKey({ columns: [t.entry, t.video] })], ); + +export const videosRelations = relations(videos, ({ many }) => ({ + evj: many(entryVideoJoin, { + relationName: "evj_video", + }), +})); + +export const evjRelations = relations(entryVideoJoin, ({ one }) => ({ + video: one(videos, { + relationName: "evj_video", + fields: [entryVideoJoin.video], + references: [videos.pk], + }), + entry: one(entries, { + relationName: "evj_entry", + fields: [entryVideoJoin.entry], + references: [entries.pk], + }), +}));