Skip to content

Commit 7774beb

Browse files
committed
.
1 parent 91473b0 commit 7774beb

File tree

6 files changed

+84
-36
lines changed

6 files changed

+84
-36
lines changed

packages/compass-data-modeling/src/components/drawer/diagram-editor-side-panel.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('DiagramEditorSidePanel', function () {
7474
result.plugin.store.dispatch(selectCollection('flights.airlines'));
7575

7676
await waitFor(() => {
77-
expect(screen.getByTitle('flights.airlines')).to.be.visible;
77+
expect(screen.getByTitle('airlines')).to.be.visible;
7878
});
7979

8080
const nameInput = screen.getByLabelText('Name');

packages/compass-data-modeling/src/components/drawer/field-drawer-content.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import toNS from 'mongodb-ns';
1010
import {
1111
createNewRelationship,
1212
deleteRelationship,
13+
renameField,
1314
selectCurrentModelFromState,
1415
selectRelationship,
1516
} from '../../store/diagram';
@@ -98,10 +99,15 @@ const FieldDrawerContent: React.FunctionComponent<FieldDrawerContentProps> = ({
9899
fieldPath[fieldPath.length - 1],
99100
(fieldName) => {
100101
const trimmedName = fieldName.trim();
101-
if (trimmedName === fieldName) {
102+
console.log(
103+
`[Rename] ${fieldPath[fieldPath.length - 1]} -> ${trimmedName}`
104+
);
105+
if (trimmedName === fieldPath[fieldPath.length - 1]) {
106+
console.log('[Rename] No change in field name, skipping');
102107
return;
103108
}
104109
if (!isFieldNameValid) {
110+
console.log('[Rename] Invalid field name, skipping');
105111
return;
106112
}
107113
onRenameField(namespace, fieldPath, [
@@ -123,6 +129,7 @@ const FieldDrawerContent: React.FunctionComponent<FieldDrawerContentProps> = ({
123129
<DMFormFieldContainer>
124130
<TextInput
125131
label="Field name"
132+
disabled={true} // TODO: enable when field renaming is implemented
126133
data-testid="data-model-collection-drawer-name-input"
127134
sizeVariant="small"
128135
value={fieldName}
@@ -186,7 +193,7 @@ export default connect(
186193
onCreateNewRelationshipClick: createNewRelationship,
187194
onEditRelationshipClick: selectRelationship,
188195
onDeleteRelationshipClick: deleteRelationship,
189-
onRenameField: () => {}, // TODO: renameField,
196+
onRenameField: renameField,
190197
onChangeFieldType: () => {}, // TODO: updateFieldSchema,
191198
}
192199
)(FieldDrawerContent);

packages/compass-data-modeling/src/services/data-model-storage.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { z } from '@mongodb-js/compass-user-data';
2-
import { JSON_SCHEMA } from 'js-yaml';
32
import type { MongoDBJSONSchema } from 'mongodb-schema';
43

54
export const FieldPathSchema = z.array(z.string());

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
getDiagramName,
2626
} from '../services/open-and-download-diagram';
2727
import type { MongoDBJSONSchema } from 'mongodb-schema';
28+
import { traverseSchema } from '../utils/nodes-and-edges';
2829

2930
function isNonEmptyArray<T>(arr: T[]): arr is [T, ...T[]] {
3031
return Array.isArray(arr) && arr.length > 0;
@@ -439,6 +440,29 @@ export function renameCollection(
439440
};
440441
}
441442

443+
export function renameField(
444+
ns: string,
445+
from: FieldPath,
446+
to: FieldPath
447+
): DataModelingThunkAction<
448+
void,
449+
ApplyEditAction | ApplyEditFailedAction | CollectionSelectedAction
450+
> {
451+
return (dispatch) => {
452+
const edit: Omit<
453+
Extract<Edit, { type: 'RenameField' }>,
454+
'id' | 'timestamp'
455+
> = {
456+
type: 'RenameField',
457+
ns,
458+
from,
459+
to,
460+
};
461+
462+
dispatch(applyEdit(edit));
463+
};
464+
}
465+
442466
export function applyEdit(
443467
rawEdit: EditAction
444468
): DataModelingThunkAction<boolean, ApplyEditAction | ApplyEditFailedAction> {
@@ -679,6 +703,44 @@ function _applyEdit(edit: Edit, model?: StaticModel): StaticModel {
679703
})),
680704
};
681705
}
706+
case 'RenameField': {
707+
return {
708+
...model,
709+
// Update relationships to point to the renamed field.
710+
relationships: model.relationships.map((relationship) => {
711+
const [local, foreign] = relationship.relationship;
712+
713+
return {
714+
...relationship,
715+
relationship: [
716+
{
717+
...local,
718+
fields:
719+
local.ns === edit.ns &&
720+
JSON.stringify(local.fields) === JSON.stringify(edit.from)
721+
? edit.to
722+
: local.fields,
723+
},
724+
{
725+
...foreign,
726+
fields:
727+
local.ns === edit.ns &&
728+
JSON.stringify(local.fields) === JSON.stringify(edit.from)
729+
? edit.to
730+
: local.fields,
731+
},
732+
],
733+
};
734+
}),
735+
collections: model.collections.map((collection) => ({
736+
...collection,
737+
// TODO: Rename the field.
738+
// jsonSchema: collection.ns !== edit.ns
739+
// ? collection.jsonSchema
740+
// : renameFieldInSchema(collection.jsonSchema, edit.from, edit.to)
741+
})),
742+
};
743+
}
682744
case 'UpdateCollectionNote': {
683745
return {
684746
...model,
@@ -763,31 +825,14 @@ export const selectCurrentModelFromState = (state: DataModelingState) => {
763825
return selectCurrentModel(selectCurrentDiagramFromState(state).edits);
764826
};
765827

766-
function extractFields(
767-
parentSchema: MongoDBJSONSchema,
768-
parentKey?: string[],
769-
fields: string[][] = []
770-
) {
771-
if ('anyOf' in parentSchema && parentSchema.anyOf) {
772-
for (const schema of parentSchema.anyOf) {
773-
extractFields(schema, parentKey, fields);
774-
}
775-
}
776-
if ('items' in parentSchema && parentSchema.items) {
777-
const items = Array.isArray(parentSchema.items)
778-
? parentSchema.items
779-
: [parentSchema.items];
780-
for (const schema of items) {
781-
extractFields(schema, parentKey, fields);
782-
}
783-
}
784-
if ('properties' in parentSchema && parentSchema.properties) {
785-
for (const [key, value] of Object.entries(parentSchema.properties)) {
786-
const fullKey = parentKey ? [...parentKey, key] : [key];
787-
fields.push(fullKey);
788-
extractFields(value, fullKey, fields);
789-
}
790-
}
828+
function extractFieldsFromSchema(parentSchema: MongoDBJSONSchema): FieldPath[] {
829+
const fields: FieldPath[] = [];
830+
traverseSchema({
831+
jsonSchema: parentSchema,
832+
visitor: ({ fieldPath }) => {
833+
fields.push(fieldPath);
834+
},
835+
});
791836
return fields;
792837
}
793838

@@ -797,7 +842,7 @@ function getFieldsForCurrentModel(
797842
const model = selectCurrentModel(edits);
798843
const fields = Object.fromEntries(
799844
model.collections.map((collection) => {
800-
return [collection.ns, extractFields(collection.jsonSchema)];
845+
return [collection.ns, extractFieldsFromSchema(collection.jsonSchema)];
801846
})
802847
);
803848
return fields;

packages/compass-data-modeling/src/utils/nodes-and-edges.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '@mongodb-js/testing-library-compass';
99
import { getFieldsFromSchema } from '../utils/nodes-and-edges';
1010

11-
describe.only('getFieldsFromSchema', function () {
11+
describe('getFieldsFromSchema', function () {
1212
const validateMixedType = async (
1313
type: React.ReactNode,
1414
expectedTooltip: RegExp

packages/compass-data-modeling/src/utils/nodes-and-edges.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ export const traverseSchema = ({
8383
jsonSchema: MongoDBJSONSchema;
8484
visitor: ({
8585
fieldPath,
86-
fieldName,
8786
fieldTypes,
8887
}: {
8988
fieldPath: FieldPath;
90-
fieldName: string;
9189
fieldTypes: string[];
9290
}) => void;
9391
parentFieldPath?: FieldPath;
@@ -128,7 +126,6 @@ export const traverseSchema = ({
128126

129127
visitor({
130128
fieldPath: newFieldPath,
131-
fieldName: name,
132129
fieldTypes: types.flat(),
133130
});
134131

@@ -158,9 +155,9 @@ export const getFieldsFromSchema = ({
158155

159156
traverseSchema({
160157
jsonSchema,
161-
visitor: ({ fieldPath, fieldName, fieldTypes }) => {
158+
visitor: ({ fieldPath, fieldTypes }) => {
162159
fields.push({
163-
name: fieldName,
160+
name: fieldPath[fieldPath.length - 1],
164161
id: fieldPath,
165162
type: getFieldTypeDisplay(fieldTypes),
166163
depth: fieldPath.length - 1,

0 commit comments

Comments
 (0)