run stuff in transaction : fix
This commit is contained in:
+81
-67
@@ -2,6 +2,20 @@ import { Kysely, Transaction } from "kysely";
|
||||
import db from "./db";
|
||||
import Database from "./db/model";
|
||||
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: {
|
||||
id: number;
|
||||
@@ -34,77 +48,76 @@ export async function checkUpdateOrCreatePlayer(data: {
|
||||
});
|
||||
}
|
||||
|
||||
export async function checkOrCreateWeapon(weapon_id: string) {
|
||||
await db
|
||||
.transaction()
|
||||
.setIsolationLevel("serializable")
|
||||
.execute(async (trx) => {
|
||||
const weapon = await trx
|
||||
.selectFrom("ToneAPI_v3.weapon")
|
||||
.select("ToneAPI_v3.weapon.weapon_id")
|
||||
.where("weapon_id", "=", weapon_id)
|
||||
.executeTakeFirst();
|
||||
if (!weapon) {
|
||||
await trx
|
||||
.insertInto("ToneAPI_v3.weapon")
|
||||
.values({
|
||||
weapon_id,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
});
|
||||
export async function checkOrCreateWeapon(weapon_id: string, trx?:Transaction<Database>) {
|
||||
const runner = async (trx:Transaction<Database>) => {
|
||||
const weapon = await trx
|
||||
.selectFrom("ToneAPI_v3.weapon")
|
||||
.select("ToneAPI_v3.weapon.weapon_id")
|
||||
.where("weapon_id", "=", weapon_id)
|
||||
.executeTakeFirst();
|
||||
if (!weapon) {
|
||||
await trx
|
||||
.insertInto("ToneAPI_v3.weapon")
|
||||
.values({
|
||||
weapon_id,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
|
||||
await createOrRunInTransaction(runner, trx)
|
||||
}
|
||||
|
||||
export async function checkOrCreateWeaponMods(weapon_mods: {
|
||||
export async function checkOrCreateWeaponMods(weapon: {
|
||||
id: string;
|
||||
mods: number;
|
||||
}) {
|
||||
await checkOrCreateWeapon(weapon_mods.id);
|
||||
await db
|
||||
.transaction()
|
||||
.setIsolationLevel("serializable")
|
||||
.execute(async (trx) => {
|
||||
const weaponMods = await trx
|
||||
.selectFrom("ToneAPI_v3.mods_on_weapon")
|
||||
.select("ToneAPI_v3.mods_on_weapon.mod_id")
|
||||
.where("mod_id", "=", weapon_mods.mods)
|
||||
.where("weapon_id", "=", weapon_mods.id)
|
||||
.executeTakeFirst();
|
||||
if (!weaponMods) {
|
||||
await trx
|
||||
.insertInto("ToneAPI_v3.mods_on_weapon")
|
||||
.values({
|
||||
mod_id: weapon_mods.mods,
|
||||
weapon_id: weapon_mods.id,
|
||||
autogenerated: true,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
});
|
||||
}, trx?:Transaction<Database>) {
|
||||
const runner = async (trx:Transaction<Database>) => {
|
||||
await checkOrCreateWeapon(weapon.id, trx);
|
||||
const weaponMods = await trx
|
||||
.selectFrom("ToneAPI_v3.mods_on_weapon")
|
||||
.select("ToneAPI_v3.mods_on_weapon.mod_id")
|
||||
.where("mod_id", "=", weapon.mods)
|
||||
.where("weapon_id", "=", weapon.id)
|
||||
.executeTakeFirst();
|
||||
if (!weaponMods) {
|
||||
await trx
|
||||
.insertInto("ToneAPI_v3.mods_on_weapon")
|
||||
.values({
|
||||
mod_id: weapon.mods,
|
||||
weapon_id: weapon.id,
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
await db
|
||||
.transaction()
|
||||
.setIsolationLevel("serializable")
|
||||
.execute(async (trx) => {
|
||||
const titan = await trx
|
||||
.selectFrom("ToneAPI_v3.titan_chassis")
|
||||
.select("ToneAPI_v3.titan_chassis.titan_id")
|
||||
.where("titan_id", "=", titan_id)
|
||||
.executeTakeFirst();
|
||||
if (!titan) {
|
||||
await trx
|
||||
.insertInto("ToneAPI_v3.titan_chassis")
|
||||
.values({
|
||||
titan_id,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
});
|
||||
const runner = async (trx:Transaction<Database>) => {
|
||||
const titan = await trx
|
||||
.selectFrom("ToneAPI_v3.titan_chassis")
|
||||
.select("ToneAPI_v3.titan_chassis.titan_id")
|
||||
.where("titan_id", "=", titan_id)
|
||||
.executeTakeFirst();
|
||||
if (!titan) {
|
||||
await trx
|
||||
.insertInto("ToneAPI_v3.titan_chassis")
|
||||
.values({
|
||||
titan_id,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
|
||||
await createOrRunInTransaction(runner, trx)
|
||||
|
||||
}
|
||||
|
||||
export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) {
|
||||
@@ -137,18 +150,18 @@ export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) {
|
||||
.executeTakeFirst();
|
||||
if (!loadout) {
|
||||
if (loadoutData.primary !== null) {
|
||||
await checkOrCreateWeaponMods(loadoutData.primary);
|
||||
await checkOrCreateWeaponMods(loadoutData.primary, trx);
|
||||
}
|
||||
if (loadoutData.secondary !== null) {
|
||||
await checkOrCreateWeaponMods(loadoutData.secondary);
|
||||
await checkOrCreateWeaponMods(loadoutData.secondary, trx);
|
||||
}
|
||||
if (loadoutData.anti_titan !== null) {
|
||||
await checkOrCreateWeaponMods(loadoutData.anti_titan);
|
||||
await checkOrCreateWeaponMods(loadoutData.anti_titan, trx);
|
||||
}
|
||||
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
|
||||
.insertInto("ToneAPI_v3.loadout")
|
||||
.values({
|
||||
@@ -168,6 +181,7 @@ export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) {
|
||||
.executeTakeFirstOrThrow();
|
||||
return result;
|
||||
}
|
||||
return loadout
|
||||
});
|
||||
if (!loadout) {
|
||||
throw Error("loadout is undefined");
|
||||
|
||||
Reference in New Issue
Block a user