test(prod sql): configs are set and basics init

This commit is contained in:
2025-08-31 21:36:31 -05:00
parent 8eefbe9df0
commit e73204424c
2 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
import sql from "mssql";
import { env } from "../utils/envValidator.js";
export const sqlConfig: sql.config = {
server: env.PROD_SERVER,
database: `AlplaPROD_${env.PROD_PLANT_TOKEN}_cus`,
user: env.PROD_USER,
password: env.PROD_PASSWORD,
options: {
encrypt: true,
trustServerCertificate: true,
},
requestTimeout: 90000, // in milliseconds
pool: {
max: 20, // Maximum number of connections in the pool
min: 0, // Minimum number of connections in the pool
idleTimeoutMillis: 10000, // How long a connection is allowed to be idle before being released
reapIntervalMillis: 1000, // how often to check for idle resourses to destory
acquireTimeoutMillis: 100000, // How long until a complete timeout happens
},
};

View File

@@ -0,0 +1,48 @@
import sql from "mssql";
import { checkHostnamePort } from "../utils/checkHostNamePort.js";
import { sqlConfig } from "./prodSqlConfig.js";
import { env } from "../utils/envValidator.js";
import { createLogger } from "../logger/logger.js";
let pool;
let connected: boolean = false;
export const initializeProdPool = async () => {
const log = createLogger({ module: "prodSql" });
const serverUp = await checkHostnamePort(`${env.PROD_SERVER}:1433`);
if (!serverUp) {
log.error(`The sql ${process.env.PROD_SERVER} is not reachable`);
return {
success: false,
message: `The sql ${env.PROD_SERVER} is not reachable`,
data: [],
};
}
// if you were restarting from the endpoint you get this lovely error
if (connected) {
log.error("There is already a connection.");
return { success: false, message: "There is already a connection." };
}
try {
pool = sql.connect(sqlConfig);
log.info(
`Connected to ${sqlConfig?.server}, and looking at ${sqlConfig?.database}`
);
connected = true;
return {
success: true,
message: "The sql server connection has been closed",
};
} catch (error) {
log.fatal(
`${JSON.stringify(
error
)}, "There was an error connecting to the pool."`
);
// throw new Error("There was an error closing the sql connection");
}
};