liwi-subscribe-store 8.3.0 → 8.3.1

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.
@@ -2,524 +2,188 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var _extends = require('@babel/runtime/helpers/esm/extends');
6
- var _asyncToGenerator = require('@babel/runtime/helpers/esm/asyncToGenerator');
7
- var _createClass = require('@babel/runtime/helpers/esm/createClass');
8
- var _regeneratorRuntime = require('@babel/runtime/regenerator');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
11
-
12
- var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
13
- var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
14
- var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
15
- var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
16
-
17
- var AbstractSubscribableStoreQuery = /*#__PURE__*/function () {
18
- function AbstractSubscribableStoreQuery() {}
19
-
20
- var _proto = AbstractSubscribableStoreQuery.prototype;
21
-
22
- _proto.changeParams = function changeParams() {
5
+ class AbstractSubscribableStoreQuery {
6
+ changeParams() {
23
7
  throw new Error('Method not supported. Please create a new query.');
24
- };
8
+ }
25
9
 
26
- _proto.changePartialParams = function changePartialParams() {
10
+ changePartialParams() {
27
11
  throw new Error('Method not supported. Please create a new query.');
28
- };
12
+ }
29
13
 
30
- _proto.setSubscribeStore = function setSubscribeStore(store) {
14
+ setSubscribeStore(store) {
31
15
  this._subscribeStore = store;
32
- };
16
+ }
33
17
 
34
- _proto.getSubscribeStore = function getSubscribeStore() {
18
+ getSubscribeStore() {
35
19
  if (!this._subscribeStore) {
36
20
  throw new Error('_subscribeStore is not initialized');
37
21
  }
38
22
 
39
23
  return this._subscribeStore;
40
- };
24
+ }
41
25
 
42
- _proto.fetchAndSubscribe = function fetchAndSubscribe(callback) {
26
+ fetchAndSubscribe(callback) {
43
27
  return this._subscribe(callback, true);
44
- };
28
+ }
45
29
 
46
- _proto.subscribe = function subscribe(callback) {
30
+ subscribe(callback) {
47
31
  return this._subscribe(callback, false);
48
- };
32
+ }
49
33
 
50
- return AbstractSubscribableStoreQuery;
51
- }();
34
+ }
52
35
 
53
36
  /* eslint-disable max-lines */
54
- var SubscribeStore = /*#__PURE__*/function () {
55
- function SubscribeStore(store) {
37
+ class SubscribeStore {
38
+ constructor(store) {
56
39
  this.listeners = new Set();
57
40
  this.store = store;
58
41
  this.keyPath = store.keyPath;
59
42
  }
60
43
 
61
- var _proto = SubscribeStore.prototype;
62
-
63
- _proto.subscribe = function subscribe(callback) {
64
- var _this = this;
44
+ get connection() {
45
+ return this.store.connection;
46
+ }
65
47
 
48
+ subscribe(callback) {
66
49
  this.listeners.add(callback);
67
- return function () {
68
- return _this.listeners.delete(callback);
69
- };
70
- };
71
-
72
- _proto.callSubscribed = function callSubscribed(action) {
73
- this.listeners.forEach(function (listener) {
74
- return listener(action);
75
- });
76
- };
50
+ return () => this.listeners.delete(callback);
51
+ }
52
+
53
+ callSubscribed(action) {
54
+ this.listeners.forEach(listener => listener(action));
55
+ }
77
56
 
78
- _proto.createQuerySingleItem = function createQuerySingleItem(options, transformer) {
79
- var query = this.store.createQuerySingleItem(options, transformer);
57
+ createQuerySingleItem(options, transformer) {
58
+ const query = this.store.createQuerySingleItem(options, transformer);
80
59
  query.setSubscribeStore(this);
81
60
  return query;
82
- };
61
+ }
83
62
 
84
- _proto.createQueryCollection = function createQueryCollection(options, transformer) {
85
- var query = this.store.createQueryCollection(options, transformer);
63
+ createQueryCollection(options, transformer) {
64
+ const query = this.store.createQueryCollection(options, transformer);
86
65
  query.setSubscribeStore(this);
87
66
  return query;
88
- };
67
+ }
89
68
 
90
- _proto.findAll = function findAll(criteria, sort) {
69
+ findAll(criteria, sort) {
91
70
  return this.store.findAll(criteria, sort);
92
- };
71
+ }
93
72
 
94
- _proto.findByKey = function findByKey(key, criteria) {
73
+ findByKey(key, criteria) {
95
74
  return this.store.findByKey(key, criteria);
96
- };
75
+ }
97
76
 
98
- _proto.findOne = function findOne(criteria, sort) {
77
+ findOne(criteria, sort) {
99
78
  return this.store.findOne(criteria, sort);
100
- };
101
-
102
- _proto.insertOne = /*#__PURE__*/function () {
103
- var _insertOne = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee(object) {
104
- var inserted;
105
- return _regeneratorRuntime__default.wrap(function _callee$(_context) {
106
- while (1) {
107
- switch (_context.prev = _context.next) {
108
- case 0:
109
- _context.next = 2;
110
- return this.store.insertOne(object);
111
-
112
- case 2:
113
- inserted = _context.sent;
114
- this.callSubscribed({
115
- type: 'inserted',
116
- next: [inserted]
117
- });
118
- return _context.abrupt("return", inserted);
119
-
120
- case 5:
121
- case "end":
122
- return _context.stop();
123
- }
124
- }
125
- }, _callee, this);
126
- }));
127
-
128
- return function insertOne() {
129
- return _insertOne.apply(this, arguments);
130
- };
131
- }();
132
-
133
- _proto.replaceOne = /*#__PURE__*/function () {
134
- var _replaceOne = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee2(object) {
135
- var replaced;
136
- return _regeneratorRuntime__default.wrap(function _callee2$(_context2) {
137
- while (1) {
138
- switch (_context2.prev = _context2.next) {
139
- case 0:
140
- _context2.next = 2;
141
- return this.store.replaceOne(object);
142
-
143
- case 2:
144
- replaced = _context2.sent;
145
- this.callSubscribed({
146
- type: 'updated',
147
- changes: [[object, replaced]]
148
- });
149
- return _context2.abrupt("return", replaced);
150
-
151
- case 5:
152
- case "end":
153
- return _context2.stop();
154
- }
155
- }
156
- }, _callee2, this);
157
- }));
158
-
159
- return function replaceOne() {
160
- return _replaceOne.apply(this, arguments);
161
- };
162
- }();
163
-
164
- _proto.replaceSeveral = /*#__PURE__*/function () {
165
- var _replaceSeveral = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee3(objects) {
166
- var replacedObjects;
167
- return _regeneratorRuntime__default.wrap(function _callee3$(_context3) {
168
- while (1) {
169
- switch (_context3.prev = _context3.next) {
170
- case 0:
171
- _context3.next = 2;
172
- return this.store.replaceSeveral(objects);
173
-
174
- case 2:
175
- replacedObjects = _context3.sent;
176
- this.callSubscribed({
177
- type: 'updated',
178
- changes: objects.map(function (prev, index) {
179
- return [prev, replacedObjects[index]];
180
- })
181
- });
182
- return _context3.abrupt("return", replacedObjects);
183
-
184
- case 5:
185
- case "end":
186
- return _context3.stop();
187
- }
188
- }
189
- }, _callee3, this);
190
- }));
191
-
192
- return function replaceSeveral() {
193
- return _replaceSeveral.apply(this, arguments);
194
- };
195
- }();
196
-
197
- _proto.upsertOne = /*#__PURE__*/function () {
198
- var _upsertOne = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee4(object, setOnInsertPartialObject) {
199
- var result;
200
- return _regeneratorRuntime__default.wrap(function _callee4$(_context4) {
201
- while (1) {
202
- switch (_context4.prev = _context4.next) {
203
- case 0:
204
- _context4.next = 2;
205
- return this.upsertOneWithInfo(object, setOnInsertPartialObject);
206
-
207
- case 2:
208
- result = _context4.sent;
209
- return _context4.abrupt("return", result.object);
210
-
211
- case 4:
212
- case "end":
213
- return _context4.stop();
214
- }
215
- }
216
- }, _callee4, this);
217
- }));
218
-
219
- return function upsertOne() {
220
- return _upsertOne.apply(this, arguments);
221
- };
222
- }();
223
-
224
- _proto.upsertOneWithInfo = /*#__PURE__*/function () {
225
- var _upsertOneWithInfo = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee5(object, setOnInsertPartialObject) {
226
- var upsertedWithInfo;
227
- return _regeneratorRuntime__default.wrap(function _callee5$(_context5) {
228
- while (1) {
229
- switch (_context5.prev = _context5.next) {
230
- case 0:
231
- _context5.next = 2;
232
- return this.store.upsertOneWithInfo(object, setOnInsertPartialObject);
233
-
234
- case 2:
235
- upsertedWithInfo = _context5.sent;
236
-
237
- if (!upsertedWithInfo.inserted) {
238
- _context5.next = 7;
239
- break;
240
- }
241
-
242
- this.callSubscribed({
243
- type: 'inserted',
244
- next: [upsertedWithInfo.object]
245
- });
246
- _context5.next = 8;
247
- break;
248
-
249
- case 7:
250
- throw new Error('TODO');
251
-
252
- case 8:
253
- return _context5.abrupt("return", upsertedWithInfo);
254
-
255
- case 9:
256
- case "end":
257
- return _context5.stop();
258
- }
259
- }
260
- }, _callee5, this);
261
- }));
262
-
263
- return function upsertOneWithInfo() {
264
- return _upsertOneWithInfo.apply(this, arguments);
265
- };
266
- }();
267
-
268
- _proto.partialUpdateByKey = /*#__PURE__*/function () {
269
- var _partialUpdateByKey = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee6(key, partialUpdate, criteria) {
270
- return _regeneratorRuntime__default.wrap(function _callee6$(_context6) {
271
- while (1) {
272
- switch (_context6.prev = _context6.next) {
273
- case 0:
274
- _context6.t0 = this;
275
- _context6.next = 3;
276
- return this.findOne(_extends__default({
277
- [this.store.keyPath]: key
278
- }, criteria));
279
-
280
- case 3:
281
- _context6.t1 = _context6.sent;
282
- _context6.t2 = partialUpdate;
283
- return _context6.abrupt("return", _context6.t0.partialUpdateOne.call(_context6.t0, _context6.t1, _context6.t2));
284
-
285
- case 6:
286
- case "end":
287
- return _context6.stop();
288
- }
289
- }
290
- }, _callee6, this);
291
- }));
292
-
293
- return function partialUpdateByKey() {
294
- return _partialUpdateByKey.apply(this, arguments);
295
- };
296
- }();
297
-
298
- _proto.partialUpdateOne = /*#__PURE__*/function () {
299
- var _partialUpdateOne = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee7(object, partialUpdate) {
300
- var updated;
301
- return _regeneratorRuntime__default.wrap(function _callee7$(_context7) {
302
- while (1) {
303
- switch (_context7.prev = _context7.next) {
304
- case 0:
305
- _context7.next = 2;
306
- return this.store.partialUpdateOne(object, partialUpdate);
307
-
308
- case 2:
309
- updated = _context7.sent;
310
- this.callSubscribed({
311
- type: 'updated',
312
- changes: [[object, updated]]
313
- });
314
- return _context7.abrupt("return", updated);
315
-
316
- case 5:
317
- case "end":
318
- return _context7.stop();
319
- }
320
- }
321
- }, _callee7, this);
322
- }));
323
-
324
- return function partialUpdateOne() {
325
- return _partialUpdateOne.apply(this, arguments);
326
- };
327
- }();
328
-
329
- _proto.partialUpdateMany = /*#__PURE__*/function () {
330
- var _partialUpdateMany = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee9(criteria, partialUpdate) {
331
- var _this2 = this;
332
-
333
- var cursor, changes;
334
- return _regeneratorRuntime__default.wrap(function _callee9$(_context9) {
335
- while (1) {
336
- switch (_context9.prev = _context9.next) {
337
- case 0:
338
- _context9.next = 2;
339
- return this.store.cursor(criteria);
340
-
341
- case 2:
342
- cursor = _context9.sent;
343
- changes = [];
344
- _context9.next = 6;
345
- return cursor.forEach( /*#__PURE__*/function () {
346
- var _ref = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee8(model) {
347
- var key, updated;
348
- return _regeneratorRuntime__default.wrap(function _callee8$(_context8) {
349
- while (1) {
350
- switch (_context8.prev = _context8.next) {
351
- case 0:
352
- key = model[_this2.store.keyPath];
353
- _context8.next = 3;
354
- return _this2.store.partialUpdateByKey(key, partialUpdate, criteria);
355
-
356
- case 3:
357
- updated = _context8.sent;
358
- changes.push([model, updated]);
359
-
360
- case 5:
361
- case "end":
362
- return _context8.stop();
363
- }
364
- }
365
- }, _callee8);
366
- }));
367
-
368
- return function () {
369
- return _ref.apply(this, arguments);
370
- };
371
- }());
372
-
373
- case 6:
374
- this.callSubscribed({
375
- type: 'updated',
376
- changes: changes
377
- });
378
-
379
- case 7:
380
- case "end":
381
- return _context9.stop();
382
- }
383
- }
384
- }, _callee9, this);
385
- }));
386
-
387
- return function partialUpdateMany() {
388
- return _partialUpdateMany.apply(this, arguments);
389
- };
390
- }();
391
-
392
- _proto.deleteByKey = /*#__PURE__*/function () {
393
- var _deleteByKey = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee10(key, criteria) {
394
- return _regeneratorRuntime__default.wrap(function _callee10$(_context10) {
395
- while (1) {
396
- switch (_context10.prev = _context10.next) {
397
- case 0:
398
- _context10.t0 = this;
399
- _context10.next = 3;
400
- return this.findByKey(key, criteria);
401
-
402
- case 3:
403
- _context10.t1 = _context10.sent;
404
- return _context10.abrupt("return", _context10.t0.deleteOne.call(_context10.t0, _context10.t1));
405
-
406
- case 5:
407
- case "end":
408
- return _context10.stop();
409
- }
410
- }
411
- }, _callee10, this);
412
- }));
413
-
414
- return function deleteByKey() {
415
- return _deleteByKey.apply(this, arguments);
416
- };
417
- }();
418
-
419
- _proto.deleteOne = /*#__PURE__*/function () {
420
- var _deleteOne = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee11(object) {
421
- return _regeneratorRuntime__default.wrap(function _callee11$(_context11) {
422
- while (1) {
423
- switch (_context11.prev = _context11.next) {
424
- case 0:
425
- _context11.next = 2;
426
- return this.store.deleteOne(object);
427
-
428
- case 2:
429
- this.callSubscribed({
430
- type: 'deleted',
431
- prev: [object]
432
- });
433
-
434
- case 3:
435
- case "end":
436
- return _context11.stop();
437
- }
438
- }
439
- }, _callee11, this);
440
- }));
441
-
442
- return function deleteOne() {
443
- return _deleteOne.apply(this, arguments);
444
- };
445
- }();
446
-
447
- _proto.deleteMany = /*#__PURE__*/function () {
448
- var _deleteMany = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee12(criteria) {
449
- var cursor, prev;
450
- return _regeneratorRuntime__default.wrap(function _callee12$(_context12) {
451
- while (1) {
452
- switch (_context12.prev = _context12.next) {
453
- case 0:
454
- _context12.next = 2;
455
- return this.store.cursor(criteria);
456
-
457
- case 2:
458
- cursor = _context12.sent;
459
- _context12.next = 5;
460
- return cursor.toArray();
461
-
462
- case 5:
463
- prev = _context12.sent;
464
- _context12.next = 8;
465
- return this.store.deleteMany(criteria);
466
-
467
- case 8:
468
- this.callSubscribed({
469
- type: 'deleted',
470
- prev: prev
471
- });
472
-
473
- case 9:
474
- case "end":
475
- return _context12.stop();
476
- }
477
- }
478
- }, _callee12, this);
479
- }));
480
-
481
- return function deleteMany() {
482
- return _deleteMany.apply(this, arguments);
483
- };
484
- }();
485
-
486
- _proto.cursor = /*#__PURE__*/function () {
487
- var _cursor = _asyncToGenerator__default( /*#__PURE__*/_regeneratorRuntime__default.mark(function _callee13(criteria, sort) {
488
- var cursor;
489
- return _regeneratorRuntime__default.wrap(function _callee13$(_context13) {
490
- while (1) {
491
- switch (_context13.prev = _context13.next) {
492
- case 0:
493
- _context13.next = 2;
494
- return this.store.cursor(criteria, sort);
495
-
496
- case 2:
497
- cursor = _context13.sent;
498
- cursor.overrideStore(this);
499
- return _context13.abrupt("return", cursor);
500
-
501
- case 5:
502
- case "end":
503
- return _context13.stop();
504
- }
505
- }
506
- }, _callee13, this);
507
- }));
508
-
509
- return function cursor() {
510
- return _cursor.apply(this, arguments);
511
- };
512
- }();
513
-
514
- _createClass__default(SubscribeStore, [{
515
- key: "connection",
516
- get: function get() {
517
- return this.store.connection;
79
+ }
80
+
81
+ async insertOne(object) {
82
+ const inserted = await this.store.insertOne(object);
83
+ this.callSubscribed({
84
+ type: 'inserted',
85
+ next: [inserted]
86
+ });
87
+ return inserted;
88
+ }
89
+
90
+ async replaceOne(object) {
91
+ const replaced = await this.store.replaceOne(object);
92
+ this.callSubscribed({
93
+ type: 'updated',
94
+ changes: [[object, replaced]]
95
+ });
96
+ return replaced;
97
+ }
98
+
99
+ async replaceSeveral(objects) {
100
+ const replacedObjects = await this.store.replaceSeveral(objects);
101
+ this.callSubscribed({
102
+ type: 'updated',
103
+ changes: objects.map((prev, index) => [prev, replacedObjects[index]])
104
+ });
105
+ return replacedObjects;
106
+ }
107
+
108
+ async upsertOne(object, setOnInsertPartialObject) {
109
+ const result = await this.upsertOneWithInfo(object, setOnInsertPartialObject);
110
+ return result.object;
111
+ }
112
+
113
+ async upsertOneWithInfo(object, setOnInsertPartialObject) {
114
+ const upsertedWithInfo = await this.store.upsertOneWithInfo(object, setOnInsertPartialObject);
115
+
116
+ if (upsertedWithInfo.inserted) {
117
+ this.callSubscribed({
118
+ type: 'inserted',
119
+ next: [upsertedWithInfo.object]
120
+ });
121
+ } else {
122
+ throw new Error('TODO');
518
123
  }
519
- }]);
520
124
 
521
- return SubscribeStore;
522
- }();
125
+ return upsertedWithInfo;
126
+ }
127
+
128
+ async partialUpdateByKey(key, partialUpdate, criteria) {
129
+ return this.partialUpdateOne(await this.findOne({
130
+ [this.store.keyPath]: key,
131
+ ...criteria
132
+ }), partialUpdate);
133
+ }
134
+
135
+ async partialUpdateOne(object, partialUpdate) {
136
+ const updated = await this.store.partialUpdateOne(object, partialUpdate);
137
+ this.callSubscribed({
138
+ type: 'updated',
139
+ changes: [[object, updated]]
140
+ });
141
+ return updated;
142
+ }
143
+
144
+ async partialUpdateMany(criteria, partialUpdate) {
145
+ const cursor = await this.store.cursor(criteria);
146
+ const changes = [];
147
+ await cursor.forEach(async model => {
148
+ const key = model[this.store.keyPath];
149
+ const updated = await this.store.partialUpdateByKey(key, partialUpdate, criteria);
150
+ changes.push([model, updated]);
151
+ });
152
+ this.callSubscribed({
153
+ type: 'updated',
154
+ changes
155
+ });
156
+ }
157
+
158
+ async deleteByKey(key, criteria) {
159
+ return this.deleteOne(await this.findByKey(key, criteria));
160
+ }
161
+
162
+ async deleteOne(object) {
163
+ await this.store.deleteOne(object);
164
+ this.callSubscribed({
165
+ type: 'deleted',
166
+ prev: [object]
167
+ });
168
+ }
169
+
170
+ async deleteMany(criteria) {
171
+ const cursor = await this.store.cursor(criteria);
172
+ const prev = await cursor.toArray();
173
+ await this.store.deleteMany(criteria);
174
+ this.callSubscribed({
175
+ type: 'deleted',
176
+ prev
177
+ });
178
+ }
179
+
180
+ async cursor(criteria, sort) {
181
+ const cursor = await this.store.cursor(criteria, sort);
182
+ cursor.overrideStore(this);
183
+ return cursor;
184
+ }
185
+
186
+ }
523
187
 
524
188
  exports.AbstractSubscribableStoreQuery = AbstractSubscribableStoreQuery;
525
189
  exports.SubscribeStore = SubscribeStore;