query result formatter
This commit is contained in:
parent
d7b92aedc1
commit
5a59a63e4a
3 changed files with 56 additions and 2 deletions
|
@ -91,7 +91,7 @@ export async function executeQuery(req: Request, res: Response): Promise<any> {
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
stats: "success",
|
stats: "success",
|
||||||
rows: rows,
|
rows: DynamicQueryBuilder.flattenQueryResult(rows),
|
||||||
total: total,
|
total: total,
|
||||||
offset: offset,
|
offset: offset,
|
||||||
count: count,
|
count: count,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Brackets, DataSource, NotBrackets, ObjectLiteral, SelectQueryBuilder, WhereExpressionBuilder } from "typeorm";
|
import { Brackets, DataSource, NotBrackets, ObjectLiteral, SelectQueryBuilder, WhereExpressionBuilder } from "typeorm";
|
||||||
import { dataSource } from "../data-source";
|
import { dataSource } from "../data-source";
|
||||||
import { ConditionStructure, DynamicQueryStructure } from "../type/dynamicQueries";
|
import { ConditionStructure, DynamicQueryStructure, FieldType, QueryResult } from "../type/dynamicQueries";
|
||||||
import { TableMeta } from "../type/tableMeta";
|
import { TableMeta } from "../type/tableMeta";
|
||||||
|
|
||||||
export default abstract class DynamicQueryBuilder {
|
export default abstract class DynamicQueryBuilder {
|
||||||
|
@ -229,4 +229,54 @@ export default abstract class DynamicQueryBuilder {
|
||||||
|
|
||||||
return { query, parameters };
|
return { query, parameters };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static flattenQueryResult(result: Array<QueryResult>): 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,10 @@ export type OrderByStructure = {
|
||||||
|
|
||||||
export type OrderByType = "ASC" | "DESC";
|
export type OrderByType = "ASC" | "DESC";
|
||||||
|
|
||||||
|
export type QueryResult = {
|
||||||
|
[key: string]: FieldType | QueryResult | Array<QueryResult>;
|
||||||
|
};
|
||||||
|
|
||||||
export const exampleQuery: DynamicQueryStructure = {
|
export const exampleQuery: DynamicQueryStructure = {
|
||||||
select: ["firstname", "lastname"],
|
select: ["firstname", "lastname"],
|
||||||
table: "member",
|
table: "member",
|
||||||
|
|
Loading…
Reference in a new issue