Skip to content
Open
Show file tree
Hide file tree
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
67 changes: 9 additions & 58 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/compass-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"@lg-chat/message": "^10.1.1",
"@lg-chat/message-prompts": "^4.2.0",
"@mongodb-js/compass-context-menu": "^0.3.2",
"@mongodb-js/diagramming": "2.2.2",
"@mongodb-js/diagramming": "^2.3.1",
"@react-aria/interactions": "^3.9.1",
"@react-aria/utils": "^3.13.1",
"@react-aria/visually-hidden": "^3.3.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ const DiagramContent: React.FunctionComponent<{
!!selectedItems &&
selectedItems.type === 'relationship' &&
selectedItems.id === relationship.id;
return relationshipToDiagramEdge(relationship, selected, nodes);
return relationshipToDiagramEdge(relationship, selected);
});
}, [model?.relationships, selectedItems, nodes]);
}, [model?.relationships, selectedItems]);

// Fit to view on initial mount
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ async function getInitialLayout({
});
return await applyLayout({
nodes,
edges: relations.map((rel) => relationshipToDiagramEdge(rel, false, [])), // nodes are not important here
edges: relations.map((rel) => relationshipToDiagramEdge(rel, false)),
direction: hasRelations ? 'STAR' : 'RECTANGLE',
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
relationshipToDiagramEdge,
} from './nodes-and-edges';
import { type Relationship } from '../services/data-model-storage';
import { type NodeProps } from '@mongodb-js/compass-components';

describe('getFieldsFromSchema', function () {
describe('flat schema', function () {
Expand Down Expand Up @@ -699,78 +698,25 @@ describe('relationshipToDiagramEdge', function () {
note: 'Test relationship',
};

const node: NodeProps = {
id: relationship.relationship[0].ns!,
title: 'Collection A',
type: 'collection',
position: { x: 0, y: 0 },
fields: [],
};

it('should forward basic properties', function () {
const isSelected = true;
const edge = relationshipToDiagramEdge(relationship, isSelected, []);
const edge = relationshipToDiagramEdge(relationship, isSelected);
expect(edge.id).to.equal(relationship.id);
expect(edge.source).to.equal(relationship.relationship[0].ns);
expect(edge.target).to.equal(relationship.relationship[1].ns);
expect(edge.selected).to.equal(isSelected);
});

it('should map cardinality to markers', function () {
const edge = relationshipToDiagramEdge(relationship, false, []);
const edge = relationshipToDiagramEdge(relationship, false);
expect(edge.markerStart).to.equal('one');
expect(edge.markerEnd).to.equal('many');
});

it('should find field indices', function () {
const nodes: NodeProps[] = [
{
...node,
id: relationship.relationship[0].ns!,
fields: [
{
id: ['otherPath'],
name: 'fieldA', // same name but different path
type: 'string',
},
{
id: ['parent', 'otherField'], // same parent but different field
name: 'otherField',
type: 'string',
},
{
id: relationship.relationship[0].fields as string[],
name: 'fieldA',
type: 'string',
},
],
},
{
...node,
id: relationship.relationship[1].ns!,
fields: [
{
id: ['otherPath'],
name: 'fieldB', // same name but different path
type: 'string',
},
{
id: relationship.relationship[1].fields as string[],
name: 'fieldB',
type: 'string',
},
{
id: ['otherParent', 'otherField'], // same parent but different field
name: 'otherField',
type: 'string',
},
],
},
];

const edge = relationshipToDiagramEdge(relationship, false, nodes);
expect(edge.sourceFieldIndex).to.equal(2);
expect(edge.targetFieldIndex).to.equal(1);
it('should map field ids', function () {
const edge = relationshipToDiagramEdge(relationship, false);
expect(edge.sourceFieldId).to.equal(relationship.relationship[0].fields);
expect(edge.targetFieldId).to.equal(relationship.relationship[1].fields);
});

it('should choose animated for incomplete relationships', function () {
Expand All @@ -785,12 +731,11 @@ describe('relationshipToDiagramEdge', function () {
relationship.relationship[1],
],
},
false,
[]
false
);
expect(incompleteEdge.animated).to.equal(true);

const completeEdge = relationshipToDiagramEdge(relationship, false, []);
const completeEdge = relationshipToDiagramEdge(relationship, false);
expect(completeEdge.animated).to.equal(false);
});
});
39 changes: 3 additions & 36 deletions packages/compass-data-modeling/src/utils/nodes-and-edges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,50 +198,17 @@ export function collectionToDiagramNode({
};
}

function findNodeByNS(ns: string, nodes: NodeProps[]): NodeProps | undefined {
return nodes.find((node) => node.id === ns);
}

function findFieldIndex({
fieldPath,
nodes,
ns,
}: {
fieldPath: string[];
nodes: NodeProps[];
ns?: string;
}): number | undefined {
if (!ns || !fieldPath.length) return undefined;
const node = findNodeByNS(ns, nodes);
if (!node) return undefined;

for (const [index, field] of node.fields.entries()) {
if (!field.id || !Array.isArray(field.id)) continue;
// TODO(COMPASS-9504 and COMPASS-9935): Accept partial paths for collapsed nodes and fields.
if (areFieldPathsEqual(field.id, fieldPath)) return index;
}
}

export function relationshipToDiagramEdge(
relationship: Relationship,
selected = false,
nodes: NodeProps[]
selected = false
): EdgeProps {
const [source, target] = relationship.relationship;
return {
id: relationship.id,
source: source.ns ?? '',
target: target.ns ?? '',
sourceFieldIndex: findFieldIndex({
fieldPath: source.fields ?? [],
nodes,
ns: source.ns ?? undefined,
}),
targetFieldIndex: findFieldIndex({
fieldPath: target.fields ?? [],
nodes,
ns: target.ns ?? undefined,
}),
sourceFieldId: source.fields ?? [],
targetFieldId: target.fields ?? [],
markerStart: source.cardinality === 1 ? 'one' : 'many',
markerEnd: target.cardinality === 1 ? 'one' : 'many',
selected,
Expand Down
Loading