You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
overseerr/server/entity/Media.ts

93 lines
1.8 KiB

import {
Entity,
PrimaryGeneratedColumn,
Column,
Index,
OneToMany,
CreateDateColumn,
UpdateDateColumn,
getRepository,
In,
} from 'typeorm';
import { MediaRequest } from './MediaRequest';
import { MediaStatus, MediaType } from '../constants/media';
import logger from '../logger';
@Entity()
class Media {
public static async getRelatedMedia(
tmdbIds: number | number[]
): Promise<Media[]> {
const mediaRepository = getRepository(Media);
try {
let finalIds: number[];
if (!Array.isArray(tmdbIds)) {
finalIds = [tmdbIds];
} else {
finalIds = tmdbIds;
}
const media = await mediaRepository.find({
tmdbId: In(finalIds),
});
return media;
} catch (e) {
logger.error(e.message);
return [];
}
}
public static async getMedia(id: number): Promise<Media | undefined> {
const mediaRepository = getRepository(Media);
try {
const media = await mediaRepository.findOne({
where: { tmdbId: id },
});
return media;
} catch (e) {
logger.error(e.message);
return undefined;
}
}
@PrimaryGeneratedColumn()
public id: number;
@Column({ type: 'varchar' })
public mediaType: MediaType;
@Column({ unique: true })
@Index()
public tmdbId: number;
@Column({ unique: true, nullable: true })
@Index()
public tvdbId?: number;
@Column({ unique: true, nullable: true })
@Index()
public imdbId?: string;
@Column({ type: 'int', default: MediaStatus.UNKNOWN })
public status: MediaStatus;
@OneToMany(() => MediaRequest, (request) => request.media)
public requests: MediaRequest[];
@CreateDateColumn()
public createdAt: Date;
@UpdateDateColumn()
public updatedAt: Date;
constructor(init?: Partial<Media>) {
Object.assign(this, init);
}
}
export default Media;