Skip to content

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

load chart

Guy Ben-Aharon9b1fea30dbfa
diff --git a/src/context/chartdb-context/chartdb-context.tsx b/src/context/chartdb-context/chartdb-context.tsx
index e8e9a2ce..5d66b994 100644
--- a/src/context/chartdb-context/chartdb-context.tsx
+++ b/src/context/chartdb-context/chartdb-context.tsx
@@ -17,7 +17,7 @@ export interface ChartDBContext {
// General operations
updateDiagramId: (id: string) => void;
updateDiagramName: (name: string) => void;
- loadDiagram: (diagramId: string) => Promise<Diagram | null>;
+ loadDiagram: (diagramId: string) => Promise<Diagram | undefined>;
// Database type operations
updateDatabaseType: (databaseType: DatabaseType) => void;
diff --git a/src/context/chartdb-context/chartdb-provider.tsx b/src/context/chartdb-context/chartdb-provider.tsx
index eebd45eb..073c97f5 100644
--- a/src/context/chartdb-context/chartdb-provider.tsx
+++ b/src/context/chartdb-context/chartdb-provider.tsx
@@ -6,12 +6,13 @@ import { DatabaseType } from '@/lib/domain/database-type';
import { DBField } from '@/lib/domain/db-field';
import { DBIndex } from '@/lib/domain/db-index';
import { DBRelationship } from '@/lib/domain/db-relationship';
-import { Diagram } from '@/lib/domain/diagram';
+import { useData } from '@/hooks/use-data';
export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
children,
}) => {
const [diagramId, setDiagramId] = React.useState('');
+ const { getDiagram } = useData();
const [diagramName, setDiagramName] = React.useState('New Diagram');
const [databaseType, setDatabaseType] = React.useState<DatabaseType>(
DatabaseType.GENERIC
@@ -286,10 +287,21 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
);
};
- const loadDiagram = (diagramId: string) => {
- // TODO: Implement loading diagram
- console.log('Loading diagram', diagramId);
- return Promise.resolve({} as Diagram);
+ const loadDiagram = async (diagramId: string) => {
+ const diagram = await getDiagram(diagramId, {
+ includeRelationships: true,
+ includeTables: true,
+ });
+
+ if (diagram) {
+ setDiagramId(diagram.id);
+ setDiagramName(diagram.name);
+ setDatabaseType(diagram.databaseType);
+ setTables(diagram.tables);
+ setRelationships(diagram.relationships);
+ }
+
+ return diagram;
};
return (
diff --git a/src/context/data-context/data-context.tsx b/src/context/data-context/data-context.tsx
index ca7a60c6..df0a12af 100644
--- a/src/context/data-context/data-context.tsx
+++ b/src/context/data-context/data-context.tsx
@@ -13,7 +13,13 @@ export interface DataContext {
// Diagram operations
addDiagram: (params: { diagram: Diagram }) => Promise<void>;
listDiagrams: () => Promise<Diagram[]>;
- getDiagram: (id: string) => Promise<Diagram | undefined>;
+ getDiagram: (
+ id: string,
+ options?: {
+ includeTables?: boolean;
+ includeRelationships?: boolean;
+ }
+ ) => Promise<Diagram | undefined>;
updateDiagram: (params: {
id: string;
diagram: Partial<Diagram>;
diff --git a/src/context/data-context/data-provider.tsx b/src/context/data-context/data-provider.tsx
index f9d70793..3e7314b8 100644
--- a/src/context/data-context/data-provider.tsx
+++ b/src/context/data-context/data-provider.tsx
@@ -65,8 +65,28 @@ export const DataProvider: React.FC<React.PropsWithChildren> = ({
return await db.diagrams.toArray();
};
- const getDiagram = async (id: string): Promise<Diagram | undefined> => {
- return await db.diagrams.get(id);
+ const getDiagram = async (
+ id: string,
+ options: {
+ includeTables?: boolean;
+ includeRelationships?: boolean;
+ } = { includeRelationships: false, includeTables: false }
+ ): Promise<Diagram | undefined> => {
+ const diagram = await db.diagrams.get(id);
+
+ if (!diagram) {
+ return undefined;
+ }
+
+ if (options.includeTables) {
+ diagram.tables = await listTables(id);
+ }
+
+ if (options.includeRelationships) {
+ diagram.relationships = await listRelationships(id);
+ }
+
+ return diagram;
};
const updateDiagram = async ({
diff --git a/src/lib/data/data-types.ts b/src/lib/data/data-types.ts
index 7f3f61d4..65b19657 100644
--- a/src/lib/data/data-types.ts
+++ b/src/lib/data/data-types.ts
@@ -1,11 +1,307 @@
import { DatabaseType } from '../domain/database-type';
-export const genericDataTypes = ['bigint', 'bfile', 'clob', 'cursor'] as const;
-export const postgresDataTypes = ['bigint', 'bigserial'] as const;
-export const mysqlDataTypes = ['bigint', 'bigserial'] as const;
+export const genericDataTypes = [
+ 'bigint',
+ 'binary',
+ 'blob',
+ 'boolean',
+ 'char',
+ 'date',
+ 'datetime',
+ 'decimal',
+ 'double',
+ 'enum',
+ 'float',
+ 'int',
+ 'json',
+ 'numeric',
+ 'real',
+ 'set',
+ 'smallint',
+ 'text',
+ 'time',
+ 'timestamp',
+ 'uuid',
+ 'varbinary',
+ 'varchar',
+] as const;
+export const postgresDataTypes = [
+ // Numeric Types
+ 'smallint',
+ 'integer',
+ 'bigint',
+ 'decimal',
+ 'numeric',
+ 'real',
+ 'double precision',
+ 'smallserial',
+ 'serial',
+ 'bigserial',
+ 'money',
+
+ // Character Types
+ 'char',
+ 'varchar',
+ 'text',
+
+ // Binary Data Types
+ 'bytea',
+
+ // Date/Time Types
+ 'date',
+ 'timestamp',
+ 'timestamp with time zone',
+ 'timestamp without time zone',
+ 'time',
+ 'time with time zone',
+ 'time without time zone',
+ 'interval',
+
+ // Boolean Type
+ 'boolean',
+
+ // Enumerated Types
+ 'enum',
+
+ // Geometric Types
+ 'point',
+ 'line',
+ 'lseg',
+ 'box',
+ 'path',
+ 'polygon',
+ 'circle',
+
+ // Network Address Types
+ 'cidr',
+ 'inet',
+ 'macaddr',
+ 'macaddr8',
+
+ // Bit String Types
+ 'bit',
+ 'bit varying',
+
+ // Text Search Types
+ 'tsvector',
+ 'tsquery',
+
+ // UUID Type
+ 'uuid',
+
+ // XML Type
+ 'xml',
+
+ // JSON Types
+ 'json',
+ 'jsonb',
+
+ // Array Types
+ 'array',
+
+ // Range Types
+ 'int4range',
+ 'int8range',
+ 'numrange',
+ 'tsrange',
+ 'tstzrange',
+ 'daterange',
+
+ // Object Identifier Types
+ 'oid',
+ 'regproc',
+ 'regprocedure',
+ 'regoper',
+ 'regoperator',
+ 'regclass',
+ 'regtype',
+ 'regrole',
+ 'regnamespace',
+ 'regconfig',
+ 'regdictionary',
+] as const;
+export const mysqlDataTypes = [
+ // Numeric Types
+ 'tinyint',
+ 'smallint',
+ 'mediumint',
+ 'int',
+ 'bigint',
+ 'decimal',
+ 'numeric',
+ 'float',
+ 'double',
+ 'bit',
+ 'bool',
+ 'boolean',
+
+ // Date and Time Types
+ 'date',
+ 'datetime',
+ 'timestamp',
+ 'time',
+ 'year',
+
+ // String Types
+ 'char',
+ 'varchar',
+ 'binary',
+ 'varbinary',
+ 'tinyblob',
+ 'blob',
+ 'mediumblob',
+ 'longblob',
+ 'tinytext',
+ 'text',
+ 'mediumtext',
+ 'longtext',
+ 'enum',
+ 'set',
+
+ // Spatial Types
+ 'geometry',
+ 'point',
+ 'linestring',
+ 'polygon',
+ 'multipoint',
+ 'multilinestring',
+ 'multipolygon',
+ 'geometrycollection',
+
+ // JSON Type
+ 'json',
+] as const;
+export const sqlServerDataTypes = [
+ // Exact Numerics
+ 'bigint',
+ 'bit',
+ 'decimal',
+ 'int',
+ 'money',
+ 'numeric',
+ 'smallint',
+ 'smallmoney',
+ 'tinyint',
+
+ // Approximate Numerics
+ 'float',
+ 'real',
+
+ // Date and Time
+ 'date',
+ 'datetime2',
+ 'datetime',
+ 'datetimeoffset',
+ 'smalldatetime',
+ 'time',
+
+ // Character Strings
+ 'char',
+ 'varchar',
+ 'text',
+
+ // Unicode Character Strings
+ 'nchar',
+ 'nvarchar',
+ 'ntext',
+
+ // Binary Strings
+ 'binary',
+ 'varbinary',
+ 'image',
+
+ // Other Data Types
+ 'cursor',
+ 'hierarchyid',
+ 'sql_variant',
+ 'timestamp',
+ 'uniqueidentifier',
+ 'xml',
+
+ // Spatial Data Types
+ 'geometry',
+ 'geography',
+
+ // JSON
+ 'json', // Note: Currently, the JSON data type is available in Azure SQL Database.
+] as const;
+export const mariadbDataTypes = [
+ // Numeric Types
+ 'tinyint',
+ 'smallint',
+ 'mediumint',
+ 'int',
+ 'bigint',
+ 'decimal',
+ 'numeric',
+ 'float',
+ 'double',
+ 'bit',
+ 'bool',
+ 'boolean',
+
+ // Date and Time Types
+ 'date',
+ 'datetime',
+ 'timestamp',
+ 'time',
+ 'year',
+
+ // String Types
+ 'char',
+ 'varchar',
+ 'binary',
+ 'varbinary',
+ 'tinyblob',
+ 'blob',
+ 'mediumblob',
+ 'longblob',
+ 'tinytext',
+ 'text',
+ 'mediumtext',
+ 'longtext',
+ 'enum',
+ 'set',
+
+ // Spatial Types
+ 'geometry',
+ 'point',
+ 'linestring',
+ 'polygon',
+ 'multipoint',
+ 'multilinestring',
+ 'multipolygon',
+ 'geometrycollection',
+
+ // JSON Type
+ 'json',
+] as const;
+export const sqliteDataTypes = [
+ // Numeric Types
+ 'integer',
+ 'real',
+ 'numeric',
+
+ // Text Type
+ 'text',
+
+ // Blob Type
+ 'blob',
+
+ // Date/Time Types (SQLite uses TEXT, REAL, or INTEGER types for dates and times)
+ 'date',
+ 'datetime',
+] as const;
export const dataTypes = [
- ...new Set([...postgresDataTypes, ...mysqlDataTypes, ...genericDataTypes]),
+ ...new Set([
+ ...postgresDataTypes,
+ ...mysqlDataTypes,
+ ...sqlServerDataTypes,
+ ...mariadbDataTypes,
+ ...sqliteDataTypes,
+ ...genericDataTypes,
+ ]),
] as const;
export const dataTypeMap: Record<
@@ -15,7 +311,7 @@ export const dataTypeMap: Record<
[DatabaseType.GENERIC]: genericDataTypes,
[DatabaseType.POSTGRESQL]: postgresDataTypes,
[DatabaseType.MYSQL]: mysqlDataTypes,
- [DatabaseType.SQL_SERVER]: [],
- [DatabaseType.MARIADB]: [],
- [DatabaseType.SQLITE]: [],
+ [DatabaseType.SQL_SERVER]: sqlServerDataTypes,
+ [DatabaseType.MARIADB]: mariadbDataTypes,
+ [DatabaseType.SQLITE]: sqliteDataTypes,
} as const;
diff --git a/src/pages/editor-page/editor-page.tsx b/src/pages/editor-page/editor-page.tsx
index f4b0fe20..cf6db026 100644
--- a/src/pages/editor-page/editor-page.tsx
+++ b/src/pages/editor-page/editor-page.tsx
@@ -11,8 +11,10 @@ import { Canvas } from './canvas/canvas';
import { useNavigate, useParams } from 'react-router-dom';
import { useCreateDiagramDialog } from '@/hooks/use-create-diagram-dialog';
import { useConfig } from '@/hooks/use-config';
+import { useChartDB } from '@/hooks/use-chartdb';
export const EditorPage: React.FC = () => {
+ const { loadDiagram } = useChartDB();
const { openCreateDiagramDialog } = useCreateDiagramDialog();
const { diagramId } = useParams<{ diagramId: string }>();
const { config } = useConfig();
@@ -23,14 +25,19 @@ export const EditorPage: React.FC = () => {
return;
}
- if (diagramId) {
- // load diagram
- console.log('load diagram to memory');
- } else if (!diagramId && config.defaultDiagramId) {
- navigate(`/diagrams/${config.defaultDiagramId}`);
- } else {
- openCreateDiagramDialog();
- }
+ const loadDefaultDiagram = async () => {
+ if (diagramId) {
+ const diagram = await loadDiagram(diagramId);
+ if (!diagram) {
+ navigate('/');
+ }
+ } else if (!diagramId && config.defaultDiagramId) {
+ navigate(`/diagrams/${config.defaultDiagramId}`);
+ } else {
+ openCreateDiagramDialog();
+ }
+ };
+ loadDefaultDiagram();
}, [diagramId, openCreateDiagramDialog, config, navigate]);
return (
diff --git a/src/pages/editor-page/top-navbar/top-navbar.tsx b/src/pages/editor-page/top-navbar/top-navbar.tsx
index ae569245..6ccaedf7 100644
--- a/src/pages/editor-page/top-navbar/top-navbar.tsx
+++ b/src/pages/editor-page/top-navbar/top-navbar.tsx
@@ -17,6 +17,7 @@ import { Check, Pencil } from 'lucide-react';
import { Input } from '@/components/input/input';
import { useChartDB } from '@/hooks/use-chartdb';
import { useClickAway, useKeyPressEvent } from 'react-use';
+import ChartDBLogo from '@/assets/logo.png';
export interface TopNavbarProps {}
@@ -46,9 +47,11 @@ export const TopNavbar: React.FC<TopNavbarProps> = () => {
setEditMode(true);
};
return (
- <nav className="flex flex-row items-center justify-between px-4">
+ <nav className="flex flex-row items-center justify-between px-4 h-12">
<div className="flex flex-1 justify-start gap-x-3">
- <div className="flex font-primary items-center">chartDB</div>
+ <div className="flex font-primary items-center">
+ <img src={ChartDBLogo} alt="chartDB" className="h-4" />
+ </div>
<div>
<Menubar className="border-none shadow-none">
<MenubarMenu>