run stuff in transaction : fix

This commit is contained in:
2023-08-13 11:56:20 +00:00
parent c5051e5387
commit 3a29c5cd88
+81 -67
View File
@@ -2,6 +2,20 @@ import { Kysely, Transaction } from "kysely";
import db from "./db"; import db from "./db";
import Database from "./db/model"; import Database from "./db/model";
import { LoadoutKillData } from "./types"; import { LoadoutKillData } from "./types";
import { Data } from 'ws';
export async function createOrRunInTransaction(callback: (trx: Transaction<Database>) => Promise<void>, trx?:Transaction<Database>){
if(trx){
await callback(trx)
}else{
await db
.transaction()
.setIsolationLevel("serializable")
.execute(callback);
}
}
export async function checkUpdateOrCreatePlayer(data: { export async function checkUpdateOrCreatePlayer(data: {
id: number; id: number;
@@ -34,77 +48,76 @@ export async function checkUpdateOrCreatePlayer(data: {
}); });
} }
export async function checkOrCreateWeapon(weapon_id: string) { export async function checkOrCreateWeapon(weapon_id: string, trx?:Transaction<Database>) {
await db const runner = async (trx:Transaction<Database>) => {
.transaction() const weapon = await trx
.setIsolationLevel("serializable") .selectFrom("ToneAPI_v3.weapon")
.execute(async (trx) => { .select("ToneAPI_v3.weapon.weapon_id")
const weapon = await trx .where("weapon_id", "=", weapon_id)
.selectFrom("ToneAPI_v3.weapon") .executeTakeFirst();
.select("ToneAPI_v3.weapon.weapon_id") if (!weapon) {
.where("weapon_id", "=", weapon_id) await trx
.executeTakeFirst(); .insertInto("ToneAPI_v3.weapon")
if (!weapon) { .values({
await trx weapon_id,
.insertInto("ToneAPI_v3.weapon") })
.values({ .execute();
weapon_id, }
}) }
.execute();
} await createOrRunInTransaction(runner, trx)
});
} }
export async function checkOrCreateWeaponMods(weapon_mods: { export async function checkOrCreateWeaponMods(weapon: {
id: string; id: string;
mods: number; mods: number;
}) { }, trx?:Transaction<Database>) {
await checkOrCreateWeapon(weapon_mods.id); const runner = async (trx:Transaction<Database>) => {
await db await checkOrCreateWeapon(weapon.id, trx);
.transaction() const weaponMods = await trx
.setIsolationLevel("serializable") .selectFrom("ToneAPI_v3.mods_on_weapon")
.execute(async (trx) => { .select("ToneAPI_v3.mods_on_weapon.mod_id")
const weaponMods = await trx .where("mod_id", "=", weapon.mods)
.selectFrom("ToneAPI_v3.mods_on_weapon") .where("weapon_id", "=", weapon.id)
.select("ToneAPI_v3.mods_on_weapon.mod_id") .executeTakeFirst();
.where("mod_id", "=", weapon_mods.mods) if (!weaponMods) {
.where("weapon_id", "=", weapon_mods.id) await trx
.executeTakeFirst(); .insertInto("ToneAPI_v3.mods_on_weapon")
if (!weaponMods) { .values({
await trx mod_id: weapon.mods,
.insertInto("ToneAPI_v3.mods_on_weapon") weapon_id: weapon.id,
.values({ autogenerated: true,
mod_id: weapon_mods.mods, })
weapon_id: weapon_mods.id, .execute();
autogenerated: true, }
}) }
.execute();
} await createOrRunInTransaction(runner, trx)
});
} }
export async function checkOrCreateTitan(titan_id: string | null) { export async function checkOrCreateTitan(titan_id: string | null, trx?:Transaction<Database>) {
if (titan_id == null) { if (titan_id == null) {
return; return;
} }
await db const runner = async (trx:Transaction<Database>) => {
.transaction() const titan = await trx
.setIsolationLevel("serializable") .selectFrom("ToneAPI_v3.titan_chassis")
.execute(async (trx) => { .select("ToneAPI_v3.titan_chassis.titan_id")
const titan = await trx .where("titan_id", "=", titan_id)
.selectFrom("ToneAPI_v3.titan_chassis") .executeTakeFirst();
.select("ToneAPI_v3.titan_chassis.titan_id") if (!titan) {
.where("titan_id", "=", titan_id) await trx
.executeTakeFirst(); .insertInto("ToneAPI_v3.titan_chassis")
if (!titan) { .values({
await trx titan_id,
.insertInto("ToneAPI_v3.titan_chassis") })
.values({ .execute();
titan_id, }
}) }
.execute();
} await createOrRunInTransaction(runner, trx)
});
} }
export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) { export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) {
@@ -137,18 +150,18 @@ export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) {
.executeTakeFirst(); .executeTakeFirst();
if (!loadout) { if (!loadout) {
if (loadoutData.primary !== null) { if (loadoutData.primary !== null) {
await checkOrCreateWeaponMods(loadoutData.primary); await checkOrCreateWeaponMods(loadoutData.primary, trx);
} }
if (loadoutData.secondary !== null) { if (loadoutData.secondary !== null) {
await checkOrCreateWeaponMods(loadoutData.secondary); await checkOrCreateWeaponMods(loadoutData.secondary, trx);
} }
if (loadoutData.anti_titan !== null) { if (loadoutData.anti_titan !== null) {
await checkOrCreateWeaponMods(loadoutData.anti_titan); await checkOrCreateWeaponMods(loadoutData.anti_titan, trx);
} }
if (loadoutData.ordnance !== null) { if (loadoutData.ordnance !== null) {
await checkOrCreateWeapon(loadoutData.ordnance.id); await checkOrCreateWeapon(loadoutData.ordnance.id, trx);
} }
await checkOrCreateTitan(loadoutData.titan); await checkOrCreateTitan(loadoutData.titan, trx);
const result = await trx const result = await trx
.insertInto("ToneAPI_v3.loadout") .insertInto("ToneAPI_v3.loadout")
.values({ .values({
@@ -168,6 +181,7 @@ export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) {
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow();
return result; return result;
} }
return loadout
}); });
if (!loadout) { if (!loadout) {
throw Error("loadout is undefined"); throw Error("loadout is undefined");