dataflux 1.14.4 → 1.14.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BasicObj.js +23 -38
- package/dist/Model.js +175 -250
- package/dist/Obj.js +28 -33
- package/dist/ObserverStore.js +96 -121
- package/dist/PersistentStore.js +9 -10
- package/dist/PubSub.js +1 -1
- package/dist/ReactStore.js +35 -41
- package/dist/Store.js +58 -77
- package/dist/SubObj.js +26 -37
- package/dist/dataflux.min.js +1 -1
- package/dist/dataflux.min.js.map +1 -1
- package/dist/modelHooksUtils.js +1 -1
- package/package.json +10 -10
package/dist/Model.js
CHANGED
|
@@ -24,16 +24,13 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
24
24
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
25
25
|
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
|
|
26
26
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
27
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i :
|
|
27
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
28
28
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
29
29
|
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
30
30
|
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
31
|
-
function
|
|
32
|
-
function
|
|
33
|
-
function
|
|
34
|
-
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
|
35
|
-
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
36
|
-
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } } /*
|
|
31
|
+
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
|
|
32
|
+
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
|
|
33
|
+
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } /*
|
|
37
34
|
* MIT License
|
|
38
35
|
*
|
|
39
36
|
* Copyright (c) 2022 Massimo Candela <https://massimocandela.com>
|
|
@@ -68,7 +65,7 @@ var _batchSize = /*#__PURE__*/new WeakMap();
|
|
|
68
65
|
var _axios = /*#__PURE__*/new WeakMap();
|
|
69
66
|
var _loadFunction = /*#__PURE__*/new WeakMap();
|
|
70
67
|
var _hiddenFields = /*#__PURE__*/new WeakMap();
|
|
71
|
-
var
|
|
68
|
+
var _Model_brand = /*#__PURE__*/new WeakSet();
|
|
72
69
|
var _addRelationByField = /*#__PURE__*/new WeakMap();
|
|
73
70
|
var _addRelationByFilter = /*#__PURE__*/new WeakMap();
|
|
74
71
|
var _removeHiddenFields = /*#__PURE__*/new WeakMap();
|
|
@@ -89,60 +86,24 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
89
86
|
_options$autoRefresh;
|
|
90
87
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
91
88
|
_classCallCheck(this, Model);
|
|
92
|
-
_classPrivateMethodInitSpec(this,
|
|
93
|
-
_classPrivateFieldInitSpec(this, _type,
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
_classPrivateFieldInitSpec(this,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
_classPrivateFieldInitSpec(this,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
_classPrivateFieldInitSpec(this, _retrieveHook, {
|
|
106
|
-
writable: true,
|
|
107
|
-
value: void 0
|
|
108
|
-
});
|
|
109
|
-
_classPrivateFieldInitSpec(this, _updateHook, {
|
|
110
|
-
writable: true,
|
|
111
|
-
value: void 0
|
|
112
|
-
});
|
|
113
|
-
_classPrivateFieldInitSpec(this, _insertHook, {
|
|
114
|
-
writable: true,
|
|
115
|
-
value: void 0
|
|
116
|
-
});
|
|
117
|
-
_classPrivateFieldInitSpec(this, _deleteHook, {
|
|
118
|
-
writable: true,
|
|
119
|
-
value: void 0
|
|
120
|
-
});
|
|
121
|
-
_classPrivateFieldInitSpec(this, _singleItemQuery, {
|
|
122
|
-
writable: true,
|
|
123
|
-
value: void 0
|
|
124
|
-
});
|
|
125
|
-
_classPrivateFieldInitSpec(this, _batchSize, {
|
|
126
|
-
writable: true,
|
|
127
|
-
value: void 0
|
|
128
|
-
});
|
|
129
|
-
_classPrivateFieldInitSpec(this, _axios, {
|
|
130
|
-
writable: true,
|
|
131
|
-
value: void 0
|
|
132
|
-
});
|
|
133
|
-
_classPrivateFieldInitSpec(this, _loadFunction, {
|
|
134
|
-
writable: true,
|
|
135
|
-
value: void 0
|
|
136
|
-
});
|
|
137
|
-
_classPrivateFieldInitSpec(this, _hiddenFields, {
|
|
138
|
-
writable: true,
|
|
139
|
-
value: void 0
|
|
140
|
-
});
|
|
89
|
+
_classPrivateMethodInitSpec(this, _Model_brand);
|
|
90
|
+
_classPrivateFieldInitSpec(this, _type, void 0);
|
|
91
|
+
_classPrivateFieldInitSpec(this, _store, void 0);
|
|
92
|
+
_classPrivateFieldInitSpec(this, _includes, void 0);
|
|
93
|
+
_classPrivateFieldInitSpec(this, _retrieveHook, void 0);
|
|
94
|
+
_classPrivateFieldInitSpec(this, _updateHook, void 0);
|
|
95
|
+
_classPrivateFieldInitSpec(this, _insertHook, void 0);
|
|
96
|
+
_classPrivateFieldInitSpec(this, _deleteHook, void 0);
|
|
97
|
+
_classPrivateFieldInitSpec(this, _singleItemQuery, void 0);
|
|
98
|
+
_classPrivateFieldInitSpec(this, _batchSize, void 0);
|
|
99
|
+
_classPrivateFieldInitSpec(this, _axios, void 0);
|
|
100
|
+
_classPrivateFieldInitSpec(this, _loadFunction, void 0);
|
|
101
|
+
_classPrivateFieldInitSpec(this, _hiddenFields, void 0);
|
|
141
102
|
_defineProperty(this, "validateObjectAttribute", function (object, key) {
|
|
142
103
|
var validate = _this.options.validate;
|
|
143
104
|
if (validate && validate[key]) {
|
|
144
105
|
try {
|
|
145
|
-
var call = validate[key](object, _classPrivateFieldGet(
|
|
106
|
+
var call = validate[key](object, _classPrivateFieldGet(_store, _this));
|
|
146
107
|
if (call !== null && call !== void 0 && call.then) {
|
|
147
108
|
call.then(function () {
|
|
148
109
|
return object.setError(false, key);
|
|
@@ -169,26 +130,26 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
169
130
|
return true;
|
|
170
131
|
});
|
|
171
132
|
_defineProperty(this, "getStore", function () {
|
|
172
|
-
return _classPrivateFieldGet(
|
|
133
|
+
return _classPrivateFieldGet(_store, _this);
|
|
173
134
|
});
|
|
174
135
|
_defineProperty(this, "setStore", function (store) {
|
|
175
|
-
if (!_classPrivateFieldGet(
|
|
136
|
+
if (!_classPrivateFieldGet(_store, _this)) {
|
|
176
137
|
var _classPrivateFieldGet2;
|
|
177
|
-
_classPrivateFieldSet(
|
|
178
|
-
_classPrivateFieldSet(
|
|
138
|
+
_classPrivateFieldSet(_store, _this, store);
|
|
139
|
+
_classPrivateFieldSet(_axios, _this, _this.options.axios || ((_classPrivateFieldGet2 = _classPrivateFieldGet(_store, _this)) === null || _classPrivateFieldGet2 === void 0 || (_classPrivateFieldGet2 = _classPrivateFieldGet2.options) === null || _classPrivateFieldGet2 === void 0 ? void 0 : _classPrivateFieldGet2.axios) || _redaxios["default"]);
|
|
179
140
|
} else {
|
|
180
141
|
throw new Error("This model was already assigned to a store.");
|
|
181
142
|
}
|
|
182
143
|
});
|
|
183
144
|
_defineProperty(this, "load", function (obj) {
|
|
184
|
-
if (_classPrivateFieldGet(
|
|
145
|
+
if (_classPrivateFieldGet(_loadFunction, _this)) {
|
|
185
146
|
return _this.getStore().whenSaved(_this.getType())["catch"](function (e) {
|
|
186
147
|
throw new Error("You cannot perform load() on an unsaved object.");
|
|
187
148
|
}).then(function () {
|
|
188
|
-
var res = _classPrivateFieldGet(
|
|
149
|
+
var res = _classPrivateFieldGet(_loadFunction, _this).call(_this, obj.toJSON()); // toJSON to avoid side effects;
|
|
189
150
|
|
|
190
151
|
if (typeof res === "string") {
|
|
191
|
-
return _classPrivateFieldGet(
|
|
152
|
+
return _classPrivateFieldGet(_axios, _this).call(_this, {
|
|
192
153
|
method: "get",
|
|
193
154
|
url: res,
|
|
194
155
|
responseType: "json"
|
|
@@ -202,10 +163,10 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
202
163
|
(0, _BasicObj.setValues)(data, _this, _SubObj["default"], obj, obj);
|
|
203
164
|
return data;
|
|
204
165
|
})["catch"](function (error) {
|
|
205
|
-
return
|
|
166
|
+
return _assertClassBrand(_Model_brand, _this, _error).call(_this, error);
|
|
206
167
|
});
|
|
207
168
|
} else {
|
|
208
|
-
return
|
|
169
|
+
return _assertClassBrand(_Model_brand, _this, _error).call(_this, "You must define a loading function in the model to enable load().");
|
|
209
170
|
}
|
|
210
171
|
});
|
|
211
172
|
_defineProperty(this, "addRelation", function (model, param2, param3) {
|
|
@@ -213,13 +174,13 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
213
174
|
_this.getStore().validateModel(model);
|
|
214
175
|
if (typeof param2 === "string" && (!param3 || typeof param3 === "string")) {
|
|
215
176
|
// explicit model, from, to
|
|
216
|
-
return _classPrivateFieldGet(
|
|
177
|
+
return _classPrivateFieldGet(_addRelationByField, _this).call(_this, model, param2, param3);
|
|
217
178
|
} else if (!param3 && typeof param2 === "function") {
|
|
218
179
|
// explicit model, filterFunction
|
|
219
|
-
return _classPrivateFieldGet(
|
|
180
|
+
return _classPrivateFieldGet(_addRelationByFilter, _this).call(_this, model, param2);
|
|
220
181
|
} else if (!param2 && !param3) {
|
|
221
182
|
// implicit model, from, to (it uses the type as local key and the id as remote key)
|
|
222
|
-
return _classPrivateFieldGet(
|
|
183
|
+
return _classPrivateFieldGet(_addRelationByField, _this).call(_this, model, model.getType(), "id");
|
|
223
184
|
} else {
|
|
224
185
|
throw new Error("Invalid relation declaration");
|
|
225
186
|
}
|
|
@@ -228,7 +189,7 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
228
189
|
}
|
|
229
190
|
});
|
|
230
191
|
_defineProperty(this, "getRelation", function (parentObject, includedType, filterFunction) {
|
|
231
|
-
var filterRelation = _classPrivateFieldGet(
|
|
192
|
+
var filterRelation = _classPrivateFieldGet(_includes, _this)[includedType];
|
|
232
193
|
if (filterRelation) {
|
|
233
194
|
return (parentObject.getModel().options.load ? parentObject.load()["catch"](function () {}) : Promise.resolve()).then(function () {
|
|
234
195
|
return _this.getStore().find(includedType, function (item) {
|
|
@@ -238,215 +199,179 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
238
199
|
});
|
|
239
200
|
});
|
|
240
201
|
} else {
|
|
241
|
-
return
|
|
202
|
+
return _assertClassBrand(_Model_brand, _this, _error).call(_this, "The relation doesn't exist");
|
|
242
203
|
}
|
|
243
204
|
});
|
|
244
205
|
_defineProperty(this, "getType", function () {
|
|
245
|
-
return _classPrivateFieldGet(
|
|
206
|
+
return _classPrivateFieldGet(_type, _this);
|
|
246
207
|
});
|
|
247
208
|
_defineProperty(this, "retrieveAll", function () {
|
|
248
|
-
return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(
|
|
209
|
+
return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(_retrieveHook, _this), null, _classPrivateFieldGet(_axios, _this)).then(function (data) {
|
|
249
210
|
if (!Array.isArray(data)) {
|
|
250
|
-
_classPrivateFieldSet(
|
|
211
|
+
_classPrivateFieldSet(_singleItemQuery, _this, true);
|
|
251
212
|
}
|
|
252
|
-
return _classPrivateFieldGet(
|
|
213
|
+
return _classPrivateFieldGet(_toArray, _this).call(_this, data);
|
|
253
214
|
});
|
|
254
215
|
});
|
|
255
216
|
_defineProperty(this, "insertObjects", function (objects) {
|
|
256
|
-
return objects.length ? _classPrivateFieldGet(
|
|
217
|
+
return objects.length ? _classPrivateFieldGet(_insertObjects, _this).call(_this, objects) : Promise.resolve();
|
|
257
218
|
});
|
|
258
219
|
_defineProperty(this, "updateObjects", function (objects) {
|
|
259
|
-
return objects.length ? _classPrivateFieldGet(
|
|
220
|
+
return objects.length ? _classPrivateFieldGet(_updateObjects, _this).call(_this, objects) : Promise.resolve();
|
|
260
221
|
});
|
|
261
222
|
_defineProperty(this, "deleteObjects", function (objects) {
|
|
262
|
-
return objects.length ? _classPrivateFieldGet(
|
|
223
|
+
return objects.length ? _classPrivateFieldGet(_deleteObjects, _this).call(_this, objects) : Promise.resolve();
|
|
263
224
|
});
|
|
264
225
|
_defineProperty(this, "factory", function (params) {
|
|
265
226
|
if (!_this.options.lazyLoad) {
|
|
266
227
|
return Promise.reject("Factory can be used only on a model declared with lazyLoad: true");
|
|
267
228
|
} else {
|
|
268
|
-
return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(
|
|
229
|
+
return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(_retrieveHook, _this), params, _classPrivateFieldGet(_axios, _this)).then(function (data) {
|
|
269
230
|
if (!Array.isArray(data)) {
|
|
270
|
-
_classPrivateFieldSet(
|
|
231
|
+
_classPrivateFieldSet(_singleItemQuery, _this, true);
|
|
271
232
|
}
|
|
272
|
-
return _classPrivateFieldGet(
|
|
233
|
+
return _classPrivateFieldGet(_toArray, _this).call(_this, data);
|
|
273
234
|
});
|
|
274
235
|
}
|
|
275
236
|
});
|
|
276
|
-
_classPrivateFieldInitSpec(this, _addRelationByField, {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
writable: true,
|
|
295
|
-
value: function value(json) {
|
|
296
|
-
var _iterator = _createForOfIteratorHelper(_classPrivateFieldGet(_this, _hiddenFields)),
|
|
297
|
-
_step;
|
|
298
|
-
try {
|
|
299
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
300
|
-
var attribute = _step.value;
|
|
301
|
-
delete json[attribute];
|
|
302
|
-
}
|
|
303
|
-
} catch (err) {
|
|
304
|
-
_iterator.e(err);
|
|
305
|
-
} finally {
|
|
306
|
-
_iterator.f();
|
|
237
|
+
_classPrivateFieldInitSpec(this, _addRelationByField, function (model, localField) {
|
|
238
|
+
var remoteField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "id";
|
|
239
|
+
var filterFunction = function filterFunction(parentObject, child) {
|
|
240
|
+
return parentObject[localField] === child[remoteField];
|
|
241
|
+
};
|
|
242
|
+
return _classPrivateFieldGet(_addRelationByFilter, _this).call(_this, model, filterFunction);
|
|
243
|
+
});
|
|
244
|
+
_classPrivateFieldInitSpec(this, _addRelationByFilter, function (model, filterFunction) {
|
|
245
|
+
var includedType = model.getType();
|
|
246
|
+
_classPrivateFieldGet(_includes, _this)[includedType] = filterFunction;
|
|
247
|
+
});
|
|
248
|
+
_classPrivateFieldInitSpec(this, _removeHiddenFields, function (json) {
|
|
249
|
+
var _iterator = _createForOfIteratorHelper(_classPrivateFieldGet(_hiddenFields, _this)),
|
|
250
|
+
_step;
|
|
251
|
+
try {
|
|
252
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
253
|
+
var attribute = _step.value;
|
|
254
|
+
delete json[attribute];
|
|
307
255
|
}
|
|
308
|
-
|
|
256
|
+
} catch (err) {
|
|
257
|
+
_iterator.e(err);
|
|
258
|
+
} finally {
|
|
259
|
+
_iterator.f();
|
|
309
260
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
}];
|
|
321
|
-
} else {
|
|
322
|
-
return data;
|
|
323
|
-
}
|
|
261
|
+
return json;
|
|
262
|
+
});
|
|
263
|
+
_classPrivateFieldInitSpec(this, _toArray, function (data) {
|
|
264
|
+
if (Array.isArray(data)) {
|
|
265
|
+
if (data.length && data.every(function (str) {
|
|
266
|
+
return ["string", "number"].includes(_typeof(str));
|
|
267
|
+
})) {
|
|
268
|
+
return [{
|
|
269
|
+
value: data
|
|
270
|
+
}];
|
|
324
271
|
} else {
|
|
325
|
-
|
|
326
|
-
return [{
|
|
327
|
-
value: data
|
|
328
|
-
}];
|
|
329
|
-
} else {
|
|
330
|
-
return [data];
|
|
331
|
-
}
|
|
272
|
+
return data;
|
|
332
273
|
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
var data = Object.values(objects).map(function (object) {
|
|
339
|
-
return _classPrivateFieldGet(_this, _removeHiddenFields).call(_this, object.toJSON());
|
|
340
|
-
});
|
|
341
|
-
if (data.value != null && Object.keys(data).length === 1) {
|
|
342
|
-
return data.value;
|
|
343
|
-
} else if (Array.isArray(data) && data.length === 1 && data[0].value != null && Object.keys(data[0]).length === 1) {
|
|
344
|
-
return data[0].value;
|
|
274
|
+
} else {
|
|
275
|
+
if (["string", "number"].includes(_typeof(data))) {
|
|
276
|
+
return [{
|
|
277
|
+
value: data
|
|
278
|
+
}];
|
|
345
279
|
} else {
|
|
346
|
-
return data;
|
|
280
|
+
return [data];
|
|
347
281
|
}
|
|
348
282
|
}
|
|
349
283
|
});
|
|
350
|
-
_classPrivateFieldInitSpec(this,
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}).then(_classPrivateFieldGet(_this, _toArray))["catch"](function (error) {
|
|
361
|
-
_classPrivateFieldGet(_this, _removeFromStoreSilentlyAfterFailure).call(_this, objects);
|
|
362
|
-
return _classPrivateFieldGet(_this, _hanldeApiError).call(_this, error, objects, operation);
|
|
363
|
-
});
|
|
284
|
+
_classPrivateFieldInitSpec(this, _unWrap, function (objects) {
|
|
285
|
+
var data = Object.values(objects).map(function (object) {
|
|
286
|
+
return _classPrivateFieldGet(_removeHiddenFields, _this).call(_this, object.toJSON());
|
|
287
|
+
});
|
|
288
|
+
if (data.value != null && Object.keys(data).length === 1) {
|
|
289
|
+
return data.value;
|
|
290
|
+
} else if (Array.isArray(data) && data.length === 1 && data[0].value != null && Object.keys(data[0]).length === 1) {
|
|
291
|
+
return data[0].value;
|
|
292
|
+
} else {
|
|
293
|
+
return data;
|
|
364
294
|
}
|
|
365
295
|
});
|
|
366
|
-
_classPrivateFieldInitSpec(this,
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
if (
|
|
370
|
-
|
|
371
|
-
(0, _BasicObj.setValues)(data[i], _this, _SubObj["default"], null, objects[i]);
|
|
372
|
-
objects[i].setId(data[i].id);
|
|
373
|
-
delete objects[i].setId;
|
|
374
|
-
}
|
|
296
|
+
_classPrivateFieldInitSpec(this, _insertObjects, function (objects) {
|
|
297
|
+
var operation = "insert";
|
|
298
|
+
return (0, _modelHooksUtils.executeHook)(operation, _classPrivateFieldGet(_insertHook, _this), _classPrivateFieldGet(_unWrap, _this).call(_this, objects), _classPrivateFieldGet(_axios, _this)).then(function (data) {
|
|
299
|
+
if (data) {
|
|
300
|
+
_classPrivateFieldGet(_assignId, _this).call(_this, data, objects);
|
|
375
301
|
}
|
|
376
|
-
|
|
302
|
+
_classPrivateFieldGet(_cleanApiError, _this).call(_this, objects);
|
|
303
|
+
return data;
|
|
304
|
+
}).then(_classPrivateFieldGet(_toArray, _this))["catch"](function (error) {
|
|
305
|
+
_classPrivateFieldGet(_removeFromStoreSilentlyAfterFailure, _this).call(_this, objects);
|
|
306
|
+
return _classPrivateFieldGet(_hanldeApiError, _this).call(_this, error, objects, operation);
|
|
307
|
+
});
|
|
377
308
|
});
|
|
378
|
-
_classPrivateFieldInitSpec(this,
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}).then(_classPrivateFieldGet(_this, _toArray))["catch"](function (error) {
|
|
386
|
-
return _classPrivateFieldGet(_this, _hanldeApiError).call(_this, error, objects, operation);
|
|
387
|
-
});
|
|
309
|
+
_classPrivateFieldInitSpec(this, _assignId, function (data, objects) {
|
|
310
|
+
if (Array.isArray(data) && Array.isArray(objects) && objects.length === data.length) {
|
|
311
|
+
for (var i = 0; i < data.length; i++) {
|
|
312
|
+
(0, _BasicObj.setValues)(data[i], _this, _SubObj["default"], null, objects[i]);
|
|
313
|
+
objects[i].setId(data[i].id);
|
|
314
|
+
delete objects[i].setId;
|
|
315
|
+
}
|
|
388
316
|
}
|
|
389
317
|
});
|
|
390
|
-
_classPrivateFieldInitSpec(this,
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
return
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
return _classPrivateFieldGet(_this, _hanldeApiError).call(_this, error, objects, operation);
|
|
399
|
-
});
|
|
400
|
-
}
|
|
318
|
+
_classPrivateFieldInitSpec(this, _updateObjects, function (objects) {
|
|
319
|
+
var operation = "update";
|
|
320
|
+
return (0, _modelHooksUtils.executeHook)(operation, _classPrivateFieldGet(_updateHook, _this), _classPrivateFieldGet(_unWrap, _this).call(_this, objects), _classPrivateFieldGet(_axios, _this)).then(function (data) {
|
|
321
|
+
_classPrivateFieldGet(_cleanApiError, _this).call(_this, objects);
|
|
322
|
+
return data;
|
|
323
|
+
}).then(_classPrivateFieldGet(_toArray, _this))["catch"](function (error) {
|
|
324
|
+
return _classPrivateFieldGet(_hanldeApiError, _this).call(_this, error, objects, operation);
|
|
325
|
+
});
|
|
401
326
|
});
|
|
402
|
-
_classPrivateFieldInitSpec(this,
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
// Set errors
|
|
412
|
-
var strError = (_ref = (_error$message = (_error4 = error) === null || _error4 === void 0 ? void 0 : _error4.message) !== null && _error$message !== void 0 ? _error$message : (_error5 = error) === null || _error5 === void 0 ? void 0 : _error5.error) !== null && _ref !== void 0 ? _ref : error;
|
|
413
|
-
Object.values(objects).map(function (object) {
|
|
414
|
-
return object.setError(strError);
|
|
415
|
-
});
|
|
416
|
-
return Promise.reject(_objectSpread(_objectSpread({}, error), {}, {
|
|
417
|
-
targets: targets,
|
|
418
|
-
operation: operation
|
|
419
|
-
}));
|
|
420
|
-
}
|
|
327
|
+
_classPrivateFieldInitSpec(this, _deleteObjects, function (objects) {
|
|
328
|
+
var operation = "delete";
|
|
329
|
+
return (0, _modelHooksUtils.executeHook)(operation, _classPrivateFieldGet(_deleteHook, _this), _classPrivateFieldGet(_unWrap, _this).call(_this, objects), _classPrivateFieldGet(_axios, _this)).then(function (data) {
|
|
330
|
+
_classPrivateFieldGet(_cleanApiError, _this).call(_this, objects);
|
|
331
|
+
return data;
|
|
332
|
+
}).then(_classPrivateFieldGet(_toArray, _this))["catch"](function (error) {
|
|
333
|
+
return _classPrivateFieldGet(_hanldeApiError, _this).call(_this, error, objects, operation);
|
|
334
|
+
});
|
|
421
335
|
});
|
|
422
|
-
_classPrivateFieldInitSpec(this,
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
336
|
+
_classPrivateFieldInitSpec(this, _hanldeApiError, function (error, objects, operation) {
|
|
337
|
+
var _error$response$data, _error2, _ref, _error$message, _error3, _error4;
|
|
338
|
+
error = (_error$response$data = (_error2 = error) === null || _error2 === void 0 || (_error2 = _error2.response) === null || _error2 === void 0 ? void 0 : _error2.data) !== null && _error$response$data !== void 0 ? _error$response$data : error;
|
|
339
|
+
var targets = objects.map(function (object) {
|
|
340
|
+
return object.getId();
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// Set errors
|
|
344
|
+
var strError = (_ref = (_error$message = (_error3 = error) === null || _error3 === void 0 ? void 0 : _error3.message) !== null && _error$message !== void 0 ? _error$message : (_error4 = error) === null || _error4 === void 0 ? void 0 : _error4.error) !== null && _ref !== void 0 ? _ref : error;
|
|
345
|
+
Object.values(objects).map(function (object) {
|
|
346
|
+
return object.setError(strError);
|
|
347
|
+
});
|
|
348
|
+
return Promise.reject(_objectSpread(_objectSpread({}, error), {}, {
|
|
349
|
+
targets: targets,
|
|
350
|
+
operation: operation
|
|
351
|
+
}));
|
|
352
|
+
});
|
|
353
|
+
_classPrivateFieldInitSpec(this, _cleanApiError, function (objects) {
|
|
354
|
+
Object.values(objects).map(function (object) {
|
|
355
|
+
return object.setError(false);
|
|
356
|
+
});
|
|
429
357
|
});
|
|
430
|
-
_classPrivateFieldInitSpec(this, _removeFromStoreSilentlyAfterFailure, {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
var target = _step2.value;
|
|
440
|
-
delete _this.getStore().models[_this.getType()].storedObjects[target];
|
|
441
|
-
}
|
|
442
|
-
} catch (err) {
|
|
443
|
-
_iterator2.e(err);
|
|
444
|
-
} finally {
|
|
445
|
-
_iterator2.f();
|
|
358
|
+
_classPrivateFieldInitSpec(this, _removeFromStoreSilentlyAfterFailure, function (objects) {
|
|
359
|
+
var _iterator2 = _createForOfIteratorHelper(objects.map(function (object) {
|
|
360
|
+
return object.getId();
|
|
361
|
+
})),
|
|
362
|
+
_step2;
|
|
363
|
+
try {
|
|
364
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
365
|
+
var target = _step2.value;
|
|
366
|
+
delete _this.getStore().models[_this.getType()].storedObjects[target];
|
|
446
367
|
}
|
|
368
|
+
} catch (err) {
|
|
369
|
+
_iterator2.e(err);
|
|
370
|
+
} finally {
|
|
371
|
+
_iterator2.f();
|
|
447
372
|
}
|
|
448
373
|
});
|
|
449
|
-
_classPrivateFieldSet(
|
|
374
|
+
_classPrivateFieldSet(_type, this, name);
|
|
450
375
|
this.options = _objectSpread(_objectSpread({}, _typeof(options) === "object" ? options : {}), {}, {
|
|
451
376
|
deep: (_options$deep = options.deep) !== null && _options$deep !== void 0 ? _options$deep : true,
|
|
452
377
|
parseMoment: (_options$parseMoment = options.parseMoment) !== null && _options$parseMoment !== void 0 ? _options$parseMoment : false,
|
|
@@ -454,15 +379,15 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
454
379
|
validate: (_options$validate = options.validate) !== null && _options$validate !== void 0 ? _options$validate : {},
|
|
455
380
|
autoRefresh: (_options$autoRefresh = options.autoRefresh) !== null && _options$autoRefresh !== void 0 ? _options$autoRefresh : false
|
|
456
381
|
});
|
|
457
|
-
_classPrivateFieldSet(
|
|
458
|
-
_classPrivateFieldSet(
|
|
459
|
-
_classPrivateFieldSet(
|
|
460
|
-
_classPrivateFieldSet(
|
|
461
|
-
_classPrivateFieldSet(
|
|
382
|
+
_classPrivateFieldSet(_store, this, null);
|
|
383
|
+
_classPrivateFieldSet(_includes, this, {});
|
|
384
|
+
_classPrivateFieldSet(_axios, this, this.options.axios || _redaxios["default"]);
|
|
385
|
+
_classPrivateFieldSet(_hiddenFields, this, this.options.hiddenFields || []);
|
|
386
|
+
_classPrivateFieldSet(_loadFunction, this, this.options.load || null);
|
|
462
387
|
if (!name || !options) {
|
|
463
388
|
throw new Error("A Model requires at least a name and a hook");
|
|
464
389
|
}
|
|
465
|
-
if (_classPrivateFieldGet(
|
|
390
|
+
if (_classPrivateFieldGet(_loadFunction, this) && typeof _classPrivateFieldGet(_loadFunction, this) !== "function") {
|
|
466
391
|
throw new Error("The load option must be a function");
|
|
467
392
|
}
|
|
468
393
|
var _ref2 = _typeof(options) === "object" ? (0, _modelHooksUtils.getHooksFromOptions)(options) : (0, _modelHooksUtils.getHooksFromUrl)(options),
|
|
@@ -471,20 +396,20 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
471
396
|
insertHook = _ref3[1],
|
|
472
397
|
updateHook = _ref3[2],
|
|
473
398
|
deleteHook = _ref3[3];
|
|
474
|
-
_classPrivateFieldSet(
|
|
475
|
-
_classPrivateFieldSet(
|
|
476
|
-
_classPrivateFieldSet(
|
|
477
|
-
_classPrivateFieldSet(
|
|
478
|
-
_classPrivateFieldSet(
|
|
479
|
-
_classPrivateFieldSet(
|
|
399
|
+
_classPrivateFieldSet(_retrieveHook, this, retrieveHook);
|
|
400
|
+
_classPrivateFieldSet(_updateHook, this, updateHook);
|
|
401
|
+
_classPrivateFieldSet(_insertHook, this, insertHook);
|
|
402
|
+
_classPrivateFieldSet(_deleteHook, this, deleteHook);
|
|
403
|
+
_classPrivateFieldSet(_singleItemQuery, this, false); // By default use arrays
|
|
404
|
+
_classPrivateFieldSet(_batchSize, this, 4); // For HTTP requests in parallel if your API doesn't support multiple resources
|
|
480
405
|
|
|
481
406
|
if (this.options.autoRefresh && typeof this.options.autoRefresh === "number") {
|
|
482
407
|
setInterval(function () {
|
|
483
|
-
_this.getStore().refresh(_classPrivateFieldGet(
|
|
408
|
+
_this.getStore().refresh(_classPrivateFieldGet(_type, _this));
|
|
484
409
|
}, this.options.autoRefresh);
|
|
485
410
|
}
|
|
486
411
|
});
|
|
487
|
-
function
|
|
412
|
+
function _error(error) {
|
|
488
413
|
error = error.message || error;
|
|
489
414
|
this.getStore().pubSub.publish("error", error);
|
|
490
415
|
return Promise.reject(error);
|