import { createContext } from 'react'; import type { DBTable } from '@/lib/domain/db-table'; import { emptyFn } from '@/lib/utils'; import { DatabaseType } from '@/lib/domain/database-type'; import type { DBField } from '@/lib/domain/db-field'; import type { DBIndex } from '@/lib/domain/db-index'; import type { DBRelationship } from '@/lib/domain/db-relationship'; import type { Diagram } from '@/lib/domain/diagram'; import type { DatabaseEdition } from '@/lib/domain/database-edition'; import type { DBSchema } from '@/lib/domain/db-schema'; import type { DBDependency } from '@/lib/domain/db-dependency'; import { EventEmitter } from 'ahooks/lib/useEventEmitter'; export type ChartDBEventType = | 'add_tables' | 'update_table' | 'remove_tables' | 'add_field' | 'remove_field' | 'load_diagram'; export type ChartDBEventBase = { action: T; data: D; }; export type CreateTableEvent = ChartDBEventBase< 'add_tables', { tables: DBTable[] } >; export type UpdateTableEvent = ChartDBEventBase< 'update_table', { id: string; table: Partial } >; export type RemoveTableEvent = ChartDBEventBase< 'remove_tables', { tableIds: string[] } >; export type AddFieldEvent = ChartDBEventBase< 'add_field', { tableId: string; field: DBField; fields: DBField[] } >; export type RemoveFieldEvent = ChartDBEventBase< 'remove_field', { tableId: string; fieldId: string; fields: DBField[] } >; export type LoadDiagramEvent = ChartDBEventBase< 'load_diagram', { diagram: Diagram } >; export type ChartDBEvent = | CreateTableEvent | UpdateTableEvent | RemoveTableEvent | AddFieldEvent | RemoveFieldEvent | LoadDiagramEvent; export interface ChartDBContext { diagramId: string; diagramName: string; databaseType: DatabaseType; tables: DBTable[]; schemas: DBSchema[]; relationships: DBRelationship[]; dependencies: DBDependency[]; currentDiagram: Diagram; events: EventEmitter; readonly?: boolean; filteredSchemas?: string[]; filterSchemas: (schemaIds: string[]) => void; // General operations updateDiagramId: (id: string) => Promise; updateDiagramName: ( name: string, options?: { updateHistory: boolean } ) => Promise; loadDiagram: (diagramId: string) => Promise; loadDiagramFromData: (diagram: Diagram) => void; updateDiagramUpdatedAt: () => Promise; clearDiagramData: () => Promise; deleteDiagram: () => Promise; // Database type operations updateDatabaseType: (databaseType: DatabaseType) => Promise; updateDatabaseEdition: (databaseEdition?: DatabaseEdition) => Promise; // Table operations createTable: ( attributes?: Partial> ) => Promise; addTable: ( table: DBTable, options?: { updateHistory: boolean } ) => Promise; addTables: ( tables: DBTable[], options?: { updateHistory: boolean } ) => Promise; getTable: (id: string) => DBTable | null; removeTable: ( id: string, options?: { updateHistory: boolean } ) => Promise; removeTables: ( ids: string[], options?: { updateHistory: boolean } ) => Promise; updateTable: ( id: string, table: Partial, options?: { updateHistory: boolean } ) => Promise; updateTablesState: ( updateFn: (tables: DBTable[]) => PartialExcept[], options?: { updateHistory: boolean; forceOverride?: boolean } ) => Promise; // Field operations getField: (tableId: string, fieldId: string) => DBField | null; updateField: ( tableId: string, fieldId: string, field: Partial, options?: { updateHistory: boolean } ) => Promise; removeField: ( tableId: string, fieldId: string, options?: { updateHistory: boolean } ) => Promise; createField: (tableId: string) => Promise; addField: ( tableId: string, field: DBField, options?: { updateHistory: boolean } ) => Promise; // Index operations createIndex: (tableId: string) => Promise; addIndex: ( tableId: string, index: DBIndex, options?: { updateHistory: boolean } ) => Promise; getIndex: (tableId: string, indexId: string) => DBIndex | null; removeIndex: ( tableId: string, indexId: string, options?: { updateHistory: boolean } ) => Promise; updateIndex: ( tableId: string, indexId: string, index: Partial, options?: { updateHistory: boolean } ) => Promise; // Relationship operations createRelationship: (params: { sourceTableId: string; targetTableId: string; sourceFieldId: string; targetFieldId: string; }) => Promise; addRelationship: ( relationship: DBRelationship, options?: { updateHistory: boolean } ) => Promise; addRelationships: ( relationships: DBRelationship[], options?: { updateHistory: boolean } ) => Promise; getRelationship: (id: string) => DBRelationship | null; removeRelationship: ( id: string, options?: { updateHistory: boolean } ) => Promise; removeRelationships: ( ids: string[], options?: { updateHistory: boolean } ) => Promise; updateRelationship: ( id: string, relationship: Partial, options?: { updateHistory: boolean } ) => Promise; // Dependency operations createDependency: (params: { tableId: string; dependentTableId: string; }) => Promise; addDependency: ( dependency: DBDependency, options?: { updateHistory: boolean } ) => Promise; addDependencies: ( dependencies: DBDependency[], options?: { updateHistory: boolean } ) => Promise; getDependency: (id: string) => DBDependency | null; removeDependency: ( id: string, options?: { updateHistory: boolean } ) => Promise; removeDependencies: ( ids: string[], options?: { updateHistory: boolean } ) => Promise; updateDependency: ( id: string, dependency: Partial, options?: { updateHistory: boolean } ) => Promise; } export const chartDBContext = createContext({ databaseType: DatabaseType.GENERIC, diagramName: '', diagramId: '', tables: [], relationships: [], dependencies: [], schemas: [], filteredSchemas: [], filterSchemas: emptyFn, currentDiagram: { id: '', name: '', databaseType: DatabaseType.GENERIC, createdAt: new Date(), updatedAt: new Date(), }, events: new EventEmitter(), // General operations updateDiagramId: emptyFn, updateDiagramName: emptyFn, updateDiagramUpdatedAt: emptyFn, loadDiagram: emptyFn, loadDiagramFromData: emptyFn, clearDiagramData: emptyFn, deleteDiagram: emptyFn, // Database type operations updateDatabaseType: emptyFn, updateDatabaseEdition: emptyFn, // Table operations createTable: emptyFn, getTable: emptyFn, addTable: emptyFn, addTables: emptyFn, removeTable: emptyFn, removeTables: emptyFn, updateTable: emptyFn, updateTablesState: 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, // Relationship operations createRelationship: emptyFn, addRelationship: emptyFn, getRelationship: emptyFn, removeRelationship: emptyFn, updateRelationship: emptyFn, removeRelationships: emptyFn, addRelationships: emptyFn, // Dependency operations createDependency: emptyFn, addDependency: emptyFn, getDependency: emptyFn, removeDependency: emptyFn, removeDependencies: emptyFn, addDependencies: emptyFn, updateDependency: emptyFn, });