Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
fix(performance): improve storage provider performance (#734)
Guy Ben-Aharonc6788b49173d
diff --git a/src/context/chartdb-context/chartdb-provider.tsx b/src/context/chartdb-context/chartdb-provider.tsxindex 57dea90e..9967cc03 100644--- a/src/context/chartdb-context/chartdb-provider.tsx+++ b/src/context/chartdb-context/chartdb-provider.tsx@@ -304,22 +304,27 @@ export const ChartDBProvider: React.FC<);const addTables: ChartDBContext['addTables'] = useCallback(- async (tables: DBTable[], options = { updateHistory: true }) => {- setTables((currentTables) => [...currentTables, ...tables]);+ async (tablesToAdd: DBTable[], options = { updateHistory: true }) => {+ setTables((currentTables) => [...currentTables, ...tablesToAdd]);const updatedAt = new Date();setDiagramUpdatedAt(updatedAt);await Promise.all([db.updateDiagram({ id: diagramId, attributes: { updatedAt } }),- ...tables.map((table) => db.addTable({ diagramId, table })),+ ...tablesToAdd.map((table) =>+ db.addTable({ diagramId, table })+ ),]);- events.emit({ action: 'add_tables', data: { tables } });+ events.emit({+ action: 'add_tables',+ data: { tables: tablesToAdd },+ });if (options.updateHistory) {addUndoAction({action: 'addTables',- redoData: { tables },- undoData: { tableIds: tables.map((t) => t.id) },+ redoData: { tables: tablesToAdd },+ undoData: { tableIds: tablesToAdd.map((t) => t.id) },});resetRedoStack();}@@ -778,13 +783,23 @@ export const ChartDBProvider: React.FC<options = { updateHistory: true }) => {const fields = getTable(tableId)?.fields ?? [];- setTables((tables) =>- tables.map((table) =>- table.id === tableId- ? { ...table, fields: [...table.fields, field] }- : table- )- );+ setTables((tables) => {+ return tables.map((table) => {+ if (table.id === tableId) {+ db.updateTable({+ id: tableId,+ attributes: {+ ...table,+ fields: [...table.fields, field],+ },+ });++ return { ...table, fields: [...table.fields, field] };+ }++ return table;+ });+ });events.emit({action: 'add_field',@@ -805,13 +820,6 @@ export const ChartDBProvider: React.FC<setDiagramUpdatedAt(updatedAt);await Promise.all([db.updateDiagram({ id: diagramId, attributes: { updatedAt } }),- db.updateTable({- id: tableId,- attributes: {- ...table,- fields: [...table.fields, field],- },- }),]);if (options.updateHistory) {diff --git a/src/context/storage-context/storage-provider.tsx b/src/context/storage-context/storage-provider.tsxindex 0b6543bf..f72897a0 100644--- a/src/context/storage-context/storage-provider.tsx+++ b/src/context/storage-context/storage-provider.tsx@@ -1,4 +1,4 @@-import React from 'react';+import React, { useCallback, useMemo } from 'react';import type { StorageContext } from './storage-context';import { storageContext } from './storage-context';import Dexie, { type EntityTable } from 'dexie';@@ -14,687 +14,706 @@ import type { DBCustomType } from '@/lib/domain/db-custom-type';export const StorageProvider: React.FC<React.PropsWithChildren> = ({children,}) => {- const db = new Dexie('ChartDB') as Dexie & {- diagrams: EntityTable<- Diagram,- 'id' // primary key "id" (for the typings only)- >;- db_tables: EntityTable<- DBTable & { diagramId: string },- 'id' // primary key "id" (for the typings only)- >;- db_relationships: EntityTable<- DBRelationship & { diagramId: string },- 'id' // primary key "id" (for the typings only)- >;- db_dependencies: EntityTable<- DBDependency & { diagramId: string },- 'id' // primary key "id" (for the typings only)- >;- areas: EntityTable<- Area & { diagramId: string },- 'id' // primary key "id" (for the typings only)- >;- db_custom_types: EntityTable<- DBCustomType & { diagramId: string },- 'id' // primary key "id" (for the typings only)- >;- config: EntityTable<- ChartDBConfig & { id: number },- 'id' // primary key "id" (for the typings only)- >;- };-- // Schema declaration:- db.version(1).stores({- diagrams: '++id, name, databaseType, createdAt, updatedAt',- db_tables:- '++id, diagramId, name, x, y, fields, indexes, color, createdAt, width',- db_relationships:- '++id, diagramId, name, sourceTableId, targetTableId, sourceFieldId, targetFieldId, type, createdAt',- config: '++id, defaultDiagramId',- });-- db.version(2).upgrade((tx) =>- tx- .table<DBTable & { diagramId: string }>('db_tables')- .toCollection()- .modify((table) => {- for (const field of table.fields) {- field.type = {- // @ts-expect-error string before- id: (field.type as string).split(' ').join('_'),- // @ts-expect-error string before- name: field.type,- };- }- })+ const db = useMemo(() => {+ const dexieDB = new Dexie('ChartDB') as Dexie & {+ diagrams: EntityTable<+ Diagram,+ 'id' // primary key "id" (for the typings only)+ >;+ db_tables: EntityTable<+ DBTable & { diagramId: string },+ 'id' // primary key "id" (for the typings only)+ >;+ db_relationships: EntityTable<+ DBRelationship & { diagramId: string },+ 'id' // primary key "id" (for the typings only)+ >;+ db_dependencies: EntityTable<+ DBDependency & { diagramId: string },+ 'id' // primary key "id" (for the typings only)+ >;+ areas: EntityTable<+ Area & { diagramId: string },+ 'id' // primary key "id" (for the typings only)+ >;+ db_custom_types: EntityTable<+ DBCustomType & { diagramId: string },+ 'id' // primary key "id" (for the typings only)+ >;+ config: EntityTable<+ ChartDBConfig & { id: number },+ 'id' // primary key "id" (for the typings only)+ >;+ };++ // Schema declaration:+ dexieDB.version(1).stores({+ diagrams: '++id, name, databaseType, createdAt, updatedAt',+ db_tables:+ '++id, diagramId, name, x, y, fields, indexes, color, createdAt, width',+ db_relationships:+ '++id, diagramId, name, sourceTableId, targetTableId, sourceFieldId, targetFieldId, type, createdAt',+ config: '++id, defaultDiagramId',+ });++ dexieDB.version(2).upgrade((tx) =>+ tx+ .table<DBTable & { diagramId: string }>('db_tables')+ .toCollection()+ .modify((table) => {+ for (const field of table.fields) {+ field.type = {+ // @ts-expect-error string before+ id: (field.type as string).split(' ').join('_'),+ // @ts-expect-error string before+ name: field.type,+ };+ }+ })+ );++ dexieDB.version(3).stores({+ diagrams:+ '++id, name, databaseType, databaseEdition, createdAt, updatedAt',+ db_tables:+ '++id, diagramId, name, x, y, fields, indexes, color, createdAt, width',+ db_relationships:+ '++id, diagramId, name, sourceTableId, targetTableId, sourceFieldId, targetFieldId, type, createdAt',+ config: '++id, defaultDiagramId',+ });++ dexieDB.version(4).stores({+ diagrams:+ '++id, name, databaseType, databaseEdition, createdAt, updatedAt',+ db_tables:+ '++id, diagramId, name, x, y, fields, indexes, color, createdAt, width, comment',+ db_relationships:+ '++id, diagramId, name, sourceTableId, targetTableId, sourceFieldId, targetFieldId, type, createdAt',+ config: '++id, defaultDiagramId',+ });++ dexieDB.version(5).stores({+ diagrams:+ '++id, name, databaseType, databaseEdition, createdAt, updatedAt',+ db_tables:+ '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment',+ db_relationships:+ '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',+ config: '++id, defaultDiagramId',+ });++ dexieDB.version(6).upgrade((tx) =>+ tx+ .table<DBRelationship & { diagramId: string }>(+ 'db_relationships'+ )+ .toCollection()+ .modify((relationship, ref) => {+ const { sourceCardinality, targetCardinality } =+ determineCardinalities(+ // @ts-expect-error string before+ relationship.type ?? 'one_to_one'+ );++ relationship.sourceCardinality = sourceCardinality;+ relationship.targetCardinality = targetCardinality;++ // @ts-expect-error string before+ delete ref.value.type;+ })+ );++ dexieDB.version(7).stores({+ diagrams:+ '++id, name, databaseType, databaseEdition, createdAt, updatedAt',+ db_tables:+ '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment',+ db_relationships:+ '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',+ db_dependencies:+ '++id, diagramId, schema, tableId, dependentSchema, dependentTableId, createdAt',+ config: '++id, defaultDiagramId',+ });++ dexieDB.version(8).stores({+ diagrams:+ '++id, name, databaseType, databaseEdition, createdAt, updatedAt',+ db_tables:+ '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment, isView, isMaterializedView, order',+ db_relationships:+ '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',+ db_dependencies:+ '++id, diagramId, schema, tableId, dependentSchema, dependentTableId, createdAt',+ config: '++id, defaultDiagramId',+ });++ dexieDB.version(9).upgrade((tx) =>+ tx+ .table<DBTable & { diagramId: string }>('db_tables')+ .toCollection()+ .modify((table) => {+ for (const field of table.fields) {+ if (typeof field.nullable === 'string') {+ field.nullable =+ (field.nullable as string).toLowerCase() ===+ 'true';+ }+ }+ })+ );++ dexieDB.version(10).stores({+ diagrams:+ '++id, name, databaseType, databaseEdition, createdAt, updatedAt',+ db_tables:+ '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment, isView, isMaterializedView, order',+ db_relationships:+ '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',+ db_dependencies:+ '++id, diagramId, schema, tableId, dependentSchema, dependentTableId, createdAt',+ areas: '++id, diagramId, name, x, y, width, height, color',+ config: '++id, defaultDiagramId',+ });++ dexieDB.version(11).stores({+ diagrams:+ '++id, name, databaseType, databaseEdition, createdAt, updatedAt',+ db_tables:+ '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment, isView, isMaterializedView, order',+ db_relationships:+ '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',+ db_dependencies:+ '++id, diagramId, schema, tableId, dependentSchema, dependentTableId, createdAt',+ areas: '++id, diagramId, name, x, y, width, height, color',+ db_custom_types:+ '++id, diagramId, schema, type, kind, values, fields',+ config: '++id, defaultDiagramId',+ });++ dexieDB.on('ready', async () => {+ const config = await dexieDB.config.get(1);++ if (!config) {+ const diagrams = await dexieDB.diagrams.toArray();++ await dexieDB.config.add({+ id: 1,+ defaultDiagramId: diagrams?.[0]?.id ?? '',+ });+ }+ });+ return dexieDB;+ }, []);++ const getConfig: StorageContext['getConfig'] =+ useCallback(async (): Promise<ChartDBConfig | undefined> => {+ return await db.config.get(1);+ }, [db]);++ const updateConfig: StorageContext['updateConfig'] = useCallback(+ async (config) => {+ await db.config.update(1, config);+ },+ [db]);- db.version(3).stores({- diagrams:- '++id, name, databaseType, databaseEdition, createdAt, updatedAt',- db_tables:- '++id, diagramId, name, x, y, fields, indexes, color, createdAt, width',- db_relationships:- '++id, diagramId, name, sourceTableId, targetTableId, sourceFieldId, targetFieldId, type, createdAt',- config: '++id, defaultDiagramId',- });-- db.version(4).stores({- diagrams:- '++id, name, databaseType, databaseEdition, createdAt, updatedAt',- db_tables:- '++id, diagramId, name, x, y, fields, indexes, color, createdAt, width, comment',- db_relationships:- '++id, diagramId, name, sourceTableId, targetTableId, sourceFieldId, targetFieldId, type, createdAt',- config: '++id, defaultDiagramId',- });-- db.version(5).stores({- diagrams:- '++id, name, databaseType, databaseEdition, createdAt, updatedAt',- db_tables:- '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment',- db_relationships:- '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',- config: '++id, defaultDiagramId',- });-- db.version(6).upgrade((tx) =>- tx- .table<DBRelationship & { diagramId: string }>('db_relationships')- .toCollection()- .modify((relationship, ref) => {- const {- sourceCardinality,- targetCardinality,- } = // @ts-expect-error string before- determineCardinalities(relationship.type ?? 'one_to_one');-- relationship.sourceCardinality = sourceCardinality;- relationship.targetCardinality = targetCardinality;-- // @ts-expect-error string before- delete ref.value.type;- })+ const addTable: StorageContext['addTable'] = useCallback(+ async ({ diagramId, table }) => {+ await db.db_tables.add({+ ...table,+ diagramId,+ });+ },+ [db]);- db.version(7).stores({- diagrams:- '++id, name, databaseType, databaseEdition, createdAt, updatedAt',- db_tables:- '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment',- db_relationships:- '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',- db_dependencies:- '++id, diagramId, schema, tableId, dependentSchema, dependentTableId, createdAt',- config: '++id, defaultDiagramId',- });-- db.version(8).stores({- diagrams:- '++id, name, databaseType, databaseEdition, createdAt, updatedAt',- db_tables:- '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment, isView, isMaterializedView, order',- db_relationships:- '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',- db_dependencies:- '++id, diagramId, schema, tableId, dependentSchema, dependentTableId, createdAt',- config: '++id, defaultDiagramId',- });-- db.version(9).upgrade((tx) =>- tx- .table<DBTable & { diagramId: string }>('db_tables')- .toCollection()- .modify((table) => {- for (const field of table.fields) {- if (typeof field.nullable === 'string') {- field.nullable =- (field.nullable as string).toLowerCase() === 'true';- }- }- })+ const getTable: StorageContext['getTable'] = useCallback(+ async ({ id, diagramId }): Promise<DBTable | undefined> => {+ return await db.db_tables.get({ id, diagramId });+ },+ [db]+ );++ const deleteDiagramTables: StorageContext['deleteDiagramTables'] =+ useCallback(+ async (diagramId) => {+ await db.db_tables+ .where('diagramId')+ .equals(diagramId)+ .delete();+ },+ [db]+ );++ const updateTable: StorageContext['updateTable'] = useCallback(+ async ({ id, attributes }) => {+ await db.db_tables.update(id, attributes);+ },+ [db]+ );++ const putTable: StorageContext['putTable'] = useCallback(+ async ({ diagramId, table }) => {+ await db.db_tables.put({ ...table, diagramId });+ },+ [db]+ );++ const deleteTable: StorageContext['deleteTable'] = useCallback(+ async ({ id, diagramId }) => {+ await db.db_tables.where({ id, diagramId }).delete();+ },+ [db]);- db.version(10).stores({- diagrams:- '++id, name, databaseType, databaseEdition, createdAt, updatedAt',- db_tables:- '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment, isView, isMaterializedView, order',- db_relationships:- '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',- db_dependencies:- '++id, diagramId, schema, tableId, dependentSchema, dependentTableId, createdAt',- areas: '++id, diagramId, name, x, y, width, height, color',- config: '++id, defaultDiagramId',- });-- db.version(11).stores({- diagrams:- '++id, name, databaseType, databaseEdition, createdAt, updatedAt',- db_tables:- '++id, diagramId, name, schema, x, y, fields, indexes, color, createdAt, width, comment, isView, isMaterializedView, order',- db_relationships:- '++id, diagramId, name, sourceSchema, sourceTableId, targetSchema, targetTableId, sourceFieldId, targetFieldId, type, createdAt',- db_dependencies:- '++id, diagramId, schema, tableId, dependentSchema, dependentTableId, createdAt',- areas: '++id, diagramId, name, x, y, width, height, color',- db_custom_types: '++id, diagramId, schema, type, kind, values, fields',- config: '++id, defaultDiagramId',- });-- db.on('ready', async () => {- const config = await getConfig();-- if (!config) {- const diagrams = await db.diagrams.toArray();-- await db.config.add({- id: 1,- defaultDiagramId: diagrams?.[0]?.id ?? '',+ const listTables: StorageContext['listTables'] = useCallback(+ async (diagramId): Promise<DBTable[]> => {+ // Fetch all tables associated with the diagram+ const tables = await db.db_tables+ .where('diagramId')+ .equals(diagramId)+ .toArray();++ return tables;+ },+ [db]+ );++ const addRelationship: StorageContext['addRelationship'] = useCallback(+ async ({ diagramId, relationship }) => {+ await db.db_relationships.add({+ ...relationship,+ diagramId,});- }- });-- const getConfig: StorageContext['getConfig'] = async (): Promise<- ChartDBConfig | undefined- > => {- return await db.config.get(1);- };-- const updateConfig: StorageContext['updateConfig'] = async (- config: Partial<ChartDBConfig>- ) => {- await db.config.update(1, config);- };-- const addDiagram: StorageContext['addDiagram'] = async ({- diagram,- }: {- diagram: Diagram;- }) => {- const promises = [];- promises.push(- db.diagrams.add({- id: diagram.id,- name: diagram.name,- databaseType: diagram.databaseType,- databaseEdition: diagram.databaseEdition,- createdAt: diagram.createdAt,- updatedAt: diagram.updatedAt,- })+ },+ [db]+ );++ const deleteDiagramRelationships: StorageContext['deleteDiagramRelationships'] =+ useCallback(+ async (diagramId) => {+ await db.db_relationships+ .where('diagramId')+ .equals(diagramId)+ .delete();+ },+ [db]);- const tables = diagram.tables ?? [];- promises.push(- ...tables.map((table) => addTable({ diagramId: diagram.id, table }))+ const getRelationship: StorageContext['getRelationship'] = useCallback(+ async ({ id, diagramId }): Promise<DBRelationship | undefined> => {+ return await db.db_relationships.get({ id, diagramId });+ },+ [db]+ );++ const updateRelationship: StorageContext['updateRelationship'] =+ useCallback(+ async ({ id, attributes }) => {+ await db.db_relationships.update(id, attributes);+ },+ [db]);- const relationships = diagram.relationships ?? [];- promises.push(- ...relationships.map((relationship) =>- addRelationship({ diagramId: diagram.id, relationship })- )+ const deleteRelationship: StorageContext['deleteRelationship'] =+ useCallback(+ async ({ id, diagramId }) => {+ await db.db_relationships.where({ id, diagramId }).delete();+ },+ [db]);- const dependencies = diagram.dependencies ?? [];- promises.push(- ...dependencies.map((dependency) =>- addDependency({ diagramId: diagram.id, dependency })- )+ const listRelationships: StorageContext['listRelationships'] = useCallback(+ async (diagramId): Promise<DBRelationship[]> => {+ // Sort relationships alphabetically+ return (+ await db.db_relationships+ .where('diagramId')+ .equals(diagramId)+ .toArray()+ ).sort((a, b) => {+ return a.name.localeCompare(b.name);+ });+ },+ [db]+ );++ const addDependency: StorageContext['addDependency'] = useCallback(+ async ({ diagramId, dependency }) => {+ await db.db_dependencies.add({+ ...dependency,+ diagramId,+ });+ },+ [db]+ );++ const getDependency: StorageContext['getDependency'] = useCallback(+ async ({ diagramId, id }) => {+ return await db.db_dependencies.get({ id, diagramId });+ },+ [db]+ );++ const updateDependency: StorageContext['updateDependency'] = useCallback(+ async ({ id, attributes }) => {+ await db.db_dependencies.update(id, attributes);+ },+ [db]+ );++ const deleteDependency: StorageContext['deleteDependency'] = useCallback(+ async ({ diagramId, id }) => {+ await db.db_dependencies.where({ id, diagramId }).delete();+ },+ [db]+ );++ const listDependencies: StorageContext['listDependencies'] = useCallback(+ async (diagramId) => {+ return await db.db_dependencies+ .where('diagramId')+ .equals(diagramId)+ .toArray();+ },+ [db]+ );++ const deleteDiagramDependencies: StorageContext['deleteDiagramDependencies'] =+ useCallback(+ async (diagramId) => {+ await db.db_dependencies+ .where('diagramId')+ .equals(diagramId)+ .delete();+ },+ [db]);- const areas = diagram.areas ?? [];- promises.push(- ...areas.map((area) => addArea({ diagramId: diagram.id, area }))+ const addArea: StorageContext['addArea'] = useCallback(+ async ({ area, diagramId }) => {+ await db.areas.add({+ ...area,+ diagramId,+ });+ },+ [db]+ );++ const getArea: StorageContext['getArea'] = useCallback(+ async ({ diagramId, id }) => {+ return await db.areas.get({ id, diagramId });+ },+ [db]+ );++ const updateArea: StorageContext['updateArea'] = useCallback(+ async ({ id, attributes }) => {+ await db.areas.update(id, attributes);+ },+ [db]+ );++ const deleteArea: StorageContext['deleteArea'] = useCallback(+ async ({ diagramId, id }) => {+ await db.areas.where({ id, diagramId }).delete();+ },+ [db]+ );++ const listAreas: StorageContext['listAreas'] = useCallback(+ async (diagramId) => {+ return await db.areas+ .where('diagramId')+ .equals(diagramId)+ .toArray();+ },+ [db]+ );++ const deleteDiagramAreas: StorageContext['deleteDiagramAreas'] =+ useCallback(+ async (diagramId) => {+ await db.areas.where('diagramId').equals(diagramId).delete();+ },+ [db]);- const customTypes = diagram.customTypes ?? [];- promises.push(- ...customTypes.map((customType) =>- addCustomType({ diagramId: diagram.id, customType })- )+ // Custom type operations+ const addCustomType: StorageContext['addCustomType'] = useCallback(+ async ({ diagramId, customType }) => {+ await db.db_custom_types.add({+ ...customType,+ diagramId,+ });+ },+ [db]+ );++ const getCustomType: StorageContext['getCustomType'] = useCallback(+ async ({ diagramId, id }): Promise<DBCustomType | undefined> => {+ return await db.db_custom_types.get({ id, diagramId });+ },+ [db]+ );++ const updateCustomType: StorageContext['updateCustomType'] = useCallback(+ async ({ id, attributes }) => {+ await db.db_custom_types.update(id, attributes);+ },+ [db]+ );++ const deleteCustomType: StorageContext['deleteCustomType'] = useCallback(+ async ({ diagramId, id }) => {+ await db.db_custom_types.where({ id, diagramId }).delete();+ },+ [db]+ );++ const listCustomTypes: StorageContext['listCustomTypes'] = useCallback(+ async (diagramId): Promise<DBCustomType[]> => {+ return (+ await db.db_custom_types+ .where('diagramId')+ .equals(diagramId)+ .toArray()+ ).sort((a, b) => {+ return a.name.localeCompare(b.name);+ });+ },+ [db]+ );++ const deleteDiagramCustomTypes: StorageContext['deleteDiagramCustomTypes'] =+ useCallback(+ async (diagramId) => {+ await db.db_custom_types+ .where('diagramId')+ .equals(diagramId)+ .delete();+ },+ [db]);- await Promise.all(promises);- };-- const listDiagrams: StorageContext['listDiagrams'] = async (- options: {- includeTables?: boolean;- includeRelationships?: boolean;- includeDependencies?: boolean;- includeAreas?: boolean;- includeCustomTypes?: boolean;- } = {- includeRelationships: false,- includeTables: false,- includeDependencies: false,- includeAreas: false,- includeCustomTypes: false,- }- ): Promise<Diagram[]> => {- let diagrams = await db.diagrams.toArray();-- if (options.includeTables) {- diagrams = await Promise.all(- diagrams.map(async (diagram) => {- diagram.tables = await listTables(diagram.id);- return diagram;+ const addDiagram: StorageContext['addDiagram'] = useCallback(+ async ({ diagram }) => {+ const promises = [];+ promises.push(+ db.diagrams.add({+ id: diagram.id,+ name: diagram.name,+ databaseType: diagram.databaseType,+ databaseEdition: diagram.databaseEdition,+ createdAt: diagram.createdAt,+ updatedAt: diagram.updatedAt,}));- }- if (options.includeRelationships) {- diagrams = await Promise.all(- diagrams.map(async (diagram) => {- diagram.relationships = await listRelationships(diagram.id);- return diagram;- })+ const tables = diagram.tables ?? [];+ promises.push(+ ...tables.map((table) =>+ addTable({ diagramId: diagram.id, table })+ ));- }- if (options.includeDependencies) {- diagrams = await Promise.all(- diagrams.map(async (diagram) => {- diagram.dependencies = await listDependencies(diagram.id);- return diagram;- })+ const relationships = diagram.relationships ?? [];+ promises.push(+ ...relationships.map((relationship) =>+ addRelationship({ diagramId: diagram.id, relationship })+ ));- }- if (options.includeAreas) {- diagrams = await Promise.all(- diagrams.map(async (diagram) => {- diagram.areas = await listAreas(diagram.id);- return diagram;- })+ const dependencies = diagram.dependencies ?? [];+ promises.push(+ ...dependencies.map((dependency) =>+ addDependency({ diagramId: diagram.id, dependency })+ ));- }- if (options.includeCustomTypes) {- diagrams = await Promise.all(- diagrams.map(async (diagram) => {- diagram.customTypes = await listCustomTypes(diagram.id);- return diagram;- })+ const areas = diagram.areas ?? [];+ promises.push(+ ...areas.map((area) => addArea({ diagramId: diagram.id, area })));- }-- return diagrams;- };-- const getDiagram: StorageContext['getDiagram'] = async (- id: string,- options: {- includeTables?: boolean;- includeRelationships?: boolean;- includeDependencies?: boolean;- includeAreas?: boolean;- includeCustomTypes?: boolean;- } = {- includeRelationships: false,- includeTables: false,- includeDependencies: false,- includeAreas: false,- includeCustomTypes: 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);- }-- if (options.includeDependencies) {- diagram.dependencies = await listDependencies(id);- }-- if (options.includeAreas) {- diagram.areas = await listAreas(id);- }-- if (options.includeCustomTypes) {- diagram.customTypes = await listCustomTypes(id);- }-- return diagram;- };-- const updateDiagram: StorageContext['updateDiagram'] = async ({- id,- attributes,- }: {- id: string;- attributes: Partial<Diagram>;- }) => {- await db.diagrams.update(id, attributes);-- if (attributes.id) {- await Promise.all([- db.db_tables- .where('diagramId')- .equals(id)- .modify({ diagramId: attributes.id }),- db.db_relationships- .where('diagramId')- .equals(id)- .modify({ diagramId: attributes.id }),- db.db_dependencies- .where('diagramId')- .equals(id)- .modify({ diagramId: attributes.id }),- db.areas.where('diagramId').equals(id).modify({- diagramId: attributes.id,- }),- db.db_custom_types- .where('diagramId')- .equals(id)- .modify({ diagramId: attributes.id }),- ]);- }- };-- const deleteDiagram: StorageContext['deleteDiagram'] = async (- id: string- ) => {- await Promise.all([- db.diagrams.delete(id),- db.db_tables.where('diagramId').equals(id).delete(),- db.db_relationships.where('diagramId').equals(id).delete(),- db.db_dependencies.where('diagramId').equals(id).delete(),- db.areas.where('diagramId').equals(id).delete(),- db.db_custom_types.where('diagramId').equals(id).delete(),- ]);- };-- const addTable: StorageContext['addTable'] = async ({- diagramId,- table,- }: {- diagramId: string;- table: DBTable;- }) => {- await db.db_tables.add({- ...table,- diagramId,- });- };-- const getTable: StorageContext['getTable'] = async ({- id,- diagramId,- }: {- diagramId: string;- id: string;- }): Promise<DBTable | undefined> => {- return await db.db_tables.get({ id, diagramId });- };-- const deleteDiagramTables: StorageContext['deleteDiagramTables'] = async (- diagramId: string- ) => {- await db.db_tables.where('diagramId').equals(diagramId).delete();- };-- const updateTable: StorageContext['updateTable'] = async ({- id,- attributes,- }) => {- await db.db_tables.update(id, attributes);- };-- const putTable: StorageContext['putTable'] = async ({- diagramId,- table,- }) => {- await db.db_tables.put({ ...table, diagramId });- };-- const deleteTable: StorageContext['deleteTable'] = async ({- id,- diagramId,- }: {- id: string;- diagramId: string;- }) => {- await db.db_tables.where({ id, diagramId }).delete();- };-- const listTables: StorageContext['listTables'] = async (- diagramId: string- ): Promise<DBTable[]> => {- // Fetch all tables associated with the diagram- const tables = await db.db_tables- .where('diagramId')- .equals(diagramId)- .toArray();-- return tables;- };-- const addRelationship: StorageContext['addRelationship'] = async ({- diagramId,- relationship,- }: {- diagramId: string;- relationship: DBRelationship;- }) => {- await db.db_relationships.add({- ...relationship,- diagramId,- });- };- const deleteDiagramRelationships: StorageContext['deleteDiagramRelationships'] =- async (diagramId: string) => {- await db.db_relationships- .where('diagramId')- .equals(diagramId)- .delete();- };+ const customTypes = diagram.customTypes ?? [];+ promises.push(+ ...customTypes.map((customType) =>+ addCustomType({ diagramId: diagram.id, customType })+ )+ );- const getRelationship: StorageContext['getRelationship'] = async ({- id,- diagramId,- }: {- diagramId: string;- id: string;- }): Promise<DBRelationship | undefined> => {- return await db.db_relationships.get({ id, diagramId });- };-- const updateRelationship: StorageContext['updateRelationship'] = async ({- id,- attributes,- }: {- id: string;- attributes: Partial<DBRelationship>;- }) => {- await db.db_relationships.update(id, attributes);- };-- const deleteRelationship: StorageContext['deleteRelationship'] = async ({- id,- diagramId,- }: {- id: string;- diagramId: string;- }) => {- await db.db_relationships.where({ id, diagramId }).delete();- };-- const listRelationships: StorageContext['listRelationships'] = async (- diagramId: string- ): Promise<DBRelationship[]> => {- // Sort relationships alphabetically- return (- await db.db_relationships- .where('diagramId')- .equals(diagramId)- .toArray()- ).sort((a, b) => {- return a.name.localeCompare(b.name);- });- };-- const addDependency: StorageContext['addDependency'] = async ({- diagramId,- dependency,- }) => {- await db.db_dependencies.add({- ...dependency,- diagramId,- });- };-- const getDependency: StorageContext['getDependency'] = async ({- diagramId,- id,- }) => {- return await db.db_dependencies.get({ id, diagramId });- };-- const updateDependency: StorageContext['updateDependency'] = async ({- id,- attributes,- }) => {- await db.db_dependencies.update(id, attributes);- };-- const deleteDependency: StorageContext['deleteDependency'] = async ({- diagramId,- id,- }) => {- await db.db_dependencies.where({ id, diagramId }).delete();- };-- const listDependencies: StorageContext['listDependencies'] = async (- diagramId- ) => {- return await db.db_dependencies- .where('diagramId')- .equals(diagramId)- .toArray();- };+ await Promise.all(promises);+ },+ [db, addArea, addCustomType, addDependency, addRelationship, addTable]+ );- const deleteDiagramDependencies: StorageContext['deleteDiagramDependencies'] =- async (diagramId) => {- await db.db_dependencies- .where('diagramId')- .equals(diagramId)- .delete();- };+ const listDiagrams: StorageContext['listDiagrams'] = useCallback(+ async (+ options = {+ includeRelationships: false,+ includeTables: false,+ includeDependencies: false,+ includeAreas: false,+ includeCustomTypes: false,+ }+ ): Promise<Diagram[]> => {+ let diagrams = await db.diagrams.toArray();++ if (options.includeTables) {+ diagrams = await Promise.all(+ diagrams.map(async (diagram) => {+ diagram.tables = await listTables(diagram.id);+ return diagram;+ })+ );+ }++ if (options.includeRelationships) {+ diagrams = await Promise.all(+ diagrams.map(async (diagram) => {+ diagram.relationships = await listRelationships(+ diagram.id+ );+ return diagram;+ })+ );+ }++ if (options.includeDependencies) {+ diagrams = await Promise.all(+ diagrams.map(async (diagram) => {+ diagram.dependencies = await listDependencies(+ diagram.id+ );+ return diagram;+ })+ );+ }++ if (options.includeAreas) {+ diagrams = await Promise.all(+ diagrams.map(async (diagram) => {+ diagram.areas = await listAreas(diagram.id);+ return diagram;+ })+ );+ }++ if (options.includeCustomTypes) {+ diagrams = await Promise.all(+ diagrams.map(async (diagram) => {+ diagram.customTypes = await listCustomTypes(diagram.id);+ return diagram;+ })+ );+ }++ return diagrams;+ },+ [+ db,+ listAreas,+ listCustomTypes,+ listDependencies,+ listRelationships,+ listTables,+ ]+ );- const addArea: StorageContext['addArea'] = async ({ area, diagramId }) => {- await db.areas.add({- ...area,- diagramId,- });- };-- const getArea: StorageContext['getArea'] = async ({ diagramId, id }) => {- return await db.areas.get({ id, diagramId });- };-- const updateArea: StorageContext['updateArea'] = async ({- id,- attributes,- }) => {- await db.areas.update(id, attributes);- };-- const deleteArea: StorageContext['deleteArea'] = async ({- diagramId,- id,- }) => {- await db.areas.where({ id, diagramId }).delete();- };-- const listAreas: StorageContext['listAreas'] = async (diagramId) => {- return await db.areas.where('diagramId').equals(diagramId).toArray();- };-- const deleteDiagramAreas: StorageContext['deleteDiagramAreas'] = async (- diagramId- ) => {- await db.areas.where('diagramId').equals(diagramId).delete();- };+ const getDiagram: StorageContext['getDiagram'] = useCallback(+ async (+ id,+ options = {+ includeRelationships: false,+ includeTables: false,+ includeDependencies: false,+ includeAreas: false,+ includeCustomTypes: 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);+ }++ if (options.includeDependencies) {+ diagram.dependencies = await listDependencies(id);+ }++ if (options.includeAreas) {+ diagram.areas = await listAreas(id);+ }++ if (options.includeCustomTypes) {+ diagram.customTypes = await listCustomTypes(id);+ }++ return diagram;+ },+ [+ db,+ listAreas,+ listCustomTypes,+ listDependencies,+ listRelationships,+ listTables,+ ]+ );- // Custom type operations- const addCustomType: StorageContext['addCustomType'] = async ({- diagramId,- customType,- }: {- diagramId: string;- customType: DBCustomType;- }) => {- await db.db_custom_types.add({- ...customType,- diagramId,- });- };-- const getCustomType: StorageContext['getCustomType'] = async ({- diagramId,- id,- }: {- diagramId: string;- id: string;- }): Promise<DBCustomType | undefined> => {- return await db.db_custom_types.get({ id, diagramId });- };-- const updateCustomType: StorageContext['updateCustomType'] = async ({- id,- attributes,- }: {- id: string;- attributes: Partial<DBCustomType>;- }) => {- await db.db_custom_types.update(id, attributes);- };-- const deleteCustomType: StorageContext['deleteCustomType'] = async ({- diagramId,- id,- }: {- id: string;- diagramId: string;- }) => {- await db.db_custom_types.where({ id, diagramId }).delete();- };-- const listCustomTypes: StorageContext['listCustomTypes'] = async (- diagramId: string- ): Promise<DBCustomType[]> => {- return (- await db.db_custom_types- .where('diagramId')- .equals(diagramId)- .toArray()- ).sort((a, b) => {- return a.name.localeCompare(b.name);- });- };+ const updateDiagram: StorageContext['updateDiagram'] = useCallback(+ async ({ id, attributes }) => {+ await db.diagrams.update(id, attributes);++ if (attributes.id) {+ await Promise.all([+ db.db_tables+ .where('diagramId')+ .equals(id)+ .modify({ diagramId: attributes.id }),+ db.db_relationships+ .where('diagramId')+ .equals(id)+ .modify({ diagramId: attributes.id }),+ db.db_dependencies+ .where('diagramId')+ .equals(id)+ .modify({ diagramId: attributes.id }),+ db.areas.where('diagramId').equals(id).modify({+ diagramId: attributes.id,+ }),+ db.db_custom_types+ .where('diagramId')+ .equals(id)+ .modify({ diagramId: attributes.id }),+ ]);+ }+ },+ [db]+ );- const deleteDiagramCustomTypes: StorageContext['deleteDiagramCustomTypes'] =- async (diagramId: string) => {- await db.db_custom_types- .where('diagramId')- .equals(diagramId)- .delete();- };+ const deleteDiagram: StorageContext['deleteDiagram'] = useCallback(+ async (id) => {+ await Promise.all([+ db.diagrams.delete(id),+ db.db_tables.where('diagramId').equals(id).delete(),+ db.db_relationships.where('diagramId').equals(id).delete(),+ db.db_dependencies.where('diagramId').equals(id).delete(),+ db.areas.where('diagramId').equals(id).delete(),+ db.db_custom_types.where('diagramId').equals(id).delete(),+ ]);+ },+ [db]+ );return (<storageContext.Provider