Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

add index, add fields

Guy Ben-Aharon0fcb739f1bda
diff --git a/package-lock.json b/package-lock.json
index e60bf97f..de3b11c2 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,6 +9,7 @@
"version": "0.0.0",
"dependencies": {
"@radix-ui/react-accordion": "^1.2.0",
+ "@radix-ui/react-checkbox": "^1.1.1",
"@radix-ui/react-collapsible": "^1.1.0",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.1",
@@ -1216,6 +1217,36 @@
}
}
},
+ "node_modules/@radix-ui/react-checkbox": {
+ "version": "1.1.1",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-checkbox/-/react-checkbox-1.1.1.tgz",
+ "integrity": "sha512-0i/EKJ222Afa1FE0C6pNJxDq1itzcl3HChE9DwskA4th4KRse8ojx8a1nVcOjwJdbpDLcz7uol77yYnQNMHdKw==",
+ "license": "MIT",
+ "dependencies": {
+ "@radix-ui/primitive": "1.1.0",
+ "@radix-ui/react-compose-refs": "1.1.0",
+ "@radix-ui/react-context": "1.1.0",
+ "@radix-ui/react-presence": "1.1.0",
+ "@radix-ui/react-primitive": "2.0.0",
+ "@radix-ui/react-use-controllable-state": "1.1.0",
+ "@radix-ui/react-use-previous": "1.1.0",
+ "@radix-ui/react-use-size": "1.1.0"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "@types/react-dom": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
+ "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ },
+ "@types/react-dom": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@radix-ui/react-collapsible": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@radix-ui/react-collapsible/-/react-collapsible-1.1.0.tgz",
diff --git a/package.json b/package.json
index 3136f0af..efd4772b 100644
--- a/package.json
+++ b/package.json
@@ -12,6 +12,7 @@
},
"dependencies": {
"@radix-ui/react-accordion": "^1.2.0",
+ "@radix-ui/react-checkbox": "^1.1.1",
"@radix-ui/react-collapsible": "^1.1.0",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.1",
diff --git a/src/components/checkbox/checkbox.tsx b/src/components/checkbox/checkbox.tsx
new file mode 100644
index 00000000..b909292f
--- /dev/null
+++ b/src/components/checkbox/checkbox.tsx
@@ -0,0 +1,28 @@
+import React from 'react';
+import * as CheckboxPrimitive from '@radix-ui/react-checkbox';
+import { CheckIcon } from '@radix-ui/react-icons';
+
+import { cn } from '@/lib/utils';
+
+const Checkbox = React.forwardRef<
+ React.ElementRef<typeof CheckboxPrimitive.Root>,
+ React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>
+>(({ className, ...props }, ref) => (
+ <CheckboxPrimitive.Root
+ ref={ref}
+ className={cn(
+ 'peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground',
+ className
+ )}
+ {...props}
+ >
+ <CheckboxPrimitive.Indicator
+ className={cn('flex items-center justify-center text-current')}
+ >
+ <CheckIcon className="h-4 w-4" />
+ </CheckboxPrimitive.Indicator>
+ </CheckboxPrimitive.Root>
+));
+Checkbox.displayName = CheckboxPrimitive.Root.displayName;
+
+export { Checkbox };
diff --git a/src/components/combobox/combobox.tsx b/src/components/combobox/combobox.tsx
index 157fe637..2a443407 100644
--- a/src/components/combobox/combobox.tsx
+++ b/src/components/combobox/combobox.tsx
@@ -34,6 +34,7 @@ interface ComboboxProps {
onChange?: (event: string | string[]) => void; // Updated to handle multiple selections
onCreate?: (value: string) => void;
emptyText?: string;
+ popoverClassName?: string;
}
export function Combobox({
@@ -45,6 +46,7 @@ export function Combobox({
emptyText,
onChange,
onCreate,
+ popoverClassName,
}: ComboboxProps) {
const [open, setOpen] = React.useState(false);
const [query, setQuery] = React.useState<string>('');
@@ -90,7 +92,9 @@ export function Combobox({
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
- <PopoverContent className="w-40 max-w-sm p-0">
+ <PopoverContent
+ className={cn('w-40 max-w-sm p-0', popoverClassName)}
+ >
<Command
filter={(value, search) => {
if (value.includes(search)) return 1;
diff --git a/src/context/chartdb-context/chartdb-context.tsx b/src/context/chartdb-context/chartdb-context.tsx
index c6c0be85..7e734e87 100644
--- a/src/context/chartdb-context/chartdb-context.tsx
+++ b/src/context/chartdb-context/chartdb-context.tsx
@@ -3,6 +3,7 @@ import { DBTable } from '@/lib/domain/db-table';
import { emptyFn } from '@/lib/utils';
import { DatabaseType } from '@/lib/domain/database-type';
import { DBField } from '@/lib/domain/db-field';
+import { DBIndex } from '@/lib/domain/db-index';
export interface ChartDBContext {
databaseType: DatabaseType;
@@ -12,17 +13,33 @@ export interface ChartDBContext {
setDatabaseType: (databaseType: DatabaseType) => void;
// Table operations
- createTable: () => void;
+ createTable: () => DBTable;
addTable: (table: DBTable) => void;
+ getTable: (id: string) => DBTable | null;
removeTable: (id: string) => void;
updateTable: (id: string, table: Partial<DBTable>) => void;
// Field operations
+ getField: (tableId: string, fieldId: string) => DBField | null;
updateField: (
tableId: string,
fieldId: string,
field: Partial<DBField>
) => void;
+ removeField: (tableId: string, fieldId: string) => void;
+ createField: (tableId: string) => DBField;
+ addField: (tableId: string, field: DBField) => void;
+
+ // Index operations
+ createIndex: (tableId: string) => DBIndex;
+ addIndex: (tableId: string, index: DBIndex) => void;
+ getIndex: (tableId: string, indexId: string) => DBIndex | null;
+ removeIndex: (tableId: string, indexId: string) => void;
+ updateIndex: (
+ tableId: string,
+ indexId: string,
+ index: Partial<DBIndex>
+ ) => void;
}
export const chartDBContext = createContext<ChartDBContext>({
@@ -34,10 +51,22 @@ export const chartDBContext = createContext<ChartDBContext>({
// Table operations
createTable: emptyFn,
+ getTable: emptyFn,
addTable: emptyFn,
removeTable: emptyFn,
updateTable: emptyFn,
// Field operations
updateField: emptyFn,
+ removeField: emptyFn,
+ createField: emptyFn,
+ addField: emptyFn,
+ getField: emptyFn,
+
+ // Index operations
+ createIndex: emptyFn,
+ addIndex: emptyFn,
+ getIndex: emptyFn,
+ removeIndex: emptyFn,
+ updateIndex: emptyFn,
});
diff --git a/src/context/chartdb-context/chartdb-provider.tsx b/src/context/chartdb-context/chartdb-provider.tsx
index 0c847489..589dbeee 100644
--- a/src/context/chartdb-context/chartdb-provider.tsx
+++ b/src/context/chartdb-context/chartdb-provider.tsx
@@ -5,6 +5,7 @@ import { nanoid } from 'nanoid';
import { chartDBContext } from './chartdb-context';
import { DatabaseType } from '@/lib/domain/database-type';
import { DBField } from '@/lib/domain/db-field';
+import { DBIndex } from '@/lib/domain/db-index';
export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
children,
@@ -19,7 +20,7 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
};
const createTable = () => {
- addTable({
+ const table: DBTable = {
id: nanoid(),
name: `table_${tables.length + 1}`,
x: 0,
@@ -38,9 +39,15 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
indexes: [],
color: randomHSLA(),
createdAt: Date.now(),
- });
+ };
+ addTable(table);
+
+ return table;
};
+ const getTable = (id: string) =>
+ tables.find((table) => table.id === id) ?? null;
+
const removeTable = (id: string) => {
setTables((tables) => tables.filter((table) => table.id !== id));
};
@@ -70,6 +77,115 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
);
};
+ const removeField = (tableId: string, fieldId: string) => {
+ setTables((tables) =>
+ tables.map((table) =>
+ table.id === tableId
+ ? {
+ ...table,
+ fields: table.fields.filter((f) => f.id !== fieldId),
+ }
+ : table
+ )
+ );
+ };
+
+ const addField = (tableId: string, field: DBField) => {
+ setTables((tables) =>
+ tables.map((table) =>
+ table.id === tableId
+ ? { ...table, fields: [...table.fields, field] }
+ : table
+ )
+ );
+ };
+
+ const getField = (tableId: string, fieldId: string) => {
+ const table = getTable(tableId);
+ return table?.fields.find((f) => f.id === fieldId) ?? null;
+ };
+
+ const createField = (tableId: string) => {
+ const table = getTable(tableId);
+ const field: DBField = {
+ id: nanoid(),
+ name: `field_${(table?.fields?.length ?? 0) + 1}`,
+ type: 'bigint',
+ unique: false,
+ nullable: true,
+ primaryKey: false,
+ createdAt: Date.now(),
+ };
+
+ addField(tableId, field);
+
+ return field;
+ };
+
+ const addIndex = (tableId: string, index: DBIndex) => {
+ setTables((tables) =>
+ tables.map((table) =>
+ table.id === tableId
+ ? { ...table, indexes: [...table.indexes, index] }
+ : table
+ )
+ );
+ };
+
+ const removeIndex = (tableId: string, indexId: string) => {
+ setTables((tables) =>
+ tables.map((table) =>
+ table.id === tableId
+ ? {
+ ...table,
+ indexes: table.indexes.filter(
+ (i) => i.id !== indexId
+ ),
+ }
+ : table
+ )
+ );
+ };
+
+ const createIndex = (tableId: string) => {
+ const table = getTable(tableId);
+ const index: DBIndex = {
+ id: nanoid(),
+ name: `index_${(table?.indexes?.length ?? 0) + 1}`,
+ fieldIds: [],
+ unique: false,
+ createdAt: Date.now(),
+ };
+
+ addIndex(tableId, index);
+
+ return index;
+ };
+
+ const getIndex = (tableId: string, indexId: string) => {
+ const table = getTable(tableId);
+ return table?.indexes.find((i) => i.id === indexId) ?? null;
+ };
+
+ const updateIndex = (
+ tableId: string,
+ indexId: string,
+ index: Partial<DBIndex>
+ ) => {
+ setTables((tables) =>
+ tables.map((table) =>
+ table.id === tableId
+ ? {
+ ...table,
+ indexes: table.indexes.map((i) =>
+ i.id === indexId ? { ...i, ...index } : i
+ ),
+ }
+ : table
+ )
+ );
+ };
+
return (
<chartDBContext.Provider
value={{
@@ -78,9 +194,19 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
setDatabaseType,
createTable,
addTable,
+ getTable,
removeTable,
updateTable,
updateField,
+ removeField,
+ createField,
+ addField,
+ addIndex,
+ createIndex,
+ removeIndex,
+ getField,
+ getIndex,
+ updateIndex,
}}
>
{children}
diff --git a/src/lib/domain/db-index.ts b/src/lib/domain/db-index.ts
index 023eb2a0..4d754956 100644
--- a/src/lib/domain/db-index.ts
+++ b/src/lib/domain/db-index.ts
@@ -1,9 +1,7 @@
-import { DBField } from './db-field';
-
export interface DBIndex {
id: string;
name: string;
unique: boolean;
fieldIds: string[];
- fields?: DBField[];
+ createdAt: number;
}
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index 094dd1aa..9dae9d54 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -13,11 +13,8 @@ export const convertToDecimal = (number: number) => {
return number / Math.pow(10, digits); // Divide the number by 10^digits
};
-// export const randomHSLA = () =>
-// `hsla(${~~(360 * Math.random())}, 70%, 72%, 0.8)`;
-
-// creates randomHSLA using nanoid library instead of math.random
export const randomHSLA = () =>
`hsla(${~~(360 * convertToDecimal(parseInt(nanoid())))}, 70%, 72%, 0.8)`;
-export const emptyFn = () => {};
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export const emptyFn = (): any => undefined;
diff --git a/src/pages/editor-page/canvas/table-node.tsx b/src/pages/editor-page/canvas/table-node.tsx
index 8b742d95..5916cc27 100644
--- a/src/pages/editor-page/canvas/table-node.tsx
+++ b/src/pages/editor-page/canvas/table-node.tsx
@@ -10,7 +10,8 @@ import {
useUpdateNodeInternals,
} from '@xyflow/react';
import { Button } from '@/components/button/button';
-import { Ellipsis, Trash2 } from 'lucide-react';
+import { Ellipsis, Table2, Trash2 } from 'lucide-react';
+import { Label } from '@/components/label/label';
export const LEFT_HANDLE_ID_PREFIX = 'left-handle';
export const RIGHT_HANDLE_ID_PREFIX = 'right-handle';
@@ -106,7 +107,10 @@ export const TableNode: React.FC<NodeProps<TableNodeProps>> = ({
style={{ backgroundColor: tableColor }}
></div>
<div className="flex items-center h-9 bg-secondary px-2 justify-between group">
- <div className="flex text-sm font-bold">table_1</div>
+ <div className="flex items-center gap-1">
+ <Table2 className="h-3.5 w-3.5 text-gray-600" />
+ <Label className="text-sm font-bold">table_1</Label>
+ </div>
<div className="flex-row hidden group-hover:flex">
<Button
variant="ghost"
diff --git a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx
new file mode 100644
index 00000000..d5166a89
--- /dev/null
+++ b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx
@@ -0,0 +1,153 @@
+import React from 'react';
+import { Ellipsis, Trash2 } from 'lucide-react';
+import { Input } from '@/components/input/input';
+import { Combobox } from '@/components/combobox/combobox';
+import { Button } from '@/components/button/button';
+import { KeyRound } from 'lucide-react';
+import { Separator } from '@/components/separator/separator';
+
+import { DBField, FieldType } from '@/lib/domain/db-field';
+import { useChartDB } from '@/hooks/use-chartdb';
+import { dataTypeMap } from '@/lib/data/data-types';
+import { Toggle } from '@/components/toggle/toggle';
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from '@/components/tooltip/tooltip';
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from '@/components/popover/popover';
+import { Label } from '@/components/label/label';
+import { Checkbox } from '@/components/checkbox/checkbox';
+
+export interface TableFieldProps {
+ field: DBField;
+ updateField: (attrs: Partial<DBField>) => void;
+ removeField: () => void;
+}
+
+export const TableField: React.FC<TableFieldProps> = ({
+ field,
+ updateField,
+ removeField,
+}) => {
+ const { databaseType } = useChartDB();
+
+ const dataFieldOptions = dataTypeMap[databaseType].map((type) => ({
+ label: type,
+ value: type,
+ }));
+
+ return (
+ <div className="flex flex-row p-1 justify-between flex-1">
+ <div className="flex basis-8/12 gap-1">
+ <Input
+ className="h-8 focus-visible:ring-0 basis-8/12"
+ type="text"
+ placeholder="Name"
+ value={field.name}
+ onChange={(e) =>
+ updateField({
+ name: e.target.value,
+ })
+ }
+ />
+ <Combobox
+ className="flex h-8 basis-4/12"
+ mode="single"
+ options={dataFieldOptions}
+ placeholder="Type"
+ selected={field.type}
+ onChange={(value) =>
+ updateField({
+ type: value as FieldType,
+ })
+ }
+ emptyText="No types found."
+ />
+ </div>
+ <div className="flex gap-1">
+ <Tooltip>
+ <TooltipTrigger>
+ <Toggle
+ variant="default"
+ className="hover:bg-primary-foreground p-2 w-[32px] text-slate-500 hover:text-slate-700 text-xs h-8"
+ pressed={field.nullable}
+ onPressedChange={(value) =>
+ updateField({
+ nullable: value,
+ })
+ }
+ >
+ N
+ </Toggle>
+ </TooltipTrigger>
+ <TooltipContent>Nullable?</TooltipContent>
+ </Tooltip>
+ <Tooltip>
+ <TooltipTrigger>
+ <Toggle
+ variant="default"
+ className="hover:bg-primary-foreground p-2 w-[32px] text-slate-500 hover:text-slate-700 h-8"
+ pressed={field.primaryKey}
+ onPressedChange={(value) =>
+ updateField({
+ primaryKey: value,
+ })
+ }
+ >
+ <KeyRound className="h-3.5" />
+ </Toggle>
+ </TooltipTrigger>
+ <TooltipContent>Primary Key</TooltipContent>
+ </Tooltip>
+ <Popover>
+ <PopoverTrigger>
+ <Button
+ variant="ghost"
+ className="hover:bg-primary-foreground p-2 w-[32px] text-slate-500 hover:text-slate-700 h-8"
+ >
+ <Ellipsis className="h-3.5 w-3.5" />
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent className="w-52">
+ <div className="flex gap-2 flex-col">
+ <div className="text-sm font-semibold">
+ Field Attributes
+ </div>
+ <Separator orientation="horizontal" />
+ <div className="flex justify-between items-center">
+ <Label
+ htmlFor="width"
+ className="text-gray-700"
+ >
+ Unique
+ </Label>
+ <Checkbox
+ checked={field.unique}
+ onCheckedChange={(value) =>
+ updateField({
+ unique: !!value,
+ })
+ }
+ />
+ </div>
+ <Separator orientation="horizontal" />
+ <Button
+ variant="outline"
+ className="flex !text-red-700 gap-2"
+ onClick={removeField}
+ >
+ <Trash2 className="text-red-700 w-3.5 h-3.5" />
+ Delete field
+ </Button>
+ </div>
+ </PopoverContent>
+ </Popover>
+ </div>
+ </div>
+ );
+};
diff --git a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-index/table-index.tsx b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-index/table-index.tsx
new file mode 100644
index 00000000..240e5bf2
--- /dev/null
+++ b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-index/table-index.tsx
@@ -0,0 +1,113 @@
+import React from 'react';
+import { Ellipsis, Trash2 } from 'lucide-react';
+import { Combobox } from '@/components/combobox/combobox';
+import { Button } from '@/components/button/button';
+import { DBIndex } from '@/lib/domain/db-index';
+import { DBField } from '@/lib/domain/db-field';
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from '@/components/popover/popover';
+import { Separator } from '@/components/separator/separator';
+import { Checkbox } from '@/components/checkbox/checkbox';
+import { Label } from '@/components/label/label';
+import { Input } from '@/components/input/input';
+
+export interface TableIndexProps {
+ index: DBIndex;
+ updateIndex: (attrs: Partial<DBIndex>) => void;
+ removeIndex: () => void;
+ fields: DBField[];
+}
+
+export const TableIndex: React.FC<TableIndexProps> = ({
+ fields,
+ index,
+ updateIndex,
+ removeIndex,
+}) => {
+ const fieldOptions = fields.map((field) => ({
+ label: field.name,
+ value: field.id,
+ }));
+ const updateIndexFields = (fieldIds: string | string[]) => {
+ const ids = Array.isArray(fieldIds) ? fieldIds : [fieldIds];
+ updateIndex({ fieldIds: ids });
+ };
+ return (
+ <div className="flex flex-row p-1 justify-between flex-1 gap-2">
+ <Combobox
+ className="flex h-8 flex-1"
+ popoverClassName="w-48"
+ mode="multiple"
+ options={fieldOptions}
+ placeholder="Select fields"
+ selected={index.fieldIds}
+ onChange={updateIndexFields}
+ emptyText="No types found."
+ />
+ <div className="flex">
+ <Popover>
+ <PopoverTrigger>
+ <Button
+ variant="ghost"
+ className="hover:bg-primary-foreground p-2 w-[32px] text-slate-500 hover:text-slate-700 h-8"
+ >
+ <Ellipsis className="h-3.5 w-3.5" />
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent className="w-52">
+ <div className="flex gap-2 flex-col">
+ <div className="text-sm font-semibold">
+ Index Attributes
+ </div>
+ <Separator orientation="horizontal" />
+ <div className="flex flex-col gap-2">
+ <Label
+ htmlFor="width"
+ className="text-gray-700"
+ >
+ Name
+ </Label>
+ <Input
+ value={index.name}
+ onChange={(value) =>
+ updateIndex({
+ name: value.target.value,
+ })
+ }
+ />
+ </div>
+ <div className="flex justify-between items-center mt-2">
+ <Label
+ htmlFor="width"
+ className="text-gray-700"
+ >
+ Unique
+ </Label>
+ <Checkbox
+ checked={index.unique}
+ onCheckedChange={(value) =>
+ updateIndex({
+ unique: !!value,
+ })
+ }
+ />
+ </div>
+ <Separator orientation="horizontal" />
+ <Button
+ variant="outline"
+ className="flex !text-red-700 gap-2"
+ onClick={removeIndex}
+ >
+ <Trash2 className="text-red-700 w-3.5 h-3.5" />
+ Delete index
+ </Button>
+ </div>
+ </PopoverContent>
+ </Popover>
+ </div>
+ </div>
+ );
+};
diff --git a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-list-item-content.tsx b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-list-item-content.tsx
index 15721f79..f809302f 100644
--- a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-list-item-content.tsx
+++ b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-list-item-content.tsx
@@ -1,9 +1,6 @@
import React from 'react';
-import { Ellipsis, Trash2, Plus, FileType2, FileKey2 } from 'lucide-react';
-import { Input } from '@/components/input/input';
-import { Combobox } from '@/components/combobox/combobox';
+import { Plus, FileType2, FileKey2 } from 'lucide-react';
import { Button } from '@/components/button/button';
-import { KeyRound } from 'lucide-react';
import {
Accordion,
AccordionItem,
@@ -12,15 +9,11 @@ import {
} from '@/components/accordion/accordion';
import { Separator } from '@/components/separator/separator';
import { DBTable } from '@/lib/domain/db-table';
-import { DBField, FieldType } from '@/lib/domain/db-field';
+import { DBField } from '@/lib/domain/db-field';
import { useChartDB } from '@/hooks/use-chartdb';
-import { dataTypeMap } from '@/lib/data/data-types';
-import { Toggle } from '@/components/toggle/toggle';
-import {
- Tooltip,
- TooltipContent,
- TooltipTrigger,
-} from '@/components/tooltip/tooltip';
+import { TableField } from './table-field/table-field';
+import { TableIndex } from './table-index/table-index';
+import { DBIndex } from '@/lib/domain/db-index';
export interface TableListItemContentProps {
table: DBTable;
@@ -29,128 +22,16 @@ export interface TableListItemContentProps {
export const TableListItemContent: React.FC<TableListItemContentProps> = ({
table,
}) => {
- const { databaseType, updateField } = useChartDB();
+ const {
+ updateField,
+ removeField,
+ createField,
+ createIndex,
+ removeIndex,
+ updateIndex,
+ } = useChartDB();
const { color } = table;
- const dataFieldOptions = dataTypeMap[databaseType].map((type) => ({
- label: type,
- value: type,
- }));
-
- const RenderField = ({ field }: { field: DBField }) => {
- return (
- <div className="flex flex-row p-1 justify-between flex-1">
- <div className="flex basis-8/12 gap-1">
- <Input
- className="h-8 focus-visible:ring-0 basis-8/12"
- type="text"
- placeholder="Name"
- value={field.name}
- onChange={(e) =>
- updateField(table.id, field.id, {
- name: e.target.value,
- })
- }
- />
- <Combobox
- className="flex h-8 basis-4/12"
- mode="single"
- options={dataFieldOptions}
- placeholder="Type"
- selected={field.type}
- onChange={(value) =>
- updateField(table.id, field.id, {
- type: value as FieldType,
- })
- }
- emptyText="No types found."
- />
- </div>
- <div className="flex gap-1">
- <Tooltip>
- <TooltipTrigger>
- <Toggle
- variant="default"
- className="hover:bg-primary-foreground p-2 w-[32px] text-slate-500 hover:text-slate-700 text-xs h-8"
- pressed={field.nullable}
- onPressedChange={(value) =>
- updateField(table.id, field.id, {
- nullable: value,
- })
- }
- >
- N
- </Toggle>
- </TooltipTrigger>
- <TooltipContent>Nullable?</TooltipContent>
- </Tooltip>
- <Tooltip>
- <TooltipTrigger>
- <Toggle
- variant="default"
- className="hover:bg-primary-foreground p-2 w-[32px] text-slate-500 hover:text-slate-700 h-8"
- pressed={field.primaryKey}
- onPressedChange={(value) =>
- updateField(table.id, field.id, {
- primaryKey: value,
- })
- }
- >
- <KeyRound className="h-3.5" />
- </Toggle>
- </TooltipTrigger>
- <TooltipContent>Primary Key</TooltipContent>
- </Tooltip>
- <Button
- variant="ghost"
- className="hover:bg-primary-foreground p-2 w-[32px] text-slate-500 hover:text-slate-700 h-8"
- >
- <Ellipsis className="h-3.5" />
- </Button>
- </div>
- </div>
- );
- };
-
- const renderIndex = () => {
- return (
- <div className="flex flex-row p-1 justify-between flex-1 gap-2">
- <Combobox
- className="flex h-8 flex-1"
- mode="multiple"
- options={[
- {
- label: 'name',
- value: 'name',
- },
- {
- label: 'address',
- value: 'address',
- },
- ]}
- placeholder="Select fields"
- selected={['name', 'address']}
- onChange={(value) => console.log(value)}
- emptyText="No types found."
- />
- <div className="flex">
- <Button
- variant="ghost"
- className="hover:bg-primary-foreground p-2 w-[32px] text-slate-500 hover:text-slate-700 h-8"
- >
- <Ellipsis className="h-3.5" />
- </Button>
- <Button
- variant="ghost"
- className="hover:bg-primary-foreground p-2 w-8 text-slate-500 hover:text-slate-700 h-8"
- >
- <Trash2 className="h-3.5 w-3.5 text-red-700" />
- </Button>
- </div>
- </div>
- );
- };
-
return (
<div
className="border-l-[6px] rounded-b-md px-1 flex flex-col gap-1"
@@ -178,6 +59,10 @@ export const TableListItemContent: React.FC<TableListItemContentProps> = ({
<Button
variant="ghost"
className="hover:bg-primary-foreground p-0 h-4 w-4 text-slate-500 hover:text-slate-700 text-xs"
+ onClick={(e) => {
+ e.stopPropagation();
+ createField(table.id);
+ }}
>
<Plus className="h-4 w-4 shrink-0 text-muted-foreground transition-transform duration-200" />
</Button>
@@ -187,7 +72,16 @@ export const TableListItemContent: React.FC<TableListItemContentProps> = ({
</AccordionTrigger>
<AccordionContent className="pb-0 pt-1">
{table.fields.map((field) => (
- <RenderField field={field} key={field.id} />
+ <TableField
+ key={field.id}
+ field={field}
+ updateField={(attrs: Partial<DBField>) =>
+ updateField(table.id, field.id, attrs)
+ }
+ removeField={() =>
+ removeField(table.id, field.id)
+ }
+ />
))}
</AccordionContent>
</AccordionItem>
@@ -207,6 +101,10 @@ export const TableListItemContent: React.FC<TableListItemContentProps> = ({
<Button
variant="ghost"
className="hover:bg-primary-foreground p-0 h-4 w-4 text-slate-500 hover:text-slate-700 text-xs"
+ onClick={(e) => {
+ e.stopPropagation();
+ createIndex(table.id);
+ }}
>
<Plus className="h-4 w-4 shrink-0 text-muted-foreground transition-transform duration-200" />
</Button>
@@ -215,21 +113,40 @@ export const TableListItemContent: React.FC<TableListItemContentProps> = ({
</div>
</AccordionTrigger>
<AccordionContent className="pb-0 pt-1">
- {renderIndex()}
- {renderIndex()}
+ {table.indexes.map((index) => (
+ <TableIndex
+ key={index.id}
+ index={index}
+ removeIndex={() =>
+ removeIndex(table.id, index.id)
+ }
+ updateIndex={(attrs: Partial<DBIndex>) =>
+ updateIndex(table.id, index.id, attrs)
+ }
+ fields={table.fields}
+ />
+ ))}
</AccordionContent>
</AccordionItem>
</Accordion>
<Separator className="" />
<div className="flex items-center justify-between flex-1">
<div>
- <Button variant="outline" className="text-xs h-8 p-2">
+ <Button
+ variant="outline"
+ className="text-xs h-8 p-2"
+ onClick={() => createIndex(table.id)}
+ >
<FileKey2 className="h-4" />
Add Index
</Button>
</div>
<div>
- <Button variant="outline" className="text-xs h-8 p-2">
+ <Button
+ variant="outline"
+ className="text-xs h-8 p-2"
+ onClick={() => createField(table.id)}
+ >
<FileType2 className="h-4" />
Add Field
</Button>
diff --git a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-header/table-list-item-header.tsx b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-header/table-list-item-header.tsx
index d3e90ffd..bcc13f92 100644
--- a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-header/table-list-item-header.tsx
+++ b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-header/table-list-item-header.tsx
@@ -32,7 +32,7 @@ export const TableListItemHeader: React.FC<TableListItemHeaderProps> = ({
const [editMode, setEditMode] = React.useState(false);
const [tableName, setTableName] = React.useState(table.name);
const inputRef = React.useRef<HTMLInputElement>(null);
- const { updateTable, removeTable } = useChartDB();
+ const { updateTable, removeTable, createIndex, createField } = useChartDB();
const editTableName = useCallback(() => {
if (!editMode) return;
@@ -68,11 +68,23 @@ export const TableListItemHeader: React.FC<TableListItemHeaderProps> = ({
<DropdownMenuLabel>Table Actions</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuGroup>
- <DropdownMenuItem className="flex justify-between">
+ <DropdownMenuItem
+ className="flex justify-between"
+ onClick={(e) => {
+ e.stopPropagation();
+ createField(table.id);
+ }}
+ >
Add field
<FileType2 className="w-3.5 h-3.5" />
</DropdownMenuItem>
- <DropdownMenuItem className="flex justify-between">
+ <DropdownMenuItem
+ className="flex justify-between"
+ onClick={(e) => {
+ e.stopPropagation();
+ createIndex(table.id);
+ }}
+ >
Add index
<FileKey2 className="w-3.5 h-3.5" />
</DropdownMenuItem>