dataflux 1.0.2 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Model.js CHANGED
@@ -9,6 +9,8 @@ var _modelHooksUtils = require("./modelHooksUtils");
9
9
 
10
10
  var _batchPromises = _interopRequireDefault(require("batch-promises"));
11
11
 
12
+ var _axios2 = _interopRequireDefault(require("axios"));
13
+
12
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
15
 
14
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); }
@@ -33,80 +35,121 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
33
35
 
34
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; }
35
37
 
36
- var Model = /*#__PURE__*/_createClass(function Model(name, options) {
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 _type = /*#__PURE__*/new WeakMap();
53
+
54
+ var _store = /*#__PURE__*/new WeakMap();
55
+
56
+ var _includes = /*#__PURE__*/new WeakMap();
57
+
58
+ var _retrieveHook = /*#__PURE__*/new WeakMap();
59
+
60
+ var _updateHook = /*#__PURE__*/new WeakMap();
61
+
62
+ var _insertHook = /*#__PURE__*/new WeakMap();
63
+
64
+ var _deleteHook = /*#__PURE__*/new WeakMap();
65
+
66
+ var _singleItemQuery = /*#__PURE__*/new WeakMap();
67
+
68
+ var _batchSize = /*#__PURE__*/new WeakMap();
69
+
70
+ var _axios = /*#__PURE__*/new WeakMap();
71
+
72
+ var _addRelationByField = /*#__PURE__*/new WeakMap();
73
+
74
+ var _addRelationByFilter = /*#__PURE__*/new WeakMap();
75
+
76
+ var _bulkOperation = /*#__PURE__*/new WeakMap();
77
+
78
+ var _toArray = /*#__PURE__*/new WeakMap();
79
+
80
+ var _insertObjects = /*#__PURE__*/new WeakMap();
81
+
82
+ var _updateObjects = /*#__PURE__*/new WeakMap();
83
+
84
+ var _deleteObjects = /*#__PURE__*/new WeakMap();
85
+
86
+ var Model = /*#__PURE__*/_createClass(function Model(name) {
37
87
  var _this = this;
38
88
 
89
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
90
+
39
91
  _classCallCheck(this, Model);
40
92
 
41
- _defineProperty(this, "_bulkOperation", function (objects, action) {
42
- if (_this.__singleItemQuery) {
43
- return (0, _batchPromises["default"])(_this.__batchSize, objects, action);
44
- } else {
45
- return action(objects);
46
- }
93
+ _classPrivateFieldInitSpec(this, _type, {
94
+ writable: true,
95
+ value: void 0
47
96
  });
48
97
 
49
- _defineProperty(this, "getType", function () {
50
- return _this.__type;
98
+ _classPrivateFieldInitSpec(this, _store, {
99
+ writable: true,
100
+ value: void 0
51
101
  });
52
102
 
53
- _defineProperty(this, "_toArray", function (data) {
54
- if (Array.isArray(data)) {
55
- return data;
56
- } else {
57
- _this.__singleItemQuery = true;
58
- return [data];
59
- }
103
+ _classPrivateFieldInitSpec(this, _includes, {
104
+ writable: true,
105
+ value: void 0
60
106
  });
61
107
 
62
- _defineProperty(this, "retrieveAll", function () {
63
- return (0, _modelHooksUtils.executeHook)("retrieve", _this.__retrieveHook, null).then(_this._toArray);
108
+ _classPrivateFieldInitSpec(this, _retrieveHook, {
109
+ writable: true,
110
+ value: void 0
64
111
  });
65
112
 
66
- _defineProperty(this, "insertObjects", function (objects) {
67
- return _this._bulkOperation(objects, _this._insertObjects);
113
+ _classPrivateFieldInitSpec(this, _updateHook, {
114
+ writable: true,
115
+ value: void 0
68
116
  });
69
117
 
70
- _defineProperty(this, "updateObjects", function (objects) {
71
- return _this._bulkOperation(objects, _this._updateObjects);
118
+ _classPrivateFieldInitSpec(this, _insertHook, {
119
+ writable: true,
120
+ value: void 0
72
121
  });
73
122
 
74
- _defineProperty(this, "deleteObjects", function (objects) {
75
- return _this._bulkOperation(objects, _this._deleteObjects);
123
+ _classPrivateFieldInitSpec(this, _deleteHook, {
124
+ writable: true,
125
+ value: void 0
76
126
  });
77
127
 
78
- _defineProperty(this, "_insertObjects", function (data) {
79
- return Promise.resolve(true);
80
- return (0, _modelHooksUtils.executeHook)("insert", _this.__insertHook, data).then(_this._toArray);
128
+ _classPrivateFieldInitSpec(this, _singleItemQuery, {
129
+ writable: true,
130
+ value: void 0
81
131
  });
82
132
 
83
- _defineProperty(this, "_updateObjects", function (data) {
84
- return Promise.resolve(true);
85
- return (0, _modelHooksUtils.executeHook)("update", _this.__updateHook, data).then(_this._toArray);
133
+ _classPrivateFieldInitSpec(this, _batchSize, {
134
+ writable: true,
135
+ value: void 0
86
136
  });
87
137
 
88
- _defineProperty(this, "_deleteObjects", function (data) {
89
- return Promise.resolve(true);
90
- return (0, _modelHooksUtils.executeHook)("update", _this.__deleteHook, data).then(_this._toArray);
138
+ _classPrivateFieldInitSpec(this, _axios, {
139
+ writable: true,
140
+ value: void 0
91
141
  });
92
142
 
93
143
  _defineProperty(this, "getStore", function () {
94
- return _this.store;
144
+ return _classPrivateFieldGet(_this, _store);
95
145
  });
96
146
 
97
- _defineProperty(this, "addRelationByField", function (model, localField) {
98
- var remoteField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "id";
99
-
100
- var filterFunction = function filterFunction(parentObject, child) {
101
- return parentObject[localField] === child[remoteField];
102
- };
103
-
104
- return _this.addRelationByFilter(model, filterFunction);
105
- });
106
-
107
- _defineProperty(this, "addRelationByFilter", function (model, filterFunction) {
108
- var includedType = model.getType();
109
- _this.__includes[includedType] = filterFunction;
147
+ _defineProperty(this, "setStore", function (store) {
148
+ if (!_classPrivateFieldGet(_this, _store)) {
149
+ _classPrivateFieldSet(_this, _store, store);
150
+ } else {
151
+ throw new Error("This model was already assigned to a store.");
152
+ }
110
153
  });
111
154
 
112
155
  _defineProperty(this, "addRelation", function (model, param2, param3) {
@@ -115,13 +158,13 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
115
158
 
116
159
  if (typeof param2 === "string" && (!param3 || typeof param3 === "string")) {
117
160
  // explicit model, from, to
118
- return _this.addRelationByField(model, param2, param3);
161
+ return _classPrivateFieldGet(_this, _addRelationByField).call(_this, model, param2, param3);
119
162
  } else if (!param3 && typeof param2 === "function") {
120
163
  // explicit model, filterFunction
121
- return _this.addRelationByFilter(model, param2);
164
+ return _classPrivateFieldGet(_this, _addRelationByFilter).call(_this, model, param2);
122
165
  } else if (!param2 && !param3) {
123
166
  // implicit model, from, to (it uses the type as local key and the id as remote key)
124
- return _this.addRelationByField(model, model.getType(), "id");
167
+ return _classPrivateFieldGet(_this, _addRelationByField).call(_this, model, model.getType(), "id");
125
168
  } else {
126
169
  throw new Error("Invalid relation declaration");
127
170
  }
@@ -131,7 +174,7 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
131
174
  });
132
175
 
133
176
  _defineProperty(this, "getRelation", function (parentObject, includedType, filterFunction) {
134
- var filterRelation = _this.__includes[includedType];
177
+ var filterRelation = _classPrivateFieldGet(_this, _includes)[includedType];
135
178
 
136
179
  if (filterRelation) {
137
180
  return _this.getStore().find(includedType, function (item) {
@@ -148,7 +191,99 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
148
191
  }
149
192
  });
150
193
 
151
- this.__type = name;
194
+ _defineProperty(this, "getType", function () {
195
+ return _classPrivateFieldGet(_this, _type);
196
+ });
197
+
198
+ _defineProperty(this, "retrieveAll", function () {
199
+ return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(_this, _retrieveHook), null, _classPrivateFieldGet(_this, _axios)).then(_classPrivateFieldGet(_this, _toArray));
200
+ });
201
+
202
+ _defineProperty(this, "insertObjects", function (objects) {
203
+ return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _insertObjects)) : Promise.resolve();
204
+ });
205
+
206
+ _defineProperty(this, "updateObjects", function (objects) {
207
+ return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _updateObjects)) : Promise.resolve();
208
+ });
209
+
210
+ _defineProperty(this, "deleteObjects", function (objects) {
211
+ return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _deleteObjects)) : Promise.resolve();
212
+ });
213
+
214
+ _classPrivateFieldInitSpec(this, _addRelationByField, {
215
+ writable: true,
216
+ value: function value(model, localField) {
217
+ var remoteField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "id";
218
+
219
+ var filterFunction = function filterFunction(parentObject, child) {
220
+ return parentObject[localField] === child[remoteField];
221
+ };
222
+
223
+ return _classPrivateFieldGet(_this, _addRelationByFilter).call(_this, model, filterFunction);
224
+ }
225
+ });
226
+
227
+ _classPrivateFieldInitSpec(this, _addRelationByFilter, {
228
+ writable: true,
229
+ value: function value(model, filterFunction) {
230
+ var includedType = model.getType();
231
+ _classPrivateFieldGet(_this, _includes)[includedType] = filterFunction;
232
+ }
233
+ });
234
+
235
+ _classPrivateFieldInitSpec(this, _bulkOperation, {
236
+ writable: true,
237
+ value: function value(objects, action) {
238
+ if (_classPrivateFieldGet(_this, _singleItemQuery)) {
239
+ return (0, _batchPromises["default"])(_classPrivateFieldGet(_this, _batchSize), objects, action);
240
+ } else {
241
+ return action(objects);
242
+ }
243
+ }
244
+ });
245
+
246
+ _classPrivateFieldInitSpec(this, _toArray, {
247
+ writable: true,
248
+ value: function value(data) {
249
+ if (Array.isArray(data)) {
250
+ return data;
251
+ } else {
252
+ _classPrivateFieldSet(_this, _singleItemQuery, true);
253
+
254
+ return [data];
255
+ }
256
+ }
257
+ });
258
+
259
+ _classPrivateFieldInitSpec(this, _insertObjects, {
260
+ writable: true,
261
+ value: function value(data) {
262
+ return (0, _modelHooksUtils.executeHook)("insert", _classPrivateFieldGet(_this, _insertHook), data, _classPrivateFieldGet(_this, _axios)).then(_classPrivateFieldGet(_this, _toArray));
263
+ }
264
+ });
265
+
266
+ _classPrivateFieldInitSpec(this, _updateObjects, {
267
+ writable: true,
268
+ value: function value(data) {
269
+ return (0, _modelHooksUtils.executeHook)("update", _classPrivateFieldGet(_this, _updateHook), data, _classPrivateFieldGet(_this, _axios)).then(_classPrivateFieldGet(_this, _toArray));
270
+ }
271
+ });
272
+
273
+ _classPrivateFieldInitSpec(this, _deleteObjects, {
274
+ writable: true,
275
+ value: function value(data) {
276
+ return (0, _modelHooksUtils.executeHook)("delete", _classPrivateFieldGet(_this, _deleteHook), data, _classPrivateFieldGet(_this, _axios)).then(_classPrivateFieldGet(_this, _toArray));
277
+ }
278
+ });
279
+
280
+ _classPrivateFieldSet(this, _type, name);
281
+
282
+ _classPrivateFieldSet(this, _store, null);
283
+
284
+ _classPrivateFieldSet(this, _includes, {});
285
+
286
+ _classPrivateFieldSet(this, _axios, options.axios || _axios2["default"]);
152
287
 
153
288
  if (!name || !options) {
154
289
  throw new Error("A Model requires at least a name and a hook");
@@ -161,15 +296,19 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
161
296
  updateHook = _ref2[2],
162
297
  deleteHook = _ref2[3];
163
298
 
164
- this.__retrieveHook = retrieveHook;
165
- this.__updateHook = updateHook;
166
- this.__insertHook = insertHook;
167
- this.__deleteHook = deleteHook;
168
- this.__singleItemQuery = false; // By default use arrays
299
+ _classPrivateFieldSet(this, _retrieveHook, retrieveHook);
300
+
301
+ _classPrivateFieldSet(this, _updateHook, updateHook);
302
+
303
+ _classPrivateFieldSet(this, _insertHook, insertHook);
304
+
305
+ _classPrivateFieldSet(this, _deleteHook, deleteHook);
306
+
307
+ _classPrivateFieldSet(this, _singleItemQuery, false); // By default use arrays
308
+
169
309
 
170
- this.__batchSize = 4; // For HTTP requests in parallel if your API doesn't support multiple resources
310
+ _classPrivateFieldSet(this, _batchSize, 4); // For HTTP requests in parallel if your API doesn't support multiple resources
171
311
 
172
- this.__includes = {};
173
312
  });
174
313
 
175
314
  exports["default"] = Model;