72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
import { type ResultSetHeader, type RowDataPacket, type QueryOptions } from "mysql2"
|
|
import pool from 'miscellaneous/db'
|
|
|
|
|
|
interface Media extends RowDataPacket {
|
|
id?: number;
|
|
code?: number;
|
|
title?: string;
|
|
released?: string;
|
|
webImg?: string;
|
|
}
|
|
|
|
export enum Table {
|
|
movies = "movies",
|
|
series = "series",
|
|
games = "games",
|
|
}
|
|
|
|
async function save(table: Table, code: number, title: string, released: string, webImg: string): Promise<number> {
|
|
try {
|
|
const options: QueryOptions = {
|
|
sql: "INSERT INTO " + table + " (code, title, released, webImg) VALUES (?,?,?,?)",
|
|
values: [code, title, released, webImg]
|
|
};
|
|
const [result, fields] = await pool.query<ResultSetHeader>(options);
|
|
return result.affectedRows;
|
|
}
|
|
catch (err) {
|
|
console.log(err);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
async function findOneAndDelete(table: Table, code: number): Promise<number> {
|
|
try {
|
|
const [result, fields] = await pool.query<ResultSetHeader>("DELETE FROM " + table + " WHERE code = ?;", [code]);
|
|
return result.affectedRows;
|
|
}
|
|
catch (err) {
|
|
console.log(err);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
async function findOne(table: Table, code: number): Promise<Media[]> {
|
|
try {
|
|
const [rows, fields] = await pool.query<Media[]>("SELECT * FROM " + table + " WHERE code = ?;", [code]);
|
|
return rows;
|
|
}
|
|
catch (err) {
|
|
console.log(err);
|
|
}
|
|
return [];
|
|
}
|
|
|
|
async function find(table: Table): Promise<Media[]> {
|
|
try {
|
|
const [rows, fields] = await pool.query<Media[]>("SELECT * FROM " + table + ";");
|
|
return rows;
|
|
}
|
|
catch (err) {
|
|
console.log(err);
|
|
}
|
|
return [];
|
|
}
|
|
|
|
export default {
|
|
save,
|
|
findOneAndDelete,
|
|
findOne,
|
|
find
|
|
}; |