@@ -6,7 +6,10 @@ import { getCurrentDiagramFromState } from './diagram';
66import { UUID } from 'bson' ;
77import type { Relationship } from '../services/data-model-storage' ;
88import { applyLayout } from '@mongodb-js/compass-components' ;
9- import { collectionToBaseNodeForLayout } from '../utils/nodes-and-edges' ;
9+ import {
10+ collectionToBaseNodeForLayout ,
11+ relationshipToDiagramEdge ,
12+ } from '../utils/nodes-and-edges' ;
1013import { inferForeignToLocalRelationshipsForCollection } from './relationships' ;
1114import { mongoLogId } from '@mongodb-js/compass-logging/provider' ;
1215
@@ -146,6 +149,29 @@ export const analysisProcessReducer: Reducer<AnalysisProcessState> = (
146149 return state ;
147150} ;
148151
152+ async function getInitialLayout ( {
153+ collections,
154+ relations,
155+ } : {
156+ collections : { ns : string ; schema : MongoDBJSONSchema } [ ] ;
157+ relations : Relationship [ ] ;
158+ } ) {
159+ const hasRelations = relations . length > 0 ;
160+ const nodes = collections . map ( ( coll ) => {
161+ return collectionToBaseNodeForLayout ( {
162+ ns : coll . ns ,
163+ jsonSchema : coll . schema ,
164+ displayPosition : [ 0 , 0 ] ,
165+ isExpanded : false ,
166+ } ) ;
167+ } ) ;
168+ return await applyLayout ( {
169+ nodes,
170+ edges : relations . map ( ( rel ) => relationshipToDiagramEdge ( rel , false ) ) ,
171+ direction : hasRelations ? 'STAR' : 'RECTANGLE' ,
172+ } ) ;
173+ }
174+
149175export function startAnalysis (
150176 name : string ,
151177 connectionId : string ,
@@ -281,17 +307,9 @@ export function startAnalysis(
281307 throw cancelController . signal . reason ;
282308 }
283309
284- const positioned = await applyLayout ( {
285- nodes : collections . map ( ( coll ) => {
286- return collectionToBaseNodeForLayout ( {
287- ns : coll . ns ,
288- jsonSchema : coll . schema ,
289- displayPosition : [ 0 , 0 ] ,
290- isExpanded : false ,
291- } ) ;
292- } ) ,
293- edges : [ ] ,
294- direction : 'LEFT_RIGHT' ,
310+ const positioned = await getInitialLayout ( {
311+ collections,
312+ relations,
295313 } ) ;
296314
297315 dispatch ( {
0 commit comments