From 12faca5fb544002c9443f8f53203c28b52811406 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 2 Mar 2025 21:44:55 +0100 Subject: [PATCH] Add db-metadata type helper --- api/src/models/collections.ts | 11 +++-------- api/src/models/movie.ts | 8 ++++---- api/src/models/season.ts | 13 +++++++++---- api/src/models/serie.ts | 25 +++++++++++++++++-------- api/src/models/studio.ts | 13 ++++++++----- api/src/models/utils/db-metadata.ts | 6 ++++++ api/src/models/utils/index.ts | 1 + api/src/models/video.ts | 18 ++++++++---------- 8 files changed, 56 insertions(+), 39 deletions(-) create mode 100644 api/src/models/utils/db-metadata.ts diff --git a/api/src/models/collections.ts b/api/src/models/collections.ts index 99958154..e7495e3c 100644 --- a/api/src/models/collections.ts +++ b/api/src/models/collections.ts @@ -2,6 +2,7 @@ import { t } from "elysia"; import type { Prettify } from "elysia/dist/types"; import { bubbleImages, duneCollection, registerExamples } from "./examples"; import { + DbMetadata, ExternalId, Genre, Image, @@ -33,7 +34,6 @@ const BaseCollection = t.Object({ }), ), - createdAt: t.String({ format: "date-time" }), nextRefresh: t.String({ format: "date-time" }), externalId: ExternalId, @@ -56,6 +56,7 @@ export const Collection = t.Intersect([ Resource(), CollectionTranslation, BaseCollection, + DbMetadata, ]); export type Collection = Prettify; @@ -68,13 +69,7 @@ export const FullCollection = t.Intersect([ export type FullCollection = Prettify; export const SeedCollection = t.Intersect([ - t.Omit(BaseCollection, [ - "kind", - "startAir", - "endAir", - "createdAt", - "nextRefresh", - ]), + t.Omit(BaseCollection, ["kind", "startAir", "endAir", "nextRefresh"]), t.Object({ slug: t.String({ format: "slug" }), translations: TranslationRecord( diff --git a/api/src/models/movie.ts b/api/src/models/movie.ts index a2d06708..210b8b8e 100644 --- a/api/src/models/movie.ts +++ b/api/src/models/movie.ts @@ -1,10 +1,10 @@ import { t } from "elysia"; import type { Prettify } from "~/utils"; import { SeedCollection } from "./collections"; -import { bubble, registerExamples } from "./examples"; -import { bubbleImages } from "./examples/bubble"; +import { bubble, bubbleImages, registerExamples } from "./examples"; import { SeedStudio } from "./studio"; import { + DbMetadata, ExternalId, Genre, Image, @@ -34,7 +34,6 @@ const BaseMovie = t.Object({ }), ), - createdAt: t.String({ format: "date-time" }), nextRefresh: t.String({ format: "date-time" }), externalId: ExternalId, @@ -59,6 +58,7 @@ export const Movie = t.Intersect([ Resource(), MovieTranslation, BaseMovie, + DbMetadata, // t.Object({ isAvailable: t.Boolean() }), ]); export type Movie = Prettify; @@ -73,7 +73,7 @@ export const FullMovie = t.Intersect([ export type FullMovie = Prettify; export const SeedMovie = t.Intersect([ - t.Omit(BaseMovie, ["kind", "createdAt", "nextRefresh"]), + t.Omit(BaseMovie, ["kind", "nextRefresh"]), t.Object({ slug: t.String({ format: "slug", examples: ["bubble"] }), translations: TranslationRecord( diff --git a/api/src/models/season.ts b/api/src/models/season.ts index cd90b215..225bbce9 100644 --- a/api/src/models/season.ts +++ b/api/src/models/season.ts @@ -1,6 +1,7 @@ import { t } from "elysia"; import type { Prettify } from "~/utils"; import { bubbleImages, madeInAbyss, registerExamples } from "./examples"; +import { DbMetadata } from "./utils"; import { SeasonId } from "./utils/external-id"; import { Image, SeedImage } from "./utils/image"; import { TranslationRecord } from "./utils/language"; @@ -11,7 +12,6 @@ export const BaseSeason = t.Object({ startAir: t.Nullable(t.String({ format: "date" })), endAir: t.Nullable(t.String({ format: "date" })), - createdAt: t.String({ format: "date-time" }), nextRefresh: t.String({ format: "date-time" }), externalId: SeasonId, @@ -27,11 +27,16 @@ export const SeasonTranslation = t.Object({ }); export type SeasonTranslation = typeof SeasonTranslation.static; -export const Season = t.Intersect([Resource(), SeasonTranslation, BaseSeason]); -export type Season = typeof Season.static; +export const Season = t.Intersect([ + Resource(), + SeasonTranslation, + BaseSeason, + DbMetadata, +]); +export type Season = Prettify; export const SeedSeason = t.Intersect([ - t.Omit(BaseSeason, ["createdAt", "nextRefresh"]), + t.Omit(BaseSeason, ["nextRefresh"]), t.Object({ translations: TranslationRecord( t.Intersect([ diff --git a/api/src/models/serie.ts b/api/src/models/serie.ts index 60d3fd01..3cce9511 100644 --- a/api/src/models/serie.ts +++ b/api/src/models/serie.ts @@ -5,11 +5,16 @@ import { SeedEntry, SeedExtra } from "./entry"; import { bubbleImages, madeInAbyss, registerExamples } from "./examples"; import { SeedSeason } from "./season"; import { SeedStudio } from "./studio"; -import { ExternalId } from "./utils/external-id"; -import { Genre } from "./utils/genres"; -import { Image, SeedImage } from "./utils/image"; -import { Language, TranslationRecord } from "./utils/language"; -import { Resource } from "./utils/resource"; +import { + DbMetadata, + ExternalId, + Genre, + Image, + Language, + Resource, + SeedImage, + TranslationRecord, +} from "./utils"; export const SerieStatus = t.UnionEnum([ "unknown", @@ -39,7 +44,6 @@ const BaseSerie = t.Object({ }), ), - createdAt: t.String({ format: "date-time" }), nextRefresh: t.String({ format: "date-time" }), externalId: ExternalId, @@ -60,7 +64,12 @@ export const SerieTranslation = t.Object({ }); export type SerieTranslation = typeof SerieTranslation.static; -export const Serie = t.Intersect([Resource(), SerieTranslation, BaseSerie]); +export const Serie = t.Intersect([ + Resource(), + SerieTranslation, + BaseSerie, + DbMetadata, +]); export type Serie = Prettify; export const FullSerie = t.Intersect([ @@ -72,7 +81,7 @@ export const FullSerie = t.Intersect([ export type FullMovie = Prettify; export const SeedSerie = t.Intersect([ - t.Omit(BaseSerie, ["kind", "createdAt", "nextRefresh"]), + t.Omit(BaseSerie, ["kind", "nextRefresh"]), t.Object({ slug: t.String({ format: "slug" }), translations: TranslationRecord( diff --git a/api/src/models/studio.ts b/api/src/models/studio.ts index 63577817..8b211fde 100644 --- a/api/src/models/studio.ts +++ b/api/src/models/studio.ts @@ -1,12 +1,10 @@ import { t } from "elysia"; import type { Prettify } from "elysia/dist/types"; import { madeInAbyss, registerExamples } from "./examples"; -import { ExternalId, Resource, TranslationRecord } from "./utils"; +import { DbMetadata, ExternalId, Resource, TranslationRecord } from "./utils"; import { Image, SeedImage } from "./utils/image"; const BaseStudio = t.Object({ - createdAt: t.String({ format: "date-time" }), - externalId: ExternalId, }); @@ -15,11 +13,16 @@ export const StudioTranslation = t.Object({ logo: t.Nullable(Image), }); -export const Studio = t.Intersect([Resource(), StudioTranslation, BaseStudio]); +export const Studio = t.Intersect([ + Resource(), + StudioTranslation, + BaseStudio, + DbMetadata, +]); export type Studio = Prettify; export const SeedStudio = t.Intersect([ - t.Omit(BaseStudio, ["createdAt"]), + BaseStudio, t.Object({ slug: t.String({ format: "slug" }), translations: TranslationRecord( diff --git a/api/src/models/utils/db-metadata.ts b/api/src/models/utils/db-metadata.ts new file mode 100644 index 00000000..ea2f9b7a --- /dev/null +++ b/api/src/models/utils/db-metadata.ts @@ -0,0 +1,6 @@ +import { t } from "elysia"; + +export const DbMetadata = t.Object({ + createdAt: t.String({ format: "date-time" }), + updatedAt: t.String({ format: "date-time" }), +}); diff --git a/api/src/models/utils/index.ts b/api/src/models/utils/index.ts index 70c08626..4326dfc6 100644 --- a/api/src/models/utils/index.ts +++ b/api/src/models/utils/index.ts @@ -7,3 +7,4 @@ export * from "./filters"; export * from "./page"; export * from "./sort"; export * from "./keyset-paginate"; +export * from "./db-metadata"; diff --git a/api/src/models/video.ts b/api/src/models/video.ts index fbc2509f..7f8f6681 100644 --- a/api/src/models/video.ts +++ b/api/src/models/video.ts @@ -1,10 +1,9 @@ import { t } from "elysia"; -import { comment } from "../utils"; +import { type Prettify, comment } from "~/utils"; import { bubbleVideo, registerExamples } from "./examples"; +import { DbMetadata, Resource } from "./utils"; -export const Video = t.Object({ - id: t.String({ format: "uuid" }), - slug: t.String({ format: "slug" }), +export const SeedVideo = t.Object({ path: t.String(), rendering: t.String({ description: comment` @@ -30,8 +29,6 @@ export const Video = t.Object({ "Kyoo will prefer playing back the highest `version` number if there are multiples rendering.", }), - createdAt: t.String({ format: "date-time" }), - guess: t.Optional( t.Recursive((Self) => t.Object( @@ -69,8 +66,9 @@ export const Video = t.Object({ ), ), }); -export type Video = typeof Video.static; -registerExamples(Video, bubbleVideo); - -export const SeedVideo = t.Omit(Video, ["id", "slug", "createdAt"]); export type SeedVideo = typeof SeedVideo.static; + +export const Video = t.Intersect([Resource(), SeedVideo, DbMetadata]); +export type Video = Prettify; + +registerExamples(Video, bubbleVideo);