@@ -501,11 +501,10 @@ const Utils = class {
501501 * @param {object } results - JSONAPI record
502502 */
503503 processIncludedRecords ( context , results ) {
504- for ( let item of get ( results , [ 'data' , 'included' ] , [ ] ) ) {
505- // Mark record as coming from included
506- const includedItem = this . jsonapiToNormItem ( item , 'isIncluded' )
507- context . commit ( 'mergeRecords' , includedItem )
508- }
504+ context . commit (
505+ 'mergeRecords' ,
506+ get ( results , [ 'data' , 'included' ] , [ ] ) . map ( ( item ) => this . jsonapiToNormItem ( item , 'isIncluded' ) )
507+ )
509508 }
510509
511510 /**
@@ -532,28 +531,34 @@ const Utils = class {
532531 * @param {object } records - Restructured records to be updated
533532 * @param {boolean } merging - Whether or not to merge or overwrite records
534533 */
535- updateRecords ( state , records , merging = this . conf . mergeRecords ) {
536- let newState = { }
534+ updateRecords ( state , records , mergeDefault = this . conf . mergeRecords ) {
537535 const storeRecords = this . normToStore ( records )
536+ let merging = { }
538537 for ( let [ type , item ] of Object . entries ( storeRecords ) ) {
539- newState [ type ] = { }
540- if ( ! this . hasProperty ( state , type ) ) {
541- state [ type ] = { }
542- // If there's no type, then there are no existing records to merge
543- merging = false
544- }
545- for ( let [ id , data ] of Object . entries ( item ) ) {
546- if ( merging ) {
547- const oldRecord = get ( state , [ type , id ] )
548- if ( oldRecord ) {
549- data = merge ( oldRecord , data )
538+ let newRecords = item
539+ if ( mergeDefault ) {
540+ if ( ! ( type in merging ) ) {
541+ merging [ type ] = true
542+ if ( ! this . hasProperty ( state , type ) ) {
543+ // If there's no type, then there are no existing records to merge with
544+ merging [ type ] = false
550545 }
551546 }
552- newState [ type ] [ id ] = data
547+ if ( merging [ type ] ) {
548+ newRecords = Object . fromEntries (
549+ Object . entries ( item ) . map ( ( [ id , data ] ) => {
550+ const oldRecord = get ( state , [ type , id ] )
551+ if ( oldRecord ) {
552+ data = merge ( oldRecord , data )
553+ }
554+ return [ id , data ]
555+ } )
556+ )
557+ }
553558 }
554559 // FIXME: Review with release of Vuex5 to see if there is a new ref()/reactive() approach
555560 // Maintain reactivity by 'touching' the 'root' state property
556- state [ type ] = Object . assign ( { } , state [ type ] , newState [ type ] )
561+ state [ type ] = Object . assign ( { } , state [ type ] , newRecords )
557562 }
558563 }
559564}
0 commit comments