I am trying to execute sql query against oracle database and retrieve the columns into the type defined in typescript code.
Can you please let me know if there is any better way to retrieve columns from SQL query result.
I am concerned about the if condition inside the for loop in below code snippet.
Here, I am retrieving only one column but what if I try to fetch more columns (for e.g. 10). Do i need to validate all these columns existence just like ‘CONFIGCATEGORY’ in below code snippet.
I am using node-oracledb library version – 6.3.0
import OracleDB from "oracledb";
export async function getAppConfigCategoriesListForUI(): Promise<appConfigCategoriesResponse> {
let appConfigCategoriesResponse: appConfigCategoriesResponse;
appConfigCategoriesResponse = {
categories: [],
message: "",
totalRecords: 0
}
const sql: string = `SELECT distinct configcategory FROM configurations`;
const execOptions: OracleDB.ExecuteOptions = { outFormat: OracleDB.OUT_FORMAT_OBJECT };
try {
const dbConnection = await OracleDB.getConnection('configpool');
const result = await dbConnection.execute(sql, [], execOptions);
const rows = result?.rows;
if (rows && typeof rows === "object") {
for(const row of rows) {
if(row && typeof row === "object" && 'CONFIGCATEGORY' in row && typeof row.CONFIGCATEGORY === "string")
appConfigCategoriesResponse.categories?.push(row.CONFIGCATEGORY as string);
}
}
appConfigCategoriesResponse.totalRecords = appConfigCategoriesResponse.categories?.length;
await dbConnection.close();
} catch (err) {
appConfigCategoriesResponse.message = "Error while retrieving application configuration categories";
logger.error(err);
console.error(err);
}
return appConfigCategoriesResponse;
}