Join Table Name & formatted results with export

This commit is contained in:
Julian Krauser 2024-12-19 10:32:58 +01:00
parent 9206a657c1
commit 604ae30901
7 changed files with 119 additions and 16 deletions

View file

@ -1,14 +1,15 @@
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { TableMeta } from "../../viewmodels/admin/query.models";
import type { DynamicQueryStructure } from "../../types/dynamicQueries";
import type { DynamicQueryStructure, FieldType } from "../../types/dynamicQueries";
import { flattenQueryResult } from "../../helpers/queryFormatter";
export const useQueryBuilderStore = defineStore("queryBuilder", {
state: () => {
return {
tableMetas: [] as Array<TableMeta>,
loading: "loading" as "loading" | "fetched" | "failed",
data: [] as Array<{ id: number; [key: string]: any }>,
data: [] as Array<{ id: FieldType; [key: string]: FieldType }>,
totalLength: 0 as number,
loadingData: "fetched" as "loading" | "fetched" | "failed",
queryError: "" as string | { sql: string; code: string; msg: string },
@ -43,7 +44,10 @@ export const useQueryBuilderStore = defineStore("queryBuilder", {
})
.then((result) => {
if (result.data.stats == "success") {
this.data = result.data.rows;
this.data = flattenQueryResult(result.data.rows).map((row) => ({
id: row.id ?? "", // Ensure id is present
...row,
}));
this.totalLength = result.data.total;
this.loadingData = "fetched";
} else {
@ -62,5 +66,28 @@ export const useQueryBuilderStore = defineStore("queryBuilder", {
this.queryError = "";
this.loadingData = "fetched";
},
exportData() {
if (this.data.length == 0) return;
const csvString = [Object.keys(this.data[0]), ...this.data.map((d) => Object.values(d))]
.map((e) => e.join(";"))
.join("\n");
// Create a Blob from the CSV string
const blob = new Blob([csvString], { type: "text/csv" });
// Create a download link
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "items.csv";
// Append the link to the document and trigger the download
document.body.appendChild(a);
a.click();
// Clean up
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
},
},
});