Skip to content

Commit 5a0478c

Browse files
committed
Bugfixes
1 parent 3c53fda commit 5a0478c

File tree

4 files changed

+50
-56
lines changed

4 files changed

+50
-56
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

src/actions/action.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import QueryBuilder from '../graphql/query-builder';
22
import Context from '../common/context';
33
import { Store } from '../orm/store';
44
import { Arguments, Data, DispatchFunction } from '../support/interfaces';
5-
import { upcaseFirstLetter } from '../support/utils';
65
import Model from '../orm/model';
76
import State from '@vuex-orm/core/lib/modules/State';
87
import Transformer from '../graphql/transformer';
@@ -37,11 +36,13 @@ export default class Action {
3736
const insertedData: Data = await Store.insertData(newData, dispatch);
3837

3938
// Try to find the record to return
40-
if (insertedData[model.pluralName] && insertedData[model.pluralName][0]) {
41-
return insertedData[model.pluralName][insertedData[model.pluralName].length - 1];
39+
const records = insertedData[model.pluralName];
40+
const newRecord = records[records.length - 1];
41+
if (newRecord) {
42+
return newRecord;
4243
} else {
43-
Context.getInstance().logger.log("Couldn't find the record of type", model.pluralName, 'in', insertedData,
44-
'. Fallback to find()');
44+
Context.getInstance().logger.log("Couldn't find the record of type '", model.pluralName, "' within",
45+
insertedData, '. Falling back to find()');
4546
return model.baseModel.query().last();
4647
}
4748
}

src/actions/persist.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class Persist extends Action {
2828
const newRecord = await Action.mutation(mutationName, args, dispatch, model);
2929

3030
// Delete the old record if necessary
31-
await this.deleteObsoleteRecord(model, oldRecord);
31+
await this.deleteObsoleteRecord(model, newRecord, oldRecord);
3232

3333
return newRecord;
3434
} else {
@@ -39,19 +39,15 @@ export default class Persist extends Action {
3939
/**
4040
* It's very likely that the server generated different ID for this record.
4141
* In this case Action.mutation has inserted a new record instead of updating the existing one.
42-
* We can see that because $isPersisted is still false then. In this case we just delete the old record.
4342
*
4443
* @param {Model} model
4544
* @param {Data} record
4645
* @returns {Promise<void>}
4746
*/
48-
private static async deleteObsoleteRecord (model: Model, record: Data) {
49-
if (record && !record.$isPersisted) {
50-
// The server generated another ID, this is very likely to happen.
51-
// in this case Action.mutation has inserted a new record instead of updating the existing one.
52-
// We can see that because $isPersisted is still false then.
53-
Context.getInstance().logger.log('Dropping deprecated record with ID', record.id);
54-
await model.baseModel.delete({ where: record.id });
47+
private static async deleteObsoleteRecord (model: Model, newRecord: Data, oldRecord: Data) {
48+
if (newRecord && oldRecord && newRecord.id !== oldRecord.id) {
49+
Context.getInstance().logger.log('Dropping deprecated record', oldRecord);
50+
return oldRecord.$delete();
5551
}
5652
}
5753
}

src/orm/store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class Store {
1515
public static async insertData (data: Data, dispatch: DispatchFunction): Promise<Data> {
1616
let insertedData: Data = {};
1717

18-
Object.keys(data).forEach(async (key) => {
18+
await Promise.all(Object.keys(data).map(async (key) => {
1919
const value = data[key];
2020
Context.getInstance().logger.log('Inserting records', value);
2121
const newData = await dispatch('insertOrUpdate', { data: value });
@@ -24,7 +24,7 @@ export class Store {
2424
if (!insertedData[dataKey]) insertedData[dataKey] = [];
2525
insertedData[dataKey] = insertedData[dataKey].concat(newData[dataKey]);
2626
});
27-
});
27+
}));
2828

2929
return insertedData;
3030
}

0 commit comments

Comments
 (0)