Skip to content

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

add schema to storage

Guy Ben-Aharonaeb3096a7638
diff --git a/src/context/storage-context/storage-provider.tsx b/src/context/storage-context/storage-provider.tsx
index 39fdfd4f..7bff41c9 100644
--- a/src/context/storage-context/storage-provider.tsx
+++ b/src/context/storage-context/storage-provider.tsx
@@ -74,6 +74,16 @@ export const StorageProvider: React.FC<React.PropsWithChildren> = ({
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.on('ready', async () => {
const config = await getConfig();
diff --git a/src/lib/domain/db-relationship.ts b/src/lib/domain/db-relationship.ts
index a9643056..95269f0f 100644
--- a/src/lib/domain/db-relationship.ts
+++ b/src/lib/domain/db-relationship.ts
@@ -5,7 +5,9 @@ import { generateId } from '@/lib/utils';
export interface DBRelationship {
id: string;
name: string;
+ sourceSchema?: string;
sourceTableId: string;
+ targetSchema?: string;
targetTableId: string;
sourceFieldId: string;
targetFieldId: string;
@@ -24,9 +26,21 @@ export const createRelationshipsFromMetadata = ({
}): DBRelationship[] => {
return foreignKeys
.map((fk: ForeignKeyInfo) => {
- const sourceTable = tables.find((table) => table.name === fk.table);
+ const schema =
+ (fk.schema ?? '').trim() === '' ? undefined : fk.schema;
+ const sourceTable = tables.find(
+ (table) => table.name === fk.table && table.schema === schema
+ );
+
+ const targetSchema =
+ (fk.reference_schema ?? '').trim() === ''
+ ? undefined
+ : fk.reference_schema;
+
const targetTable = tables.find(
- (table) => table.name === fk.reference_table
+ (table) =>
+ table.name === fk.reference_table &&
+ table.schema === targetSchema
);
const sourceField = sourceTable?.fields.find(
(field) => field.name === fk.column
@@ -39,13 +53,15 @@ export const createRelationshipsFromMetadata = ({
return {
id: generateId(),
name: fk.foreign_key_name,
+ sourceSchema: schema,
+ targetSchema: targetSchema,
sourceTableId: sourceTable.id,
targetTableId: targetTable.id,
sourceFieldId: sourceField.id,
targetFieldId: targetField.id,
type: 'many_to_one', // This could be adjusted based on your logic
createdAt: Date.now(),
- };
+ } as DBRelationship;
}
return null;
diff --git a/src/lib/domain/db-table.ts b/src/lib/domain/db-table.ts
index 59be055d..f4ba11d7 100644
--- a/src/lib/domain/db-table.ts
+++ b/src/lib/domain/db-table.ts
@@ -12,6 +12,7 @@ import { generateId } from '../utils';
export interface DBTable {
id: string;
name: string;
+ schema?: string;
x: number;
y: number;
fields: DBField[];
@@ -109,10 +110,16 @@ export const createTablesFromMetadata = ({
view.view_name === tableInfo.table
);
+ const schema =
+ (tableInfo.schema ?? '').trim() === ''
+ ? undefined
+ : tableInfo.schema;
+
// Initial random positions; these will be adjusted later
return {
id: generateId(),
name: tableInfo.table,
+ schema,
x: Math.random() * 1000, // Placeholder X
y: Math.random() * 800, // Placeholder Y
fields,