Skip to content

Commit

Permalink
fix: table structure
Browse files Browse the repository at this point in the history
  • Loading branch information
invm committed Aug 31, 2023
1 parent aaab4d2 commit 656e41a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useAppSelector } from "services/Context";
import { createSignal, For, Match, onMount, Switch } from "solid-js";
import { invoke } from "@tauri-apps/api";
import { TableStructureResult } from "interfaces";
import { TableStructureResult, TableStrucureEntities } from "interfaces";
import { t } from "utils/i18n";
import { Loader } from "components/UI";
import { createStore } from "solid-js/store";
Expand All @@ -15,9 +15,8 @@ export const TableStructureTab = () => {
} = useAppSelector();
const [loading, setLoading] = createSignal(true);
const [tabIdx, setTabIdx] = createSignal(0);
const tabs = ["columns", "indices", "constraints", "triggers"];
const [data, setData] = createStore<Record<string, Record<string, any>[]>>(
{}
const [data, setData] = createStore<TableStructureResult>(
{} as TableStructureResult
);

onMount(async () => {
Expand Down Expand Up @@ -46,7 +45,7 @@ export const TableStructureTab = () => {
<div>
<div class="flex justify-center">
<div class="tabs tabs-boxed gap-1">
{tabs.map((tab, idx) => (
{TableStrucureEntities.map((tab, idx) => (
<button
onClick={() => setTabIdx(idx)}
tabIndex={0}
Expand All @@ -60,7 +59,7 @@ export const TableStructureTab = () => {
</div>
<div>
<Switch fallback={<div>Not found</div>}>
<For each={tabs}>
<For each={TableStrucureEntities}>
{(item, idx) => (
<Match when={tabIdx() === idx()}>
<StructureTable data={data[item]} />
Expand Down
22 changes: 12 additions & 10 deletions src/components/Screens/Console/Sidebar/TableColumnsCollapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ export const TableColumnsCollapse = (props: {

const menu_id = "sidebar-table-menu";

const { show } = useContextMenu({ id: menu_id, props: { id: "" } });
const { show } = useContextMenu({
id: menu_id,
props: { table: props.title },
});

const openTableStructureTab = () => {
const openTableStructureTab = (table: string) => {
setContentStore("tabs", [
...contentStore.tabs,
newContentTab(props.title, "TableStructureTab"),
newContentTab(table, "TableStructureTab"),
]);
setContentStore("idx", contentStore.tabs.length - 1);
updateStore();
};

return (
<div
class="w-full"
onContextMenu={(e) => {
show(e);
}}
>
<div class="w-full" onContextMenu={(e) => show(e)}>
<Menu id={menu_id} animation={animation.fade} theme={"dark"}>
<Item onClick={() => openTableStructureTab()}>
<Item
onClick={({ props }) => {
openTableStructureTab(props.table);
}}
>
{t("components.sidebar.show_table_structure")}
</Item>
</Menu>
Expand Down
40 changes: 36 additions & 4 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,41 @@ export type DbSchema = {

export type QueryResult = { result: Record<string, any>[]; message?: string };

export const TableStrucureEntity = {
Columns: "columns",
Indices: "indices",
Constraints: "constraints",
Triggers: "triggers",
} as const;

export const TableStrucureEntities = [
TableStrucureEntity.Columns,
TableStrucureEntity.Indices,
TableStrucureEntity.Constraints,
TableStrucureEntity.Triggers,
] as const;

export type TableStructureResult = {
columns: Record<string, any>[];
constraints: Record<string, any>[];
indices: Record<string, any>[];
triggers: Record<string, any>[];
[TableStrucureEntity.Columns]: Record<string, any>[];
[TableStrucureEntity.Constraints]: Record<string, any>[];
[TableStrucureEntity.Indices]: Record<string, any>[];
[TableStrucureEntity.Triggers]: Record<string, any>[];
};

const STRUCTURE_TYPES = {
Column: "Column",
Constraint: "Constraint",
Index: "Index",
Trigger: "Trigger",
} as const;

export const SORT_ORDER = {
[Schemes.Mysql]: {
[STRUCTURE_TYPES.Column]: [
"COLUMN_NAME",
"COLUMN_TYPE",
"IS_NULLABLE",
"CHARACTER_MAXIMUM_LENGTH",
],
},
};
12 changes: 8 additions & 4 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { SORT_ORDER, TableStructureResult } from "interfaces";

export const omit = (obj: any, ...keys: string[]) => {
const copy = { ...obj };
keys.forEach(key => delete copy[key]);
keys.forEach((key) => delete copy[key]);
return copy;
}
};

export const firstKey = (obj: Record<string, any>) => {
for (const key in obj) return key;
}
};

export const randomId = () => Math.random().toString(36).substring(12);

Expand All @@ -21,5 +23,7 @@ export const debounce = (func: Function, wait: number) => {

clearTimeout(timer);
timer = setTimeout(later, wait);
}
};
};

export const sortTableStructure = (tableStructure: TableStructureResult) => { };

0 comments on commit 656e41a

Please sign in to comment.