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, loading: "loading" as "loading" | "fetched" | "failed", data: [] as Array<{ id: number; [key: string]: any }>, totalLength: 0 as number, loadingData: "fetched" as "loading" | "fetched" | "failed", queryError: "" as string | { sql: string; code: string; msg: string }, query: undefined as undefined | DynamicQueryStructure | string, 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) { if (this.query == undefined) return; this.loadingData = "loading"; http .post(`/admin/querybuilder/query?offset=${offset}&count=${count}`, { query: this.query, }) .then((result) => { 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"; } }) .catch((err) => { this.queryError = "An error occurred"; this.loadingData = "failed"; }); }, clearResults() { this.data = []; this.totalLength = 0; this.queryError = ""; this.loadingData = "fetched"; }, }, });