Skip to content

Commit eee718c

Browse files
committed
Return new records
1 parent cd35b97 commit eee718c

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8340,7 +8340,7 @@ var VuexORMApollo = /** @class */ (function () {
83408340
var state = _a.state, dispatch = _a.dispatch;
83418341
var id = _b.id, args = _b.args;
83428342
return __awaiter(this, void 0, void 0, function () {
8343-
var model, data, mutationName, oldRecord;
8343+
var model, data, mutationName, newRecord, oldRecord;
83448344
return __generator(this, function (_c) {
83458345
switch (_c.label) {
83468346
case 0:
@@ -8352,7 +8352,7 @@ var VuexORMApollo = /** @class */ (function () {
83528352
mutationName = "create" + upcaseFirstLetter(model.singularName);
83538353
return [4 /*yield*/, this.mutate(mutationName, args, dispatch, model, false)];
83548354
case 1:
8355-
_c.sent();
8355+
newRecord = _c.sent();
83568356
oldRecord = model.baseModel.getters('find')(id);
83578357
if (!(oldRecord && !oldRecord.$isPersisted)) return [3 /*break*/, 3];
83588358
// The server generated another ID, this is very likely to happen.
@@ -8363,9 +8363,7 @@ var VuexORMApollo = /** @class */ (function () {
83638363
case 2:
83648364
_c.sent();
83658365
_c.label = 3;
8366-
case 3:
8367-
// TODO is this save?
8368-
return [2 /*return*/, model.baseModel.getters('query')().withAll().last()];
8366+
case 3: return [2 /*return*/, newRecord];
83698367
case 4: return [2 /*return*/];
83708368
}
83718369
});
@@ -8416,20 +8414,15 @@ var VuexORMApollo = /** @class */ (function () {
84168414
return __awaiter(this, void 0, void 0, function () {
84178415
var model, mutationName;
84188416
return __generator(this, function (_c) {
8419-
switch (_c.label) {
8420-
case 0:
8421-
if (!data) return [3 /*break*/, 2];
8422-
model = this.context.getModel(state.$name);
8423-
args = args || {};
8424-
args['id'] = data.id;
8425-
args[model.singularName] = this.queryBuilder.transformOutgoingData(model, data);
8426-
mutationName = "update" + upcaseFirstLetter(model.singularName);
8427-
return [4 /*yield*/, this.mutate(mutationName, args, dispatch, model, false)];
8428-
case 1:
8429-
_c.sent();
8430-
return [2 /*return*/, model.baseModel.getters('find')(data.id)];
8431-
case 2: return [2 /*return*/];
8417+
if (data) {
8418+
model = this.context.getModel(state.$name);
8419+
args = args || {};
8420+
args['id'] = data.id;
8421+
args[model.singularName] = this.queryBuilder.transformOutgoingData(model, data);
8422+
mutationName = "update" + upcaseFirstLetter(model.singularName);
8423+
return [2 /*return*/, this.mutate(mutationName, args, dispatch, model, false)];
84328424
}
8425+
return [2 /*return*/];
84338426
});
84348427
});
84358428
};
@@ -8470,7 +8463,7 @@ var VuexORMApollo = /** @class */ (function () {
84708463
*/
84718464
VuexORMApollo.prototype.mutate = function (name, variables, dispatch, model, multiple) {
84728465
return __awaiter(this, void 0, void 0, function () {
8473-
var id, query, newData;
8466+
var id, query, newData, insertedData;
84748467
return __generator(this, function (_a) {
84758468
switch (_a.label) {
84768469
case 0:
@@ -8483,8 +8476,8 @@ var VuexORMApollo = /** @class */ (function () {
84838476
if (!(name !== "delete" + upcaseFirstLetter(model.singularName))) return [3 /*break*/, 3];
84848477
return [4 /*yield*/, this.insertData(newData, dispatch)];
84858478
case 2:
8486-
_a.sent();
8487-
return [2 /*return*/, true]; // FIXME RETURN THE NEW RECORD!!
8479+
insertedData = _a.sent();
8480+
return [2 /*return*/, insertedData[model.pluralName][0]];
84888481
case 3: return [2 /*return*/, true];
84898482
case 4: return [2 /*return*/];
84908483
}
@@ -8535,22 +8528,29 @@ var VuexORMApollo = /** @class */ (function () {
85358528
VuexORMApollo.prototype.insertData = function (data, dispatch) {
85368529
return __awaiter(this, void 0, void 0, function () {
85378530
var _this = this;
8531+
var insertedData;
85388532
return __generator(this, function (_a) {
8533+
insertedData = {};
85398534
Object.keys(data).forEach(function (key) { return __awaiter(_this, void 0, void 0, function () {
8540-
var value;
8535+
var value, newData;
85418536
return __generator(this, function (_a) {
85428537
switch (_a.label) {
85438538
case 0:
85448539
value = data[key];
85458540
this.context.logger.log('Inserting records', value);
85468541
return [4 /*yield*/, dispatch('insertOrUpdate', { data: value })];
85478542
case 1:
8548-
_a.sent();
8543+
newData = _a.sent();
8544+
Object.keys(newData).forEach(function (dataKey) {
8545+
if (!insertedData[dataKey])
8546+
insertedData[dataKey] = [];
8547+
insertedData[dataKey] = insertedData[dataKey].concat(newData[dataKey]);
8548+
});
85498549
return [2 /*return*/];
85508550
}
85518551
});
85528552
}); });
8553-
return [2 /*return*/];
8553+
return [2 /*return*/, insertedData];
85548554
});
85558555
});
85568556
};

src/interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export interface ORMModel {
3737
dispatch (name: string, ...params: Array<any>): any;
3838
getters (name: string, ...params: Array<any>): any;
3939
attr (defaultValue: any): Field;
40+
boolean (defaultValue: boolean): Field;
41+
number (defaultValue: number): Field;
42+
string (defaultValue: string): Field;
4043
}
4144

4245
export interface Field {

src/vuex-orm-apollo.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default class VuexORMApollo {
118118
args[model.singularName] = this.queryBuilder.transformOutgoingData(model, data);
119119

120120
const mutationName = `create${upcaseFirstLetter(model.singularName)}`;
121-
await this.mutate(mutationName, args, dispatch, model, false);
121+
const newRecord = await this.mutate(mutationName, args, dispatch, model, false);
122122

123123
const oldRecord = model.baseModel.getters('find')(id);
124124

@@ -130,8 +130,7 @@ export default class VuexORMApollo {
130130
await model.baseModel.dispatch('delete', { where: oldRecord.id });
131131
}
132132

133-
// TODO is this save?
134-
return model.baseModel.getters('query')().withAll().last();
133+
return newRecord;
135134
}
136135
}
137136

@@ -181,9 +180,7 @@ export default class VuexORMApollo {
181180
args[model.singularName] = this.queryBuilder.transformOutgoingData(model, data);
182181

183182
const mutationName = `update${upcaseFirstLetter(model.singularName)}`;
184-
await this.mutate(mutationName, args, dispatch, model, false);
185-
186-
return model.baseModel.getters('find')(data.id);
183+
return this.mutate(mutationName, args, dispatch, model, false);
187184
}
188185
}
189186

@@ -226,8 +223,8 @@ export default class VuexORMApollo {
226223
const newData = await this.apolloRequest(model, query, variables, true);
227224

228225
if (name !== `delete${upcaseFirstLetter(model.singularName)}`) {
229-
await this.insertData(newData, dispatch);
230-
return true; // FIXME RETURN THE NEW RECORD!!
226+
const insertedData: Data = await this.insertData(newData, dispatch);
227+
return insertedData[model.pluralName][0];
231228
}
232229

233230
return true;
@@ -266,11 +263,20 @@ export default class VuexORMApollo {
266263
* @param {Data} data New data to insert/update
267264
* @param {Function} dispatch Vuex Dispatch method for the model
268265
*/
269-
private async insertData (data: Data, dispatch: DispatchFunction) {
266+
private async insertData (data: Data, dispatch: DispatchFunction): Promise<Data> {
267+
let insertedData: Data = {};
268+
270269
Object.keys(data).forEach(async (key) => {
271270
const value = data[key];
272271
this.context.logger.log('Inserting records', value);
273-
await dispatch('insertOrUpdate', { data: value });
272+
const newData = await dispatch('insertOrUpdate', { data: value });
273+
274+
Object.keys(newData).forEach((dataKey) => {
275+
if (!insertedData[dataKey]) insertedData[dataKey] = [];
276+
insertedData[dataKey] = insertedData[dataKey].concat(newData[dataKey]);
277+
});
274278
});
279+
280+
return insertedData;
275281
}
276282
}

0 commit comments

Comments
 (0)