Skip to content

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

import diagram refactor (#275)

Guy Ben-Aharon447312127b61
diff --git a/src/lib/data/import-metadata/metadata-types/database-metadata.ts b/src/lib/data/import-metadata/metadata-types/database-metadata.ts
index 10e1bdf7..99a7c962 100644
--- a/src/lib/data/import-metadata/metadata-types/database-metadata.ts
+++ b/src/lib/data/import-metadata/metadata-types/database-metadata.ts
@@ -27,11 +27,11 @@ export const isDatabaseMetadata = (obj: any): boolean => {
);
};
-export function loadDatabaseMetadata(jsonString: string): DatabaseMetadata {
+export const loadDatabaseMetadata = (jsonString: string): DatabaseMetadata => {
try {
const parsedData: DatabaseMetadata = JSON.parse(jsonString);
return parsedData;
} catch (parseError) {
throw new Error(`Error parsing JSON data: ${parseError}`);
}
-}
+};
diff --git a/src/lib/data/import-metadata/metadata-types/index-info.ts b/src/lib/data/import-metadata/metadata-types/index-info.ts
index c602bef6..c95df818 100644
--- a/src/lib/data/import-metadata/metadata-types/index-info.ts
+++ b/src/lib/data/import-metadata/metadata-types/index-info.ts
@@ -1,3 +1,6 @@
+import { schemaNameToDomainSchemaName } from '@/lib/domain/db-schema';
+import type { TableInfo } from './table-info';
+
export interface IndexInfo {
schema: string;
table: string;
@@ -11,3 +14,46 @@ export interface IndexInfo {
direction: string;
column_position: number;
}
+
+export type AggregatedIndexInfo = Omit<IndexInfo, 'column'> & {
+ columns: { name: string; position: number }[];
+};
+
+export const createAggregatedIndexes = ({
+ tableInfo,
+ tableSchema,
+ indexes,
+}: {
+ tableInfo: TableInfo;
+ indexes: IndexInfo[];
+ tableSchema?: string;
+}): AggregatedIndexInfo[] => {
+ const tableIndexes = indexes.filter((idx) => {
+ const indexSchema = schemaNameToDomainSchemaName(idx.schema);
+
+ return idx.table === tableInfo.table && indexSchema === tableSchema;
+ });
+
+ return Object.values(
+ tableIndexes.reduce(
+ (acc, idx) => {
+ const key = `${idx.schema}_${idx.name}`;
+ if (!acc[key]) {
+ acc[key] = {
+ ...idx,
+ columns: [
+ { name: idx.column, position: idx.column_position },
+ ],
+ };
+ } else {
+ acc[key].columns.push({
+ name: idx.column,
+ position: idx.column_position,
+ });
+ }
+ return acc;
+ },
+ {} as Record<string, AggregatedIndexInfo>
+ )
+ );
+};
diff --git a/src/lib/domain/db-field.ts b/src/lib/domain/db-field.ts
index f0b3b5cf..7dcf0dab 100644
--- a/src/lib/domain/db-field.ts
+++ b/src/lib/domain/db-field.ts
@@ -1,4 +1,10 @@
import type { DataType } from '../data/data-types/data-types';
+import type { ColumnInfo } from '../data/import-metadata/metadata-types/column-info';
+import type { AggregatedIndexInfo } from '../data/import-metadata/metadata-types/index-info';
+import type { PrimaryKeyInfo } from '../data/import-metadata/metadata-types/primary-key-info';
+import type { TableInfo } from '../data/import-metadata/metadata-types/table-info';
+import { generateId } from '../utils';
+import { schemaNameToDomainSchemaName } from './db-schema';
export interface DBField {
id: string;
@@ -15,3 +21,73 @@ export interface DBField {
collation?: string;
comments?: string;
}
+
+export const createFieldsFromMetadata = ({
+ columns,
+ tableSchema,
+ tableInfo,
+ primaryKeys,
+ aggregatedIndexes,
+}: {
+ columns: ColumnInfo[];
+ tableSchema?: string;
+ tableInfo: TableInfo;
+ primaryKeys: PrimaryKeyInfo[];
+ aggregatedIndexes: AggregatedIndexInfo[];
+}) => {
+ const uniqueColumns = columns
+ .filter(
+ (col) =>
+ schemaNameToDomainSchemaName(col.schema) === tableSchema &&
+ col.table === tableInfo.table
+ )
+ .reduce((acc, col) => {
+ if (!acc.has(col.name)) {
+ acc.set(col.name, col);
+ }
+ return acc;
+ }, new Map<string, ColumnInfo>());
+
+ const sortedColumns = Array.from(uniqueColumns.values()).sort(
+ (a, b) => a.ordinal_position - b.ordinal_position
+ );
+
+ const tablePrimaryKeys = primaryKeys
+ .filter(
+ (pk) =>
+ pk.table === tableInfo.table &&
+ schemaNameToDomainSchemaName(pk.schema) === tableSchema
+ )
+ .map((pk) => pk.column.trim());
+
+ return sortedColumns.map(
+ (col: ColumnInfo): DBField => ({
+ id: generateId(),
+ name: col.name,
+ type: {
+ id: col.type.split(' ').join('_').toLowerCase(),
+ name: col.type.toLowerCase(),
+ },
+ primaryKey: tablePrimaryKeys.includes(col.name),
+ unique: Object.values(aggregatedIndexes).some(
+ (idx) =>
+ idx.unique &&
+ idx.columns.length === 1 &&
+ idx.columns[0].name === col.name
+ ),
+ nullable: col.nullable,
+ ...(col.character_maximum_length &&
+ col.character_maximum_length !== 'null'
+ ? { character_maximum_length: col.character_maximum_length }
+ : {}),
+ ...(col.precision?.precision
+ ? { precision: col.precision.precision }
+ : {}),
+ ...(col.precision?.scale ? { scale: col.precision.scale } : {}),
+ ...(col.default ? { default: col.default } : {}),
+ ...(col.collation ? { collation: col.collation } : {}),
+ createdAt: Date.now(),
+ comments: col.comment ? col.comment : undefined,
+ })
+ );
+};
diff --git a/src/lib/domain/db-index.ts b/src/lib/domain/db-index.ts
index 4d754956..7d02346e 100644
--- a/src/lib/domain/db-index.ts
+++ b/src/lib/domain/db-index.ts
@@ -1,3 +1,7 @@
+import type { AggregatedIndexInfo } from '../data/import-metadata/metadata-types/index-info';
+import { generateId } from '../utils';
+import type { DBField } from './db-field';
+
export interface DBIndex {
id: string;
name: string;
@@ -5,3 +9,23 @@ export interface DBIndex {
fieldIds: string[];
createdAt: number;
}
+
+export const createIndexesFromMetadata = ({
+ aggregatedIndexes,
+ fields,
+}: {
+ aggregatedIndexes: AggregatedIndexInfo[];
+ fields: DBField[];
+}): DBIndex[] =>
+ aggregatedIndexes.map(
+ (idx): DBIndex => ({
+ id: generateId(),
+ name: idx.name,
+ unique: idx.unique,
+ fieldIds: idx.columns
+ .sort((a, b) => a.position - b.position)
+ .map((c) => fields.find((f) => f.name === c.name)?.id)
+ .filter((id): id is string => id !== undefined),
+ createdAt: Date.now(),
+ })
+ );
diff --git a/src/lib/domain/db-table.ts b/src/lib/domain/db-table.ts
index f2f381d4..e49a66cf 100644
--- a/src/lib/domain/db-table.ts
+++ b/src/lib/domain/db-table.ts
@@ -1,12 +1,9 @@
-import type { DBIndex } from './db-index';
-import type { DBField } from './db-field';
+import { createIndexesFromMetadata, type DBIndex } from './db-index';
+import { createFieldsFromMetadata, type DBField } from './db-field';
import type { TableInfo } from '../data/import-metadata/metadata-types/table-info';
-import type { ColumnInfo } from '../data/import-metadata/metadata-types/column-info';
-import type { IndexInfo } from '../data/import-metadata/metadata-types/index-info';
+import { createAggregatedIndexes } from '../data/import-metadata/metadata-types/index-info';
import { materializedViewColor, viewColor, randomColor } from '@/lib/colors';
import type { DBRelationship } from './db-relationship';
-import type { PrimaryKeyInfo } from '../data/import-metadata/metadata-types/primary-key-info';
-import type { ViewInfo } from '../data/import-metadata/metadata-types/view-info';
import {
decodeBase64ToUtf16LE,
decodeBase64ToUtf8,
@@ -18,6 +15,7 @@ import {
schemaNameToSchemaId,
} from './db-schema';
import { DatabaseType } from './database-type';
+import type { DatabaseMetadata } from '../data/import-metadata/metadata-types/database-metadata';
export interface DBTable {
id: string;
@@ -63,124 +61,42 @@ export const decodeViewDefinition = (
};
export const createTablesFromMetadata = ({
- tableInfos,
- columns,
- indexes,
- primaryKeys,
- views,
+ databaseMetadata,
databaseType,
}: {
- tableInfos: TableInfo[];
- columns: ColumnInfo[];
- indexes: IndexInfo[];
- primaryKeys: PrimaryKeyInfo[];
- views: ViewInfo[];
+ databaseMetadata: DatabaseMetadata;
databaseType: DatabaseType;
}): DBTable[] => {
+ const {
+ tables: tableInfos,
+ pk_info: primaryKeys,
+ columns,
+ indexes,
+ views: views,
+ } = databaseMetadata;
+
return tableInfos.map((tableInfo: TableInfo) => {
const tableSchema = schemaNameToDomainSchemaName(tableInfo.schema);
- // Filter, make unique, and sort columns based on ordinal_position
- const uniqueColumns = new Map<string, ColumnInfo>();
- columns
- .filter(
- (col) =>
- schemaNameToDomainSchemaName(col.schema) === tableSchema &&
- col.table === tableInfo.table
- )
- .forEach((col) => {
- if (!uniqueColumns.has(col.name)) {
- uniqueColumns.set(col.name, col);
- }
- });
-
- const sortedColumns = Array.from(uniqueColumns.values()).sort(
- (a, b) => a.ordinal_position - b.ordinal_position
- );
-
- const tablePrimaryKeys = primaryKeys
- .filter(
- (pk) =>
- pk.table === tableInfo.table &&
- schemaNameToDomainSchemaName(pk.schema) === tableSchema
- )
- .map((pk) => pk.column.trim());
-
- const tableIndexes = indexes.filter((idx) => {
- const indexSchema = schemaNameToDomainSchemaName(idx.schema);
-
- return idx.table === tableInfo.table && indexSchema === tableSchema;
- });
// Aggregate indexes with multiple columns
- const aggregatedIndexes = tableIndexes.reduce(
- (acc, idx) => {
- const key = `${idx.schema}_${idx.name}`;
- if (!acc[key]) {
- acc[key] = {
- ...idx,
- columns: [
- { name: idx.column, position: idx.column_position },
- ],
- };
- } else {
- acc[key].columns.push({
- name: idx.column,
- position: idx.column_position,
- });
- }
- return acc;
- },
- {} as Record<
- string,
- Omit<IndexInfo, 'column'> & {
- columns: { name: string; position: number }[];
- }
- >
- );
+ const aggregatedIndexes = createAggregatedIndexes({
+ tableInfo,
+ tableSchema,
+ indexes,
+ });
- const fields: DBField[] = sortedColumns.map(
- (col: ColumnInfo): DBField => ({
- id: generateId(),
- name: col.name,
- type: {
- id: col.type.split(' ').join('_').toLowerCase(),
- name: col.type.toLowerCase(),
- },
- primaryKey: tablePrimaryKeys.includes(col.name),
- unique: Object.values(aggregatedIndexes).some(
- (idx) =>
- idx.unique &&
- idx.columns.length === 1 &&
- idx.columns[0].name === col.name
- ),
- nullable: col.nullable,
- ...(col.character_maximum_length &&
- col.character_maximum_length !== 'null'
- ? { character_maximum_length: col.character_maximum_length }
- : {}),
- ...(col.precision?.precision
- ? { precision: col.precision.precision }
- : {}),
- ...(col.precision?.scale ? { scale: col.precision.scale } : {}),
- ...(col.default ? { default: col.default } : {}),
- ...(col.collation ? { collation: col.collation } : {}),
- createdAt: Date.now(),
- comments: col.comment ? col.comment : undefined,
- })
- );
+ const fields = createFieldsFromMetadata({
+ aggregatedIndexes,
+ columns,
+ primaryKeys,
+ tableInfo,
+ tableSchema,
+ });
- const dbIndexes: DBIndex[] = Object.values(aggregatedIndexes).map(
- (idx): DBIndex => ({
- id: generateId(),
- name: idx.name,
- unique: idx.unique,
- fieldIds: idx.columns
- .sort((a, b) => a.position - b.position)
- .map((c) => fields.find((f) => f.name === c.name)?.id)
- .filter((id): id is string => id !== undefined),
- createdAt: Date.now(),
- })
- );
+ const dbIndexes = createIndexesFromMetadata({
+ aggregatedIndexes,
+ fields,
+ });
// Determine if the current table is a view by checking against viewInfo
const isView = views.some(
@@ -191,6 +107,7 @@ export const createTablesFromMetadata = ({
const isMaterializedView = views.some(
(view) =>
+ schemaNameToDomainSchemaName(view.schema) === tableSchema &&
view.view_name === tableInfo.table &&
decodeViewDefinition(databaseType, view.view_definition)
.toLowerCase()
diff --git a/src/lib/domain/diagram.ts b/src/lib/domain/diagram.ts
index b29c3934..3c69d66d 100644
--- a/src/lib/domain/diagram.ts
+++ b/src/lib/domain/diagram.ts
@@ -31,39 +31,24 @@ export const loadFromDatabaseMetadata = async ({
diagramNumber?: number;
databaseEdition?: DatabaseEdition;
}): Promise<Diagram> => {
- const {
- tables: tableInfos,
- pk_info: primaryKeys,
- columns,
- indexes,
- fk_info: foreignKeys,
- views: views,
- } = databaseMetadata;
+ const { fk_info: foreignKeys, views: views } = databaseMetadata;
- // First pass: Create tables without final positions
const tables = createTablesFromMetadata({
- tableInfos,
- columns,
- indexes,
- primaryKeys,
- views,
+ databaseMetadata,
databaseType,
});
- // First pass: Create relationships
const relationships = createRelationshipsFromMetadata({
foreignKeys,
tables,
});
- // First pass: Create dependencies
const dependencies = await createDependenciesFromMetadata({
views,
tables,
databaseType,
});
- // Second pass: Adjust table positions based on relationships
const adjustedTables = adjustTablePositions({
tables,
relationships,