2024-12-12 16:34:03 +01:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
import { http } from "@/serverCom";
|
|
|
|
import type { TableMeta } from "../../viewmodels/admin/query.models";
|
|
|
|
import type { DynamicQueryStructure } from "../../types/dynamicQueries";
|
|
|
|
|
|
|
|
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 }>,
|
|
|
|
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-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") {
|
|
|
|
this.data = result.data.rows;
|
|
|
|
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-12 16:34:03 +01:00
|
|
|
},
|
|
|
|
});
|