@@ -29,11 +29,8 @@ 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 { traverseSchema , updateSchema } from '../utils/schema-traversal' ;
33- import {
34- isRelationshipInvolvingField ,
35- isSameFieldOrAncestor ,
36- } from '../utils/utils' ;
32+ import { traverseSchema } from '../utils/schema-traversal' ;
33+ import { applyEdit as _applyEdit } from './apply-edit' ;
3734
3835function isNonEmptyArray < T > ( arr : T [ ] ) : arr is [ T , ...T [ ] ] {
3936 return Array . isArray ( arr ) && arr . length > 0 ;
@@ -761,207 +758,6 @@ export function addCollection(
761758 } ;
762759}
763760
764- function renameFieldInRelationshipSide (
765- side : Relationship [ 'relationship' ] [ 0 ] ,
766- edit : Extract < Edit , { type : 'RenameField' } >
767- ) : Relationship [ 'relationship' ] [ 0 ] {
768- if (
769- side . ns !== edit . ns ||
770- ! side . fields ||
771- ! isSameFieldOrAncestor ( edit . field , side . fields )
772- ) {
773- return side ;
774- }
775- return {
776- ...side ,
777- fields : [
778- ...side . fields . slice ( 0 , edit . field . length - 1 ) ,
779- edit . newName ,
780- ...side . fields . slice ( edit . field . length ) ,
781- ] ,
782- } ;
783- }
784-
785- function _applyEdit ( edit : Edit , model ?: StaticModel ) : StaticModel {
786- if ( edit . type === 'SetModel' ) {
787- return edit . model ;
788- }
789- if ( ! model ) {
790- throw new Error ( 'Editing a model that has not been initialized' ) ;
791- }
792- switch ( edit . type ) {
793- case 'AddCollection' : {
794- const newCollection : DataModelCollection = {
795- ns : edit . ns ,
796- jsonSchema : edit . initialSchema ,
797- displayPosition : edit . position ,
798- indexes : [ ] ,
799- } ;
800- return {
801- ...model ,
802- collections : [ ...model . collections , newCollection ] ,
803- } ;
804- }
805- case 'AddRelationship' : {
806- return {
807- ...model ,
808- relationships : [ ...model . relationships , edit . relationship ] ,
809- } ;
810- }
811- case 'RemoveRelationship' : {
812- return {
813- ...model ,
814- relationships : model . relationships . filter (
815- ( relationship ) => relationship . id !== edit . relationshipId
816- ) ,
817- } ;
818- }
819- case 'UpdateRelationship' : {
820- const existingRelationship = model . relationships . find ( ( r ) => {
821- return r . id === edit . relationship . id ;
822- } ) ;
823- if ( ! existingRelationship ) {
824- throw new Error ( 'Can not update non-existent relationship' ) ;
825- }
826- return {
827- ...model ,
828- relationships : model . relationships . map ( ( r ) => {
829- return r === existingRelationship ? edit . relationship : r ;
830- } ) ,
831- } ;
832- }
833- case 'MoveCollection' : {
834- return {
835- ...model ,
836- collections : model . collections . map ( ( collection ) => {
837- if ( collection . ns === edit . ns ) {
838- return {
839- ...collection ,
840- displayPosition : edit . newPosition ,
841- } ;
842- }
843- return collection ;
844- } ) ,
845- } ;
846- }
847- case 'RemoveCollection' : {
848- return {
849- ...model ,
850- // Remove any relationships involving the collection being removed.
851- relationships : model . relationships . filter ( ( r ) => {
852- return ! (
853- r . relationship [ 0 ] . ns === edit . ns || r . relationship [ 1 ] . ns === edit . ns
854- ) ;
855- } ) ,
856- collections : model . collections . filter (
857- ( collection ) => collection . ns !== edit . ns
858- ) ,
859- } ;
860- }
861- case 'RenameCollection' : {
862- return {
863- ...model ,
864- // Update relationships to point to the renamed namespace.
865- relationships : model . relationships . map ( ( relationship ) => {
866- const [ local , foreign ] = relationship . relationship ;
867-
868- return {
869- ...relationship ,
870- relationship : [
871- {
872- ...local ,
873- ns : local . ns === edit . fromNS ? edit . toNS : local . ns ,
874- } ,
875- {
876- ...foreign ,
877- ns : foreign . ns === edit . fromNS ? edit . toNS : foreign . ns ,
878- } ,
879- ] ,
880- } ;
881- } ) ,
882- collections : model . collections . map ( ( collection ) => ( {
883- ...collection ,
884- // Rename the collection.
885- ns : collection . ns === edit . fromNS ? edit . toNS : collection . ns ,
886- } ) ) ,
887- } ;
888- }
889- case 'UpdateCollectionNote' : {
890- return {
891- ...model ,
892- collections : model . collections . map ( ( collection ) => {
893- if ( collection . ns === edit . ns ) {
894- return {
895- ...collection ,
896- note : edit . note ,
897- } ;
898- }
899- return collection ;
900- } ) ,
901- } ;
902- }
903- case 'RemoveField' : {
904- return {
905- ...model ,
906- // Remove any relationships involving the field being removed.
907- relationships : model . relationships . filter ( ( { relationship } ) => {
908- return ! isRelationshipInvolvingField (
909- relationship ,
910- edit . ns ,
911- edit . field
912- ) ;
913- } ) ,
914- collections : model . collections . map ( ( collection ) => {
915- if ( collection . ns !== edit . ns ) return collection ;
916- return {
917- ...collection ,
918- jsonSchema : updateSchema ( {
919- jsonSchema : collection . jsonSchema ,
920- fieldPath : edit . field ,
921- update : 'removeField' ,
922- } ) ,
923- } ;
924- } ) ,
925- } ;
926- }
927- case 'RenameField' : {
928- return {
929- ...model ,
930- // Update any relationships involving the field being renamed.
931- relationships : model . relationships . map ( ( r ) => {
932- if (
933- ! isRelationshipInvolvingField ( r . relationship , edit . ns , edit . field )
934- ) {
935- return r ;
936- }
937- return {
938- ...r ,
939- relationship : [
940- renameFieldInRelationshipSide ( r . relationship [ 0 ] , edit ) ,
941- renameFieldInRelationshipSide ( r . relationship [ 1 ] , edit ) ,
942- ] as const ,
943- } ;
944- } ) ,
945- collections : model . collections . map ( ( collection ) => {
946- if ( collection . ns !== edit . ns ) return collection ;
947- return {
948- ...collection ,
949- jsonSchema : updateSchema ( {
950- jsonSchema : collection . jsonSchema ,
951- fieldPath : edit . field ,
952- update : 'renameField' ,
953- newFieldName : edit . newName ,
954- } ) ,
955- } ;
956- } ) ,
957- } ;
958- }
959- default : {
960- return model ;
961- }
962- }
963- }
964-
965761/**
966762 * @internal Exported for testing purposes only, use `selectCurrentModel` or
967763 * `selectCurrentModelFromState` instead
0 commit comments