@@ -10126,27 +10126,32 @@ var Store = /** @class */ (function () {
10126
10126
var _this = this ;
10127
10127
var insertedData ;
10128
10128
return __generator$1 ( this , function ( _a ) {
10129
- insertedData = { } ;
10130
- Object . keys ( data ) . forEach ( function ( key ) { return __awaiter$1 ( _this , void 0 , void 0 , function ( ) {
10131
- var value , newData ;
10132
- return __generator$1 ( this , function ( _a ) {
10133
- switch ( _a . label ) {
10134
- case 0 :
10135
- value = data [ key ] ;
10136
- Context . getInstance ( ) . logger . log ( 'Inserting records' , value ) ;
10137
- return [ 4 /*yield*/ , dispatch ( 'insertOrUpdate' , { data : value } ) ] ;
10138
- case 1 :
10139
- newData = _a . sent ( ) ;
10140
- Object . keys ( newData ) . forEach ( function ( dataKey ) {
10141
- if ( ! insertedData [ dataKey ] )
10142
- insertedData [ dataKey ] = [ ] ;
10143
- insertedData [ dataKey ] = insertedData [ dataKey ] . concat ( newData [ dataKey ] ) ;
10129
+ switch ( _a . label ) {
10130
+ case 0 :
10131
+ insertedData = { } ;
10132
+ return [ 4 /*yield*/ , Promise . all ( Object . keys ( data ) . map ( function ( key ) { return __awaiter$1 ( _this , void 0 , void 0 , function ( ) {
10133
+ var value , newData ;
10134
+ return __generator$1 ( this , function ( _a ) {
10135
+ switch ( _a . label ) {
10136
+ case 0 :
10137
+ value = data [ key ] ;
10138
+ Context . getInstance ( ) . logger . log ( 'Inserting records' , value ) ;
10139
+ return [ 4 /*yield*/ , dispatch ( 'insertOrUpdate' , { data : value } ) ] ;
10140
+ case 1 :
10141
+ newData = _a . sent ( ) ;
10142
+ Object . keys ( newData ) . forEach ( function ( dataKey ) {
10143
+ if ( ! insertedData [ dataKey ] )
10144
+ insertedData [ dataKey ] = [ ] ;
10145
+ insertedData [ dataKey ] = insertedData [ dataKey ] . concat ( newData [ dataKey ] ) ;
10146
+ } ) ;
10147
+ return [ 2 /*return*/ ] ;
10148
+ }
10144
10149
} ) ;
10145
- return [ 2 /*return*/ ] ;
10146
- }
10147
- } ) ;
10148
- } ) ; } ) ;
10149
- return [ 2 /*return*/ , insertedData ] ;
10150
+ } ) ; } ) ) ] ;
10151
+ case 1 :
10152
+ _a . sent ( ) ;
10153
+ return [ 2 /*return*/ , insertedData ] ;
10154
+ }
10150
10155
} ) ;
10151
10156
} ) ;
10152
10157
} ;
@@ -10238,7 +10243,7 @@ var Action = /** @class */ (function () {
10238
10243
Action . mutation = function ( name , variables , dispatch , model , multiple ) {
10239
10244
if ( multiple === void 0 ) { multiple = false ; }
10240
10245
return __awaiter$2 ( this , void 0 , void 0 , function ( ) {
10241
- var query , newData , insertedData ;
10246
+ var query , newData , insertedData , records , newRecord ;
10242
10247
return __generator$2 ( this , function ( _a ) {
10243
10248
switch ( _a . label ) {
10244
10249
case 0 :
@@ -10251,12 +10256,13 @@ var Action = /** @class */ (function () {
10251
10256
return [ 4 /*yield*/ , Store . insertData ( newData , dispatch ) ] ;
10252
10257
case 2 :
10253
10258
insertedData = _a . sent ( ) ;
10254
- // Try to find the record to return
10255
- if ( insertedData [ model . pluralName ] && insertedData [ model . pluralName ] [ 0 ] ) {
10256
- return [ 2 /*return*/ , insertedData [ model . pluralName ] [ insertedData [ model . pluralName ] . length - 1 ] ] ;
10259
+ records = insertedData [ model . pluralName ] ;
10260
+ newRecord = records [ records . length - 1 ] ;
10261
+ if ( newRecord ) {
10262
+ return [ 2 /*return*/ , newRecord ] ;
10257
10263
}
10258
10264
else {
10259
- Context . getInstance ( ) . logger . log ( "Couldn't find the record of type" , model . pluralName , 'in' , insertedData , '. Fallback to find()' ) ;
10265
+ Context . getInstance ( ) . logger . log ( "Couldn't find the record of type ' " , model . pluralName , "' within" , insertedData , '. Falling back to find()' ) ;
10260
10266
return [ 2 /*return*/ , model . baseModel . query ( ) . last ( ) ] ;
10261
10267
}
10262
10268
_a . label = 3 ;
@@ -10645,7 +10651,7 @@ var Persist = /** @class */ (function (_super) {
10645
10651
case 1 :
10646
10652
newRecord = _c . sent ( ) ;
10647
10653
// Delete the old record if necessary
10648
- return [ 4 /*yield*/ , this . deleteObsoleteRecord ( model , oldRecord ) ] ;
10654
+ return [ 4 /*yield*/ , this . deleteObsoleteRecord ( model , newRecord , oldRecord ) ] ;
10649
10655
case 2 :
10650
10656
// Delete the old record if necessary
10651
10657
_c . sent ( ) ;
@@ -10658,28 +10664,19 @@ var Persist = /** @class */ (function (_super) {
10658
10664
/**
10659
10665
* It's very likely that the server generated different ID for this record.
10660
10666
* In this case Action.mutation has inserted a new record instead of updating the existing one.
10661
- * We can see that because $isPersisted is still false then. In this case we just delete the old record.
10662
10667
*
10663
10668
* @param {Model } model
10664
10669
* @param {Data } record
10665
10670
* @returns {Promise<void> }
10666
10671
*/
10667
- Persist . deleteObsoleteRecord = function ( model , record ) {
10672
+ Persist . deleteObsoleteRecord = function ( model , newRecord , oldRecord ) {
10668
10673
return __awaiter$6 ( this , void 0 , void 0 , function ( ) {
10669
10674
return __generator$6 ( this , function ( _a ) {
10670
- switch ( _a . label ) {
10671
- case 0 :
10672
- if ( ! ( record && ! record . $isPersisted ) ) return [ 3 /*break*/ , 2 ] ;
10673
- // The server generated another ID, this is very likely to happen.
10674
- // in this case Action.mutation has inserted a new record instead of updating the existing one.
10675
- // We can see that because $isPersisted is still false then.
10676
- Context . getInstance ( ) . logger . log ( 'Dropping deprecated record with ID' , record . id ) ;
10677
- return [ 4 /*yield*/ , model . baseModel . delete ( { where : record . id } ) ] ;
10678
- case 1 :
10679
- _a . sent ( ) ;
10680
- _a . label = 2 ;
10681
- case 2 : return [ 2 /*return*/ ] ;
10675
+ if ( newRecord && oldRecord && newRecord . id !== oldRecord . id ) {
10676
+ Context . getInstance ( ) . logger . log ( 'Dropping deprecated record' , oldRecord ) ;
10677
+ return [ 2 /*return*/ , oldRecord . $delete ( ) ] ;
10682
10678
}
10679
+ return [ 2 /*return*/ ] ;
10683
10680
} ) ;
10684
10681
} ) ;
10685
10682
} ;
0 commit comments