Type json in drizzle schemas

This commit is contained in:
Zoe Roux 2024-11-01 21:47:18 +01:00
parent 908e06c88f
commit ed5d677ae1
No known key found for this signature in database
8 changed files with 1163 additions and 727 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE "kyoo"."shows" ALTER COLUMN "createdAt" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "kyoo"."shows" ALTER COLUMN "nextRefresh" SET NOT NULL;

View File

@ -102,16 +102,12 @@
"entries_id_unique": { "entries_id_unique": {
"name": "entries_id_unique", "name": "entries_id_unique",
"nullsNotDistinct": false, "nullsNotDistinct": false,
"columns": [ "columns": ["id"]
"id"
]
}, },
"entries_slug_unique": { "entries_slug_unique": {
"name": "entries_slug_unique", "name": "entries_slug_unique",
"nullsNotDistinct": false, "nullsNotDistinct": false,
"columns": [ "columns": ["slug"]
"slug"
]
} }
}, },
"checkConstraints": { "checkConstraints": {
@ -157,12 +153,8 @@
"tableFrom": "entries_translation", "tableFrom": "entries_translation",
"tableTo": "entries", "tableTo": "entries",
"schemaTo": "kyoo", "schemaTo": "kyoo",
"columnsFrom": [ "columnsFrom": ["pk"],
"pk" "columnsTo": ["pk"],
],
"columnsTo": [
"pk"
],
"onDelete": "cascade", "onDelete": "cascade",
"onUpdate": "no action" "onUpdate": "no action"
} }
@ -170,10 +162,7 @@
"compositePrimaryKeys": { "compositePrimaryKeys": {
"entries_translation_pk_language_pk": { "entries_translation_pk_language_pk": {
"name": "entries_translation_pk_language_pk", "name": "entries_translation_pk_language_pk",
"columns": [ "columns": ["pk", "language"]
"pk",
"language"
]
} }
}, },
"uniqueConstraints": {}, "uniqueConstraints": {},
@ -184,13 +173,7 @@
"kyoo.entry_type": { "kyoo.entry_type": {
"name": "entry_type", "name": "entry_type",
"schema": "kyoo", "schema": "kyoo",
"values": [ "values": ["unknown", "episode", "movie", "special", "extra"]
"unknown",
"episode",
"movie",
"special",
"extra"
]
} }
}, },
"schemas": {}, "schemas": {},

View File

@ -109,16 +109,12 @@
"entries_id_unique": { "entries_id_unique": {
"name": "entries_id_unique", "name": "entries_id_unique",
"nullsNotDistinct": false, "nullsNotDistinct": false,
"columns": [ "columns": ["id"]
"id"
]
}, },
"entries_slug_unique": { "entries_slug_unique": {
"name": "entries_slug_unique", "name": "entries_slug_unique",
"nullsNotDistinct": false, "nullsNotDistinct": false,
"columns": [ "columns": ["slug"]
"slug"
]
} }
}, },
"checkConstraints": { "checkConstraints": {
@ -164,12 +160,8 @@
"tableFrom": "entries_translation", "tableFrom": "entries_translation",
"tableTo": "entries", "tableTo": "entries",
"schemaTo": "kyoo", "schemaTo": "kyoo",
"columnsFrom": [ "columnsFrom": ["pk"],
"pk" "columnsTo": ["pk"],
],
"columnsTo": [
"pk"
],
"onDelete": "cascade", "onDelete": "cascade",
"onUpdate": "no action" "onUpdate": "no action"
} }
@ -177,10 +169,7 @@
"compositePrimaryKeys": { "compositePrimaryKeys": {
"entries_translation_pk_language_pk": { "entries_translation_pk_language_pk": {
"name": "entries_translation_pk_language_pk", "name": "entries_translation_pk_language_pk",
"columns": [ "columns": ["pk", "language"]
"pk",
"language"
]
} }
}, },
"uniqueConstraints": {}, "uniqueConstraints": {},
@ -270,12 +259,8 @@
"tableFrom": "show_translations", "tableFrom": "show_translations",
"tableTo": "shows", "tableTo": "shows",
"schemaTo": "kyoo", "schemaTo": "kyoo",
"columnsFrom": [ "columnsFrom": ["pk"],
"pk" "columnsTo": ["pk"],
],
"columnsTo": [
"pk"
],
"onDelete": "cascade", "onDelete": "cascade",
"onUpdate": "no action" "onUpdate": "no action"
} }
@ -283,10 +268,7 @@
"compositePrimaryKeys": { "compositePrimaryKeys": {
"show_translations_pk_language_pk": { "show_translations_pk_language_pk": {
"name": "show_translations_pk_language_pk", "name": "show_translations_pk_language_pk",
"columns": [ "columns": ["pk", "language"]
"pk",
"language"
]
} }
}, },
"uniqueConstraints": {}, "uniqueConstraints": {},
@ -398,16 +380,12 @@
"shows_id_unique": { "shows_id_unique": {
"name": "shows_id_unique", "name": "shows_id_unique",
"nullsNotDistinct": false, "nullsNotDistinct": false,
"columns": [ "columns": ["id"]
"id"
]
}, },
"shows_slug_unique": { "shows_slug_unique": {
"name": "shows_slug_unique", "name": "shows_slug_unique",
"nullsNotDistinct": false, "nullsNotDistinct": false,
"columns": [ "columns": ["slug"]
"slug"
]
} }
}, },
"checkConstraints": { "checkConstraints": {
@ -422,13 +400,7 @@
"kyoo.entry_type": { "kyoo.entry_type": {
"name": "entry_type", "name": "entry_type",
"schema": "kyoo", "schema": "kyoo",
"values": [ "values": ["unknown", "episode", "movie", "special", "extra"]
"unknown",
"episode",
"movie",
"special",
"extra"
]
}, },
"kyoo.genres": { "kyoo.genres": {
"name": "genres", "name": "genres",
@ -462,20 +434,12 @@
"kyoo.show_kind": { "kyoo.show_kind": {
"name": "show_kind", "name": "show_kind",
"schema": "kyoo", "schema": "kyoo",
"values": [ "values": ["serie", "movie"]
"serie",
"movie"
]
}, },
"kyoo.show_status": { "kyoo.show_status": {
"name": "show_status", "name": "show_status",
"schema": "kyoo", "schema": "kyoo",
"values": [ "values": ["unknown", "finished", "airing", "planned"]
"unknown",
"finished",
"airing",
"planned"
]
} }
}, },
"schemas": { "schemas": {

View File

@ -0,0 +1,455 @@
{
"id": "1948acaf-7a29-4521-988d-439653779e39",
"prevId": "0f48a319-94fe-4bcc-b63c-28ce280abc9a",
"version": "7",
"dialect": "postgresql",
"tables": {
"kyoo.entries": {
"name": "entries",
"schema": "kyoo",
"columns": {
"pk": {
"name": "pk",
"type": "integer",
"primaryKey": true,
"notNull": true,
"identity": {
"type": "always",
"name": "entries_pk_seq",
"schema": "kyoo",
"increment": "1",
"startWith": "1",
"minValue": "1",
"maxValue": "2147483647",
"cache": "1",
"cycle": false
}
},
"id": {
"name": "id",
"type": "uuid",
"primaryKey": false,
"notNull": true,
"default": "gen_random_uuid()"
},
"slug": {
"name": "slug",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"order": {
"name": "order",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"seasonNumber": {
"name": "seasonNumber",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"episodeNumber": {
"name": "episodeNumber",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"type": {
"name": "type",
"type": "entry_type",
"typeSchema": "kyoo",
"primaryKey": false,
"notNull": true
},
"airDate": {
"name": "airDate",
"type": "date",
"primaryKey": false,
"notNull": false
},
"runtime": {
"name": "runtime",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"thumbnails": {
"name": "thumbnails",
"type": "jsonb",
"primaryKey": false,
"notNull": false
},
"externalId": {
"name": "externalId",
"type": "jsonb",
"primaryKey": false,
"notNull": true,
"default": "'{}'::jsonb"
},
"createdAt": {
"name": "createdAt",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"nextRefresh": {
"name": "nextRefresh",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"entries_id_unique": {
"name": "entries_id_unique",
"nullsNotDistinct": false,
"columns": ["id"]
},
"entries_slug_unique": {
"name": "entries_slug_unique",
"nullsNotDistinct": false,
"columns": ["slug"]
}
},
"checkConstraints": {
"orderPositive": {
"name": "orderPositive",
"value": "\"entries\".\"order\" >= 0"
}
}
},
"kyoo.entries_translation": {
"name": "entries_translation",
"schema": "kyoo",
"columns": {
"pk": {
"name": "pk",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"language": {
"name": "language",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {
"entries_translation_pk_entries_pk_fk": {
"name": "entries_translation_pk_entries_pk_fk",
"tableFrom": "entries_translation",
"tableTo": "entries",
"schemaTo": "kyoo",
"columnsFrom": ["pk"],
"columnsTo": ["pk"],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {
"entries_translation_pk_language_pk": {
"name": "entries_translation_pk_language_pk",
"columns": ["pk", "language"]
}
},
"uniqueConstraints": {},
"checkConstraints": {}
},
"kyoo.show_translations": {
"name": "show_translations",
"schema": "kyoo",
"columns": {
"pk": {
"name": "pk",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"language": {
"name": "language",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
},
"tagline": {
"name": "tagline",
"type": "text",
"primaryKey": false,
"notNull": false
},
"aliases": {
"name": "aliases",
"type": "text[]",
"primaryKey": false,
"notNull": true
},
"tags": {
"name": "tags",
"type": "text[]",
"primaryKey": false,
"notNull": true
},
"trailerUrl": {
"name": "trailerUrl",
"type": "text",
"primaryKey": false,
"notNull": false
},
"poster": {
"name": "poster",
"type": "jsonb",
"primaryKey": false,
"notNull": false
},
"thumbnail": {
"name": "thumbnail",
"type": "jsonb",
"primaryKey": false,
"notNull": false
},
"banner": {
"name": "banner",
"type": "jsonb",
"primaryKey": false,
"notNull": false
},
"logo": {
"name": "logo",
"type": "jsonb",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {
"show_translations_pk_shows_pk_fk": {
"name": "show_translations_pk_shows_pk_fk",
"tableFrom": "show_translations",
"tableTo": "shows",
"schemaTo": "kyoo",
"columnsFrom": ["pk"],
"columnsTo": ["pk"],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {
"show_translations_pk_language_pk": {
"name": "show_translations_pk_language_pk",
"columns": ["pk", "language"]
}
},
"uniqueConstraints": {},
"checkConstraints": {}
},
"kyoo.shows": {
"name": "shows",
"schema": "kyoo",
"columns": {
"pk": {
"name": "pk",
"type": "integer",
"primaryKey": true,
"notNull": true,
"identity": {
"type": "always",
"name": "shows_pk_seq",
"schema": "kyoo",
"increment": "1",
"startWith": "1",
"minValue": "1",
"maxValue": "2147483647",
"cache": "1",
"cycle": false
}
},
"id": {
"name": "id",
"type": "uuid",
"primaryKey": false,
"notNull": true,
"default": "gen_random_uuid()"
},
"slug": {
"name": "slug",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"kind": {
"name": "kind",
"type": "show_kind",
"typeSchema": "kyoo",
"primaryKey": false,
"notNull": true
},
"genres": {
"name": "genres",
"type": "genres[]",
"primaryKey": false,
"notNull": true
},
"rating": {
"name": "rating",
"type": "smallint",
"primaryKey": false,
"notNull": false
},
"status": {
"name": "status",
"type": "show_status",
"typeSchema": "kyoo",
"primaryKey": false,
"notNull": true
},
"startAir": {
"name": "startAir",
"type": "date",
"primaryKey": false,
"notNull": false
},
"endAir": {
"name": "endAir",
"type": "date",
"primaryKey": false,
"notNull": false
},
"originalLanguage": {
"name": "originalLanguage",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"externalId": {
"name": "externalId",
"type": "jsonb",
"primaryKey": false,
"notNull": true,
"default": "'{}'::jsonb"
},
"createdAt": {
"name": "createdAt",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"nextRefresh": {
"name": "nextRefresh",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"shows_id_unique": {
"name": "shows_id_unique",
"nullsNotDistinct": false,
"columns": ["id"]
},
"shows_slug_unique": {
"name": "shows_slug_unique",
"nullsNotDistinct": false,
"columns": ["slug"]
}
},
"checkConstraints": {
"ratingValid": {
"name": "ratingValid",
"value": "0 <= \"shows\".\"rating\" && \"shows\".\"rating\" <= 100"
}
}
}
},
"enums": {
"kyoo.entry_type": {
"name": "entry_type",
"schema": "kyoo",
"values": ["unknown", "episode", "movie", "special", "extra"]
},
"kyoo.genres": {
"name": "genres",
"schema": "kyoo",
"values": [
"action",
"adventure",
"animation",
"comedy",
"crime",
"documentary",
"drama",
"family",
"fantasy",
"history",
"horror",
"music",
"mystery",
"romance",
"science-fiction",
"thriller",
"war",
"western",
"kids",
"reality",
"politics",
"soap",
"talk"
]
},
"kyoo.show_kind": {
"name": "show_kind",
"schema": "kyoo",
"values": ["serie", "movie"]
},
"kyoo.show_status": {
"name": "show_status",
"schema": "kyoo",
"values": ["unknown", "finished", "airing", "planned"]
}
},
"schemas": {
"kyoo": "kyoo"
},
"sequences": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@ -15,6 +15,13 @@
"when": 1730477283024, "when": 1730477283024,
"tag": "0001_shows", "tag": "0001_shows",
"breakpoints": true "breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1730487641214,
"tag": "0002_shows",
"breakpoints": true
} }
] ]
} }

View File

@ -10,7 +10,7 @@ import {
uuid, uuid,
varchar, varchar,
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
import { language, schema } from "./utils"; import { image, language, schema } from "./utils";
export const entryType = schema.enum("entry_type", [ export const entryType = schema.enum("entry_type", [
"unknown", "unknown",
@ -33,7 +33,7 @@ export const entries = schema.table(
type: entryType().notNull(), type: entryType().notNull(),
airDate: date(), airDate: date(),
runtime: integer(), runtime: integer(),
thumbnails: jsonb(), thumbnails: image(),
externalId: jsonb().notNull().default({}), externalId: jsonb().notNull().default({}),

View File

@ -11,10 +11,15 @@ import {
uuid, uuid,
varchar, varchar,
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
import { language, schema } from "./utils"; import { externalid, image, language, schema } from "./utils";
export const showKind = schema.enum("show_kind", ["serie", "movie"]); export const showKind = schema.enum("show_kind", ["serie", "movie"]);
export const showStatus = schema.enum("show_status", ["unknown", "finished", "airing", "planned"]); export const showStatus = schema.enum("show_status", [
"unknown",
"finished",
"airing",
"planned",
]);
export const genres = schema.enum("genres", [ export const genres = schema.enum("genres", [
"action", "action",
"adventure", "adventure",
@ -51,17 +56,20 @@ export const shows = schema.table(
genres: genres().array().notNull(), genres: genres().array().notNull(),
rating: smallint(), rating: smallint(),
status: showStatus().notNull(), status: showStatus().notNull(),
startAir: date(), startAir: date({ mode: "date" }),
endAir: date(), endAir: date({ mode: "date" }),
originalLanguage: language(), originalLanguage: language(),
externalId: jsonb().notNull().default({}), externalId: externalid(),
createdAt: timestamp({ withTimezone: true }).defaultNow(), createdAt: timestamp({ withTimezone: true }).notNull().defaultNow(),
nextRefresh: timestamp({ withTimezone: true }), nextRefresh: timestamp({ withTimezone: true }).notNull(),
}, },
(t) => ({ (t) => ({
ratingValid: check("ratingValid", sql`0 <= ${t.rating} && ${t.rating} <= 100`), ratingValid: check(
"ratingValid",
sql`0 <= ${t.rating} && ${t.rating} <= 100`,
),
}), }),
); );
@ -78,10 +86,10 @@ export const showTranslations = schema.table(
aliases: text().array().notNull(), aliases: text().array().notNull(),
tags: text().array().notNull(), tags: text().array().notNull(),
trailerUrl: text(), trailerUrl: text(),
poster: jsonb(), poster: image(),
thumbnail: jsonb(), thumbnail: image(),
banner: jsonb(), banner: image(),
logo: jsonb(), logo: image(),
}, },
(t) => ({ (t) => ({
pk: primaryKey({ columns: [t.pk, t.language] }), pk: primaryKey({ columns: [t.pk, t.language] }),

View File

@ -1,5 +1,22 @@
import { pgSchema, varchar } from "drizzle-orm/pg-core"; import { jsonb, pgSchema, varchar } from "drizzle-orm/pg-core";
export const schema = pgSchema("kyoo"); export const schema = pgSchema("kyoo");
export const language = () => varchar({ length: 255 }); export const language = () => varchar({ length: 255 });
export const image = () =>
jsonb().$type<{ source: string; blurhash: string }>();
export const externalid = () =>
jsonb()
.$type<
Record<
string,
{
dataId: string;
link: string | null;
}
>
>()
.notNull()
.default({});