import Plex from "@lib/plex"; import { BaseEntity, Column, Entity, In, PrimaryColumn } from "typeorm"; @Entity() export class PlexMovie extends BaseEntity { @PrimaryColumn({ length: 27 }) imdbId!: string; @Column({ type: "int", nullable: true, unique: true }) tmdbId!: number|null; /** * Check if a movie is on Plex given its TMDb ID */ public static async isOnPlex(tmdbId: number|string) { return await PlexMovie.count({ where: { tmdbId } }) > 0; } /** * Get the set of IMDb IDs stored in the library */ public static async imdbSet() { let rows = await PlexMovie.createQueryBuilder("plex_movie") .select("imdbId") .getRawMany(); return new Set(rows.map(row => row.imdbId)); } /** * Insert a set of IMDb IDs into the database */ public static async insertImdbSet(imdbIds: Set) { await PlexMovie.createQueryBuilder() .insert() .into("plex_movie") .values([...imdbIds].map(imdbId => ({ imdbId, tmdbId: null }))) .execute(); } /** * Remove the given set of IMDb IDs from the library */ public static async removeImdbSet(imdbIds: Set) { await PlexMovie.delete({ imdbId: In([...imdbIds]) }); } }