Skip to content

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

fix delete tables & relationships issue with redo&undo

Guy Ben-Aharon3465656945e3
diff --git a/src/context/chartdb-context/chartdb-provider.tsx b/src/context/chartdb-context/chartdb-provider.tsx
index 4b06c693..8bd4c4d7 100644
--- a/src/context/chartdb-context/chartdb-provider.tsx
+++ b/src/context/chartdb-context/chartdb-provider.tsx
@@ -305,12 +305,27 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
const removeTable: ChartDBContext['removeTable'] = useCallback(
async (id: string, options = { updateHistory: true }) => {
const table = getTable(id);
+ const relationshipsToRemove = relationships.filter(
+ (relationship) =>
+ relationship.sourceTableId === id ||
+ relationship.targetTableId === id
+ );
+ setRelationships((relationships) =>
+ relationships.filter(
+ (relationship) =>
+ relationship.sourceTableId !== id &&
+ relationship.targetTableId !== id
+ )
+ );
setTables((tables) => tables.filter((table) => table.id !== id));
const updatedAt = new Date();
setDiagramUpdatedAt(updatedAt);
await Promise.all([
db.updateDiagram({ id: diagramId, attributes: { updatedAt } }),
+ ...relationshipsToRemove.map((relationship) =>
+ db.deleteRelationship({ diagramId, id: relationship.id })
+ ),
db.deleteTable({ diagramId, id }),
]);
@@ -318,12 +333,20 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
addUndoAction({
action: 'removeTable',
redoData: { tableId: id },
- undoData: { table },
+ undoData: { table, relationships: relationshipsToRemove },
});
resetRedoStack();
}
},
- [db, diagramId, setTables, addUndoAction, resetRedoStack, getTable]
+ [
+ db,
+ diagramId,
+ setTables,
+ addUndoAction,
+ resetRedoStack,
+ getTable,
+ relationships,
+ ]
);
const updateTable: ChartDBContext['updateTable'] = useCallback(
@@ -382,26 +405,50 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
const prevTables = deepCopy(tables);
const updatedTables = updateTables(tables);
+
+ const tablesToDelete = prevTables.filter(
+ (table) => !updatedTables.some((t) => t.id === table.id)
+ );
+
+ const relationshipsToRemove = relationships.filter((relationship) =>
+ tablesToDelete.some(
+ (table) =>
+ table.id === relationship.sourceTableId ||
+ table.id === relationship.targetTableId
+ )
+ );
+
+ setRelationships((relationships) =>
+ relationships.filter(
+ (relationship) =>
+ !relationshipsToRemove.some(
+ (r) => r.id === relationship.id
+ )
+ )
+ );
+
setTables(updateTables);
const promises = [];
for (const updatedTable of updatedTables) {
promises.push(
- db.updateTable({
- id: updatedTable.id,
- attributes: updatedTable,
+ db.putTable({
+ diagramId,
+ table: updatedTable,
})
);
}
- const tablesToDelete = tables.filter(
- (table) => !updatedTables.some((t) => t.id === table.id)
- );
-
for (const table of tablesToDelete) {
promises.push(db.deleteTable({ diagramId, id: table.id }));
}
+ for (const relationship of relationshipsToRemove) {
+ promises.push(
+ db.deleteRelationship({ diagramId, id: relationship.id })
+ );
+ }
+
const updatedAt = new Date();
setDiagramUpdatedAt(updatedAt);
promises.push(
@@ -414,12 +461,23 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({
addUndoAction({
action: 'updateTablesState',
redoData: { tables: updatedTables },
- undoData: { tables: prevTables },
+ undoData: {
+ tables: prevTables,
+ relationships: relationshipsToRemove,
+ },
});
resetRedoStack();
}
},
- [db, tables, setTables, diagramId, addUndoAction, resetRedoStack]
+ [
+ db,
+ tables,
+ setTables,
+ diagramId,
+ addUndoAction,
+ resetRedoStack,
+ relationships,
+ ]
);
const getField: ChartDBContext['getField'] = useCallback(
diff --git a/src/context/history-context/history-provider.tsx b/src/context/history-context/history-provider.tsx
index 8f247801..9d6a0996 100644
--- a/src/context/history-context/history-provider.tsx
+++ b/src/context/history-context/history-provider.tsx
@@ -130,8 +130,11 @@ export const HistoryProvider: React.FC<React.PropsWithChildren> = ({
addTable: ({ undoData: { tableId } }) => {
return removeTable(tableId, { updateHistory: false });
},
- removeTable: ({ undoData: { table } }) => {
- return addTable(table, { updateHistory: false });
+ removeTable: async ({ undoData: { table, relationships } }) => {
+ await Promise.all([
+ addTable(table, { updateHistory: false }),
+ addRelationships(relationships, { updateHistory: false }),
+ ]);
},
updateTable: ({ undoData: { tableId, table } }) => {
return updateTable(tableId, table, { updateHistory: false });
@@ -167,11 +170,16 @@ export const HistoryProvider: React.FC<React.PropsWithChildren> = ({
updateHistory: false,
});
},
- updateTablesState: ({ undoData: { tables } }) => {
- return updateTablesState(() => tables, {
- updateHistory: false,
- forceOverride: true,
- });
+ updateTablesState: async ({
+ undoData: { tables, relationships },
+ }) => {
+ await Promise.all([
+ updateTablesState(() => tables, {
+ updateHistory: false,
+ forceOverride: true,
+ }),
+ addRelationships(relationships, { updateHistory: false }),
+ ]);
},
addIndex: ({ undoData: { tableId, indexId } }) => {
return removeIndex(tableId, indexId, { updateHistory: false });
diff --git a/src/context/history-context/redo-undo-action.ts b/src/context/history-context/redo-undo-action.ts
index 05d4f4f3..49201677 100644
--- a/src/context/history-context/redo-undo-action.ts
+++ b/src/context/history-context/redo-undo-action.ts
@@ -33,13 +33,13 @@ type RedoUndoActionAddTable = RedoUndoActionBase<
type RedoUndoActionRemoveTable = RedoUndoActionBase<
'removeTable',
{ tableId: string },
- { table: DBTable }
+ { table: DBTable; relationships: DBRelationship[] }
>;
type RedoUndoActionUpdateTablesState = RedoUndoActionBase<
'updateTablesState',
{ tables: DBTable[] },
- { tables: DBTable[] }
+ { tables: DBTable[]; relationships: DBRelationship[] }
>;
type RedoUndoActionAddField = RedoUndoActionBase<
diff --git a/src/context/storage-context/storage-context.tsx b/src/context/storage-context/storage-context.tsx
index 9238b00e..73e3dfae 100644
--- a/src/context/storage-context/storage-context.tsx
+++ b/src/context/storage-context/storage-context.tsx
@@ -39,6 +39,7 @@ export interface StorageContext {
id: string;
attributes: Partial<DBTable>;
}) => Promise<void>;
+ putTable: (params: { diagramId: string; table: DBTable }) => Promise<void>;
deleteTable: (params: { diagramId: string; id: string }) => Promise<void>;
listTables: (diagramId: string) => Promise<DBTable[]>;
deleteDiagramTables: (diagramId: string) => Promise<void>;
@@ -77,6 +78,7 @@ export const storageContext = createContext<StorageContext>({
addTable: emptyFn,
getTable: emptyFn,
updateTable: emptyFn,
+ putTable: emptyFn,
deleteTable: emptyFn,
listTables: emptyFn,
deleteDiagramTables: emptyFn,
diff --git a/src/context/storage-context/storage-provider.tsx b/src/context/storage-context/storage-provider.tsx
index 7bff41c9..a4d5b524 100644
--- a/src/context/storage-context/storage-provider.tsx
+++ b/src/context/storage-context/storage-provider.tsx
@@ -250,6 +250,13 @@ export const StorageProvider: React.FC<React.PropsWithChildren> = ({
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,
@@ -358,6 +365,7 @@ export const StorageProvider: React.FC<React.PropsWithChildren> = ({
addTable,
getTable,
updateTable,
+ putTable,
deleteTable,
listTables,
addRelationship,
diff --git a/src/pages/editor-page/editor-page.tsx b/src/pages/editor-page/editor-page.tsx
index 908f6978..bf502f07 100644
--- a/src/pages/editor-page/editor-page.tsx
+++ b/src/pages/editor-page/editor-page.tsx
@@ -93,7 +93,7 @@ export const EditorPage: React.FC = () => {
return (
<>
<section
- className={`bg-background ${isDesktop ? 'h-screen w-screen' : 'h-dvh w-dvw'} flex flex-col overflow-x-hidden`}
+ className={`bg-background ${isDesktop ? 'h-screen w-screen' : 'h-dvh w-dvw'} flex select-none flex-col overflow-x-hidden`}
>
<TopNavbar />
{isDesktop ? (