mirror of
https://github.com/BreizhHardware/Jellystat.git
synced 2026-01-18 16:27:20 +01:00
Add numeric conversion for query results and implement isNumeric helper function
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user