Skip to content

fix(compass-data-modeling): visualise nested fields COMPASS-9488 #7061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions packages/compass-data-modeling/src/components/diagram-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { UUID } from 'bson';
import DiagramEditorToolbar from './diagram-editor-toolbar';
import ExportDiagramModal from './export-diagram-modal';
import { useLogger } from '@mongodb-js/compass-logging/provider';
import type { MongoDBJSONSchema } from 'mongodb-schema';

const loadingContainerStyles = css({
width: '100%',
Expand Down Expand Up @@ -114,6 +115,39 @@ const editorContainerPlaceholderButtonStyles = css({
paddingTop: spacing[200],
});

const getFieldsFromSchema = (
jsonSchema: MongoDBJSONSchema,
depth = 0
): NodeProps['fields'] => {
let fields = [] as NodeProps['fields'];
if (jsonSchema.anyOf) {
for (const variant of jsonSchema.anyOf) {
fields = [...fields, ...getFieldsFromSchema(variant, depth + 1)];
}
}
if (!jsonSchema.properties) return [];
for (const [name, field] of Object.entries(jsonSchema.properties)) {
const type =
field.bsonType === undefined
? 'Unknown'
: typeof field.bsonType === 'string'
? field.bsonType
: // TODO: Show possible types of the field
field.bsonType[0];
fields.push({
name,
type,
depth: depth,
glyphs: type === 'objectId' ? ['key'] : [],
});
if (field.properties) {
fields = [...fields, ...getFieldsFromSchema(field, depth + 1)];
}
}

return fields;
};

const DiagramEditor: React.FunctionComponent<{
diagramLabel: string;
step: DataModelingState['step'];
Expand Down Expand Up @@ -246,22 +280,7 @@ const DiagramEditor: React.FunctionComponent<{
y: coll.displayPosition[1],
},
title: coll.ns,
fields: Object.entries(coll.jsonSchema.properties ?? {}).map(
([name, field]) => {
const type =
field.bsonType === undefined
? 'Unknown'
: typeof field.bsonType === 'string'
? field.bsonType
: // TODO: Show possible types of the field
field.bsonType[0];
return {
name,
type,
glyphs: type === 'objectId' ? ['key'] : [],
};
}
),
fields: getFieldsFromSchema(coll.jsonSchema),
})
);
}, [model?.collections]);
Expand Down
Loading