@@ -9,6 +9,7 @@ import type {
99 FieldPath ,
1010 Relationship ,
1111} from '../services/data-model-storage' ;
12+ import { traverseSchema } from './schema-traversal' ;
1213
1314function getBsonTypeName ( bsonType : string ) {
1415 switch ( bsonType ) {
@@ -72,73 +73,6 @@ export const getHighlightedFields = (
7273 return selection ;
7374} ;
7475
75- // TODO: add support for update
76- // renaming fields, deleting fields, adding fields...
77- // or maybe we can have two traverses? one for parsing, one for updating?
78- export const traverseSchema = ( {
79- jsonSchema,
80- parentFieldPath = [ ] ,
81- visitor,
82- } : {
83- jsonSchema : MongoDBJSONSchema ;
84- visitor : ( {
85- fieldPath,
86- fieldTypes,
87- } : {
88- fieldPath : FieldPath ;
89- fieldTypes : string [ ] ;
90- } ) => void ;
91- parentFieldPath ?: FieldPath ;
92- } ) : void => {
93- if ( ! jsonSchema || ! jsonSchema . properties ) {
94- return ;
95- }
96- for ( const [ name , field ] of Object . entries ( jsonSchema . properties ) ) {
97- // field has types, properties and (optional) children
98- // types are either direct, or from anyof
99- // children are either direct (properties), from anyOf, items or items.anyOf
100- const types : ( string | string [ ] ) [ ] = [ ] ;
101- const children : ( MongoDBJSONSchema | MongoDBJSONSchema [ ] ) [ ] = [ ] ;
102- if ( field . bsonType ) {
103- types . push ( field . bsonType ) ;
104- }
105- if ( field . properties ) {
106- children . push ( field ) ;
107- }
108- if ( field . items ) {
109- children . push ( ( field . items as MongoDBJSONSchema ) . anyOf || field . items ) ;
110- }
111- if ( field . anyOf ) {
112- for ( const variant of field . anyOf ) {
113- if ( variant . bsonType ) {
114- types . push ( variant . bsonType ) ;
115- }
116- if ( variant . properties ) {
117- children . push ( variant ) ;
118- }
119- if ( variant . items ) {
120- children . push ( variant . items ) ;
121- }
122- }
123- }
124-
125- const newFieldPath = [ ...parentFieldPath , name ] ;
126-
127- visitor ( {
128- fieldPath : newFieldPath ,
129- fieldTypes : types . flat ( ) ,
130- } ) ;
131-
132- children . flat ( ) . forEach ( ( child ) =>
133- traverseSchema ( {
134- jsonSchema : child ,
135- visitor,
136- parentFieldPath : newFieldPath ,
137- } )
138- ) ;
139- }
140- } ;
141-
14276export const getFieldsFromSchema = ( {
14377 jsonSchema,
14478 highlightedFields = [ ] ,
0 commit comments