dataflux 1.4.5 → 1.5.3
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 +44 -7
- package/dist/BasicObj.js +132 -0
- package/dist/Model.js +5 -2
- package/dist/Obj.js +86 -114
- package/dist/SubObj.js +135 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -87,7 +87,7 @@ Nothing else to do! After your edit, the store will do a single PUT request to t
|
|
|
87
87
|
|
|
88
88
|
### Example 2
|
|
89
89
|
|
|
90
|
-
DataFlux automatically sends the edited objects back to the API to be saved. However, you can disable this behavior and manually instruct the store when to save.
|
|
90
|
+
DataFlux automatically sends the edited objects back to the API to be saved. However, you can disable this behavior and manually instruct the store when to save.
|
|
91
91
|
|
|
92
92
|
```js
|
|
93
93
|
// To disable autoSave you must declare the store (in store.js) as follows
|
|
@@ -100,7 +100,7 @@ The same example above now becomes:
|
|
|
100
100
|
// Find the author Dante Alighieri
|
|
101
101
|
store.find("author", ({name, surname}) => name == "Dante" && surname == "Alighieri")
|
|
102
102
|
.then(([author]) => {
|
|
103
|
-
|
|
103
|
+
|
|
104
104
|
// When autoSave is false, author.set("country", "Italy") and
|
|
105
105
|
// author.country = "Italy" are equivalent
|
|
106
106
|
author.country = "Italy"
|
|
@@ -174,12 +174,12 @@ You can also do multiple subscriptions at once:
|
|
|
174
174
|
|
|
175
175
|
```js
|
|
176
176
|
const subscriptions = [
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
["book", ({title}) => title === "The little prince"], // Model name and filter function
|
|
178
|
+
["author"], // No filter function, all objects returned
|
|
179
179
|
];
|
|
180
180
|
|
|
181
181
|
const callback = ({book, author}) => {
|
|
182
|
-
|
|
182
|
+
// Objects are ready
|
|
183
183
|
};
|
|
184
184
|
|
|
185
185
|
const subKey = store.multipleSubscribe(requests, callback); // Subscribe
|
|
@@ -221,7 +221,7 @@ class MyComponent extends React.Component {
|
|
|
221
221
|
onTitleChange={(title) => book.set("title", title)}
|
|
222
222
|
// onTitleChange will alter the book and so the current
|
|
223
223
|
// state of "books" (a setState will be performed).
|
|
224
|
-
|
|
224
|
+
|
|
225
225
|
// Alternatively:
|
|
226
226
|
// onTitleChange={store.handleChange(book, "title")}
|
|
227
227
|
// is a syntactic sugar of the function above
|
|
@@ -275,6 +275,7 @@ All the possible options for a model creation are (they are all optional):
|
|
|
275
275
|
| 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 |
|
|
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
|
+
| deep | A boolean defining if nested objects should be enriched with the object methods. | true |
|
|
278
279
|
|
|
279
280
|
### Operations
|
|
280
281
|
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.
|
|
@@ -492,11 +493,47 @@ Each object created is enriched with the following methods.
|
|
|
492
493
|
| getRelation(model, filterFunction) | To get all the objects respecting a specific relation with this object (see [model relations](#model-relations)). |
|
|
493
494
|
| save() | Method to save the object. You can do `store.save()` instead. |
|
|
494
495
|
| destroy() | Method to delete the object. You can do `store.delete()` instead. |
|
|
495
|
-
|
|
496
496
|
| toJSON() | It returns a pure JSON representation of the object. |
|
|
497
497
|
| toString() | It returns a string representation of the object. |
|
|
498
498
|
| getFingerprint() | It returns a hash of the object. The hash changes at every change of the object or of any nested object. Useful to detect object changes. |
|
|
499
499
|
| getModel() | It returns the model of this object. Mostly useful to do `object.getModel().getType()` and obtain a string defining the type of the object. |
|
|
500
|
+
### Deeper Objects
|
|
501
|
+
When a model is declared with the option `deep: true` (default, see [model creation](#models-creation)), all the sub objects will also offer many of the methods above.
|
|
502
|
+
|
|
503
|
+
Imagine the API returns:
|
|
504
|
+
|
|
505
|
+
```json
|
|
506
|
+
[
|
|
507
|
+
{
|
|
508
|
+
"title": "The little prince",
|
|
509
|
+
"reviews": [
|
|
510
|
+
{
|
|
511
|
+
"stars": 4,
|
|
512
|
+
"comment": "comment 1"
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
"stars": 3,
|
|
516
|
+
"comment": "comment 2"
|
|
517
|
+
}
|
|
518
|
+
]
|
|
519
|
+
},
|
|
520
|
+
...
|
|
521
|
+
]
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
You can operate on the reviews similarly to how you operate on the main model's objects (book).
|
|
525
|
+
|
|
526
|
+
```js
|
|
527
|
+
store.find("book")
|
|
528
|
+
.then(([book]) => {
|
|
529
|
+
const firstReview = book.reviews[0];
|
|
530
|
+
|
|
531
|
+
// Examples of what you can do:
|
|
532
|
+
firstReview.detroy(); // The first review is removed from the array book.reviews
|
|
533
|
+
firstReview.set("stars", 5); // Set the stars of the first review to 5
|
|
534
|
+
});
|
|
535
|
+
```
|
|
536
|
+
|
|
500
537
|
|
|
501
538
|
## Editing objects
|
|
502
539
|
The option `autoSave` can be `true`, `false`, or a number (milliseconds).
|
package/dist/BasicObj.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.dateRegex = exports.BasicObj = void 0;
|
|
7
|
+
|
|
8
|
+
var _moment = _interopRequireDefault(require("moment/moment"));
|
|
9
|
+
|
|
10
|
+
var _uuid = require("uuid");
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
+
|
|
14
|
+
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); } }
|
|
15
|
+
|
|
16
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
17
|
+
|
|
18
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
19
|
+
|
|
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
|
+
|
|
22
|
+
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
23
|
+
|
|
24
|
+
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
25
|
+
|
|
26
|
+
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
|
27
|
+
|
|
28
|
+
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; } }
|
|
29
|
+
|
|
30
|
+
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
|
31
|
+
|
|
32
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
33
|
+
|
|
34
|
+
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
|
35
|
+
|
|
36
|
+
var dateRegex = new RegExp("^[0-9][0-9][0-9][0-9]-[0-9].*T[0-9].*Z$");
|
|
37
|
+
exports.dateRegex = dateRegex;
|
|
38
|
+
|
|
39
|
+
var _setHidden = /*#__PURE__*/new WeakMap();
|
|
40
|
+
|
|
41
|
+
var _id = /*#__PURE__*/new WeakMap();
|
|
42
|
+
|
|
43
|
+
var BasicObj = /*#__PURE__*/_createClass(function BasicObj(values, model) {
|
|
44
|
+
var _this = this;
|
|
45
|
+
|
|
46
|
+
_classCallCheck(this, BasicObj);
|
|
47
|
+
|
|
48
|
+
_classPrivateFieldInitSpec(this, _setHidden, {
|
|
49
|
+
writable: true,
|
|
50
|
+
value: {}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
_classPrivateFieldInitSpec(this, _id, {
|
|
54
|
+
writable: true,
|
|
55
|
+
value: null
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
_defineProperty(this, "getId", function () {
|
|
59
|
+
if (!_classPrivateFieldGet(_this, _id)) {
|
|
60
|
+
if (_this.id && (typeof _this.id === "string" || typeof _this.id === "number")) {
|
|
61
|
+
_classPrivateFieldSet(_this, _id, _this.id.toString());
|
|
62
|
+
} else {
|
|
63
|
+
_classPrivateFieldSet(_this, _id, (0, _uuid.v4)());
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return _classPrivateFieldGet(_this, _id);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
_defineProperty(this, "get", function (attribute, defaultValue) {
|
|
71
|
+
var _ref, _classPrivateFieldGet2;
|
|
72
|
+
|
|
73
|
+
return (_ref = (_classPrivateFieldGet2 = _classPrivateFieldGet(_this, _setHidden)[attribute]) !== null && _classPrivateFieldGet2 !== void 0 ? _classPrivateFieldGet2 : _this[attribute]) !== null && _ref !== void 0 ? _ref : defaultValue;
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
_defineProperty(this, "set", function (attribute, value, hidden) {
|
|
77
|
+
if (hidden) {
|
|
78
|
+
_classPrivateFieldGet(_this, _setHidden)[attribute] = value;
|
|
79
|
+
} else {
|
|
80
|
+
if (attribute === "id") {
|
|
81
|
+
throw new Error("You cannot change the ID");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
_this[attribute] = value;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return _this.update();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
_defineProperty(this, "setConstant", function (attribute, value) {
|
|
91
|
+
var _classPrivateFieldGet3;
|
|
92
|
+
|
|
93
|
+
_classPrivateFieldGet(_this, _setHidden)[attribute] = (_classPrivateFieldGet3 = _classPrivateFieldGet(_this, _setHidden)[attribute]) !== null && _classPrivateFieldGet3 !== void 0 ? _classPrivateFieldGet3 : value;
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
_defineProperty(this, "toJSON", function () {
|
|
97
|
+
var attrs = Object.keys(_this);
|
|
98
|
+
var out = {};
|
|
99
|
+
|
|
100
|
+
for (var _i = 0, _attrs = attrs; _i < _attrs.length; _i++) {
|
|
101
|
+
var a = _attrs[_i];
|
|
102
|
+
|
|
103
|
+
if (_this[a] instanceof _moment["default"]) {
|
|
104
|
+
out[a] = _this[a].toISOString();
|
|
105
|
+
} else if (_this[a] instanceof Date) {
|
|
106
|
+
out[a] = (0, _moment["default"])(_this[a]).toISOString();
|
|
107
|
+
} else if (_this[a].toJSON) {
|
|
108
|
+
out[a] = _this[a].toJSON();
|
|
109
|
+
} else if (Array.isArray(_this[a]) && _this[a].every(function (i) {
|
|
110
|
+
return i.toJSON;
|
|
111
|
+
})) {
|
|
112
|
+
out[a] = _this[a].map(function (i) {
|
|
113
|
+
return i.toJSON();
|
|
114
|
+
});
|
|
115
|
+
} else if (typeof _this[a] !== "function") {
|
|
116
|
+
out[a] = _this[a];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return out;
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
_defineProperty(this, "toString", function () {
|
|
124
|
+
return JSON.stringify(_this.toJSON());
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
_defineProperty(this, "update", function () {
|
|
128
|
+
return Promise.resolve();
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
exports.BasicObj = BasicObj;
|
package/dist/Model.js
CHANGED
|
@@ -110,7 +110,9 @@ var _updateObjects = /*#__PURE__*/new WeakMap();
|
|
|
110
110
|
var _deleteObjects = /*#__PURE__*/new WeakMap();
|
|
111
111
|
|
|
112
112
|
var Model = /*#__PURE__*/_createClass(function Model(name) {
|
|
113
|
-
var _this = this
|
|
113
|
+
var _this = this,
|
|
114
|
+
_options$deep,
|
|
115
|
+
_options$parseMoment;
|
|
114
116
|
|
|
115
117
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
116
118
|
|
|
@@ -375,7 +377,8 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
|
|
|
375
377
|
_classPrivateFieldSet(this, _type, name);
|
|
376
378
|
|
|
377
379
|
this.options = {
|
|
378
|
-
|
|
380
|
+
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
|
|
379
382
|
};
|
|
380
383
|
|
|
381
384
|
_classPrivateFieldSet(this, _store, null);
|
package/dist/Obj.js
CHANGED
|
@@ -9,16 +9,36 @@ var _fingerprint = _interopRequireDefault(require("./fingerprint"));
|
|
|
9
9
|
|
|
10
10
|
var _uuid = require("uuid");
|
|
11
11
|
|
|
12
|
+
var _BasicObj2 = require("./BasicObj");
|
|
13
|
+
|
|
12
14
|
var _moment = _interopRequireDefault(require("moment/moment"));
|
|
13
15
|
|
|
16
|
+
var _SubObj = _interopRequireDefault(require("./SubObj"));
|
|
17
|
+
|
|
14
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
15
19
|
|
|
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); }
|
|
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); } }
|
|
17
23
|
|
|
18
24
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
19
25
|
|
|
20
26
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
21
27
|
|
|
28
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
29
|
+
|
|
30
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
31
|
+
|
|
32
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
33
|
+
|
|
34
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
35
|
+
|
|
36
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
37
|
+
|
|
38
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
39
|
+
|
|
40
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
41
|
+
|
|
22
42
|
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; }
|
|
23
43
|
|
|
24
44
|
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
@@ -35,134 +55,86 @@ function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!priva
|
|
|
35
55
|
|
|
36
56
|
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
|
37
57
|
|
|
38
|
-
var dateRegex = new RegExp("^[0-9][0-9][0-9][0-9]-[0-9].*T[0-9].*Z$");
|
|
39
|
-
|
|
40
58
|
var _loaded = /*#__PURE__*/new WeakMap();
|
|
41
59
|
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
var Obj = /*#__PURE__*/_createClass(function Obj(values, _model) {
|
|
45
|
-
var _this = this;
|
|
46
|
-
|
|
47
|
-
_classCallCheck(this, Obj);
|
|
60
|
+
var Obj = /*#__PURE__*/function (_BasicObj) {
|
|
61
|
+
_inherits(Obj, _BasicObj);
|
|
48
62
|
|
|
49
|
-
|
|
50
|
-
writable: true,
|
|
51
|
-
value: false
|
|
52
|
-
});
|
|
63
|
+
var _super = _createSuper(Obj);
|
|
53
64
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
value: {}
|
|
57
|
-
});
|
|
65
|
+
function Obj(values, _model) {
|
|
66
|
+
var _this;
|
|
58
67
|
|
|
59
|
-
|
|
60
|
-
if (_classPrivateFieldGet(_this, _loaded)) {
|
|
61
|
-
return Promise.resolve(_this);
|
|
62
|
-
} else {
|
|
63
|
-
var model = _this.getModel();
|
|
68
|
+
_classCallCheck(this, Obj);
|
|
64
69
|
|
|
65
|
-
|
|
66
|
-
_classPrivateFieldSet(_this, _loaded, true);
|
|
70
|
+
_this = _super.call(this, values, _model);
|
|
67
71
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
});
|
|
72
|
+
_classPrivateFieldInitSpec(_assertThisInitialized(_this), _loaded, {
|
|
73
|
+
writable: true,
|
|
74
|
+
value: false
|
|
75
|
+
});
|
|
74
76
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
_defineProperty(_assertThisInitialized(_this), "load", function () {
|
|
78
|
+
if (_classPrivateFieldGet(_assertThisInitialized(_this), _loaded)) {
|
|
79
|
+
return Promise.resolve(_assertThisInitialized(_this));
|
|
80
|
+
} else {
|
|
81
|
+
var model = _this.getModel();
|
|
78
82
|
|
|
79
|
-
|
|
80
|
-
|
|
83
|
+
return model.load(_assertThisInitialized(_this)).then(function () {
|
|
84
|
+
_classPrivateFieldSet(_assertThisInitialized(_this), _loaded, true);
|
|
81
85
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return _this.getModel().getRelation(_this, type, filterFunction);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
_defineProperty(this, "set", function (attribute, value, hidden) {
|
|
90
|
-
if (hidden) {
|
|
91
|
-
_classPrivateFieldGet(_this, _setHidden)[attribute] = value;
|
|
92
|
-
} else {
|
|
93
|
-
if (attribute === "id") {
|
|
94
|
-
throw new Error("You cannot change the ID");
|
|
86
|
+
return model.getStore().update([_assertThisInitialized(_this)], true); // Propagate update
|
|
87
|
+
}).then(function () {
|
|
88
|
+
return _assertThisInitialized(_this);
|
|
89
|
+
}); // return always this
|
|
95
90
|
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
_defineProperty(_assertThisInitialized(_this), "getFingerprint", function () {
|
|
94
|
+
return (0, _fingerprint["default"])(_this.toJSON());
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
_defineProperty(_assertThisInitialized(_this), "getRelation", function (type, filterFunction) {
|
|
98
|
+
return _this.getModel().getRelation(_assertThisInitialized(_this), type, filterFunction);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
_defineProperty(_assertThisInitialized(_this), "save", function () {
|
|
102
|
+
return _this.getModel().getStore().save([_assertThisInitialized(_this)]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
_defineProperty(_assertThisInitialized(_this), "destroy", function () {
|
|
106
|
+
return _this.getModel().getStore()["delete"]([_assertThisInitialized(_this)]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
_defineProperty(_assertThisInitialized(_this), "update", function () {
|
|
110
|
+
return _this.getModel().getStore().update([_assertThisInitialized(_this)]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
Object.keys(values).forEach(function (key) {
|
|
114
|
+
var value = values[key];
|
|
115
|
+
|
|
116
|
+
if (_model.options.parseMoment && _BasicObj2.dateRegex.test(value)) {
|
|
117
|
+
var mmnt = (0, _moment["default"])(value);
|
|
118
|
+
_this[key] = mmnt.isValid() ? mmnt : value;
|
|
119
|
+
} else if (_model.options.deep && _typeof(value) === "object" && !Array.isArray(value)) {
|
|
120
|
+
_this[key] = new _SubObj["default"](_assertThisInitialized(_this), key, value, _model);
|
|
121
|
+
} else if (_model.options.deep && Array.isArray(value)) {
|
|
122
|
+
_this[key] = value.map(function (i) {
|
|
123
|
+
return new _SubObj["default"](_assertThisInitialized(_this), key, i, _model);
|
|
124
|
+
});
|
|
125
|
+
} else {
|
|
126
|
+
_this[key] = value;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
96
129
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return _this.getModel().getStore().update([_this]);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
_defineProperty(this, "setConstant", function (attribute, value) {
|
|
104
|
-
var _classPrivateFieldGet3;
|
|
105
|
-
|
|
106
|
-
_classPrivateFieldGet(_this, _setHidden)[attribute] = (_classPrivateFieldGet3 = _classPrivateFieldGet(_this, _setHidden)[attribute]) !== null && _classPrivateFieldGet3 !== void 0 ? _classPrivateFieldGet3 : value;
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
_defineProperty(this, "save", function () {
|
|
110
|
-
return _this.getModel().getStore().save([_this]);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
_defineProperty(this, "destroy", function () {
|
|
114
|
-
return _this.getModel().getStore()["delete"]([_this]);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
_defineProperty(this, "toJSON", function () {
|
|
118
|
-
var attrs = Object.keys(_this);
|
|
119
|
-
var out = {};
|
|
120
|
-
|
|
121
|
-
for (var _i = 0, _attrs = attrs; _i < _attrs.length; _i++) {
|
|
122
|
-
var a = _attrs[_i];
|
|
130
|
+
_this.getModel = function () {
|
|
131
|
+
return _model;
|
|
132
|
+
};
|
|
123
133
|
|
|
124
|
-
|
|
125
|
-
out[a] = _this[a].toISOString();
|
|
126
|
-
} else if (_this[a] instanceof Date) {
|
|
127
|
-
out[a] = (0, _moment["default"])(_this[a]).toISOString();
|
|
128
|
-
} else if (typeof _this[a] !== "function") {
|
|
129
|
-
out[a] = _this[a];
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return out;
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
_defineProperty(this, "toString", function () {
|
|
137
|
-
return JSON.stringify(_this.toJSON());
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
this.getModel = function () {
|
|
141
|
-
return _model;
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
var frEach = _model.options.parseMoment ? function (key) {
|
|
145
|
-
if (dateRegex.test(values[key])) {
|
|
146
|
-
var mmnt = (0, _moment["default"])(values[key]);
|
|
147
|
-
_this[key] = mmnt.isValid() ? mmnt : values[key];
|
|
148
|
-
} else {
|
|
149
|
-
_this[key] = values[key];
|
|
150
|
-
}
|
|
151
|
-
} : function (key) {
|
|
152
|
-
return _this[key] = values[key];
|
|
153
|
-
};
|
|
154
|
-
Object.keys(values).forEach(frEach);
|
|
155
|
-
var id;
|
|
156
|
-
|
|
157
|
-
if (this.id && (typeof this.id === "string" || typeof this.id === "number")) {
|
|
158
|
-
id = this.id.toString();
|
|
159
|
-
} else {
|
|
160
|
-
id = (0, _uuid.v4)();
|
|
134
|
+
return _this;
|
|
161
135
|
}
|
|
162
136
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
};
|
|
166
|
-
});
|
|
137
|
+
return _createClass(Obj);
|
|
138
|
+
}(_BasicObj2.BasicObj);
|
|
167
139
|
|
|
168
140
|
exports["default"] = Obj;
|
package/dist/SubObj.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _BasicObj2 = require("./BasicObj");
|
|
9
|
+
|
|
10
|
+
var _moment = _interopRequireDefault(require("moment/moment"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
+
|
|
14
|
+
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); }
|
|
15
|
+
|
|
16
|
+
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
|
+
|
|
18
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
19
|
+
|
|
20
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
21
|
+
|
|
22
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
23
|
+
|
|
24
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
25
|
+
|
|
26
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
27
|
+
|
|
28
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
29
|
+
|
|
30
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
31
|
+
|
|
32
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
33
|
+
|
|
34
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
35
|
+
|
|
36
|
+
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; }
|
|
37
|
+
|
|
38
|
+
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
39
|
+
|
|
40
|
+
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
41
|
+
|
|
42
|
+
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
|
43
|
+
|
|
44
|
+
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
|
45
|
+
|
|
46
|
+
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
|
47
|
+
|
|
48
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
49
|
+
|
|
50
|
+
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; } }
|
|
51
|
+
|
|
52
|
+
var _model = /*#__PURE__*/new WeakMap();
|
|
53
|
+
|
|
54
|
+
var _parent = /*#__PURE__*/new WeakMap();
|
|
55
|
+
|
|
56
|
+
var _parentField = /*#__PURE__*/new WeakMap();
|
|
57
|
+
|
|
58
|
+
var SubObj = /*#__PURE__*/function (_BasicObj) {
|
|
59
|
+
_inherits(SubObj, _BasicObj);
|
|
60
|
+
|
|
61
|
+
var _super = _createSuper(SubObj);
|
|
62
|
+
|
|
63
|
+
function SubObj(parent, field, values, model) {
|
|
64
|
+
var _this;
|
|
65
|
+
|
|
66
|
+
_classCallCheck(this, SubObj);
|
|
67
|
+
|
|
68
|
+
_this = _super.call(this, values, model);
|
|
69
|
+
|
|
70
|
+
_classPrivateFieldInitSpec(_assertThisInitialized(_this), _model, {
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
_classPrivateFieldInitSpec(_assertThisInitialized(_this), _parent, {
|
|
76
|
+
writable: true,
|
|
77
|
+
value: void 0
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
_classPrivateFieldInitSpec(_assertThisInitialized(_this), _parentField, {
|
|
81
|
+
writable: true,
|
|
82
|
+
value: void 0
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
_defineProperty(_assertThisInitialized(_this), "save", function () {
|
|
86
|
+
return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore().save([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
_defineProperty(_assertThisInitialized(_this), "destroy", function () {
|
|
90
|
+
var _classPrivateFieldGet2;
|
|
91
|
+
|
|
92
|
+
if (Array.isArray(_classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)])) {
|
|
93
|
+
_classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)] = _classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)].filter(function (i) {
|
|
94
|
+
return i.getId() !== _this.getId();
|
|
95
|
+
});
|
|
96
|
+
} else if ((_classPrivateFieldGet2 = _classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)]) !== null && _classPrivateFieldGet2 !== void 0 && _classPrivateFieldGet2.getId) {
|
|
97
|
+
_classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)] = null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore().update([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
_defineProperty(_assertThisInitialized(_this), "update", function () {
|
|
104
|
+
return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore().update([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
_classPrivateFieldSet(_assertThisInitialized(_this), _model, model);
|
|
108
|
+
|
|
109
|
+
_classPrivateFieldSet(_assertThisInitialized(_this), _parent, parent);
|
|
110
|
+
|
|
111
|
+
_classPrivateFieldSet(_assertThisInitialized(_this), _parentField, field);
|
|
112
|
+
|
|
113
|
+
Object.keys(values).forEach(function (key) {
|
|
114
|
+
var value = values[key];
|
|
115
|
+
|
|
116
|
+
if (model.options.parseMoment && _BasicObj2.dateRegex.test(value)) {
|
|
117
|
+
var mmnt = (0, _moment["default"])(value);
|
|
118
|
+
_this[key] = mmnt.isValid() ? mmnt : value;
|
|
119
|
+
} else if (model.options.deep && _typeof(value) === "object" && !Array.isArray(value)) {
|
|
120
|
+
_this[key] = new SubObj(_classPrivateFieldGet(_assertThisInitialized(_this), _parent), key, value, model);
|
|
121
|
+
} else if (model.options.deep && Array.isArray(value)) {
|
|
122
|
+
_this[key] = value.map(function (i) {
|
|
123
|
+
return new SubObj(_classPrivateFieldGet(_assertThisInitialized(_this), _parent), key, i, model);
|
|
124
|
+
});
|
|
125
|
+
} else {
|
|
126
|
+
_this[key] = value;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return _this;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return _createClass(SubObj);
|
|
133
|
+
}(_BasicObj2.BasicObj);
|
|
134
|
+
|
|
135
|
+
exports["default"] = SubObj;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dataflux",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.3",
|
|
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",
|