dataflux 1.6.0 → 1.7.1
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/LICENSE +1 -1
- package/README.md +67 -10
- package/dist/Model.js +27 -21
- package/dist/PersistentStore.js +24 -3
- package/dist/PubSub.js +23 -0
- package/dist/Store.js +63 -22
- package/dist/SubObj.js +0 -4
- package/dist/modelHooksUtils.js +19 -2
- package/package.json +1 -1
- package/CNAME +0 -1
- package/_config.yml +0 -1
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2022-present Massimo Candela
|
|
3
|
+
Copyright (c) 2022-present Massimo Candela <https://massimocandela.com>
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -276,6 +276,7 @@ All the possible options for a model creation are (they are all optional):
|
|
|
276
276
|
| 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. | |
|
|
277
277
|
| 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. |
|
|
278
278
|
| deep | A boolean defining if nested objects should be enriched with the object methods. | true |
|
|
279
|
+
| 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 |
|
|
279
280
|
|
|
280
281
|
### Operations
|
|
281
282
|
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.
|
|
@@ -341,28 +342,84 @@ const book = new Model("book", options);
|
|
|
341
342
|
|
|
342
343
|
#### Operation function
|
|
343
344
|
|
|
344
|
-
To be even more flexible, you can pass functions
|
|
345
|
+
To be even more flexible, you can pass functions to generate the API urls or retrieve the data. An operation function can return a url or a promise. If the function returns a promise, the promise must resolve in an array of JSON objects when these are ready.
|
|
345
346
|
|
|
346
347
|
|
|
348
|
+
Example of operation function returning from the API and submitting to the API an array of JSON objects.
|
|
349
|
+
|
|
347
350
|
```js
|
|
348
351
|
const options = {
|
|
349
352
|
retrieve: () => {
|
|
350
353
|
// 1) get the data from the API
|
|
351
354
|
// 2) tranforms the data
|
|
352
355
|
// 3) return the data to the store
|
|
353
|
-
return
|
|
356
|
+
return axios({ // Example with axios, but you can use whatever you prefer
|
|
357
|
+
url: "https://api.example.net/example",
|
|
358
|
+
method: "get"
|
|
359
|
+
});
|
|
354
360
|
},
|
|
355
361
|
insert: (data) => {
|
|
356
362
|
// 1) recieve the data from the store
|
|
357
363
|
// 2) transform the data however you like
|
|
358
|
-
// 3) send data to server
|
|
359
|
-
return
|
|
364
|
+
// 3) send data to server
|
|
365
|
+
return axios({
|
|
366
|
+
url: "https://api.example.net/example",
|
|
367
|
+
data,
|
|
368
|
+
method: "post"
|
|
369
|
+
});
|
|
360
370
|
}
|
|
361
371
|
};
|
|
362
372
|
|
|
363
373
|
const book = new Model("book", options);
|
|
364
374
|
```
|
|
365
375
|
|
|
376
|
+
#### Object factory
|
|
377
|
+
|
|
378
|
+
In the examples we saw above, objects are retrieved from an API returning one or more objects. However, sometimes object creation requires a more complex logic. This can be summarized as: the object must be created based on some input parameter
|
|
379
|
+
|
|
380
|
+
Typical examples are:
|
|
381
|
+
* There is no API returning all the objects of a given type, you can only access specific objects based on a parameter (e.g., based on the ID).
|
|
382
|
+
* It doesn't make sense to retrieve all the objects of a given type, since the client needs to access only to a subset of them.
|
|
383
|
+
* The APIs to get/post/put/delete objects are parametric (e.g., you need to specify the ID in the url).
|
|
384
|
+
* The model is polymorphic, and the final object's format is based on some parameter;
|
|
385
|
+
* The model is polymorphic, a different API is used to retrieve the objects based on some input parameter (e.g., they are all books, but there are different APIs by genre).
|
|
386
|
+
|
|
387
|
+
This is a well-know problem, described by the [factory design pattern](https://refactoring.guru/design-patterns/factory-method).
|
|
388
|
+
In DataFlux, the store provides for this use case the `.factory()` method that allows you to implement Factory.
|
|
389
|
+
|
|
390
|
+
To create a factory, you must declare a model as follows:
|
|
391
|
+
|
|
392
|
+
```js
|
|
393
|
+
const author = new Model("author", {
|
|
394
|
+
lazyLoad: true, // It MUST be lazyLoaded
|
|
395
|
+
retrieve: (params) => { // The retrieve function now takes some parameters
|
|
396
|
+
|
|
397
|
+
if (params) {
|
|
398
|
+
// You can return a URL or directly one or more JSON objects
|
|
399
|
+
return `https://api.example.net/authors/${params.id}`
|
|
400
|
+
} else {
|
|
401
|
+
return Promise.resolve([]); // It's important to handle the base case where params is null
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
store.addModel(author);
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
It is important to notice in the example above, how `lazyLoad` must be set to `true` and how the retrieve function returns a URL based on an input parameter. As always, the operation function can return a URL (DataFlux will download the objects) or directly a collection of objects.
|
|
410
|
+
|
|
411
|
+
> If you don't specify the insert/update/delete operation functions, the same URL of the retrieve function will be used.
|
|
412
|
+
|
|
413
|
+
Once the parametric retrieve function is declared, you can instantiate the objects with the `store.factory()` method:
|
|
414
|
+
|
|
415
|
+
```js
|
|
416
|
+
store.factory("author", {id: 4});
|
|
417
|
+
```
|
|
418
|
+
Invoking `store.factory()` will create a new object in the "author" collection.
|
|
419
|
+
|
|
420
|
+
> store.factory() will not return the object. It just inserts the object in the collection. You will need to use any of the usual .find/.findOne/.findAll/.subscribe to retrieve it.
|
|
421
|
+
|
|
422
|
+
|
|
366
423
|
#### Object enrichment
|
|
367
424
|
|
|
368
425
|
DataFlux objects can have a `load()` method which enables you to load extra attributes of an object.
|
|
@@ -526,13 +583,13 @@ You can operate on the reviews similarly to how you operate on the main model's
|
|
|
526
583
|
|
|
527
584
|
```js
|
|
528
585
|
store.find("book")
|
|
529
|
-
|
|
530
|
-
|
|
586
|
+
.then(([book]) => {
|
|
587
|
+
const firstReview = book.reviews[0];
|
|
531
588
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
589
|
+
// Examples of what you can do:
|
|
590
|
+
firstReview.detroy(); // The first review is removed from the array book.reviews
|
|
591
|
+
firstReview.set("stars", 5); // Set the stars of the first review to 5
|
|
592
|
+
});
|
|
536
593
|
```
|
|
537
594
|
|
|
538
595
|
|
package/dist/Model.js
CHANGED
|
@@ -11,6 +11,10 @@ var _batchPromises = _interopRequireDefault(require("batch-promises"));
|
|
|
11
11
|
|
|
12
12
|
var _axios2 = _interopRequireDefault(require("axios"));
|
|
13
13
|
|
|
14
|
+
var _BasicObj = require("./BasicObj");
|
|
15
|
+
|
|
16
|
+
var _SubObj = _interopRequireDefault(require("./SubObj"));
|
|
17
|
+
|
|
14
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
15
19
|
|
|
16
20
|
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
@@ -23,6 +27,10 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
|
|
|
23
27
|
|
|
24
28
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
25
29
|
|
|
30
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
31
|
+
|
|
32
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
33
|
+
|
|
26
34
|
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
27
35
|
|
|
28
36
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
@@ -55,18 +63,6 @@ function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!priva
|
|
|
55
63
|
|
|
56
64
|
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; } }
|
|
57
65
|
|
|
58
|
-
var applyData = function applyData(obj, data) {
|
|
59
|
-
for (var att in data) {
|
|
60
|
-
if (att !== "id" || obj.id === undefined || att === "id" && obj.id === data.id) {
|
|
61
|
-
obj[att] = data[att];
|
|
62
|
-
} else {
|
|
63
|
-
return Promise.reject("The loading function cannot change the id of the object.");
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return Promise.resolve(obj);
|
|
68
|
-
};
|
|
69
|
-
|
|
70
66
|
var _type = /*#__PURE__*/new WeakMap();
|
|
71
67
|
|
|
72
68
|
var _store = /*#__PURE__*/new WeakMap();
|
|
@@ -194,7 +190,7 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
|
|
|
194
190
|
|
|
195
191
|
_defineProperty(this, "load", function (obj) {
|
|
196
192
|
if (_classPrivateFieldGet(_this, _loadFunction)) {
|
|
197
|
-
return _this.getStore().whenSaved(_this.getType())["catch"](function () {
|
|
193
|
+
return _this.getStore().whenSaved(_this.getType())["catch"](function (e) {
|
|
198
194
|
throw new Error("You cannot perform load() on an unsaved object.");
|
|
199
195
|
}).then(function () {
|
|
200
196
|
var res = _classPrivateFieldGet(_this, _loadFunction).call(_this, obj.toJSON());
|
|
@@ -211,7 +207,8 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
|
|
|
211
207
|
return res;
|
|
212
208
|
}
|
|
213
209
|
}).then(function (data) {
|
|
214
|
-
|
|
210
|
+
(0, _BasicObj.setValues)(data, _this, _SubObj["default"], null, obj);
|
|
211
|
+
return data;
|
|
215
212
|
})["catch"](function (error) {
|
|
216
213
|
return _classPrivateMethodGet(_this, _error, _error2).call(_this, error);
|
|
217
214
|
});
|
|
@@ -245,7 +242,7 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
|
|
|
245
242
|
var filterRelation = _classPrivateFieldGet(_this, _includes)[includedType];
|
|
246
243
|
|
|
247
244
|
if (filterRelation) {
|
|
248
|
-
return parentObject.load()["catch"](function () {}).then(function () {
|
|
245
|
+
return (parentObject.getModel().options.load ? parentObject.load()["catch"](function () {}) : Promise.resolve()).then(function () {
|
|
249
246
|
return _this.getStore().find(includedType, function (item) {
|
|
250
247
|
return filterRelation(parentObject, item);
|
|
251
248
|
}).then(function (data) {
|
|
@@ -285,6 +282,14 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
|
|
|
285
282
|
return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _deleteObjects)) : Promise.resolve();
|
|
286
283
|
});
|
|
287
284
|
|
|
285
|
+
_defineProperty(this, "factory", function (params) {
|
|
286
|
+
if (!_this.options.lazyLoad) {
|
|
287
|
+
return Promise.reject("Factory can be used only on a model declared with lazyLoad: true");
|
|
288
|
+
} else {
|
|
289
|
+
return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(_this, _retrieveHook), params, _classPrivateFieldGet(_this, _axios));
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
|
|
288
293
|
_classPrivateFieldInitSpec(this, _addRelationByField, {
|
|
289
294
|
writable: true,
|
|
290
295
|
value: function value(model, localField) {
|
|
@@ -376,20 +381,21 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
|
|
|
376
381
|
|
|
377
382
|
_classPrivateFieldSet(this, _type, name);
|
|
378
383
|
|
|
379
|
-
this.options = {
|
|
384
|
+
this.options = _objectSpread(_objectSpread({}, options), {}, {
|
|
380
385
|
deep: (_options$deep = options.deep) !== null && _options$deep !== void 0 ? _options$deep : true,
|
|
381
|
-
parseMoment: (_options$parseMoment = options.parseMoment) !== null && _options$parseMoment !== void 0 ? _options$parseMoment : false
|
|
382
|
-
|
|
386
|
+
parseMoment: (_options$parseMoment = options.parseMoment) !== null && _options$parseMoment !== void 0 ? _options$parseMoment : false,
|
|
387
|
+
lazyLoad: options.lazyLoad
|
|
388
|
+
});
|
|
383
389
|
|
|
384
390
|
_classPrivateFieldSet(this, _store, null);
|
|
385
391
|
|
|
386
392
|
_classPrivateFieldSet(this, _includes, {});
|
|
387
393
|
|
|
388
|
-
_classPrivateFieldSet(this, _axios, options.axios || _axios2["default"]);
|
|
394
|
+
_classPrivateFieldSet(this, _axios, this.options.axios || _axios2["default"]);
|
|
389
395
|
|
|
390
|
-
_classPrivateFieldSet(this, _hiddenFields, options.hiddenFields || []);
|
|
396
|
+
_classPrivateFieldSet(this, _hiddenFields, this.options.hiddenFields || []);
|
|
391
397
|
|
|
392
|
-
_classPrivateFieldSet(this, _loadFunction, options.load || null);
|
|
398
|
+
_classPrivateFieldSet(this, _loadFunction, this.options.load || null);
|
|
393
399
|
|
|
394
400
|
if (!name || !options) {
|
|
395
401
|
throw new Error("A Model requires at least a name and a hook");
|
package/dist/PersistentStore.js
CHANGED
|
@@ -11,6 +11,12 @@ var _Store2 = _interopRequireDefault(require("./Store"));
|
|
|
11
11
|
|
|
12
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
13
|
|
|
14
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
15
|
+
|
|
16
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
17
|
+
|
|
18
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
19
|
+
|
|
14
20
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
15
21
|
|
|
16
22
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
@@ -196,12 +202,27 @@ var PersistentStore = /*#__PURE__*/function (_Store) {
|
|
|
196
202
|
value: function update(objects, skipSave) {
|
|
197
203
|
var _this5 = this;
|
|
198
204
|
|
|
199
|
-
return _get(_getPrototypeOf(PersistentStore.prototype), "update", this).call(this, objects).then(function (
|
|
200
|
-
if (
|
|
205
|
+
return _get(_getPrototypeOf(PersistentStore.prototype), "update", this).call(this, objects).then(function (objects) {
|
|
206
|
+
if (skipSave) {
|
|
207
|
+
var _iterator = _createForOfIteratorHelper(objects),
|
|
208
|
+
_step;
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
212
|
+
var object = _step.value;
|
|
213
|
+
var type = object.getModel().getType();
|
|
214
|
+
_this5.models[type].storedObjects[object.getId()].fingerprint = object.getFingerprint();
|
|
215
|
+
}
|
|
216
|
+
} catch (err) {
|
|
217
|
+
_iterator.e(err);
|
|
218
|
+
} finally {
|
|
219
|
+
_iterator.f();
|
|
220
|
+
}
|
|
221
|
+
} else {
|
|
201
222
|
_this5.delayedSave();
|
|
202
223
|
}
|
|
203
224
|
|
|
204
|
-
return
|
|
225
|
+
return objects;
|
|
205
226
|
});
|
|
206
227
|
}
|
|
207
228
|
}]);
|
package/dist/PubSub.js
CHANGED
|
@@ -19,6 +19,29 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
|
|
|
19
19
|
|
|
20
20
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
21
21
|
|
|
22
|
+
/*
|
|
23
|
+
* MIT License
|
|
24
|
+
*
|
|
25
|
+
* Copyright (c) 2022 Massimo Candela <https://massimocandela.com>
|
|
26
|
+
*
|
|
27
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
28
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
29
|
+
* in the Software without restriction, including without limitation the rights
|
|
30
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
31
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
32
|
+
* furnished to do so, subject to the following conditions:
|
|
33
|
+
*
|
|
34
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
35
|
+
* copies or substantial portions of the Software.
|
|
36
|
+
*
|
|
37
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
38
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
39
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
40
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
41
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
42
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
43
|
+
* SOFTWARE.
|
|
44
|
+
*/
|
|
22
45
|
var PubSub = /*#__PURE__*/_createClass(function PubSub() {
|
|
23
46
|
var _this = this;
|
|
24
47
|
|
package/dist/Store.js
CHANGED
|
@@ -29,6 +29,8 @@ function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclarati
|
|
|
29
29
|
|
|
30
30
|
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
31
31
|
|
|
32
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
33
|
+
|
|
32
34
|
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
|
33
35
|
|
|
34
36
|
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
@@ -67,6 +69,10 @@ var Store = /*#__PURE__*/function () {
|
|
|
67
69
|
|
|
68
70
|
_classPrivateMethodInitSpec(this, _error);
|
|
69
71
|
|
|
72
|
+
_defineProperty(this, "getModels", function () {
|
|
73
|
+
return Object.keys(_this.models);
|
|
74
|
+
});
|
|
75
|
+
|
|
70
76
|
_classPrivateFieldInitSpec(this, _deleteByObject, {
|
|
71
77
|
writable: true,
|
|
72
78
|
value: function value(object) {
|
|
@@ -114,16 +120,19 @@ var Store = /*#__PURE__*/function () {
|
|
|
114
120
|
var type = model.getType();
|
|
115
121
|
|
|
116
122
|
if (!_this2.models[type]) {
|
|
123
|
+
var _model$options$lazyLo;
|
|
124
|
+
|
|
117
125
|
_this2.models[type] = {
|
|
118
126
|
model: model,
|
|
119
127
|
storedObjects: {}
|
|
120
128
|
};
|
|
121
129
|
model.setStore(_this2);
|
|
130
|
+
var lazyLoad = (_model$options$lazyLo = model.options.lazyLoad) !== null && _model$options$lazyLo !== void 0 ? _model$options$lazyLo : _this2.options.lazyLoad;
|
|
122
131
|
|
|
123
|
-
if (
|
|
124
|
-
resolve(_classPrivateMethodGet(_this2, _loadObjects, _loadObjects2).call(_this2, type));
|
|
125
|
-
} else {
|
|
132
|
+
if (lazyLoad) {
|
|
126
133
|
resolve();
|
|
134
|
+
} else {
|
|
135
|
+
resolve(_classPrivateMethodGet(_this2, _loadObjects, _loadObjects2).call(_this2, type));
|
|
127
136
|
}
|
|
128
137
|
} else {
|
|
129
138
|
var error = "The model already exists";
|
|
@@ -290,6 +299,38 @@ var Store = /*#__PURE__*/function () {
|
|
|
290
299
|
};
|
|
291
300
|
});
|
|
292
301
|
}
|
|
302
|
+
}, {
|
|
303
|
+
key: "factory",
|
|
304
|
+
value: function factory(type, params) {
|
|
305
|
+
var _this8 = this;
|
|
306
|
+
|
|
307
|
+
var item = this.models[type];
|
|
308
|
+
this.pubSub.publish("loading", {
|
|
309
|
+
status: "start",
|
|
310
|
+
model: type
|
|
311
|
+
});
|
|
312
|
+
return item.promise = item.model.factory(params).then(function (items) {
|
|
313
|
+
var _iterator3 = _createForOfIteratorHelper(items),
|
|
314
|
+
_step3;
|
|
315
|
+
|
|
316
|
+
try {
|
|
317
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
318
|
+
var _item = _step3.value;
|
|
319
|
+
|
|
320
|
+
_classPrivateMethodGet(_this8, _insertObject, _insertObject2).call(_this8, type, _item, false);
|
|
321
|
+
}
|
|
322
|
+
} catch (err) {
|
|
323
|
+
_iterator3.e(err);
|
|
324
|
+
} finally {
|
|
325
|
+
_iterator3.f();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
_this8.pubSub.publish("loading", {
|
|
329
|
+
status: "end",
|
|
330
|
+
model: type
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
}
|
|
293
334
|
}]);
|
|
294
335
|
|
|
295
336
|
return Store;
|
|
@@ -304,25 +345,25 @@ function _error2(error) {
|
|
|
304
345
|
}
|
|
305
346
|
|
|
306
347
|
function _deleteByFilter2(type, filterFunction) {
|
|
307
|
-
var
|
|
348
|
+
var _this9 = this;
|
|
308
349
|
|
|
309
350
|
return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
|
|
310
|
-
var deleted = Object.values(
|
|
351
|
+
var deleted = Object.values(_this9.models[type].storedObjects).filter(function (i) {
|
|
311
352
|
return filterFunction(i.object);
|
|
312
353
|
});
|
|
313
354
|
|
|
314
|
-
var
|
|
315
|
-
|
|
355
|
+
var _iterator4 = _createForOfIteratorHelper(deleted),
|
|
356
|
+
_step4;
|
|
316
357
|
|
|
317
358
|
try {
|
|
318
|
-
for (
|
|
319
|
-
var object =
|
|
359
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
360
|
+
var object = _step4.value;
|
|
320
361
|
object.status = "deleted";
|
|
321
362
|
}
|
|
322
363
|
} catch (err) {
|
|
323
|
-
|
|
364
|
+
_iterator4.e(err);
|
|
324
365
|
} finally {
|
|
325
|
-
|
|
366
|
+
_iterator4.f();
|
|
326
367
|
}
|
|
327
368
|
|
|
328
369
|
return deleted.map(function (i) {
|
|
@@ -332,7 +373,7 @@ function _deleteByFilter2(type, filterFunction) {
|
|
|
332
373
|
}
|
|
333
374
|
|
|
334
375
|
function _getPromise2(type) {
|
|
335
|
-
var
|
|
376
|
+
var _this10 = this;
|
|
336
377
|
|
|
337
378
|
if (!this.models[type]) {
|
|
338
379
|
return Promise.reject("The model doesn't exist");
|
|
@@ -340,7 +381,7 @@ function _getPromise2(type) {
|
|
|
340
381
|
return Promise.reject("The model is not loaded");
|
|
341
382
|
} else if (!this.models[type].promise && this.options.lazyLoad) {
|
|
342
383
|
return _classPrivateMethodGet(this, _loadObjects, _loadObjects2).call(this, type).then(function () {
|
|
343
|
-
return
|
|
384
|
+
return _this10.models[type].promise;
|
|
344
385
|
});
|
|
345
386
|
} else {
|
|
346
387
|
return this.models[type].promise;
|
|
@@ -367,7 +408,7 @@ function _insertObject2(type, item) {
|
|
|
367
408
|
}
|
|
368
409
|
|
|
369
410
|
function _loadObjects2(type) {
|
|
370
|
-
var
|
|
411
|
+
var _this11 = this;
|
|
371
412
|
|
|
372
413
|
var item = this.models[type];
|
|
373
414
|
this.pubSub.publish("loading", {
|
|
@@ -375,22 +416,22 @@ function _loadObjects2(type) {
|
|
|
375
416
|
model: type
|
|
376
417
|
});
|
|
377
418
|
return item.promise = item.model.retrieveAll().then(function (items) {
|
|
378
|
-
var
|
|
379
|
-
|
|
419
|
+
var _iterator5 = _createForOfIteratorHelper(items),
|
|
420
|
+
_step5;
|
|
380
421
|
|
|
381
422
|
try {
|
|
382
|
-
for (
|
|
383
|
-
var
|
|
423
|
+
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
424
|
+
var _item2 = _step5.value;
|
|
384
425
|
|
|
385
|
-
_classPrivateMethodGet(
|
|
426
|
+
_classPrivateMethodGet(_this11, _insertObject, _insertObject2).call(_this11, type, _item2, false);
|
|
386
427
|
}
|
|
387
428
|
} catch (err) {
|
|
388
|
-
|
|
429
|
+
_iterator5.e(err);
|
|
389
430
|
} finally {
|
|
390
|
-
|
|
431
|
+
_iterator5.f();
|
|
391
432
|
}
|
|
392
433
|
|
|
393
|
-
|
|
434
|
+
_this11.pubSub.publish("loading", {
|
|
394
435
|
status: "end",
|
|
395
436
|
model: type
|
|
396
437
|
});
|
package/dist/SubObj.js
CHANGED
|
@@ -9,10 +9,6 @@ exports["default"] = void 0;
|
|
|
9
9
|
|
|
10
10
|
var _BasicObj2 = require("./BasicObj");
|
|
11
11
|
|
|
12
|
-
var _moment = _interopRequireDefault(require("moment/moment"));
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
15
|
-
|
|
16
12
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
17
13
|
|
|
18
14
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
package/dist/modelHooksUtils.js
CHANGED
|
@@ -65,7 +65,18 @@ var createHookItem = function createHookItem(optionItem, defaultMethod, defaultU
|
|
|
65
65
|
|
|
66
66
|
case "function":
|
|
67
67
|
return function (data) {
|
|
68
|
-
|
|
68
|
+
var res = optionItem(data);
|
|
69
|
+
|
|
70
|
+
if (typeof res === "string") {
|
|
71
|
+
return {
|
|
72
|
+
method: defaultMethod,
|
|
73
|
+
url: res,
|
|
74
|
+
fields: options.fields || [],
|
|
75
|
+
headers: options.headers || {}
|
|
76
|
+
};
|
|
77
|
+
} else {
|
|
78
|
+
return Promise.resolve(res);
|
|
79
|
+
}
|
|
69
80
|
};
|
|
70
81
|
|
|
71
82
|
case "object":
|
|
@@ -114,7 +125,13 @@ var executeHook = function executeHook(type, hook, data, axios) {
|
|
|
114
125
|
return getDataStringHook(hook, data, axios);
|
|
115
126
|
|
|
116
127
|
case "function":
|
|
117
|
-
|
|
128
|
+
var res = hook(data);
|
|
129
|
+
|
|
130
|
+
if (res.method && res.url && res.headers) {
|
|
131
|
+
return getDataStringHook(res, data, axios);
|
|
132
|
+
} else {
|
|
133
|
+
return res;
|
|
134
|
+
}
|
|
118
135
|
|
|
119
136
|
default:
|
|
120
137
|
return Promise.reject("The ".concat(type, " hook must be a URL or a function returning a promise"));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dataflux",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.1",
|
|
4
4
|
"description": "DataFlux, automatically interfaces with your REST APIs to create a 2-way-synced local data store. Transparently manages data propagation in the React state.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": "dist/index.js",
|
package/CNAME
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
dataflux.js.org
|
package/_config.yml
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
theme: jekyll-theme-cayman
|