dataflux 1.23.2 → 1.24.0
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/README.md +20 -19
- package/dist/Model.js +19 -11
- package/dist/Store.js +7 -5
- package/dist/dataflux.min.js +1 -1
- package/dist/dataflux.min.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -350,25 +350,26 @@ const book = new Model("book", options);
|
|
|
350
350
|
|
|
351
351
|
All the possible options for a model creation are (they are all optional):
|
|
352
352
|
|
|
353
|
-
| Name
|
|
354
|
-
|
|
355
|
-
| retrieve
|
|
356
|
-
| insert
|
|
357
|
-
| update
|
|
358
|
-
| delete
|
|
359
|
-
| fields
|
|
360
|
-
| headers
|
|
361
|
-
| load
|
|
362
|
-
| axios
|
|
363
|
-
| parseMoment
|
|
364
|
-
| hiddenFields
|
|
365
|
-
| deep
|
|
366
|
-
| lazyLoad
|
|
367
|
-
| validate
|
|
368
|
-
| autoRefresh
|
|
369
|
-
| autoSave
|
|
370
|
-
| pre(object, objects)
|
|
371
|
-
| post
|
|
353
|
+
| Name | Description | Default |
|
|
354
|
+
|-------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------|
|
|
355
|
+
| retrieve | Describes the operation to retrieve the collection of objects from the REST API. It can be an operation object or a function. See [operations](#operations). | `{method: "get"}` |
|
|
356
|
+
| insert | Describes the operation to insert a new object in the collection. It can be an operation object or a function. See [operations](#operations). | `{method: "post"}` |
|
|
357
|
+
| update | Describes the operation to update objects of the collection. It can be an operation object or a function. See [operations](#operations). | `{method: "put"}` |
|
|
358
|
+
| delete | Describes the operation to remove objects from the collection. It can be an operation object or a function. See [operations](#operations). | `{method: "delete"}` |
|
|
359
|
+
| fields | An array of strings defining which attributes the retrieved objects should have. Essentially, it allows you to contemporarily specify the [X-Fields header](https://flask-restplus.readthedocs.io/en/stable/mask.html) and the [fields GET parameter](https://developers.google.com/slides/api/guides/performance#partial). This reduces transfer size and memory usage. E.g., if you have a collection of books, of which you are interested only in the name, you can define `fields: ["name"]`. In combination with `load` it allows for partial lazy load of the objects. | All the fields |
|
|
360
|
+
| headers | A dictionary of headers for the HTTP request. E.g., `{"Authorization": "bearer XXXX"}`. | No headers |
|
|
361
|
+
| load | A function that allows to enrich the objects on demand. E.g., you can use `fields` to download only the titles of a collection of books, and `load` to load completely the object. See [object enrichment](#object-enrichment). |
|
|
362
|
+
| axios | It allows to specify an axios instance to be used for the queries. If not specified, a new one will be used. | A new axios instance |
|
|
363
|
+
| parseMoment | Automatically creates Moment.js objects out of ISO8601 strings. E.g., if an object has a property `createdAt: "2022-01-07T21:38:50.295Z"`, this will be transformed to a moment object. | |
|
|
364
|
+
| hiddenFields | An array of attribute names that will never be sent back to the API. E.g., if you set `hiddenFields: ["pages"]`, a book object can contain an attribute `pages` locally, but this will be stripped out in PUT/POST requests. |
|
|
365
|
+
| deep | A boolean defining if nested objects should be enriched with the object methods. | true |
|
|
366
|
+
| lazyLoad | A boolean defining if the model should be lazy loaded on the first use. This takes precedence over the lazyLoad declared during store initialization. | false |
|
|
367
|
+
| validate | A dictionary containing functions to validate the objects of this model. See [objects validation](#objects-validation) | no validation |
|
|
368
|
+
| autoRefresh | Set auto refresh for the specific model. See `autoRefresh` in the [store config](#configuration). | |
|
|
369
|
+
| autoSave | It allows to specify `autoSave` at model level. If store.autoSave is true, `autoSave: false` at model level will allow to disable autoSave only for the specific model. |
|
|
370
|
+
| pre(object, objects) | A function that is executed for every object retrieved, before creating the collection in the store. It is useful for pre-processing every data item received from the API. It receives in input a object of the collection (first parameter) and the entire collection (second parameter). |
|
|
371
|
+
| post | A function that is executed for every object retrieved before delete, update, and insert operations. It is useful for post-processing every data item before sending them back to the API. It receives in input a object of the collection (first parameter) and the entire collection (second parameter). |
|
|
372
|
+
| filter(object, objects) | A function executed for each retrieved object before the collection is created in the store. If the function returns true, the object is added to the store; if it returns false, the object is excluded. |
|
|
372
373
|
|
|
373
374
|
### Operations
|
|
374
375
|
As described in the table above, there are four possible operations: **retrieve, insert, update,** and **delete**. An operation can be defined as an operation object or a function.
|
package/dist/Model.js
CHANGED
|
@@ -94,8 +94,10 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
94
94
|
_ref8,
|
|
95
95
|
_options$pre,
|
|
96
96
|
_ref9,
|
|
97
|
-
_options$
|
|
97
|
+
_options$filter,
|
|
98
98
|
_ref0,
|
|
99
|
+
_options$hiddenFields,
|
|
100
|
+
_ref1,
|
|
99
101
|
_options$post;
|
|
100
102
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
101
103
|
var defaults = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
@@ -234,6 +236,11 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
234
236
|
return (_this$options = _this.options) !== null && _this$options !== void 0 && _this$options.pre ? data.map(function (n) {
|
|
235
237
|
return _this.options.pre(n, data);
|
|
236
238
|
}) : data;
|
|
239
|
+
}).then(function (data) {
|
|
240
|
+
var _this$options2;
|
|
241
|
+
return (_this$options2 = _this.options) !== null && _this$options2 !== void 0 && _this$options2.filter ? data.filter(function (n) {
|
|
242
|
+
return _this.options.filter(n, data);
|
|
243
|
+
}) : data;
|
|
237
244
|
});
|
|
238
245
|
});
|
|
239
246
|
_defineProperty(this, "insertObjects", function (objects) {
|
|
@@ -318,8 +325,8 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
318
325
|
var data = arrayObjects.map(function (object) {
|
|
319
326
|
return _classPrivateFieldGet(_removeHiddenFields, _this).call(_this, object.toJSON());
|
|
320
327
|
}).map(function (object) {
|
|
321
|
-
var _this$
|
|
322
|
-
return (_this$
|
|
328
|
+
var _this$options3;
|
|
329
|
+
return (_this$options3 = _this.options) !== null && _this$options3 !== void 0 && _this$options3.post ? _this.options.post(object, arrayObjects) : object;
|
|
323
330
|
});
|
|
324
331
|
if (data.value != null && Object.keys(data).length === 1) {
|
|
325
332
|
return data.value;
|
|
@@ -421,8 +428,9 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
421
428
|
autoSave: (_ref6 = (_options$autoSave = options.autoSave) !== null && _options$autoSave !== void 0 ? _options$autoSave : defaults.autoSave) !== null && _ref6 !== void 0 ? _ref6 : null,
|
|
422
429
|
autoRefresh: (_ref7 = (_options$autoRefresh = options.autoRefresh) !== null && _options$autoRefresh !== void 0 ? _options$autoRefresh : defaults.autoRefresh) !== null && _ref7 !== void 0 ? _ref7 : false,
|
|
423
430
|
pre: (_ref8 = (_options$pre = options.pre) !== null && _options$pre !== void 0 ? _options$pre : defaults.pre) !== null && _ref8 !== void 0 ? _ref8 : null,
|
|
424
|
-
|
|
425
|
-
|
|
431
|
+
filter: (_ref9 = (_options$filter = options.filter) !== null && _options$filter !== void 0 ? _options$filter : defaults.filter) !== null && _ref9 !== void 0 ? _ref9 : null,
|
|
432
|
+
hiddenFields: (_ref0 = (_options$hiddenFields = options.hiddenFields) !== null && _options$hiddenFields !== void 0 ? _options$hiddenFields : defaults.hiddenFields) !== null && _ref0 !== void 0 ? _ref0 : [],
|
|
433
|
+
post: (_ref1 = (_options$post = options.post) !== null && _options$post !== void 0 ? _options$post : defaults.post) !== null && _ref1 !== void 0 ? _ref1 : null
|
|
426
434
|
});
|
|
427
435
|
_classPrivateFieldSet(_store, this, null);
|
|
428
436
|
_classPrivateFieldSet(_includes, this, {});
|
|
@@ -435,12 +443,12 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
|
|
|
435
443
|
if (_classPrivateFieldGet(_loadFunction, this) && typeof _classPrivateFieldGet(_loadFunction, this) !== "function") {
|
|
436
444
|
throw new Error("The load option must be a function");
|
|
437
445
|
}
|
|
438
|
-
var
|
|
439
|
-
|
|
440
|
-
retrieveHook =
|
|
441
|
-
insertHook =
|
|
442
|
-
updateHook =
|
|
443
|
-
deleteHook =
|
|
446
|
+
var _ref10 = _typeof(options) === "object" ? (0, _modelHooksUtils.getHooksFromOptions)(options) : (0, _modelHooksUtils.getHooksFromUrl)(options),
|
|
447
|
+
_ref11 = _slicedToArray(_ref10, 4),
|
|
448
|
+
retrieveHook = _ref11[0],
|
|
449
|
+
insertHook = _ref11[1],
|
|
450
|
+
updateHook = _ref11[2],
|
|
451
|
+
deleteHook = _ref11[3];
|
|
444
452
|
_classPrivateFieldSet(_retrieveHook, this, retrieveHook);
|
|
445
453
|
_classPrivateFieldSet(_updateHook, this, updateHook);
|
|
446
454
|
_classPrivateFieldSet(_insertHook, this, insertHook);
|
package/dist/Store.js
CHANGED
|
@@ -306,11 +306,9 @@ var Store = exports["default"] = /*#__PURE__*/function () {
|
|
|
306
306
|
key: "delete",
|
|
307
307
|
value: function _delete(typeOrObjects, filterFunction) {
|
|
308
308
|
if (typeof typeOrObjects === "string" && typeof filterFunction === "function") {
|
|
309
|
-
|
|
310
|
-
return _assertClassBrand(_Store_brand, this, _deleteByFilter).call(this, type, filterFunction);
|
|
309
|
+
return _assertClassBrand(_Store_brand, this, _deleteByFilter).call(this, typeOrObjects, filterFunction);
|
|
311
310
|
} else if (Array.isArray(typeOrObjects) && typeOrObjects.length && !filterFunction) {
|
|
312
|
-
|
|
313
|
-
return Promise.all(objects.map(_classPrivateFieldGet(_deleteByObject, this))).then(function (data) {
|
|
311
|
+
return Promise.all(typeOrObjects.map(_classPrivateFieldGet(_deleteByObject, this))).then(function (data) {
|
|
314
312
|
return data.flat();
|
|
315
313
|
});
|
|
316
314
|
} else {
|
|
@@ -537,7 +535,11 @@ function _deleteByFilter(type, filterFunction) {
|
|
|
537
535
|
try {
|
|
538
536
|
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
|
|
539
537
|
var object = _step6.value;
|
|
540
|
-
object.status
|
|
538
|
+
if (object.status === "new") {
|
|
539
|
+
delete _this11.models[type].storedObjects[object.getId()];
|
|
540
|
+
} else {
|
|
541
|
+
object.status = "deleted";
|
|
542
|
+
}
|
|
541
543
|
}
|
|
542
544
|
} catch (err) {
|
|
543
545
|
_iterator6.e(err);
|