@@ -470,34 +470,40 @@ export function serviceMixin(entity: Entity, transform: TransformMixin, connecti
470470 . relation ( relName )
471471 . of ( id ) ;
472472
473+ if ( relationObject . relationType === 'many-to-many' && ( body as BaseData [ ] ) . some ( i => i . attributes ) ) {
474+ const repoTarget = this . repository . manager . getRepository < EntitySchema > ( relationObject . junctionEntityMetadata . target ) ;
475+
476+ const entityTarget = repoTarget . target as any
477+ const ownerPropertyName = relationObject . junctionEntityMetadata . ownerColumns [ 0 ] . propertyName
478+ const inversePropertyName = relationObject . junctionEntityMetadata . inverseColumns [ 0 ] . propertyName
479+ const bodyParams = body as BaseData [ ] ;
480+
481+ const currentData = bodyParams . map ( ( body ) => {
482+ const target = new entityTarget ( ) ;
483+
484+ target [ ownerPropertyName ] = id
485+ target [ inversePropertyName ] = body . id
486+ if ( body . attributes ) {
487+ Object . entries ( body . attributes ) . forEach ( ( [ key , val ] ) => {
488+ target [ key ] = val ;
489+ } ) ;
490+ }
491+ return target ;
492+ } )
493+ const removeData = bodyParams . map ( body => ( {
494+ [ ownerPropertyName ] : id ,
495+ [ inversePropertyName ] : body . id
496+ } ) ) ;
497+ await repoTarget . createQueryBuilder ( ) . delete ( ) . where ( removeData ) . execute ( ) ;
498+ await repoTarget . save ( currentData ) ;
499+ return ;
500+ }
501+
473502 if ( [ 'one-to-many' , 'many-to-many' ] . includes ( relationObject . relationType ) ) {
474503 const currentEntities = await builderDeleteRelationships . loadMany ( ) ;
475504 const idsToDelete = currentEntities . map ( entity => entity . id ) ;
476505 const idsToAdd = body !== null ? ( body as BaseData [ ] ) . map ( i => i . id ) : [ ] ;
477-
478506 await builderDeleteRelationships . addAndRemove ( idsToAdd , idsToDelete ) ;
479-
480- if ( relationObject . relationType === 'many-to-many' && ( body as BaseData [ ] ) . some ( i => i . attributes ) ) {
481- const repoTarget = this . repository . manager . getRepository < EntitySchema > ( relationObject . junctionEntityMetadata . target ) ;
482- const currentData = await repoTarget . find ( {
483- where : {
484- [ relationObject . junctionEntityMetadata . ownColumns [ 0 ] . databasePath ] : id
485- }
486- } ) ;
487- const propertyName = relationObject . junctionEntityMetadata . inverseColumns [ 0 ] . propertyName
488- const bodyParams = body as BaseData [ ] ;
489- currentData . forEach ( ( item ) => {
490- bodyParams . forEach ( body => {
491- if ( body . attributes && parseInt ( item [ propertyName ] , 10 ) === parseInt ( body . id , 10 ) ) {
492- Object . entries ( body . attributes ) . forEach ( ( [ key , val ] ) => {
493- item [ key ] = val ;
494- } ) ;
495- }
496- } )
497- } ) ;
498- await repoTarget . save ( currentData ) ;
499- }
500-
501507 } else if ( body !== null ) {
502508 const { id } = Array . isArray ( body ) ? body . shift ( ) : body ;
503509 await builderDeleteRelationships . set ( id ) ;
@@ -580,6 +586,29 @@ export function serviceMixin(entity: Entity, transform: TransformMixin, connecti
580586 const relations = this . repository . metadata . relations . find ( item => {
581587 return item . propertyName === relName ;
582588 } ) ;
589+
590+ if ( relations . relationType === 'many-to-many' && ( body as BaseData [ ] ) . some ( item => item . attributes ) ) {
591+ const repoTarget = this . repository . manager . getRepository < EntitySchema > ( relations . junctionEntityMetadata . target ) ;
592+ const entityTarget = repoTarget . target as any
593+
594+ const ownerPropertyName = relations . junctionEntityMetadata . ownerColumns [ 0 ] . propertyName
595+ const inversePropertyName = relations . junctionEntityMetadata . inverseColumns [ 0 ] . propertyName
596+ const bodyParams = body as BaseData [ ] ;
597+ const currentData = bodyParams . map ( ( body ) => {
598+ const target = new entityTarget ( entityTarget ) ;
599+ target [ ownerPropertyName ] = id
600+ target [ inversePropertyName ] = body . id
601+ if ( body . attributes ) {
602+ Object . entries ( body . attributes ) . forEach ( ( [ key , val ] ) => {
603+ target [ key ] = val ;
604+ } ) ;
605+ }
606+ return target ;
607+ } )
608+ await repoTarget . save ( currentData ) ;
609+ return ;
610+ }
611+
583612 const postBuilder = this . repository . createQueryBuilder ( )
584613 . relation ( relName )
585614 . of ( id ) ;
@@ -590,26 +619,6 @@ export function serviceMixin(entity: Entity, transform: TransformMixin, connecti
590619 const { id } = Array . isArray ( body ) ? body . shift ( ) : body ;
591620 await postBuilder . set ( id ) ;
592621 }
593- if ( relations . relationType === 'many-to-many' && ( body as BaseData [ ] ) . some ( item => item . attributes ) ) {
594- const repoTarget = this . repository . manager . getRepository < EntitySchema > ( relations . junctionEntityMetadata . target ) ;
595- const currentData = await repoTarget . find ( {
596- where : {
597- [ relations . junctionEntityMetadata . ownColumns [ 0 ] . databasePath ] : id
598- }
599- } ) ;
600- const propertyName = relations . junctionEntityMetadata . inverseColumns [ 0 ] . propertyName
601- const bodyParams = body as BaseData [ ] ;
602- currentData . forEach ( ( item ) => {
603- bodyParams . forEach ( body => {
604- if ( body . attributes && parseInt ( item [ propertyName ] , 10 ) === parseInt ( body . id , 10 ) ) {
605- Object . entries ( body . attributes ) . forEach ( ( [ key , val ] ) => {
606- item [ key ] = val ;
607- } ) ;
608- }
609- } )
610- } ) ;
611- await repoTarget . save ( currentData ) ;
612- }
613622
614623 }
615624
0 commit comments