@@ -10126,27 +10126,32 @@ var Store = /** @class */ (function () {
1012610126 var _this = this ;
1012710127 var insertedData ;
1012810128 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+ }
1014410149 } ) ;
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+ }
1015010155 } ) ;
1015110156 } ) ;
1015210157 } ;
@@ -10238,7 +10243,7 @@ var Action = /** @class */ (function () {
1023810243 Action . mutation = function ( name , variables , dispatch , model , multiple ) {
1023910244 if ( multiple === void 0 ) { multiple = false ; }
1024010245 return __awaiter$2 ( this , void 0 , void 0 , function ( ) {
10241- var query , newData , insertedData ;
10246+ var query , newData , insertedData , records , newRecord ;
1024210247 return __generator$2 ( this , function ( _a ) {
1024310248 switch ( _a . label ) {
1024410249 case 0 :
@@ -10251,12 +10256,13 @@ var Action = /** @class */ (function () {
1025110256 return [ 4 /*yield*/ , Store . insertData ( newData , dispatch ) ] ;
1025210257 case 2 :
1025310258 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 ] ;
1025710263 }
1025810264 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()' ) ;
1026010266 return [ 2 /*return*/ , model . baseModel . query ( ) . last ( ) ] ;
1026110267 }
1026210268 _a . label = 3 ;
@@ -10645,7 +10651,7 @@ var Persist = /** @class */ (function (_super) {
1064510651 case 1 :
1064610652 newRecord = _c . sent ( ) ;
1064710653 // Delete the old record if necessary
10648- return [ 4 /*yield*/ , this . deleteObsoleteRecord ( model , oldRecord ) ] ;
10654+ return [ 4 /*yield*/ , this . deleteObsoleteRecord ( model , newRecord , oldRecord ) ] ;
1064910655 case 2 :
1065010656 // Delete the old record if necessary
1065110657 _c . sent ( ) ;
@@ -10658,28 +10664,19 @@ var Persist = /** @class */ (function (_super) {
1065810664 /**
1065910665 * It's very likely that the server generated different ID for this record.
1066010666 * 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.
1066210667 *
1066310668 * @param {Model } model
1066410669 * @param {Data } record
1066510670 * @returns {Promise<void> }
1066610671 */
10667- Persist . deleteObsoleteRecord = function ( model , record ) {
10672+ Persist . deleteObsoleteRecord = function ( model , newRecord , oldRecord ) {
1066810673 return __awaiter$6 ( this , void 0 , void 0 , function ( ) {
1066910674 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 ( ) ] ;
1068210678 }
10679+ return [ 2 /*return*/ ] ;
1068310680 } ) ;
1068410681 } ) ;
1068510682 } ;
0 commit comments