From 42dba58325825166ff2a87da43bd351ef8ef314c Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Wed, 4 Jan 2023 15:37:35 -0500 Subject: [PATCH] reduce unnecessary database connections --- helpers/lotOccupancyDB/moveLotStatusUp.js | 6 +++--- helpers/lotOccupancyDB/moveLotStatusUp.ts | 12 ++++++------ helpers/lotOccupancyDB/moveLotTypeUp.js | 6 +++--- helpers/lotOccupancyDB/moveLotTypeUp.ts | 11 +++++------ helpers/lotOccupancyDB/moveOccupancyTypeUp.js | 6 +++--- helpers/lotOccupancyDB/moveOccupancyTypeUp.ts | 12 ++++++------ helpers/lotOccupancyDB/moveWorkOrderTypeUp.js | 6 +++--- helpers/lotOccupancyDB/moveWorkOrderTypeUp.ts | 12 ++++++------ 8 files changed, 35 insertions(+), 36 deletions(-) diff --git a/helpers/lotOccupancyDB/moveLotStatusUp.js b/helpers/lotOccupancyDB/moveLotStatusUp.js index 430ddc43..39f1a923 100644 --- a/helpers/lotOccupancyDB/moveLotStatusUp.js +++ b/helpers/lotOccupancyDB/moveLotStatusUp.js @@ -22,9 +22,9 @@ export function moveLotStatusUp(lotStatusId) { return result.changes > 0; } export function moveLotStatusUpToTop(lotStatusId) { - const database = sqlite(databasePath); const currentOrderNumber = getLotStatusById(typeof lotStatusId === "string" ? Number.parseInt(lotStatusId) : lotStatusId).orderNumber; if (currentOrderNumber > 0) { + const database = sqlite(databasePath); database .prepare("update LotStatuses set orderNumber = -1 where lotStatusId = ?") .run(lotStatusId); @@ -34,9 +34,9 @@ export function moveLotStatusUpToTop(lotStatusId) { where recordDelete_timeMillis is null and orderNumber < ?`) .run(currentOrderNumber); + database.close(); + clearLotStatusesCache(); } - database.close(); - clearLotStatusesCache(); return true; } export default moveLotStatusUp; diff --git a/helpers/lotOccupancyDB/moveLotStatusUp.ts b/helpers/lotOccupancyDB/moveLotStatusUp.ts index 1d64c8a6..9db9362e 100644 --- a/helpers/lotOccupancyDB/moveLotStatusUp.ts +++ b/helpers/lotOccupancyDB/moveLotStatusUp.ts @@ -37,13 +37,13 @@ export function moveLotStatusUp(lotStatusId: number | string): boolean { } export function moveLotStatusUpToTop(lotStatusId: number | string): boolean { - const database = sqlite(databasePath); - const currentOrderNumber: number = getLotStatusById( typeof lotStatusId === "string" ? Number.parseInt(lotStatusId) : lotStatusId ).orderNumber; if (currentOrderNumber > 0) { + const database = sqlite(databasePath); + database .prepare("update LotStatuses set orderNumber = -1 where lotStatusId = ?") .run(lotStatusId); @@ -56,12 +56,12 @@ export function moveLotStatusUpToTop(lotStatusId: number | string): boolean { and orderNumber < ?` ) .run(currentOrderNumber); + + database.close(); + + clearLotStatusesCache(); } - database.close(); - - clearLotStatusesCache(); - return true; } diff --git a/helpers/lotOccupancyDB/moveLotTypeUp.js b/helpers/lotOccupancyDB/moveLotTypeUp.js index f65078e5..5cfcbf1a 100644 --- a/helpers/lotOccupancyDB/moveLotTypeUp.js +++ b/helpers/lotOccupancyDB/moveLotTypeUp.js @@ -22,8 +22,8 @@ export function moveLotTypeUp(lotTypeId) { } export function moveLotTypeUpToTop(lotTypeId) { const currentOrderNumber = getLotTypeById(typeof lotTypeId === "string" ? Number.parseInt(lotTypeId) : lotTypeId).orderNumber; - const database = sqlite(databasePath); if (currentOrderNumber > 0) { + const database = sqlite(databasePath); database.prepare("update LotTypes set orderNumber = -1 where lotTypeId = ?").run(lotTypeId); database .prepare(`update LotTypes @@ -31,9 +31,9 @@ export function moveLotTypeUpToTop(lotTypeId) { where recordDelete_timeMillis is null and orderNumber < ?`) .run(currentOrderNumber); + database.close(); + clearLotTypesCache(); } - database.close(); - clearLotTypesCache(); return true; } export default moveLotTypeUp; diff --git a/helpers/lotOccupancyDB/moveLotTypeUp.ts b/helpers/lotOccupancyDB/moveLotTypeUp.ts index 135beeb9..4d6b84fc 100644 --- a/helpers/lotOccupancyDB/moveLotTypeUp.ts +++ b/helpers/lotOccupancyDB/moveLotTypeUp.ts @@ -40,9 +40,8 @@ export function moveLotTypeUpToTop(lotTypeId: number | string): boolean { typeof lotTypeId === "string" ? Number.parseInt(lotTypeId) : lotTypeId ).orderNumber; - const database = sqlite(databasePath); - if (currentOrderNumber > 0) { + const database = sqlite(databasePath); database.prepare("update LotTypes set orderNumber = -1 where lotTypeId = ?").run(lotTypeId); database @@ -53,12 +52,12 @@ export function moveLotTypeUpToTop(lotTypeId: number | string): boolean { and orderNumber < ?` ) .run(currentOrderNumber); + + database.close(); + + clearLotTypesCache(); } - database.close(); - - clearLotTypesCache(); - return true; } diff --git a/helpers/lotOccupancyDB/moveOccupancyTypeUp.js b/helpers/lotOccupancyDB/moveOccupancyTypeUp.js index 03bab98f..3ecfecbe 100644 --- a/helpers/lotOccupancyDB/moveOccupancyTypeUp.js +++ b/helpers/lotOccupancyDB/moveOccupancyTypeUp.js @@ -22,8 +22,8 @@ export function moveOccupancyTypeUp(occupancyTypeId) { } export function moveOccupancyTypeUpToTop(occupancyTypeId) { const currentOrderNumber = getOccupancyTypeById(typeof occupancyTypeId === "string" ? Number.parseInt(occupancyTypeId) : occupancyTypeId).orderNumber; - const database = sqlite(databasePath); if (currentOrderNumber > 0) { + const database = sqlite(databasePath); database .prepare("update OccupancyTypes set orderNumber = -1 where occupancyTypeId = ?") .run(occupancyTypeId); @@ -33,9 +33,9 @@ export function moveOccupancyTypeUpToTop(occupancyTypeId) { where recordDelete_timeMillis is null and orderNumber < ?`) .run(currentOrderNumber); + database.close(); + clearOccupancyTypesCache(); } - database.close(); - clearOccupancyTypesCache(); return true; } export default moveOccupancyTypeUp; diff --git a/helpers/lotOccupancyDB/moveOccupancyTypeUp.ts b/helpers/lotOccupancyDB/moveOccupancyTypeUp.ts index 90c3a32d..6fdee9be 100644 --- a/helpers/lotOccupancyDB/moveOccupancyTypeUp.ts +++ b/helpers/lotOccupancyDB/moveOccupancyTypeUp.ts @@ -40,9 +40,9 @@ export function moveOccupancyTypeUpToTop(occupancyTypeId: number | string): bool typeof occupancyTypeId === "string" ? Number.parseInt(occupancyTypeId) : occupancyTypeId ).orderNumber; - const database = sqlite(databasePath); - if (currentOrderNumber > 0) { + const database = sqlite(databasePath); + database .prepare("update OccupancyTypes set orderNumber = -1 where occupancyTypeId = ?") .run(occupancyTypeId); @@ -55,12 +55,12 @@ export function moveOccupancyTypeUpToTop(occupancyTypeId: number | string): bool and orderNumber < ?` ) .run(currentOrderNumber); + + database.close(); + + clearOccupancyTypesCache(); } - database.close(); - - clearOccupancyTypesCache(); - return true; } diff --git a/helpers/lotOccupancyDB/moveWorkOrderTypeUp.js b/helpers/lotOccupancyDB/moveWorkOrderTypeUp.js index bff4f561..8ca829a3 100644 --- a/helpers/lotOccupancyDB/moveWorkOrderTypeUp.js +++ b/helpers/lotOccupancyDB/moveWorkOrderTypeUp.js @@ -22,8 +22,8 @@ export function moveWorkOrderTypeUp(workOrderTypeId) { } export function moveWorkOrderTypeUpToTop(workOrderTypeId) { const currentOrderNumber = getWorkOrderTypeById(typeof workOrderTypeId === "string" ? Number.parseInt(workOrderTypeId) : workOrderTypeId).orderNumber; - const database = sqlite(databasePath); if (currentOrderNumber > 0) { + const database = sqlite(databasePath); database .prepare("update WorkOrderTypes set orderNumber = -1 where workOrderTypeId = ?") .run(workOrderTypeId); @@ -33,9 +33,9 @@ export function moveWorkOrderTypeUpToTop(workOrderTypeId) { where recordDelete_timeMillis is null and orderNumber < ?`) .run(currentOrderNumber); + database.close(); + clearWorkOrderTypesCache(); } - database.close(); - clearWorkOrderTypesCache(); return true; } export default moveWorkOrderTypeUp; diff --git a/helpers/lotOccupancyDB/moveWorkOrderTypeUp.ts b/helpers/lotOccupancyDB/moveWorkOrderTypeUp.ts index f3633248..863b0cdc 100644 --- a/helpers/lotOccupancyDB/moveWorkOrderTypeUp.ts +++ b/helpers/lotOccupancyDB/moveWorkOrderTypeUp.ts @@ -40,9 +40,9 @@ export function moveWorkOrderTypeUpToTop(workOrderTypeId: number | string): bool typeof workOrderTypeId === "string" ? Number.parseInt(workOrderTypeId) : workOrderTypeId ).orderNumber; - const database = sqlite(databasePath); - if (currentOrderNumber > 0) { + const database = sqlite(databasePath); + database .prepare("update WorkOrderTypes set orderNumber = -1 where workOrderTypeId = ?") .run(workOrderTypeId); @@ -55,12 +55,12 @@ export function moveWorkOrderTypeUpToTop(workOrderTypeId: number | string): bool and orderNumber < ?` ) .run(currentOrderNumber); + + database.close(); + + clearWorkOrderTypesCache(); } - database.close(); - - clearWorkOrderTypesCache(); - return true; }