@@ -41,6 +41,7 @@ import { UUID } from 'bson';
4141import DiagramEditorToolbar from './diagram-editor-toolbar' ;
4242import ExportDiagramModal from './export-diagram-modal' ;
4343import { useLogger } from '@mongodb-js/compass-logging/provider' ;
44+ import type { MongoDBJSONSchema } from 'mongodb-schema' ;
4445
4546const loadingContainerStyles = css ( {
4647 width : '100%' ,
@@ -144,6 +145,33 @@ const editorContainerPlaceholderButtonStyles = css({
144145 paddingTop : spacing [ 200 ] ,
145146} ) ;
146147
148+ const getFieldsFromSchema = (
149+ jsonSchema : MongoDBJSONSchema ,
150+ depth = 0
151+ ) : NodeProps [ 'fields' ] => {
152+ let fields = [ ] as NodeProps [ 'fields' ] ;
153+ if ( jsonSchema . anyOf ) {
154+ for ( const variant of jsonSchema . anyOf ) {
155+ fields = [ ...fields , ...getFieldsFromSchema ( variant , depth + 1 ) ] ;
156+ }
157+ }
158+ if ( ! jsonSchema . properties ) return [ ] ;
159+ for ( const [ name , field ] of Object . entries ( jsonSchema . properties ) ) {
160+ const type = getFieldTypeDisplay ( field ) ;
161+ fields . push ( {
162+ name,
163+ type,
164+ depth : depth ,
165+ glyphs : type === 'objectId' ? [ 'key' ] : [ ] ,
166+ } ) ;
167+ if ( field . properties ) {
168+ fields = [ ...fields , ...getFieldsFromSchema ( field , depth + 1 ) ] ;
169+ }
170+ }
171+
172+ return fields ;
173+ } ;
174+
147175const DiagramEditor : React . FunctionComponent < {
148176 diagramLabel : string ;
149177 step : DataModelingState [ 'step' ] ;
@@ -276,16 +304,7 @@ const DiagramEditor: React.FunctionComponent<{
276304 y : coll . displayPosition [ 1 ] ,
277305 } ,
278306 title : coll . ns ,
279- fields : Object . entries ( coll . jsonSchema . properties ?? { } ) . map (
280- ( [ name , field ] ) => {
281- const type = getFieldTypeDisplay ( field ) ;
282- return {
283- name,
284- type,
285- glyphs : type === 'objectId' ? [ 'key' ] : [ ] ,
286- } ;
287- }
288- ) ,
307+ fields : getFieldsFromSchema ( coll . jsonSchema ) ,
289308 } )
290309 ) ;
291310 } , [ model ?. collections ] ) ;
0 commit comments