dataflux 1.0.4 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +270 -127
- package/dist/Model.js +241 -57
- package/dist/ObserverStore.js +178 -131
- package/dist/PersistentStore.js +104 -75
- package/dist/PubSub.js +59 -0
- package/dist/ReactStore.js +22 -2
- package/dist/Store.js +295 -151
- package/dist/StoreObject.js +33 -0
- package/dist/fingerprint.js +8 -12
- package/dist/modelHooksUtils.js +61 -19
- package/package.json +6 -5
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,160 @@ 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
|
-
|
|
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 _loadFunction = /*#__PURE__*/new WeakMap();
|
|
73
|
+
|
|
74
|
+
var _addRelationByField = /*#__PURE__*/new WeakMap();
|
|
75
|
+
|
|
76
|
+
var _addRelationByFilter = /*#__PURE__*/new WeakMap();
|
|
77
|
+
|
|
78
|
+
var _bulkOperation = /*#__PURE__*/new WeakMap();
|
|
79
|
+
|
|
80
|
+
var _toArray = /*#__PURE__*/new WeakMap();
|
|
81
|
+
|
|
82
|
+
var _insertObjects = /*#__PURE__*/new WeakMap();
|
|
83
|
+
|
|
84
|
+
var _updateObjects = /*#__PURE__*/new WeakMap();
|
|
85
|
+
|
|
86
|
+
var _deleteObjects = /*#__PURE__*/new WeakMap();
|
|
87
|
+
|
|
88
|
+
var Model = /*#__PURE__*/_createClass(function Model(name) {
|
|
37
89
|
var _this = this;
|
|
38
90
|
|
|
91
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
92
|
+
|
|
39
93
|
_classCallCheck(this, Model);
|
|
40
94
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
} else {
|
|
45
|
-
return action(objects);
|
|
46
|
-
}
|
|
95
|
+
_classPrivateFieldInitSpec(this, _type, {
|
|
96
|
+
writable: true,
|
|
97
|
+
value: void 0
|
|
47
98
|
});
|
|
48
99
|
|
|
49
|
-
|
|
50
|
-
|
|
100
|
+
_classPrivateFieldInitSpec(this, _store, {
|
|
101
|
+
writable: true,
|
|
102
|
+
value: void 0
|
|
51
103
|
});
|
|
52
104
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
} else {
|
|
57
|
-
_this.__singleItemQuery = true;
|
|
58
|
-
return [data];
|
|
59
|
-
}
|
|
105
|
+
_classPrivateFieldInitSpec(this, _includes, {
|
|
106
|
+
writable: true,
|
|
107
|
+
value: void 0
|
|
60
108
|
});
|
|
61
109
|
|
|
62
|
-
|
|
63
|
-
|
|
110
|
+
_classPrivateFieldInitSpec(this, _retrieveHook, {
|
|
111
|
+
writable: true,
|
|
112
|
+
value: void 0
|
|
64
113
|
});
|
|
65
114
|
|
|
66
|
-
|
|
67
|
-
|
|
115
|
+
_classPrivateFieldInitSpec(this, _updateHook, {
|
|
116
|
+
writable: true,
|
|
117
|
+
value: void 0
|
|
68
118
|
});
|
|
69
119
|
|
|
70
|
-
|
|
71
|
-
|
|
120
|
+
_classPrivateFieldInitSpec(this, _insertHook, {
|
|
121
|
+
writable: true,
|
|
122
|
+
value: void 0
|
|
72
123
|
});
|
|
73
124
|
|
|
74
|
-
|
|
75
|
-
|
|
125
|
+
_classPrivateFieldInitSpec(this, _deleteHook, {
|
|
126
|
+
writable: true,
|
|
127
|
+
value: void 0
|
|
76
128
|
});
|
|
77
129
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
130
|
+
_classPrivateFieldInitSpec(this, _singleItemQuery, {
|
|
131
|
+
writable: true,
|
|
132
|
+
value: void 0
|
|
81
133
|
});
|
|
82
134
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
135
|
+
_classPrivateFieldInitSpec(this, _batchSize, {
|
|
136
|
+
writable: true,
|
|
137
|
+
value: void 0
|
|
86
138
|
});
|
|
87
139
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
140
|
+
_classPrivateFieldInitSpec(this, _axios, {
|
|
141
|
+
writable: true,
|
|
142
|
+
value: void 0
|
|
91
143
|
});
|
|
92
144
|
|
|
93
|
-
|
|
94
|
-
|
|
145
|
+
_classPrivateFieldInitSpec(this, _loadFunction, {
|
|
146
|
+
writable: true,
|
|
147
|
+
value: void 0
|
|
95
148
|
});
|
|
96
149
|
|
|
97
|
-
_defineProperty(this, "
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
var filterFunction = function filterFunction(parentObject, child) {
|
|
101
|
-
return parentObject[localField] === child[remoteField];
|
|
102
|
-
};
|
|
150
|
+
_defineProperty(this, "getStore", function () {
|
|
151
|
+
return _classPrivateFieldGet(_this, _store);
|
|
152
|
+
});
|
|
103
153
|
|
|
104
|
-
|
|
154
|
+
_defineProperty(this, "setStore", function (store) {
|
|
155
|
+
if (!_classPrivateFieldGet(_this, _store)) {
|
|
156
|
+
_classPrivateFieldSet(_this, _store, store);
|
|
157
|
+
} else {
|
|
158
|
+
throw new Error("This model was already assigned to a store.");
|
|
159
|
+
}
|
|
105
160
|
});
|
|
106
161
|
|
|
107
|
-
_defineProperty(this, "
|
|
108
|
-
|
|
109
|
-
|
|
162
|
+
_defineProperty(this, "load", function (obj) {
|
|
163
|
+
return new Promise(function (resolve, reject) {
|
|
164
|
+
var applyData = function applyData(data) {
|
|
165
|
+
for (var att in data) {
|
|
166
|
+
if (att !== "id" || obj.id === undefined || att === "id" && obj.id === data.id) {
|
|
167
|
+
obj[att] = data[att];
|
|
168
|
+
} else {
|
|
169
|
+
return Promise.reject("The loading function cannot change the id of the object.");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
if (_classPrivateFieldGet(_this, _loadFunction)) {
|
|
175
|
+
var res = _classPrivateFieldGet(_this, _loadFunction).call(_this, obj.toJson());
|
|
176
|
+
|
|
177
|
+
if (typeof res === "string") {
|
|
178
|
+
_classPrivateFieldGet(_this, _axios).call(_this, {
|
|
179
|
+
method: "get",
|
|
180
|
+
url: res,
|
|
181
|
+
responseType: "json"
|
|
182
|
+
}).then(function (data) {
|
|
183
|
+
return applyData(data.data);
|
|
184
|
+
}).then(resolve);
|
|
185
|
+
} else {
|
|
186
|
+
res.then(applyData).then(resolve);
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
reject("You must define a loading function in the model to enable load().");
|
|
190
|
+
}
|
|
191
|
+
});
|
|
110
192
|
});
|
|
111
193
|
|
|
112
194
|
_defineProperty(this, "addRelation", function (model, param2, param3) {
|
|
@@ -115,13 +197,13 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
|
|
|
115
197
|
|
|
116
198
|
if (typeof param2 === "string" && (!param3 || typeof param3 === "string")) {
|
|
117
199
|
// explicit model, from, to
|
|
118
|
-
return _this.
|
|
200
|
+
return _classPrivateFieldGet(_this, _addRelationByField).call(_this, model, param2, param3);
|
|
119
201
|
} else if (!param3 && typeof param2 === "function") {
|
|
120
202
|
// explicit model, filterFunction
|
|
121
|
-
return _this.
|
|
203
|
+
return _classPrivateFieldGet(_this, _addRelationByFilter).call(_this, model, param2);
|
|
122
204
|
} else if (!param2 && !param3) {
|
|
123
205
|
// implicit model, from, to (it uses the type as local key and the id as remote key)
|
|
124
|
-
return _this.
|
|
206
|
+
return _classPrivateFieldGet(_this, _addRelationByField).call(_this, model, model.getType(), "id");
|
|
125
207
|
} else {
|
|
126
208
|
throw new Error("Invalid relation declaration");
|
|
127
209
|
}
|
|
@@ -131,7 +213,7 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
|
|
|
131
213
|
});
|
|
132
214
|
|
|
133
215
|
_defineProperty(this, "getRelation", function (parentObject, includedType, filterFunction) {
|
|
134
|
-
var filterRelation = _this
|
|
216
|
+
var filterRelation = _classPrivateFieldGet(_this, _includes)[includedType];
|
|
135
217
|
|
|
136
218
|
if (filterRelation) {
|
|
137
219
|
return _this.getStore().find(includedType, function (item) {
|
|
@@ -148,12 +230,110 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
|
|
|
148
230
|
}
|
|
149
231
|
});
|
|
150
232
|
|
|
151
|
-
this
|
|
233
|
+
_defineProperty(this, "getType", function () {
|
|
234
|
+
return _classPrivateFieldGet(_this, _type);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
_defineProperty(this, "retrieveAll", function () {
|
|
238
|
+
return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(_this, _retrieveHook), null, _classPrivateFieldGet(_this, _axios)).then(_classPrivateFieldGet(_this, _toArray));
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
_defineProperty(this, "insertObjects", function (objects) {
|
|
242
|
+
return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _insertObjects)) : Promise.resolve();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
_defineProperty(this, "updateObjects", function (objects) {
|
|
246
|
+
return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _updateObjects)) : Promise.resolve();
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
_defineProperty(this, "deleteObjects", function (objects) {
|
|
250
|
+
return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _deleteObjects)) : Promise.resolve();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
_classPrivateFieldInitSpec(this, _addRelationByField, {
|
|
254
|
+
writable: true,
|
|
255
|
+
value: function value(model, localField) {
|
|
256
|
+
var remoteField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "id";
|
|
257
|
+
|
|
258
|
+
var filterFunction = function filterFunction(parentObject, child) {
|
|
259
|
+
return parentObject[localField] === child[remoteField];
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
return _classPrivateFieldGet(_this, _addRelationByFilter).call(_this, model, filterFunction);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
_classPrivateFieldInitSpec(this, _addRelationByFilter, {
|
|
267
|
+
writable: true,
|
|
268
|
+
value: function value(model, filterFunction) {
|
|
269
|
+
var includedType = model.getType();
|
|
270
|
+
_classPrivateFieldGet(_this, _includes)[includedType] = filterFunction;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
_classPrivateFieldInitSpec(this, _bulkOperation, {
|
|
275
|
+
writable: true,
|
|
276
|
+
value: function value(objects, action) {
|
|
277
|
+
if (_classPrivateFieldGet(_this, _singleItemQuery)) {
|
|
278
|
+
return (0, _batchPromises["default"])(_classPrivateFieldGet(_this, _batchSize), objects, action);
|
|
279
|
+
} else {
|
|
280
|
+
return action(objects);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
_classPrivateFieldInitSpec(this, _toArray, {
|
|
286
|
+
writable: true,
|
|
287
|
+
value: function value(data) {
|
|
288
|
+
if (Array.isArray(data)) {
|
|
289
|
+
return data;
|
|
290
|
+
} else {
|
|
291
|
+
_classPrivateFieldSet(_this, _singleItemQuery, true);
|
|
292
|
+
|
|
293
|
+
return [data];
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
_classPrivateFieldInitSpec(this, _insertObjects, {
|
|
299
|
+
writable: true,
|
|
300
|
+
value: function value(data) {
|
|
301
|
+
return (0, _modelHooksUtils.executeHook)("insert", _classPrivateFieldGet(_this, _insertHook), data, _classPrivateFieldGet(_this, _axios)).then(_classPrivateFieldGet(_this, _toArray));
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
_classPrivateFieldInitSpec(this, _updateObjects, {
|
|
306
|
+
writable: true,
|
|
307
|
+
value: function value(data) {
|
|
308
|
+
return (0, _modelHooksUtils.executeHook)("update", _classPrivateFieldGet(_this, _updateHook), data, _classPrivateFieldGet(_this, _axios)).then(_classPrivateFieldGet(_this, _toArray));
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
_classPrivateFieldInitSpec(this, _deleteObjects, {
|
|
313
|
+
writable: true,
|
|
314
|
+
value: function value(data) {
|
|
315
|
+
return (0, _modelHooksUtils.executeHook)("delete", _classPrivateFieldGet(_this, _deleteHook), data, _classPrivateFieldGet(_this, _axios)).then(_classPrivateFieldGet(_this, _toArray));
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
_classPrivateFieldSet(this, _type, name);
|
|
320
|
+
|
|
321
|
+
_classPrivateFieldSet(this, _store, null);
|
|
322
|
+
|
|
323
|
+
_classPrivateFieldSet(this, _includes, {});
|
|
324
|
+
|
|
325
|
+
_classPrivateFieldSet(this, _axios, options.axios || _axios2["default"]);
|
|
326
|
+
|
|
327
|
+
_classPrivateFieldSet(this, _loadFunction, options.load || null);
|
|
152
328
|
|
|
153
329
|
if (!name || !options) {
|
|
154
330
|
throw new Error("A Model requires at least a name and a hook");
|
|
155
331
|
}
|
|
156
332
|
|
|
333
|
+
if (_classPrivateFieldGet(this, _loadFunction) && typeof _classPrivateFieldGet(this, _loadFunction) !== "function") {
|
|
334
|
+
throw new Error("The load option must be a function");
|
|
335
|
+
}
|
|
336
|
+
|
|
157
337
|
var _ref = _typeof(options) === "object" ? (0, _modelHooksUtils.getHooksFromOptions)(options) : (0, _modelHooksUtils.getHooksFromUrl)(options),
|
|
158
338
|
_ref2 = _slicedToArray(_ref, 4),
|
|
159
339
|
retrieveHook = _ref2[0],
|
|
@@ -161,15 +341,19 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
|
|
|
161
341
|
updateHook = _ref2[2],
|
|
162
342
|
deleteHook = _ref2[3];
|
|
163
343
|
|
|
164
|
-
this
|
|
165
|
-
|
|
166
|
-
this
|
|
167
|
-
|
|
168
|
-
this
|
|
344
|
+
_classPrivateFieldSet(this, _retrieveHook, retrieveHook);
|
|
345
|
+
|
|
346
|
+
_classPrivateFieldSet(this, _updateHook, updateHook);
|
|
347
|
+
|
|
348
|
+
_classPrivateFieldSet(this, _insertHook, insertHook);
|
|
349
|
+
|
|
350
|
+
_classPrivateFieldSet(this, _deleteHook, deleteHook);
|
|
351
|
+
|
|
352
|
+
_classPrivateFieldSet(this, _singleItemQuery, false); // By default use arrays
|
|
353
|
+
|
|
169
354
|
|
|
170
|
-
this
|
|
355
|
+
_classPrivateFieldSet(this, _batchSize, 4); // For HTTP requests in parallel if your API doesn't support multiple resources
|
|
171
356
|
|
|
172
|
-
this.__includes = {};
|
|
173
357
|
});
|
|
174
358
|
|
|
175
359
|
exports["default"] = Model;
|