run stuff in transaction : fix
This commit is contained in:
+43
-29
@@ -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,11 +48,8 @@ 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()
|
|
||||||
.setIsolationLevel("serializable")
|
|
||||||
.execute(async (trx) => {
|
|
||||||
const weapon = await trx
|
const weapon = await trx
|
||||||
.selectFrom("ToneAPI_v3.weapon")
|
.selectFrom("ToneAPI_v3.weapon")
|
||||||
.select("ToneAPI_v3.weapon.weapon_id")
|
.select("ToneAPI_v3.weapon.weapon_id")
|
||||||
@@ -52,45 +63,44 @@ export async function checkOrCreateWeapon(weapon_id: string) {
|
|||||||
})
|
})
|
||||||
.execute();
|
.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()
|
|
||||||
.setIsolationLevel("serializable")
|
|
||||||
.execute(async (trx) => {
|
|
||||||
const weaponMods = await trx
|
const weaponMods = await trx
|
||||||
.selectFrom("ToneAPI_v3.mods_on_weapon")
|
.selectFrom("ToneAPI_v3.mods_on_weapon")
|
||||||
.select("ToneAPI_v3.mods_on_weapon.mod_id")
|
.select("ToneAPI_v3.mods_on_weapon.mod_id")
|
||||||
.where("mod_id", "=", weapon_mods.mods)
|
.where("mod_id", "=", weapon.mods)
|
||||||
.where("weapon_id", "=", weapon_mods.id)
|
.where("weapon_id", "=", weapon.id)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
if (!weaponMods) {
|
if (!weaponMods) {
|
||||||
await trx
|
await trx
|
||||||
.insertInto("ToneAPI_v3.mods_on_weapon")
|
.insertInto("ToneAPI_v3.mods_on_weapon")
|
||||||
.values({
|
.values({
|
||||||
mod_id: weapon_mods.mods,
|
mod_id: weapon.mods,
|
||||||
weapon_id: weapon_mods.id,
|
weapon_id: weapon.id,
|
||||||
autogenerated: true,
|
autogenerated: true,
|
||||||
})
|
})
|
||||||
.execute();
|
.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()
|
|
||||||
.setIsolationLevel("serializable")
|
|
||||||
.execute(async (trx) => {
|
|
||||||
const titan = await trx
|
const titan = await trx
|
||||||
.selectFrom("ToneAPI_v3.titan_chassis")
|
.selectFrom("ToneAPI_v3.titan_chassis")
|
||||||
.select("ToneAPI_v3.titan_chassis.titan_id")
|
.select("ToneAPI_v3.titan_chassis.titan_id")
|
||||||
@@ -104,7 +114,10 @@ export async function checkOrCreateTitan(titan_id: string | null) {
|
|||||||
})
|
})
|
||||||
.execute();
|
.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");
|
||||||
|
|||||||
Reference in New Issue
Block a user