dataflux 1.0.4 → 1.1.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 +188 -130
- package/dist/Model.js +186 -60
- package/dist/ObserverStore.js +178 -131
- package/dist/PersistentStore.js +107 -75
- package/dist/PubSub.js +59 -0
- package/dist/Store.js +283 -152
- package/dist/fingerprint.js +11 -13
- package/dist/modelHooksUtils.js +31 -14
- package/package.json +5 -5
package/dist/Model.js
CHANGED
|
@@ -33,80 +33,112 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
|
|
|
33
33
|
|
|
34
34
|
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
35
|
|
|
36
|
+
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
37
|
+
|
|
38
|
+
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
39
|
+
|
|
40
|
+
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
|
41
|
+
|
|
42
|
+
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
|
43
|
+
|
|
44
|
+
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
|
45
|
+
|
|
46
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
47
|
+
|
|
48
|
+
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; } }
|
|
49
|
+
|
|
50
|
+
var _type = /*#__PURE__*/new WeakMap();
|
|
51
|
+
|
|
52
|
+
var _store = /*#__PURE__*/new WeakMap();
|
|
53
|
+
|
|
54
|
+
var _includes = /*#__PURE__*/new WeakMap();
|
|
55
|
+
|
|
56
|
+
var _retrieveHook = /*#__PURE__*/new WeakMap();
|
|
57
|
+
|
|
58
|
+
var _updateHook = /*#__PURE__*/new WeakMap();
|
|
59
|
+
|
|
60
|
+
var _insertHook = /*#__PURE__*/new WeakMap();
|
|
61
|
+
|
|
62
|
+
var _deleteHook = /*#__PURE__*/new WeakMap();
|
|
63
|
+
|
|
64
|
+
var _singleItemQuery = /*#__PURE__*/new WeakMap();
|
|
65
|
+
|
|
66
|
+
var _batchSize = /*#__PURE__*/new WeakMap();
|
|
67
|
+
|
|
68
|
+
var _addRelationByField = /*#__PURE__*/new WeakMap();
|
|
69
|
+
|
|
70
|
+
var _addRelationByFilter = /*#__PURE__*/new WeakMap();
|
|
71
|
+
|
|
72
|
+
var _bulkOperation = /*#__PURE__*/new WeakMap();
|
|
73
|
+
|
|
74
|
+
var _toArray = /*#__PURE__*/new WeakMap();
|
|
75
|
+
|
|
76
|
+
var _insertObjects = /*#__PURE__*/new WeakMap();
|
|
77
|
+
|
|
78
|
+
var _updateObjects = /*#__PURE__*/new WeakMap();
|
|
79
|
+
|
|
80
|
+
var _deleteObjects = /*#__PURE__*/new WeakMap();
|
|
81
|
+
|
|
36
82
|
var Model = /*#__PURE__*/_createClass(function Model(name, options) {
|
|
37
83
|
var _this = this;
|
|
38
84
|
|
|
39
85
|
_classCallCheck(this, Model);
|
|
40
86
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
} else {
|
|
45
|
-
return action(objects);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
_defineProperty(this, "getType", function () {
|
|
50
|
-
return _this.__type;
|
|
87
|
+
_classPrivateFieldInitSpec(this, _type, {
|
|
88
|
+
writable: true,
|
|
89
|
+
value: void 0
|
|
51
90
|
});
|
|
52
91
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
} else {
|
|
57
|
-
_this.__singleItemQuery = true;
|
|
58
|
-
return [data];
|
|
59
|
-
}
|
|
92
|
+
_classPrivateFieldInitSpec(this, _store, {
|
|
93
|
+
writable: true,
|
|
94
|
+
value: void 0
|
|
60
95
|
});
|
|
61
96
|
|
|
62
|
-
|
|
63
|
-
|
|
97
|
+
_classPrivateFieldInitSpec(this, _includes, {
|
|
98
|
+
writable: true,
|
|
99
|
+
value: void 0
|
|
64
100
|
});
|
|
65
101
|
|
|
66
|
-
|
|
67
|
-
|
|
102
|
+
_classPrivateFieldInitSpec(this, _retrieveHook, {
|
|
103
|
+
writable: true,
|
|
104
|
+
value: void 0
|
|
68
105
|
});
|
|
69
106
|
|
|
70
|
-
|
|
71
|
-
|
|
107
|
+
_classPrivateFieldInitSpec(this, _updateHook, {
|
|
108
|
+
writable: true,
|
|
109
|
+
value: void 0
|
|
72
110
|
});
|
|
73
111
|
|
|
74
|
-
|
|
75
|
-
|
|
112
|
+
_classPrivateFieldInitSpec(this, _insertHook, {
|
|
113
|
+
writable: true,
|
|
114
|
+
value: void 0
|
|
76
115
|
});
|
|
77
116
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
117
|
+
_classPrivateFieldInitSpec(this, _deleteHook, {
|
|
118
|
+
writable: true,
|
|
119
|
+
value: void 0
|
|
81
120
|
});
|
|
82
121
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
122
|
+
_classPrivateFieldInitSpec(this, _singleItemQuery, {
|
|
123
|
+
writable: true,
|
|
124
|
+
value: void 0
|
|
86
125
|
});
|
|
87
126
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
127
|
+
_classPrivateFieldInitSpec(this, _batchSize, {
|
|
128
|
+
writable: true,
|
|
129
|
+
value: void 0
|
|
91
130
|
});
|
|
92
131
|
|
|
93
132
|
_defineProperty(this, "getStore", function () {
|
|
94
|
-
return _this
|
|
133
|
+
return _classPrivateFieldGet(_this, _store);
|
|
95
134
|
});
|
|
96
135
|
|
|
97
|
-
_defineProperty(this, "
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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;
|
|
136
|
+
_defineProperty(this, "setStore", function (store) {
|
|
137
|
+
if (!_classPrivateFieldGet(_this, _store)) {
|
|
138
|
+
_classPrivateFieldSet(_this, _store, store);
|
|
139
|
+
} else {
|
|
140
|
+
throw new Error("This model was already assigned to a store.");
|
|
141
|
+
}
|
|
110
142
|
});
|
|
111
143
|
|
|
112
144
|
_defineProperty(this, "addRelation", function (model, param2, param3) {
|
|
@@ -115,13 +147,13 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
|
|
|
115
147
|
|
|
116
148
|
if (typeof param2 === "string" && (!param3 || typeof param3 === "string")) {
|
|
117
149
|
// explicit model, from, to
|
|
118
|
-
return _this.
|
|
150
|
+
return _classPrivateFieldGet(_this, _addRelationByField).call(_this, model, param2, param3);
|
|
119
151
|
} else if (!param3 && typeof param2 === "function") {
|
|
120
152
|
// explicit model, filterFunction
|
|
121
|
-
return _this.
|
|
153
|
+
return _classPrivateFieldGet(_this, _addRelationByFilter).call(_this, model, param2);
|
|
122
154
|
} else if (!param2 && !param3) {
|
|
123
155
|
// implicit model, from, to (it uses the type as local key and the id as remote key)
|
|
124
|
-
return _this.
|
|
156
|
+
return _classPrivateFieldGet(_this, _addRelationByField).call(_this, model, model.getType(), "id");
|
|
125
157
|
} else {
|
|
126
158
|
throw new Error("Invalid relation declaration");
|
|
127
159
|
}
|
|
@@ -131,7 +163,7 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
|
|
|
131
163
|
});
|
|
132
164
|
|
|
133
165
|
_defineProperty(this, "getRelation", function (parentObject, includedType, filterFunction) {
|
|
134
|
-
var filterRelation = _this
|
|
166
|
+
var filterRelation = _classPrivateFieldGet(_this, _includes)[includedType];
|
|
135
167
|
|
|
136
168
|
if (filterRelation) {
|
|
137
169
|
return _this.getStore().find(includedType, function (item) {
|
|
@@ -148,7 +180,97 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
|
|
|
148
180
|
}
|
|
149
181
|
});
|
|
150
182
|
|
|
151
|
-
this
|
|
183
|
+
_defineProperty(this, "getType", function () {
|
|
184
|
+
return _classPrivateFieldGet(_this, _type);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
_defineProperty(this, "retrieveAll", function () {
|
|
188
|
+
return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(_this, _retrieveHook), null, _this.getStore().axios).then(_classPrivateFieldGet(_this, _toArray));
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
_defineProperty(this, "insertObjects", function (objects) {
|
|
192
|
+
return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _insertObjects)) : Promise.resolve();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
_defineProperty(this, "updateObjects", function (objects) {
|
|
196
|
+
return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _updateObjects)) : Promise.resolve();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
_defineProperty(this, "deleteObjects", function (objects) {
|
|
200
|
+
return objects.length ? _classPrivateFieldGet(_this, _bulkOperation).call(_this, objects, _classPrivateFieldGet(_this, _deleteObjects)) : Promise.resolve();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
_classPrivateFieldInitSpec(this, _addRelationByField, {
|
|
204
|
+
writable: true,
|
|
205
|
+
value: function value(model, localField) {
|
|
206
|
+
var remoteField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "id";
|
|
207
|
+
|
|
208
|
+
var filterFunction = function filterFunction(parentObject, child) {
|
|
209
|
+
return parentObject[localField] === child[remoteField];
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
return _classPrivateFieldGet(_this, _addRelationByFilter).call(_this, model, filterFunction);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
_classPrivateFieldInitSpec(this, _addRelationByFilter, {
|
|
217
|
+
writable: true,
|
|
218
|
+
value: function value(model, filterFunction) {
|
|
219
|
+
var includedType = model.getType();
|
|
220
|
+
_classPrivateFieldGet(_this, _includes)[includedType] = filterFunction;
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
_classPrivateFieldInitSpec(this, _bulkOperation, {
|
|
225
|
+
writable: true,
|
|
226
|
+
value: function value(objects, action) {
|
|
227
|
+
if (_classPrivateFieldGet(_this, _singleItemQuery)) {
|
|
228
|
+
return (0, _batchPromises["default"])(_classPrivateFieldGet(_this, _batchSize), objects, action);
|
|
229
|
+
} else {
|
|
230
|
+
return action(objects);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
_classPrivateFieldInitSpec(this, _toArray, {
|
|
236
|
+
writable: true,
|
|
237
|
+
value: function value(data) {
|
|
238
|
+
if (Array.isArray(data)) {
|
|
239
|
+
return data;
|
|
240
|
+
} else {
|
|
241
|
+
_classPrivateFieldSet(_this, _singleItemQuery, true);
|
|
242
|
+
|
|
243
|
+
return [data];
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
_classPrivateFieldInitSpec(this, _insertObjects, {
|
|
249
|
+
writable: true,
|
|
250
|
+
value: function value(data) {
|
|
251
|
+
return (0, _modelHooksUtils.executeHook)("insert", _classPrivateFieldGet(_this, _insertHook), data, _this.getStore().axios).then(_classPrivateFieldGet(_this, _toArray));
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
_classPrivateFieldInitSpec(this, _updateObjects, {
|
|
256
|
+
writable: true,
|
|
257
|
+
value: function value(data) {
|
|
258
|
+
return (0, _modelHooksUtils.executeHook)("update", _classPrivateFieldGet(_this, _updateHook), data, _this.getStore().axios).then(_classPrivateFieldGet(_this, _toArray));
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
_classPrivateFieldInitSpec(this, _deleteObjects, {
|
|
263
|
+
writable: true,
|
|
264
|
+
value: function value(data) {
|
|
265
|
+
return (0, _modelHooksUtils.executeHook)("delete", _classPrivateFieldGet(_this, _deleteHook), data, _this.getStore().axios).then(_classPrivateFieldGet(_this, _toArray));
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
_classPrivateFieldSet(this, _type, name);
|
|
270
|
+
|
|
271
|
+
_classPrivateFieldSet(this, _store, null);
|
|
272
|
+
|
|
273
|
+
_classPrivateFieldSet(this, _includes, {});
|
|
152
274
|
|
|
153
275
|
if (!name || !options) {
|
|
154
276
|
throw new Error("A Model requires at least a name and a hook");
|
|
@@ -161,15 +283,19 @@ var Model = /*#__PURE__*/_createClass(function Model(name, options) {
|
|
|
161
283
|
updateHook = _ref2[2],
|
|
162
284
|
deleteHook = _ref2[3];
|
|
163
285
|
|
|
164
|
-
this
|
|
165
|
-
|
|
166
|
-
this
|
|
167
|
-
|
|
168
|
-
this
|
|
286
|
+
_classPrivateFieldSet(this, _retrieveHook, retrieveHook);
|
|
287
|
+
|
|
288
|
+
_classPrivateFieldSet(this, _updateHook, updateHook);
|
|
289
|
+
|
|
290
|
+
_classPrivateFieldSet(this, _insertHook, insertHook);
|
|
291
|
+
|
|
292
|
+
_classPrivateFieldSet(this, _deleteHook, deleteHook);
|
|
293
|
+
|
|
294
|
+
_classPrivateFieldSet(this, _singleItemQuery, false); // By default use arrays
|
|
295
|
+
|
|
169
296
|
|
|
170
|
-
this
|
|
297
|
+
_classPrivateFieldSet(this, _batchSize, 4); // For HTTP requests in parallel if your API doesn't support multiple resources
|
|
171
298
|
|
|
172
|
-
this.__includes = {};
|
|
173
299
|
});
|
|
174
300
|
|
|
175
301
|
exports["default"] = Model;
|