@@ -14,7 +14,7 @@ import {
1414 type StaticModel ,
1515} from '../services/data-model-storage' ;
1616import { AnalysisProcessActionTypes } from './analysis-process' ;
17- import { memoize } from 'lodash' ;
17+ import { memoize , update } from 'lodash' ;
1818import type { DataModelingState , DataModelingThunkAction } from './reducer' ;
1919import {
2020 openToast ,
@@ -29,14 +29,12 @@ import type { MongoDBJSONSchema } from 'mongodb-schema';
2929import { getCoordinatesForNewNode } from '@mongodb-js/diagramming' ;
3030import { collectionToDiagramNode } from '../utils/nodes-and-edges' ;
3131import toNS from 'mongodb-ns' ;
32- import {
33- removeFieldFromSchema ,
34- traverseSchema ,
35- updateSchema ,
36- } from '../utils/schema-traversal' ;
32+ import { traverseSchema , updateSchema } from '../utils/schema-traversal' ;
3733import {
3834 areFieldPathsEqual ,
3935 isRelationshipInvolvingField ,
36+ isRelationshipOfAField ,
37+ isSameFieldOrChild ,
4038} from '../utils/utils' ;
4139
4240function isNonEmptyArray < T > ( arr : T [ ] ) : arr is [ T , ...T [ ] ] {
@@ -755,6 +753,27 @@ export function addCollection(
755753 } ;
756754}
757755
756+ function renameFieldInRelationshipSide (
757+ side : Relationship [ 'relationship' ] [ 0 ] ,
758+ edit : Extract < Edit , { type : 'RenameField' } >
759+ ) : Relationship [ 'relationship' ] [ 0 ] {
760+ if (
761+ side . ns !== edit . ns ||
762+ ! side . fields ||
763+ ! isSameFieldOrChild ( side . fields , edit . from )
764+ ) {
765+ return side ;
766+ }
767+ return {
768+ ...side ,
769+ fields : [
770+ ...side . fields . slice ( 0 , edit . from . length - 1 ) ,
771+ edit . to ,
772+ ...side . fields . slice ( edit . from . length ) ,
773+ ] ,
774+ } ;
775+ }
776+
758777function _applyEdit ( edit : Edit , model ?: StaticModel ) : StaticModel {
759778 if ( edit . type === 'SetModel' ) {
760779 return edit . model ;
@@ -902,8 +921,18 @@ function _applyEdit(edit: Edit, model?: StaticModel): StaticModel {
902921 ...model ,
903922 // Update any relationships involving the field being renamed.
904923 relationships : model . relationships . map ( ( r ) => {
905- // TODO
906- return r ;
924+ if (
925+ ! isRelationshipInvolvingField ( r . relationship , edit . ns , edit . from )
926+ ) {
927+ return r ;
928+ }
929+ return {
930+ ...r ,
931+ relationship : [
932+ renameFieldInRelationshipSide ( r . relationship [ 0 ] , edit ) ,
933+ renameFieldInRelationshipSide ( r . relationship [ 1 ] , edit ) ,
934+ ] as const ,
935+ } ;
907936 } ) ,
908937 collections : model . collections . map ( ( collection ) => {
909938 if ( collection . ns !== edit . ns ) return collection ;
0 commit comments