Skip to content

Commit

Permalink
v5 api: initial schemas, types & basic routes (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoriya authored Jan 10, 2025
2 parents f59dd37 + fe6f4fd commit 3aee4bd
Show file tree
Hide file tree
Showing 77 changed files with 7,781 additions and 6 deletions.
14 changes: 14 additions & 0 deletions api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# vi: ft=sh
# shellcheck disable=SC2034

# either an hard-coded secret to decode jwts or empty to use keibi's public secret.
# this should only be used in tests
JWT_SECRET=
# keibi's server to retrive the public jwt secret
AUHT_SERVER=http://auth:4568

POSTGRES_USER=kyoo
POSTGRES_PASSWORD=password
POSTGRES_DB=kyooDB
POSTGRES_SERVER=postgres
POSTGRES_PORT=5432
2 changes: 2 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
**/*.bun
12 changes: 6 additions & 6 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ erDiagram
datetime next_refresh
jsonb external_id
}
season_translations {
guid id PK,FK
string language PK
Expand All @@ -107,17 +107,17 @@ erDiagram
jsonb poster
jsonb banner
jsonb logo
jsonb thumbnail
jsonb thumbnail
}
seasons ||--|{ season_translations : has
seasons ||--o{ entries : has
shows ||--|{ seasons : has
watched_shows {
guid show_id PK, FK
guid user_id PK, FK
status status "completed|watching|droped|planned"
uint seen_entry_count "NN"
uint seen_entry_count "NN"
}
shows ||--|{ watched_shows : has
Expand All @@ -129,7 +129,7 @@ erDiagram
datetime played_date
}
entries ||--|{ watched_entries : has
roles {
guid show_id PK, FK
guid staff_id PK, FK
Expand All @@ -152,7 +152,7 @@ erDiagram
datetime next_refresh
jsonb external_id
}
staff_translations {
guid id PK,FK
string language PK
Expand Down
6 changes: 6 additions & 0 deletions api/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["../biome.json"],
"formatter": {
"lineWidth": 80
}
}
237 changes: 237 additions & 0 deletions api/bun.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
preload = ["./tests/setup.ts"]
14 changes: 14 additions & 0 deletions api/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from "drizzle-kit";

export default defineConfig({
out: "./drizzle",
schema: "./src/db/schema",
dialect: "postgresql",
casing: "snake_case",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
migrations: {
schema: "kyoo",
},
});
102 changes: 102 additions & 0 deletions api/drizzle/0000_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
CREATE TYPE "kyoo"."entry_type" AS ENUM('unknown', 'episode', 'movie', 'special', 'extra');--> statement-breakpoint
CREATE TYPE "kyoo"."genres" AS ENUM('action', 'adventure', 'animation', 'comedy', 'crime', 'documentary', 'drama', 'family', 'fantasy', 'history', 'horror', 'music', 'mystery', 'romance', 'science-fiction', 'thriller', 'war', 'western', 'kids', 'reality', 'politics', 'soap', 'talk');--> statement-breakpoint
CREATE TYPE "kyoo"."show_kind" AS ENUM('serie', 'movie');--> statement-breakpoint
CREATE TYPE "kyoo"."show_status" AS ENUM('unknown', 'finished', 'airing', 'planned');--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "kyoo"."entries" (
"pk" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "kyoo"."entries_pk_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
"id" uuid DEFAULT gen_random_uuid() NOT NULL,
"slug" varchar(255) NOT NULL,
"show_pk" integer,
"order" integer NOT NULL,
"season_number" integer,
"episode_number" integer,
"type" "kyoo"."entry_type" NOT NULL,
"air_date" date,
"runtime" integer,
"thumbnails" jsonb,
"external_id" jsonb DEFAULT '{}'::jsonb NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
"next_refresh" timestamp with time zone,
CONSTRAINT "entries_id_unique" UNIQUE("id"),
CONSTRAINT "entries_slug_unique" UNIQUE("slug"),
CONSTRAINT "entries_showPk_seasonNumber_episodeNumber_unique" UNIQUE("show_pk","season_number","episode_number"),
CONSTRAINT "order_positive" CHECK ("entries"."order" >= 0)
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "kyoo"."entries_translation" (
"pk" integer NOT NULL,
"language" varchar(255) NOT NULL,
"name" text,
"description" text,
CONSTRAINT "entries_translation_pk_language_pk" PRIMARY KEY("pk","language")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "kyoo"."show_translations" (
"pk" integer NOT NULL,
"language" varchar(255) NOT NULL,
"name" text NOT NULL,
"description" text,
"tagline" text,
"aliases" text[] NOT NULL,
"tags" text[] NOT NULL,
"trailer_url" text,
"poster" jsonb,
"thumbnail" jsonb,
"banner" jsonb,
"logo" jsonb,
CONSTRAINT "show_translations_pk_language_pk" PRIMARY KEY("pk","language")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "kyoo"."shows" (
"pk" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "kyoo"."shows_pk_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
"id" uuid DEFAULT gen_random_uuid() NOT NULL,
"slug" varchar(255) NOT NULL,
"kind" "kyoo"."show_kind" NOT NULL,
"genres" "kyoo"."genres"[] NOT NULL,
"rating" smallint,
"runtime" integer,
"status" "kyoo"."show_status" NOT NULL,
"start_air" date,
"end_air" date,
"original_language" varchar(255),
"external_id" jsonb DEFAULT '{}'::jsonb NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"next_refresh" timestamp with time zone NOT NULL,
CONSTRAINT "shows_id_unique" UNIQUE("id"),
CONSTRAINT "shows_slug_unique" UNIQUE("slug"),
CONSTRAINT "rating_valid" CHECK ("shows"."rating" between 0 and 100),
CONSTRAINT "runtime_valid" CHECK ("shows"."runtime" >= 0)
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "kyoo"."videos" (
"pk" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "kyoo"."videos_pk_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
"id" uuid DEFAULT gen_random_uuid() NOT NULL,
"path" text NOT NULL,
"rendering" integer,
"part" integer,
"version" integer,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "videos_id_unique" UNIQUE("id"),
CONSTRAINT "videos_path_unique" UNIQUE("path"),
CONSTRAINT "rendering_pos" CHECK ("videos"."rendering" >= 0),
CONSTRAINT "part_pos" CHECK ("videos"."part" >= 0),
CONSTRAINT "version_pos" CHECK ("videos"."version" >= 0)
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "kyoo"."entries" ADD CONSTRAINT "entries_show_pk_shows_pk_fk" FOREIGN KEY ("show_pk") REFERENCES "kyoo"."shows"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "kyoo"."entries_translation" ADD CONSTRAINT "entries_translation_pk_entries_pk_fk" FOREIGN KEY ("pk") REFERENCES "kyoo"."entries"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "kyoo"."show_translations" ADD CONSTRAINT "show_translations_pk_shows_pk_fk" FOREIGN KEY ("pk") REFERENCES "kyoo"."shows"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
7 changes: 7 additions & 0 deletions api/drizzle/0001_video.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE "kyoo"."videos" DROP CONSTRAINT "rendering_pos";--> statement-breakpoint
ALTER TABLE "kyoo"."videos" ALTER COLUMN "rendering" SET DATA TYPE text;--> statement-breakpoint
ALTER TABLE "kyoo"."videos" ALTER COLUMN "rendering" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "kyoo"."videos" ALTER COLUMN "version" SET DEFAULT 1;--> statement-breakpoint
ALTER TABLE "kyoo"."videos" ALTER COLUMN "version" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "kyoo"."videos" ADD COLUMN "slug" varchar(255) NOT NULL;--> statement-breakpoint
ALTER TABLE "kyoo"."videos" ADD CONSTRAINT "videos_slug_unique" UNIQUE("slug");
40 changes: 40 additions & 0 deletions api/drizzle/0002_seasons.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
CREATE TABLE IF NOT EXISTS "kyoo"."season_translation" (
"pk" integer NOT NULL,
"language" varchar(255) NOT NULL,
"name" text,
"description" text,
"poster" jsonb,
"thumbnail" jsonb,
"logo" jsonb,
"banner" jsonb,
CONSTRAINT "season_translation_pk_language_pk" PRIMARY KEY("pk","language")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "kyoo"."seasons" (
"pk" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "kyoo"."seasons_pk_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
"id" uuid DEFAULT gen_random_uuid() NOT NULL,
"slug" varchar(255) NOT NULL,
"show_pk" integer,
"season_number" integer NOT NULL,
"start_air" date,
"end_air" date,
"external_id" jsonb DEFAULT '{}'::jsonb NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
"next_refresh" timestamp with time zone,
CONSTRAINT "seasons_id_unique" UNIQUE("id"),
CONSTRAINT "seasons_slug_unique" UNIQUE("slug"),
CONSTRAINT "seasons_showPk_seasonNumber_unique" UNIQUE("show_pk","season_number")
);
--> statement-breakpoint
ALTER TABLE "kyoo"."entries" ALTER COLUMN "order" DROP NOT NULL;--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "kyoo"."season_translation" ADD CONSTRAINT "season_translation_pk_seasons_pk_fk" FOREIGN KEY ("pk") REFERENCES "kyoo"."seasons"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "kyoo"."seasons" ADD CONSTRAINT "seasons_show_pk_shows_pk_fk" FOREIGN KEY ("show_pk") REFERENCES "kyoo"."shows"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
3 changes: 3 additions & 0 deletions api/drizzle/0003_order.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "kyoo"."entries" ALTER COLUMN "order" SET DATA TYPE real;--> statement-breakpoint
ALTER TABLE "kyoo"."entries_translation" ADD COLUMN "tagline" text;--> statement-breakpoint
ALTER TABLE "kyoo"."season_translation" DROP COLUMN IF EXISTS "logo";
56 changes: 56 additions & 0 deletions api/drizzle/0004_jointures.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
CREATE TABLE IF NOT EXISTS "kyoo"."entry_video_jointure" (
"entry" integer NOT NULL,
"video" integer NOT NULL,
"slug" varchar(255) NOT NULL,
CONSTRAINT "entry_video_jointure_entry_video_pk" PRIMARY KEY("entry","video"),
CONSTRAINT "entry_video_jointure_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
ALTER TABLE "kyoo"."entries_translation" RENAME TO "entry_translations";--> statement-breakpoint
ALTER TABLE "kyoo"."season_translation" RENAME TO "season_translations";--> statement-breakpoint
ALTER TABLE "kyoo"."videos" DROP CONSTRAINT "videos_slug_unique";--> statement-breakpoint
ALTER TABLE "kyoo"."entries" DROP CONSTRAINT "order_positive";--> statement-breakpoint
ALTER TABLE "kyoo"."shows" DROP CONSTRAINT "rating_valid";--> statement-breakpoint
ALTER TABLE "kyoo"."shows" DROP CONSTRAINT "runtime_valid";--> statement-breakpoint
ALTER TABLE "kyoo"."videos" DROP CONSTRAINT "part_pos";--> statement-breakpoint
ALTER TABLE "kyoo"."videos" DROP CONSTRAINT "version_pos";--> statement-breakpoint
ALTER TABLE "kyoo"."entry_translations" DROP CONSTRAINT "entries_translation_pk_entries_pk_fk";
--> statement-breakpoint
ALTER TABLE "kyoo"."season_translations" DROP CONSTRAINT "season_translation_pk_seasons_pk_fk";
--> statement-breakpoint
ALTER TABLE "kyoo"."entry_translations" DROP CONSTRAINT "entries_translation_pk_language_pk";--> statement-breakpoint
ALTER TABLE "kyoo"."season_translations" DROP CONSTRAINT "season_translation_pk_language_pk";--> statement-breakpoint
ALTER TABLE "kyoo"."entry_translations" ADD CONSTRAINT "entry_translations_pk_language_pk" PRIMARY KEY("pk","language");--> statement-breakpoint
ALTER TABLE "kyoo"."season_translations" ADD CONSTRAINT "season_translations_pk_language_pk" PRIMARY KEY("pk","language");--> statement-breakpoint
ALTER TABLE "kyoo"."entry_translations" ADD COLUMN "poster" jsonb;--> statement-breakpoint
ALTER TABLE "kyoo"."videos" ADD COLUMN "guess" jsonb DEFAULT '{}'::jsonb NOT NULL;--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "kyoo"."entry_video_jointure" ADD CONSTRAINT "entry_video_jointure_entry_entries_pk_fk" FOREIGN KEY ("entry") REFERENCES "kyoo"."entries"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "kyoo"."entry_video_jointure" ADD CONSTRAINT "entry_video_jointure_video_videos_pk_fk" FOREIGN KEY ("video") REFERENCES "kyoo"."videos"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "kyoo"."entry_translations" ADD CONSTRAINT "entry_translations_pk_entries_pk_fk" FOREIGN KEY ("pk") REFERENCES "kyoo"."entries"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "kyoo"."season_translations" ADD CONSTRAINT "season_translations_pk_seasons_pk_fk" FOREIGN KEY ("pk") REFERENCES "kyoo"."seasons"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
ALTER TABLE "kyoo"."videos" DROP COLUMN IF EXISTS "slug";--> statement-breakpoint
ALTER TABLE "kyoo"."entries" ADD CONSTRAINT "order_positive" CHECK ("kyoo"."entries"."order" >= 0);--> statement-breakpoint
ALTER TABLE "kyoo"."shows" ADD CONSTRAINT "rating_valid" CHECK ("kyoo"."shows"."rating" between 0 and 100);--> statement-breakpoint
ALTER TABLE "kyoo"."shows" ADD CONSTRAINT "runtime_valid" CHECK ("kyoo"."shows"."runtime" >= 0);--> statement-breakpoint
ALTER TABLE "kyoo"."videos" ADD CONSTRAINT "part_pos" CHECK ("kyoo"."videos"."part" >= 0);--> statement-breakpoint
ALTER TABLE "kyoo"."videos" ADD CONSTRAINT "version_pos" CHECK ("kyoo"."videos"."version" >= 0);
Loading

0 comments on commit 3aee4bd

Please sign in to comment.