test(prod sql): configs are set and basics init
This commit is contained in:
48
app/src/pkg/prodSql/prodSqlConnect.ts
Normal file
48
app/src/pkg/prodSql/prodSqlConnect.ts
Normal 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");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user