fix(sql server): reduced the risk of error when missing data

This commit is contained in:
2025-03-13 15:43:15 -05:00
parent 58975ca117
commit 6158f254b7

View File

@@ -4,11 +4,32 @@ import {createLog} from "../logger/logger.js";
import {db} from "../../../database/dbclient.js";
import {settings} from "../../../database/schema/settings.js";
import {eq} from "drizzle-orm";
import {installed} from "../../index.js";
let pool: any;
let connected: boolean = false;
export const initializeProdPool = async () => {
if (connected) return {success: false, message: "There is already a connection."};
if (!installed) {
createLog("info", "lst", "sqlProd", "The server was not installed will reconnect in 5 seconds");
setTimeout(() => {
initializeProdPool();
}, 5 * 1000);
return {success: false, message: "The server is not installed."};
}
// make sure the server is not set to localhost this will prevent some weird issues later but can be localhost on the dev
const serverLoc = await db.select().from(settings).where(eq(settings.name, "dbServer"));
if (serverLoc[0].value === "localhost" && process.env.NODE_ENV !== "development") {
createLog("error", "lst", "sqlProd", "The server is set to localhost, and you are not in development mode.");
return {success: false, message: "The server is set to localhost, and you are not in development mode."};
}
// if you were restarting from the endpoint you get this lovely error
if (connected) {
createLog("error", "lst", "sqlProd", "There is already a connection.");
return {success: false, message: "There is already a connection."};
}
try {
const config = await prodSqlConfig();
pool = await sql.connect(config!);
@@ -40,6 +61,9 @@ export const closePool = async () => {
};
export async function query(queryToRun: string, name: string) {
/**
* We no longer need to send over the plant token change as we do it inside the query function.
*/
const plantToken = await db.select().from(settings).where(eq(settings.name, "plantToken"));
const query = queryToRun.replaceAll("test1", plantToken[0].value);
try {