From 5a59a63e4a110272e41d9034432d890dc3cd648a Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Fri, 27 Dec 2024 13:17:23 +0100 Subject: [PATCH] query result formatter --- .../admin/queryBuilderController.ts | 2 +- src/helpers/dynamicQueryBuilder.ts | 52 ++++++++++++++++++- src/type/dynamicQueries.ts | 4 ++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/controller/admin/queryBuilderController.ts b/src/controller/admin/queryBuilderController.ts index be4a68e..e6bb519 100644 --- a/src/controller/admin/queryBuilderController.ts +++ b/src/controller/admin/queryBuilderController.ts @@ -91,7 +91,7 @@ export async function executeQuery(req: Request, res: Response): Promise { res.json({ stats: "success", - rows: rows, + rows: DynamicQueryBuilder.flattenQueryResult(rows), total: total, offset: offset, count: count, diff --git a/src/helpers/dynamicQueryBuilder.ts b/src/helpers/dynamicQueryBuilder.ts index ee6ce4c..1dc1b94 100644 --- a/src/helpers/dynamicQueryBuilder.ts +++ b/src/helpers/dynamicQueryBuilder.ts @@ -1,6 +1,6 @@ import { Brackets, DataSource, NotBrackets, ObjectLiteral, SelectQueryBuilder, WhereExpressionBuilder } from "typeorm"; import { dataSource } from "../data-source"; -import { ConditionStructure, DynamicQueryStructure } from "../type/dynamicQueries"; +import { ConditionStructure, DynamicQueryStructure, FieldType, QueryResult } from "../type/dynamicQueries"; import { TableMeta } from "../type/tableMeta"; export default abstract class DynamicQueryBuilder { @@ -229,4 +229,54 @@ export default abstract class DynamicQueryBuilder { return { query, parameters }; } + + public static flattenQueryResult(result: Array): Array<{ [key: string]: FieldType }> { + function flatten(row: QueryResult, prefix: string = ""): Array<{ [key: string]: FieldType }> { + let results: Array<{ [key: string]: FieldType }> = [{}]; + + for (const key in row) { + const value = row[key]; + const newKey = prefix ? `${prefix}_${key}` : key; + + if (Array.isArray(value) && value.every((item) => typeof item === "object" && item !== null)) { + const arrayResults: Array<{ [key: string]: FieldType }> = []; + value.forEach((item) => { + const flattenedItems = flatten(item, newKey); + arrayResults.push(...flattenedItems); + }); + + const tempResults: Array<{ [key: string]: FieldType }> = []; + results.forEach((res) => { + arrayResults.forEach((arrRes) => { + tempResults.push({ ...res, ...arrRes }); + }); + }); + results = tempResults; + } else if (value && typeof value === "object" && !Array.isArray(value)) { + const objResults = flatten(value as QueryResult, newKey); + const tempResults: Array<{ [key: string]: FieldType }> = []; + results.forEach((res) => { + objResults.forEach((objRes) => { + tempResults.push({ ...res, ...objRes }); + }); + }); + results = tempResults; + } else { + results.forEach((res) => { + res[newKey] = String(value); + }); + } + } + return results; + } + + const flattenedResults: Array<{ [key: string]: FieldType }> = []; + + result.forEach((item) => { + const flattenedItems = flatten(item); + flattenedResults.push(...flattenedItems); + }); + + return flattenedResults; + } } diff --git a/src/type/dynamicQueries.ts b/src/type/dynamicQueries.ts index 9e1f046..7ae4a79 100644 --- a/src/type/dynamicQueries.ts +++ b/src/type/dynamicQueries.ts @@ -54,6 +54,10 @@ export type OrderByStructure = { export type OrderByType = "ASC" | "DESC"; +export type QueryResult = { + [key: string]: FieldType | QueryResult | Array; +}; + export const exampleQuery: DynamicQueryStructure = { select: ["firstname", "lastname"], table: "member",