dataflux 1.5.0 → 1.5.4

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 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
- ["book", ({title}) => title === "The little prince"], // Model name and filter function
178
- ["author"], // No filter function, all objects returned
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
- // Objects are ready
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
@@ -493,11 +493,47 @@ Each object created is enriched with the following methods.
493
493
  | getRelation(model, filterFunction) | To get all the objects respecting a specific relation with this object (see [model relations](#model-relations)). |
494
494
  | save() | Method to save the object. You can do `store.save()` instead. |
495
495
  | destroy() | Method to delete the object. You can do `store.delete()` instead. |
496
-
497
496
  | toJSON() | It returns a pure JSON representation of the object. |
498
497
  | toString() | It returns a string representation of the object. |
499
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. |
500
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
+
501
537
 
502
538
  ## Editing objects
503
539
  The option `autoSave` can be `true`, `false`, or a number (milliseconds).
package/dist/BasicObj.js CHANGED
@@ -4,9 +4,12 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.dateRegex = exports.BasicObj = void 0;
7
+ exports.setValues = setValues;
7
8
 
8
9
  var _moment = _interopRequireDefault(require("moment/moment"));
9
10
 
11
+ var _uuid = require("uuid");
12
+
10
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
14
 
12
15
  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); } }
@@ -21,17 +24,44 @@ function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedec
21
24
 
22
25
  function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
23
26
 
27
+ function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
28
+
29
+ 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; } }
30
+
24
31
  function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
25
32
 
26
33
  function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
27
34
 
28
35
  function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
29
36
 
37
+ 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); }
38
+
30
39
  var dateRegex = new RegExp("^[0-9][0-9][0-9][0-9]-[0-9].*T[0-9].*Z$");
31
40
  exports.dateRegex = dateRegex;
32
41
 
42
+ function setValues(values, model, SubObj, parent, context) {
43
+ Object.keys(values).forEach(function (key) {
44
+ var value = values[key];
45
+
46
+ if (model.options.parseMoment && value != null && dateRegex.test(value)) {
47
+ var mmnt = (0, _moment["default"])(value);
48
+ context[key] = mmnt.isValid() ? mmnt : value;
49
+ } else if (model.options.deep && value != null && _typeof(value) === "object" && !Array.isArray(value)) {
50
+ context[key] = new SubObj(parent, key, value, model);
51
+ } else if (model.options.deep && value != null && Array.isArray(value)) {
52
+ context[key] = value.map(function (i) {
53
+ return new SubObj(parent, key, i, model);
54
+ });
55
+ } else {
56
+ context[key] = value;
57
+ }
58
+ });
59
+ }
60
+
33
61
  var _setHidden = /*#__PURE__*/new WeakMap();
34
62
 
63
+ var _id = /*#__PURE__*/new WeakMap();
64
+
35
65
  var BasicObj = /*#__PURE__*/_createClass(function BasicObj(values, model) {
36
66
  var _this = this;
37
67
 
@@ -42,6 +72,23 @@ var BasicObj = /*#__PURE__*/_createClass(function BasicObj(values, model) {
42
72
  value: {}
43
73
  });
44
74
 
75
+ _classPrivateFieldInitSpec(this, _id, {
76
+ writable: true,
77
+ value: null
78
+ });
79
+
80
+ _defineProperty(this, "getId", function () {
81
+ if (!_classPrivateFieldGet(_this, _id)) {
82
+ if (_this.id && (typeof _this.id === "string" || typeof _this.id === "number")) {
83
+ _classPrivateFieldSet(_this, _id, _this.id.toString());
84
+ } else {
85
+ _classPrivateFieldSet(_this, _id, (0, _uuid.v4)());
86
+ }
87
+ }
88
+
89
+ return _classPrivateFieldGet(_this, _id);
90
+ });
91
+
45
92
  _defineProperty(this, "get", function (attribute, defaultValue) {
46
93
  var _ref, _classPrivateFieldGet2;
47
94
 
package/dist/Obj.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ 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); }
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
@@ -7,18 +9,12 @@ exports["default"] = void 0;
7
9
 
8
10
  var _fingerprint = _interopRequireDefault(require("./fingerprint"));
9
11
 
10
- var _uuid = require("uuid");
11
-
12
12
  var _BasicObj2 = require("./BasicObj");
13
13
 
14
- var _moment = _interopRequireDefault(require("moment/moment"));
15
-
16
14
  var _SubObj = _interopRequireDefault(require("./SubObj"));
17
15
 
18
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
19
17
 
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
-
22
18
  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); } }
23
19
 
24
20
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
@@ -110,39 +106,12 @@ var Obj = /*#__PURE__*/function (_BasicObj) {
110
106
  return _this.getModel().getStore().update([_assertThisInitialized(_this)]);
111
107
  });
112
108
 
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), 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), i, _model);
124
- });
125
- } else {
126
- _this[key] = value;
127
- }
128
- });
109
+ (0, _BasicObj2.setValues)(values, _model, _SubObj["default"], _assertThisInitialized(_this), _assertThisInitialized(_this));
129
110
 
130
111
  _this.getModel = function () {
131
112
  return _model;
132
113
  };
133
114
 
134
- var id;
135
-
136
- if (_this.id && (typeof _this.id === "string" || typeof _this.id === "number")) {
137
- id = _this.id.toString();
138
- } else {
139
- id = (0, _uuid.v4)();
140
- }
141
-
142
- _this.getId = function () {
143
- return id;
144
- };
145
-
146
115
  return _this;
147
116
  }
148
117
 
package/dist/SubObj.js CHANGED
@@ -1,20 +1,18 @@
1
1
  "use strict";
2
2
 
3
+ 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); }
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
6
8
  exports["default"] = void 0;
7
9
 
8
- var _uuid = require("uuid");
9
-
10
10
  var _BasicObj2 = require("./BasicObj");
11
11
 
12
12
  var _moment = _interopRequireDefault(require("moment/moment"));
13
13
 
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
15
 
16
- 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); }
17
-
18
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); } }
19
17
 
20
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; }
@@ -55,12 +53,14 @@ var _model = /*#__PURE__*/new WeakMap();
55
53
 
56
54
  var _parent = /*#__PURE__*/new WeakMap();
57
55
 
56
+ var _parentField = /*#__PURE__*/new WeakMap();
57
+
58
58
  var SubObj = /*#__PURE__*/function (_BasicObj) {
59
59
  _inherits(SubObj, _BasicObj);
60
60
 
61
61
  var _super = _createSuper(SubObj);
62
62
 
63
- function SubObj(parent, values, model) {
63
+ function SubObj(parent, field, values, model) {
64
64
  var _this;
65
65
 
66
66
  _classCallCheck(this, SubObj);
@@ -77,12 +77,27 @@ var SubObj = /*#__PURE__*/function (_BasicObj) {
77
77
  value: void 0
78
78
  });
79
79
 
80
+ _classPrivateFieldInitSpec(_assertThisInitialized(_this), _parentField, {
81
+ writable: true,
82
+ value: void 0
83
+ });
84
+
80
85
  _defineProperty(_assertThisInitialized(_this), "save", function () {
81
86
  return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore().save([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
82
87
  });
83
88
 
84
89
  _defineProperty(_assertThisInitialized(_this), "destroy", function () {
85
- return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore()["delete"]([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
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)]);
86
101
  });
87
102
 
88
103
  _defineProperty(_assertThisInitialized(_this), "update", function () {
@@ -93,27 +108,9 @@ var SubObj = /*#__PURE__*/function (_BasicObj) {
93
108
 
94
109
  _classPrivateFieldSet(_assertThisInitialized(_this), _parent, parent);
95
110
 
96
- Object.keys(values).forEach(function (key) {
97
- var value = values[key];
98
-
99
- if (model.options.parseMoment && _BasicObj2.dateRegex.test(value)) {
100
- var mmnt = (0, _moment["default"])(value);
101
- _this[key] = mmnt.isValid() ? mmnt : value;
102
- } else if (model.options.deep && _typeof(value) === "object" && !Array.isArray(value)) {
103
- _this[key] = new SubObj(_assertThisInitialized(_this), value, model);
104
- } else if (model.options.deep && Array.isArray(value)) {
105
- _this[key] = value.map(function (i) {
106
- return new SubObj(_assertThisInitialized(_this), i, model);
107
- });
108
- } else {
109
- _this[key] = value;
110
- }
111
- });
112
-
113
- _this.getId = function () {
114
- return (0, _uuid.v4)();
115
- };
111
+ _classPrivateFieldSet(_assertThisInitialized(_this), _parentField, field);
116
112
 
113
+ (0, _BasicObj2.setValues)(values, model, SubObj, _classPrivateFieldGet(_assertThisInitialized(_this), _parent), _assertThisInitialized(_this));
117
114
  return _this;
118
115
  }
119
116
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dataflux",
3
- "version": "1.5.0",
3
+ "version": "1.5.4",
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",