2024-12-12 16:34:03 +01:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
import { http } from "@/serverCom";
|
|
|
|
import type { TableMeta } from "../../viewmodels/admin/query.models";
|
2024-12-19 10:32:58 +01:00
|
|
|
import type { DynamicQueryStructure, FieldType } from "../../types/dynamicQueries";
|
|
|
|
import { flattenQueryResult } from "../../helpers/queryFormatter";
|
2024-12-12 16:34:03 +01:00
|
|
|
|
|
|
|
export const useQueryBuilderStore = defineStore("queryBuilder", {
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
tableMetas: [] as Array<TableMeta>,
|
|
|
|
loading: "loading" as "loading" | "fetched" | "failed",
|
2024-12-19 10:32:58 +01:00
|
|
|
data: [] as Array<{ id: FieldType; [key: string]: FieldType }>,
|
2024-12-12 16:34:03 +01:00
|
|
|
totalLength: 0 as number,
|
2024-12-16 17:49:18 +01:00
|
|
|
loadingData: "fetched" as "loading" | "fetched" | "failed",
|
|
|
|
queryError: "" as string | { sql: string; code: string; msg: string },
|
2024-12-17 16:52:03 +01:00
|
|
|
query: undefined as undefined | DynamicQueryStructure | string,
|
2024-12-18 22:27:44 +01:00
|
|
|
activeQueryId: undefined as undefined | number,
|
2024-12-12 16:34:03 +01:00
|
|
|
isLoadedQuery: undefined as undefined | number,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
fetchTableMetas() {
|
|
|
|
this.loading = "loading";
|
|
|
|
http
|
|
|
|
.get("/admin/querybuilder/tables")
|
|
|
|
.then((result) => {
|
|
|
|
this.tableMetas = result.data;
|
|
|
|
this.loading = "fetched";
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.loading = "failed";
|
|
|
|
});
|
|
|
|
},
|
|
|
|
sendQuery(offset = 0, count = 25) {
|
2024-12-18 14:29:33 +01:00
|
|
|
this.queryError = "";
|
|
|
|
this.data = [];
|
|
|
|
this.totalLength = 0;
|
|
|
|
if (this.query == undefined || this.query == "" || (typeof this.query != "string" && this.query.table == ""))
|
|
|
|
return;
|
2024-12-12 16:34:03 +01:00
|
|
|
this.loadingData = "loading";
|
|
|
|
http
|
2024-12-16 13:56:06 +01:00
|
|
|
.post(`/admin/querybuilder/query?offset=${offset}&count=${count}`, {
|
2024-12-12 16:34:03 +01:00
|
|
|
query: this.query,
|
|
|
|
})
|
|
|
|
.then((result) => {
|
2024-12-16 17:49:18 +01:00
|
|
|
if (result.data.stats == "success") {
|
2024-12-19 10:32:58 +01:00
|
|
|
this.data = flattenQueryResult(result.data.rows).map((row) => ({
|
|
|
|
id: row.id ?? "", // Ensure id is present
|
|
|
|
...row,
|
|
|
|
}));
|
2024-12-16 17:49:18 +01:00
|
|
|
this.totalLength = result.data.total;
|
|
|
|
this.loadingData = "fetched";
|
|
|
|
} else {
|
|
|
|
this.queryError = result.data ?? "An error occurred";
|
|
|
|
this.loadingData = "failed";
|
|
|
|
}
|
2024-12-12 16:34:03 +01:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2024-12-16 17:49:18 +01:00
|
|
|
this.queryError = "An error occurred";
|
2024-12-12 16:34:03 +01:00
|
|
|
this.loadingData = "failed";
|
|
|
|
});
|
|
|
|
},
|
2024-12-16 13:56:06 +01:00
|
|
|
clearResults() {
|
|
|
|
this.data = [];
|
|
|
|
this.totalLength = 0;
|
2024-12-16 17:49:18 +01:00
|
|
|
this.queryError = "";
|
|
|
|
this.loadingData = "fetched";
|
2024-12-16 13:56:06 +01:00
|
|
|
},
|
2024-12-19 10:32:58 +01:00
|
|
|
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);
|
|
|
|
},
|
2024-12-12 16:34:03 +01:00
|
|
|
},
|
|
|
|
});
|