import { LogEntry, SingleMetadata } from "../contract"; import { HttpError } from "../http-error"; import { db } from "./init"; import { zip } from "lodash-es"; import { json } from "./kysely-mariadb-json"; declare global { interface BigInt { /** Convert to BigInt to string form in JSON.stringify */ toJSON: () => string; } } BigInt.prototype.toJSON = function () { return this.toString(); }; export async function postLog( gameName: string, version: string, saveGuid: string, sessionGuid: string, entries: LogEntry[] // message: string, // category: string | undefined, // metadata: SingleMetadata[] ) { if ( gameName === "" || sessionGuid === "" || entries.some((entry) => entry.message === "") || version === "" ) { const errorMessage = `One of the necessary fields was missing: gameName=${gameName}, sessionGuid=${sessionGuid}, messages=${entries .map((e) => e.message) .join(", ")}, version=${version}`; throw new HttpError(422, errorMessage); } else { await db.transaction().execute(async (trx) => { // upsert the session info await trx .insertInto("session_info") .ignore() .values({ id: sessionGuid, game_name: gameName, save_id: saveGuid, version, }) .execute(); // add the main log lines with their metadata await trx .insertInto("log_entries") .values( entries.map((entry) => ({ session_info_id: sessionGuid, message: entry.message, timestamp: new Date(), category: entry.category, metadata: json(entry.metadata.map((x) => [x.key, x.value])), })) ) .returning("id") .execute(); }); } }