type IDatabase::prepare with overloads

This commit is contained in:
mini-bomba
2025-09-10 16:46:31 +02:00
parent 3711286ef2
commit 1b99a8534c
5 changed files with 29 additions and 27 deletions

View File

@@ -3,7 +3,6 @@ import { CronJob } from "cron";
import { config as serverConfig } from "../config"; import { config as serverConfig } from "../config";
import { Logger } from "../utils/logger"; import { Logger } from "../utils/logger";
import { db } from "../databases/databases"; import { db } from "../databases/databases";
import { DBSegment } from "../types/segments.model";
const jobConfig = serverConfig?.crons?.downvoteSegmentArchive; const jobConfig = serverConfig?.crons?.downvoteSegmentArchive;
@@ -14,18 +13,18 @@ export const archiveDownvoteSegment = async (dayLimit: number, voteLimit: number
Logger.info(`DownvoteSegmentArchiveJob starts at ${timeNow}`); Logger.info(`DownvoteSegmentArchiveJob starts at ${timeNow}`);
try { try {
// insert into archive sponsorTime // insert into archive sponsorTime
await db.prepare( await db.prepare(
"run", "run",
`INSERT INTO "archivedSponsorTimes" `INSERT INTO "archivedSponsorTimes"
SELECT * SELECT *
FROM "sponsorTimes" FROM "sponsorTimes"
WHERE "votes" < ? AND (? - "timeSubmitted") > ?`, WHERE "votes" < ? AND (? - "timeSubmitted") > ?`,
[ [
voteLimit, voteLimit,
timeNow, timeNow,
threshold threshold
] ]
) as DBSegment[]; );
} catch (err) { } catch (err) {
Logger.error("Execption when insert segment in archivedSponsorTimes"); Logger.error("Execption when insert segment in archivedSponsorTimes");
@@ -35,15 +34,15 @@ export const archiveDownvoteSegment = async (dayLimit: number, voteLimit: number
// remove from sponsorTime // remove from sponsorTime
try { try {
await db.prepare( await db.prepare(
"run", "run",
'DELETE FROM "sponsorTimes" WHERE "votes" < ? AND (? - "timeSubmitted") > ?', 'DELETE FROM "sponsorTimes" WHERE "votes" < ? AND (? - "timeSubmitted") > ?',
[ [
voteLimit, voteLimit,
timeNow, timeNow,
threshold threshold
] ]
) as DBSegment[]; );
} catch (err) { } catch (err) {
Logger.error("Execption when deleting segment in sponsorTimes"); Logger.error("Execption when deleting segment in sponsorTimes");

View File

@@ -6,11 +6,14 @@ export interface QueryOption {
export interface IDatabase { export interface IDatabase {
init(): Promise<void>; init(): Promise<void>;
prepare(type: QueryType, query: string, params?: any[], options?: QueryOption): Promise<any | any[] | void>; prepare(type: "run", query: string, params?: any[], options?: QueryOption): Promise<void>;
prepare(type: "get", query: string, params?: any[], options?: QueryOption): Promise<any>;
prepare(type: "all", query: string, params?: any[], options?: QueryOption): Promise<any[]>;
prepare(type: QueryType, query: string, params?: any[], options?: QueryOption): Promise<any>;
highLoad(): boolean; highLoad(): boolean;
shouldUseRedisTimeout(): boolean; shouldUseRedisTimeout(): boolean;
} }
export type QueryType = "get" | "all" | "run"; export type QueryType = "get" | "all" | "run";

View File

@@ -109,7 +109,7 @@ export class Postgres implements IDatabase {
} }
} }
async prepare(type: QueryType, query: string, params?: any[], options: QueryOption = {}): Promise<any[]> { async prepare(type: QueryType, query: string, params?: any[], options: QueryOption = {}): Promise<any> {
// Convert query to use numbered parameters // Convert query to use numbered parameters
let count = 1; let count = 1;
for (let char = 0; char < query.length; char++) { for (let char = 0; char < query.length; char++) {

View File

@@ -13,7 +13,7 @@ export class Sqlite implements IDatabase {
} }
// eslint-disable-next-line require-await // eslint-disable-next-line require-await
async prepare(type: QueryType, query: string, params: any[] = []): Promise<any[]> { async prepare(type: QueryType, query: string, params: any[] = []): Promise<any> {
// Logger.debug(`prepare (sqlite): type: ${type}, query: ${query}, params: ${params}`); // Logger.debug(`prepare (sqlite): type: ${type}, query: ${query}, params: ${params}`);
const preparedQuery = this.db.prepare(Sqlite.processQuery(query)); const preparedQuery = this.db.prepare(Sqlite.processQuery(query));

View File

@@ -7,7 +7,7 @@ import { isUserBanned } from "../utils/checkBan";
import { HashedUserID } from "../types/user.model"; import { HashedUserID } from "../types/user.model";
import { isRequestInvalid } from "../utils/requestValidator"; import { isRequestInvalid } from "../utils/requestValidator";
function logUserNameChange(userID: string, newUserName: string, oldUserName: string, updatedByAdmin: boolean): Promise<Response> { function logUserNameChange(userID: string, newUserName: string, oldUserName: string, updatedByAdmin: boolean): Promise<void> {
return privateDB.prepare("run", return privateDB.prepare("run",
`INSERT INTO "userNameLogs"("userID", "newUserName", "oldUserName", "updatedByAdmin", "updatedAt") VALUES(?, ?, ?, ?, ?)`, `INSERT INTO "userNameLogs"("userID", "newUserName", "oldUserName", "updatedByAdmin", "updatedAt") VALUES(?, ?, ?, ?, ?)`,
[userID, newUserName, oldUserName, + updatedByAdmin, new Date().getTime()] [userID, newUserName, oldUserName, + updatedByAdmin, new Date().getTime()]