test(prod sql): configs are set and basics init
This commit is contained in:
20
app/src/pkg/prodSql/prodSqlConfig.ts
Normal file
20
app/src/pkg/prodSql/prodSqlConfig.ts
Normal 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
|
||||
},
|
||||
};
|
||||
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