@@ -38,6 +38,7 @@ import { UUID } from 'bson';
3838import DiagramEditorToolbar from './diagram-editor-toolbar' ;
3939import ExportDiagramModal from './export-diagram-modal' ;
4040import { useLogger } from '@mongodb-js/compass-logging/provider' ;
41+ import type { MongoDBJSONSchema } from 'mongodb-schema' ;
4142
4243const loadingContainerStyles = css ( {
4344 width : '100%' ,
@@ -114,6 +115,39 @@ const editorContainerPlaceholderButtonStyles = css({
114115 paddingTop : spacing [ 200 ] ,
115116} ) ;
116117
118+ const getFieldsFromSchema = (
119+ jsonSchema : MongoDBJSONSchema ,
120+ depth = 0
121+ ) : NodeProps [ 'fields' ] => {
122+ let fields = [ ] as NodeProps [ 'fields' ] ;
123+ if ( jsonSchema . anyOf ) {
124+ for ( const variant of jsonSchema . anyOf ) {
125+ fields = [ ...fields , ...getFieldsFromSchema ( variant , depth + 1 ) ] ;
126+ }
127+ }
128+ if ( ! jsonSchema . properties ) return [ ] ;
129+ for ( const [ name , field ] of Object . entries ( jsonSchema . properties ) ) {
130+ const type =
131+ field . bsonType === undefined
132+ ? 'Unknown'
133+ : typeof field . bsonType === 'string'
134+ ? field . bsonType
135+ : // TODO: Show possible types of the field
136+ field . bsonType [ 0 ] ;
137+ fields . push ( {
138+ name,
139+ type,
140+ depth : depth ,
141+ glyphs : type === 'objectId' ? [ 'key' ] : [ ] ,
142+ } ) ;
143+ if ( field . properties ) {
144+ fields = [ ...fields , ...getFieldsFromSchema ( field , depth + 1 ) ] ;
145+ }
146+ }
147+
148+ return fields ;
149+ } ;
150+
117151const DiagramEditor : React . FunctionComponent < {
118152 diagramLabel : string ;
119153 step : DataModelingState [ 'step' ] ;
@@ -246,22 +280,7 @@ const DiagramEditor: React.FunctionComponent<{
246280 y : coll . displayPosition [ 1 ] ,
247281 } ,
248282 title : coll . ns ,
249- fields : Object . entries ( coll . jsonSchema . properties ?? { } ) . map (
250- ( [ name , field ] ) => {
251- const type =
252- field . bsonType === undefined
253- ? 'Unknown'
254- : typeof field . bsonType === 'string'
255- ? field . bsonType
256- : // TODO: Show possible types of the field
257- field . bsonType [ 0 ] ;
258- return {
259- name,
260- type,
261- glyphs : type === 'objectId' ? [ 'key' ] : [ ] ,
262- } ;
263- }
264- ) ,
283+ fields : getFieldsFromSchema ( coll . jsonSchema ) ,
265284 } )
266285 ) ;
267286 } , [ model ?. collections ] ) ;
0 commit comments