Add numeric conversion for query results and implement isNumeric helper function

This commit is contained in:
CyferShepard
2025-04-06 01:20:49 +02:00
parent ccc8469716
commit c720e56c08
2 changed files with 43 additions and 1 deletions

View File

@@ -96,6 +96,11 @@ function buildCTE(cte) {
return query;
}
// Helper function to check if a value is numeric
function isNumeric(value) {
return !isNaN(value) && !isNaN(parseFloat(value));
}
async function query({
cte,
select = ["*"],
@@ -155,10 +160,25 @@ async function query({
const countResult = await client.query(countQuery, values);
const totalRows = parseInt(countResult.rows.length > 0 ? countResult.rows[0].count : 0, 10);
const skippedColumns = ["Name"];
// Convert integer fields in the result rows
const convertedRows = result.rows.map((row) => {
return Object.keys(row).reduce((acc, key) => {
const value = row[key];
if (skippedColumns.includes(key)) {
acc[key] = value; // Keep the original value for skipped columns
return acc; // Skip the rowid field
}
// Convert numeric strings to integers if applicable
acc[key] = isNumeric(value) ? parseInt(value, 10) : value;
return acc;
}, {});
});
// Return the structured response
return {
pages: Math.ceil(totalRows / pageSize),
results: result.rows,
results: convertedRows,
};
} catch (error) {
// console.timeEnd("queryWithPagingAndJoins");

View File

@@ -166,6 +166,10 @@ async function insertBulk(table_name, data, columns) {
};
}
function isNumeric(value) {
return !isNaN(value) && !isNaN(parseFloat(value));
}
async function query(text, params, refreshViews = false) {
try {
const result = await pool.query(text, params);
@@ -175,6 +179,24 @@ async function query(text, params, refreshViews = false) {
refreshMaterializedView(view);
}
}
const skippedColumns = ["Name"];
// Convert integer fields in the result rows
const convertedRows = result.rows.map((row) => {
return Object.keys(row).reduce((acc, key) => {
const value = row[key];
if (skippedColumns.includes(key)) {
acc[key] = value; // Keep the original value for skipped columns
return acc; // Skip the rowid field
}
// Convert numeric strings to integers if applicable
acc[key] = isNumeric(value) ? parseInt(value, 10) : value;
return acc;
}, {});
});
result.rows = convertedRows;
return result;
} catch (error) {
if (error?.routine === "auth_failed") {