prostgles-client 4.0.174 → 4.0.177
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/.eslintignore +1 -0
- package/.prettierignore +2 -0
- package/.prettierrc +4 -0
- package/.vscode/settings.json +10 -1
- package/dist/Auth.d.ts +2 -2
- package/dist/Auth.d.ts.map +1 -1
- package/dist/Auth.js +28 -18
- package/dist/SyncedTable/SyncedTable.d.ts +12 -12
- package/dist/SyncedTable/SyncedTable.d.ts.map +1 -1
- package/dist/SyncedTable/SyncedTable.js +92 -78
- package/dist/SyncedTable/getMultiSyncSubscription.d.ts.map +1 -1
- package/dist/SyncedTable/getMultiSyncSubscription.js +11 -9
- package/dist/index.js +1 -1
- package/dist/index.no-sync.js +1 -1
- package/dist/prostgles.d.ts +24 -0
- package/dist/prostgles.d.ts.map +1 -1
- package/package.json +3 -2
- package/tsconfig.json +1 -3
- package/dist/typeTests.d.ts +0 -2
- package/dist/typeTests.d.ts.map +0 -1
- package/dist/typeTests.js +0 -37
- package/lib/Auth.ts +0 -155
- package/lib/FunctionQueuer.ts +0 -71
- package/lib/SyncedTable/SyncedTable.ts +0 -1078
- package/lib/SyncedTable/getMultiSyncSubscription.ts +0 -67
- package/lib/SyncedTable/getSingleSyncSubscription.ts +0 -0
- package/lib/getDbHandler.ts +0 -152
- package/lib/getMethods.ts +0 -30
- package/lib/getSqlHandler.ts +0 -174
- package/lib/getSubscriptionHandler.ts +0 -211
- package/lib/getSyncHandler.ts +0 -201
- package/lib/md5.ts +0 -183
- package/lib/prostgles-full-cdn.ts +0 -5
- package/lib/prostgles-full.ts +0 -8
- package/lib/prostgles.ts +0 -351
- package/lib/react-hooks.ts +0 -356
- package/lib/typeTests.ts +0 -64
- package/lib/useProstglesClient.ts +0 -92
- package/tests/package-lock.json +0 -71
- package/tests/package.json +0 -17
- package/tests/test.ts +0 -10
- package/tests/tsconfig.json +0 -27
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.quickClone = exports.SyncedTable = exports.debug = void 0;
|
|
4
4
|
const prostgles_types_1 = require("prostgles-types");
|
|
5
|
+
const prostgles_1 = require("../prostgles");
|
|
5
6
|
const getMultiSyncSubscription_1 = require("./getMultiSyncSubscription");
|
|
6
7
|
const DEBUG_KEY = "DEBUG_SYNCEDTABLE";
|
|
7
8
|
const hasWnd = typeof window !== "undefined";
|
|
@@ -14,7 +15,7 @@ exports.debug = debug;
|
|
|
14
15
|
const STORAGE_TYPES = {
|
|
15
16
|
array: "array",
|
|
16
17
|
localStorage: "localStorage",
|
|
17
|
-
object: "object"
|
|
18
|
+
object: "object",
|
|
18
19
|
};
|
|
19
20
|
class SyncedTable {
|
|
20
21
|
/**
|
|
@@ -34,7 +35,7 @@ class SyncedTable {
|
|
|
34
35
|
get singleSubscriptions() {
|
|
35
36
|
return this._singleSubscriptions;
|
|
36
37
|
}
|
|
37
|
-
constructor({ name, filter, onChange, onReady, onDebug, db, skipFirstTrigger = false, select = "*", storageType = "object", patchText = false, patchJSON = false, onError }) {
|
|
38
|
+
constructor({ name, filter, onChange, onReady, onDebug, db, skipFirstTrigger = false, select = "*", storageType = "object", patchText = false, patchJSON = false, onError, }) {
|
|
38
39
|
this.throttle = 100;
|
|
39
40
|
this.batch_size = 50;
|
|
40
41
|
this.skipFirstTrigger = false;
|
|
@@ -51,11 +52,13 @@ class SyncedTable {
|
|
|
51
52
|
*/
|
|
52
53
|
this.updatePatches = async (walData) => {
|
|
53
54
|
var _a, _b;
|
|
54
|
-
let remaining = walData.map(d => d.current);
|
|
55
|
+
let remaining = walData.map((d) => d.current);
|
|
55
56
|
const patched = [], patchedItems = [];
|
|
56
|
-
if (this.columns.length &&
|
|
57
|
+
if (this.columns.length &&
|
|
58
|
+
((_a = this.tableHandler) === null || _a === void 0 ? void 0 : _a.updateBatch) &&
|
|
59
|
+
(this.patchText || this.patchJSON)) {
|
|
57
60
|
// const jCols = this.columns.filter(c => c.data_type === "json")
|
|
58
|
-
const txtCols = this.columns.filter(c => c.data_type === "text");
|
|
61
|
+
const txtCols = this.columns.filter((c) => c.data_type === "text");
|
|
59
62
|
if (this.patchText && txtCols.length) {
|
|
60
63
|
remaining = [];
|
|
61
64
|
const id_keys = [this.synced_field, ...this.id_fields];
|
|
@@ -63,7 +66,7 @@ class SyncedTable {
|
|
|
63
66
|
const { current, initial } = { ...d };
|
|
64
67
|
let patchedDelta;
|
|
65
68
|
if (initial) {
|
|
66
|
-
txtCols.map(c => {
|
|
69
|
+
txtCols.map((c) => {
|
|
67
70
|
if (!id_keys.includes(c.name) && c.name in current) {
|
|
68
71
|
patchedDelta !== null && patchedDelta !== void 0 ? patchedDelta : (patchedDelta = { ...current });
|
|
69
72
|
patchedDelta[c.name] = (0, prostgles_types_1.getTextPatch)(initial[c.name], current[c.name]);
|
|
@@ -71,10 +74,7 @@ class SyncedTable {
|
|
|
71
74
|
});
|
|
72
75
|
if (patchedDelta && this.wal) {
|
|
73
76
|
patchedItems.push(patchedDelta);
|
|
74
|
-
patched.push([
|
|
75
|
-
this.wal.getIdObj(patchedDelta),
|
|
76
|
-
this.wal.getDeltaObj(patchedDelta)
|
|
77
|
-
]);
|
|
77
|
+
patched.push([this.wal.getIdObj(patchedDelta), this.wal.getDeltaObj(patchedDelta)]);
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
// console.log("json-stable-stringify ???")
|
|
@@ -98,7 +98,7 @@ class SyncedTable {
|
|
|
98
98
|
remaining = remaining.concat(patchedItems);
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
return remaining.filter(d => d);
|
|
101
|
+
return remaining.filter((d) => d);
|
|
102
102
|
};
|
|
103
103
|
/**
|
|
104
104
|
* Notifies multi subs with ALL data + deltas. Attaches handles on data if required
|
|
@@ -111,7 +111,9 @@ class SyncedTable {
|
|
|
111
111
|
const items = [], deltas = [], ids = [];
|
|
112
112
|
changes.map(({ idObj, newItem, delta }) => {
|
|
113
113
|
/* Single subs do not care about the filter */
|
|
114
|
-
this.singleSubscriptions
|
|
114
|
+
this.singleSubscriptions
|
|
115
|
+
.filter((s) => this.matchesIdObj(s.idObj, idObj))
|
|
116
|
+
.map(async (s) => {
|
|
115
117
|
try {
|
|
116
118
|
await s.notify(newItem, delta);
|
|
117
119
|
}
|
|
@@ -128,9 +130,9 @@ class SyncedTable {
|
|
|
128
130
|
});
|
|
129
131
|
if (this.onChange || this.multiSubscriptions.length) {
|
|
130
132
|
const allItems = [], allDeltas = [];
|
|
131
|
-
this.getItems().map(d => {
|
|
133
|
+
this.getItems().map((d) => {
|
|
132
134
|
allItems.push({ ...d });
|
|
133
|
-
const dIdx = items.findIndex(_d => this.matchesIdObj(d, _d));
|
|
135
|
+
const dIdx = items.findIndex((_d) => this.matchesIdObj(d, _d));
|
|
134
136
|
allDeltas.push(deltas[dIdx]);
|
|
135
137
|
});
|
|
136
138
|
/* Notify main subscription */
|
|
@@ -154,8 +156,8 @@ class SyncedTable {
|
|
|
154
156
|
}
|
|
155
157
|
};
|
|
156
158
|
this.unsubscribe = (onChange) => {
|
|
157
|
-
this.singleSubscriptions = this.singleSubscriptions.filter(s => s._onChange !== onChange);
|
|
158
|
-
this.multiSubscriptions = this.multiSubscriptions.filter(s => s._onChange !== onChange);
|
|
159
|
+
this.singleSubscriptions = this.singleSubscriptions.filter((s) => s._onChange !== onChange);
|
|
160
|
+
this.multiSubscriptions = this.multiSubscriptions.filter((s) => s._onChange !== onChange);
|
|
159
161
|
(0, exports.debug)("unsubscribe", this);
|
|
160
162
|
return "ok";
|
|
161
163
|
};
|
|
@@ -186,10 +188,9 @@ class SyncedTable {
|
|
|
186
188
|
*/
|
|
187
189
|
this.checkItemCols = (item) => {
|
|
188
190
|
if (this.columns.length) {
|
|
189
|
-
const badCols = Object.keys({ ...item })
|
|
190
|
-
.filter(k => !this.columns.find(c => c.name === k));
|
|
191
|
+
const badCols = Object.keys({ ...item }).filter((k) => !this.columns.find((c) => c.name === k));
|
|
191
192
|
if (badCols.length) {
|
|
192
|
-
throw
|
|
193
|
+
throw `Unexpected columns in sync item update: ` + badCols.join(", ");
|
|
193
194
|
}
|
|
194
195
|
}
|
|
195
196
|
};
|
|
@@ -217,10 +218,10 @@ class SyncedTable {
|
|
|
217
218
|
const idObj = { ...item.idObj };
|
|
218
219
|
let delta = { ...item.delta };
|
|
219
220
|
/* Convert undefined to null because:
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
Object.keys(delta).map(k => {
|
|
221
|
+
1) JSON.stringify drops these keys
|
|
222
|
+
2) Postgres does not have undefined
|
|
223
|
+
*/
|
|
224
|
+
Object.keys(delta).map((k) => {
|
|
224
225
|
if (delta[k] === undefined)
|
|
225
226
|
delta[k] = null;
|
|
226
227
|
});
|
|
@@ -248,7 +249,8 @@ class SyncedTable {
|
|
|
248
249
|
}
|
|
249
250
|
/* Update existing -> Expecting delta */
|
|
250
251
|
if (oldItem) {
|
|
251
|
-
status =
|
|
252
|
+
status =
|
|
253
|
+
oldItem[this.synced_field] < newItem[this.synced_field] ? "updated" : "unchanged";
|
|
252
254
|
/* Insert new item */
|
|
253
255
|
}
|
|
254
256
|
else {
|
|
@@ -288,10 +290,10 @@ class SyncedTable {
|
|
|
288
290
|
// }
|
|
289
291
|
walItems.push({
|
|
290
292
|
initial: oldItem,
|
|
291
|
-
current: { ...newItem }
|
|
293
|
+
current: { ...newItem },
|
|
292
294
|
});
|
|
293
295
|
}
|
|
294
|
-
if (
|
|
296
|
+
if (!(0, prostgles_types_1.isEmpty)(changeInfo.delta)) {
|
|
295
297
|
results.push(changeInfo);
|
|
296
298
|
}
|
|
297
299
|
/* TODO: Deletes from server */
|
|
@@ -299,10 +301,10 @@ class SyncedTable {
|
|
|
299
301
|
// items = this.getItems();
|
|
300
302
|
// }
|
|
301
303
|
return true;
|
|
302
|
-
})).catch(err => {
|
|
304
|
+
})).catch((err) => {
|
|
303
305
|
console.error("SyncedTable failed upsert: ", err);
|
|
304
306
|
});
|
|
305
|
-
(_a = this.notifyWal) === null || _a === void 0 ? void 0 : _a.addData(results.map(d => ({ initial: d.oldItem, current: d.newItem })));
|
|
307
|
+
(_a = this.notifyWal) === null || _a === void 0 ? void 0 : _a.addData(results.map((d) => ({ initial: d.oldItem, current: d.newItem })));
|
|
306
308
|
/* Push to server */
|
|
307
309
|
if (!from_server && walItems.length) {
|
|
308
310
|
(_b = this.wal) === null || _b === void 0 ? void 0 : _b.addData(walItems);
|
|
@@ -325,7 +327,7 @@ class SyncedTable {
|
|
|
325
327
|
else {
|
|
326
328
|
this.itemsObj = items.reduce((a, v) => ({
|
|
327
329
|
...a,
|
|
328
|
-
[this.getIdStr(v)]:
|
|
330
|
+
[this.getIdStr(v)]: { ...v },
|
|
329
331
|
}), {});
|
|
330
332
|
}
|
|
331
333
|
};
|
|
@@ -348,7 +350,7 @@ class SyncedTable {
|
|
|
348
350
|
}
|
|
349
351
|
}
|
|
350
352
|
else if (this.storageType === STORAGE_TYPES.array) {
|
|
351
|
-
items = this.items.map(d => ({ ...d }));
|
|
353
|
+
items = this.items.map((d) => ({ ...d }));
|
|
352
354
|
}
|
|
353
355
|
else {
|
|
354
356
|
items = Object.values({ ...this.itemsObj });
|
|
@@ -356,15 +358,15 @@ class SyncedTable {
|
|
|
356
358
|
if (this.id_fields.length && this.synced_field) {
|
|
357
359
|
const s_fields = [this.synced_field, ...this.id_fields.sort()];
|
|
358
360
|
items = items
|
|
359
|
-
.filter(d => {
|
|
360
|
-
return !this.filter ||
|
|
361
|
-
.find(key => d[key] !== this.filter[key]
|
|
362
|
-
// typeof d[key] === typeof this.filter[key] &&
|
|
363
|
-
// d[key].toString && this.filter[key].toString &&
|
|
364
|
-
// d[key].toString() !== this.filter[key].toString()
|
|
365
|
-
);
|
|
361
|
+
.filter((d) => {
|
|
362
|
+
return (!this.filter ||
|
|
363
|
+
!(0, prostgles_types_1.getKeys)(this.filter).find((key) => d[key] !== this.filter[key]));
|
|
366
364
|
})
|
|
367
|
-
.sort((a, b) => s_fields
|
|
365
|
+
.sort((a, b) => s_fields
|
|
366
|
+
.map((key) => (a[key] < b[key] ? -1
|
|
367
|
+
: a[key] > b[key] ? 1
|
|
368
|
+
: 0))
|
|
369
|
+
.find((v) => v));
|
|
368
370
|
}
|
|
369
371
|
else
|
|
370
372
|
throw "id_fields AND/OR synced_field missing";
|
|
@@ -379,8 +381,9 @@ class SyncedTable {
|
|
|
379
381
|
const items = this.getItems();
|
|
380
382
|
// params = params || {};
|
|
381
383
|
// const { from_synced, to_synced, offset = 0, limit = null } = params;
|
|
382
|
-
let res = items
|
|
383
|
-
.
|
|
384
|
+
let res = items
|
|
385
|
+
.map((c) => ({ ...c }))
|
|
386
|
+
.filter((c) => (!Number.isFinite(from_synced) || +c[this.synced_field] >= +from_synced) &&
|
|
384
387
|
(!Number.isFinite(to_synced) || +c[this.synced_field] <= +to_synced));
|
|
385
388
|
if (offset || limit)
|
|
386
389
|
res = res.splice(offset !== null && offset !== void 0 ? offset : 0, limit || res.length);
|
|
@@ -391,7 +394,7 @@ class SyncedTable {
|
|
|
391
394
|
this.select = select;
|
|
392
395
|
this.onChange = onChange;
|
|
393
396
|
if (onDebug) {
|
|
394
|
-
this.onDebug = evt => onDebug({ ...evt, type: "sync", tableName: name }, this);
|
|
397
|
+
this.onDebug = (evt) => onDebug({ ...evt, type: "sync", tableName: name }, this);
|
|
395
398
|
}
|
|
396
399
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
397
400
|
if (!STORAGE_TYPES[storageType])
|
|
@@ -416,7 +419,11 @@ class SyncedTable {
|
|
|
416
419
|
this.skipFirstTrigger = skipFirstTrigger;
|
|
417
420
|
this.multiSubscriptions = [];
|
|
418
421
|
this.singleSubscriptions = [];
|
|
419
|
-
this.onError =
|
|
422
|
+
this.onError =
|
|
423
|
+
onError ||
|
|
424
|
+
function (err) {
|
|
425
|
+
console.error("Sync internal error: ", err);
|
|
426
|
+
};
|
|
420
427
|
const onSyncRequest = (syncBatchParams) => {
|
|
421
428
|
var _a;
|
|
422
429
|
let clientSyncInfo = { c_lr: undefined, c_fr: undefined, c_count: 0 };
|
|
@@ -425,7 +432,7 @@ class SyncedTable {
|
|
|
425
432
|
clientSyncInfo = {
|
|
426
433
|
c_fr: this.getRowSyncObj(batch[0]),
|
|
427
434
|
c_lr: this.getRowSyncObj(batch[batch.length - 1]),
|
|
428
|
-
c_count: batch.length
|
|
435
|
+
c_count: batch.length,
|
|
429
436
|
};
|
|
430
437
|
}
|
|
431
438
|
(_a = this.onDebug) === null || _a === void 0 ? void 0 : _a.call(this, { command: "onUpdates", data: { syncBatchParams, batch, clientSyncInfo } });
|
|
@@ -446,20 +453,20 @@ class SyncedTable {
|
|
|
446
453
|
}
|
|
447
454
|
else if ("isSynced" in onUpdatesParams && onUpdatesParams.isSynced && !this.isSynced) {
|
|
448
455
|
this.isSynced = onUpdatesParams.isSynced;
|
|
449
|
-
const items = this.getItems().map(d => ({ ...d }));
|
|
456
|
+
const items = this.getItems().map((d) => ({ ...d }));
|
|
450
457
|
this.setItems([]);
|
|
451
|
-
const updateItems = items.map(d => ({
|
|
458
|
+
const updateItems = items.map((d) => ({
|
|
452
459
|
idObj: this.getIdObj(d),
|
|
453
|
-
delta: { ...d }
|
|
460
|
+
delta: { ...d },
|
|
454
461
|
}));
|
|
455
462
|
await this.upsert(updateItems, true);
|
|
456
463
|
}
|
|
457
464
|
else if ("data" in onUpdatesParams) {
|
|
458
465
|
/* Delta left empty so we can prepare it here */
|
|
459
|
-
const updateItems = onUpdatesParams.data.map(d => {
|
|
466
|
+
const updateItems = onUpdatesParams.data.map((d) => {
|
|
460
467
|
return {
|
|
461
468
|
idObj: this.getIdObj(d),
|
|
462
|
-
delta: d
|
|
469
|
+
delta: d,
|
|
463
470
|
};
|
|
464
471
|
});
|
|
465
472
|
await this.upsert(updateItems, true);
|
|
@@ -474,9 +481,13 @@ class SyncedTable {
|
|
|
474
481
|
synced_field,
|
|
475
482
|
throttle,
|
|
476
483
|
};
|
|
477
|
-
db[this.name]
|
|
484
|
+
db[this.name]
|
|
485
|
+
._sync(filter, { select }, { onSyncRequest, onPullRequest, onUpdates })
|
|
486
|
+
.then((s) => {
|
|
478
487
|
this.dbSync = s;
|
|
479
|
-
function confirmExit() {
|
|
488
|
+
function confirmExit() {
|
|
489
|
+
return "Data may be lost. Are you sure?";
|
|
490
|
+
}
|
|
480
491
|
/**
|
|
481
492
|
* Some syncs can be read only. Any changes are local
|
|
482
493
|
*/
|
|
@@ -507,14 +518,14 @@ class SyncedTable {
|
|
|
507
518
|
onSendEnd: () => {
|
|
508
519
|
if (hasWnd)
|
|
509
520
|
window.onbeforeunload = null;
|
|
510
|
-
}
|
|
521
|
+
},
|
|
511
522
|
});
|
|
512
523
|
this.notifyWal = new prostgles_types_1.WAL({
|
|
513
524
|
...opts,
|
|
514
525
|
batch_size: Infinity,
|
|
515
526
|
throttle: 5,
|
|
516
527
|
onSend: async (items, fullItems) => {
|
|
517
|
-
this._notifySubscribers(fullItems.map(d => {
|
|
528
|
+
this._notifySubscribers(fullItems.map((d) => {
|
|
518
529
|
var _a;
|
|
519
530
|
return ({
|
|
520
531
|
delta: this.getDelta((_a = d.initial) !== null && _a !== void 0 ? _a : {}, d.current),
|
|
@@ -522,7 +533,7 @@ class SyncedTable {
|
|
|
522
533
|
newItem: d.current,
|
|
523
534
|
});
|
|
524
535
|
}));
|
|
525
|
-
}
|
|
536
|
+
},
|
|
526
537
|
});
|
|
527
538
|
onReady();
|
|
528
539
|
});
|
|
@@ -545,7 +556,7 @@ class SyncedTable {
|
|
|
545
556
|
setTimeout(() => {
|
|
546
557
|
resolve(res);
|
|
547
558
|
}, 0);
|
|
548
|
-
}
|
|
559
|
+
},
|
|
549
560
|
});
|
|
550
561
|
}
|
|
551
562
|
catch (err) {
|
|
@@ -561,7 +572,7 @@ class SyncedTable {
|
|
|
561
572
|
sync(onChange, handlesOnData = true) {
|
|
562
573
|
const { sub, handles } = getMultiSyncSubscription_1.getMultiSyncSubscription.bind(this)({
|
|
563
574
|
onChange: onChange,
|
|
564
|
-
handlesOnData
|
|
575
|
+
handlesOnData,
|
|
565
576
|
});
|
|
566
577
|
this.multiSubscriptions.push(sub);
|
|
567
578
|
if (!this.skipFirstTrigger) {
|
|
@@ -629,7 +640,7 @@ class SyncedTable {
|
|
|
629
640
|
newData.$cloneSync = handles.$cloneSync;
|
|
630
641
|
}
|
|
631
642
|
return onChange(newData, delta);
|
|
632
|
-
}
|
|
643
|
+
},
|
|
633
644
|
};
|
|
634
645
|
this.singleSubscriptions.push(sub);
|
|
635
646
|
setTimeout(() => {
|
|
@@ -641,18 +652,21 @@ class SyncedTable {
|
|
|
641
652
|
return Object.freeze({ ...handles });
|
|
642
653
|
}
|
|
643
654
|
getIdStr(d) {
|
|
644
|
-
return this.id_fields
|
|
655
|
+
return this.id_fields
|
|
656
|
+
.sort()
|
|
657
|
+
.map((key) => `${d[key] || ""}`)
|
|
658
|
+
.join(".");
|
|
645
659
|
}
|
|
646
660
|
getIdObj(d) {
|
|
647
661
|
const res = {};
|
|
648
|
-
this.id_fields.sort().map(key => {
|
|
662
|
+
this.id_fields.sort().map((key) => {
|
|
649
663
|
res[key] = d[key];
|
|
650
664
|
});
|
|
651
665
|
return res;
|
|
652
666
|
}
|
|
653
667
|
getRowSyncObj(d) {
|
|
654
668
|
const res = {};
|
|
655
|
-
[this.synced_field, ...this.id_fields].sort().map(key => {
|
|
669
|
+
[this.synced_field, ...this.id_fields].sort().map((key) => {
|
|
656
670
|
res[key] = d[key];
|
|
657
671
|
});
|
|
658
672
|
return res;
|
|
@@ -661,10 +675,10 @@ class SyncedTable {
|
|
|
661
675
|
return Boolean(item &&
|
|
662
676
|
(!this.filter ||
|
|
663
677
|
(0, prostgles_types_1.isEmpty)(this.filter) ||
|
|
664
|
-
!Object.keys(this.filter).find(k => this.filter[k] !== item[k])));
|
|
678
|
+
!Object.keys(this.filter).find((k) => this.filter[k] !== item[k])));
|
|
665
679
|
}
|
|
666
680
|
matchesIdObj(a, b) {
|
|
667
|
-
return Boolean(a && b && !this.id_fields.sort().find(k => a[k] !== b[k]));
|
|
681
|
+
return Boolean(a && b && !this.id_fields.sort().find((k) => a[k] !== b[k]));
|
|
668
682
|
}
|
|
669
683
|
// TODO: offline-first deletes if allow_delete = true
|
|
670
684
|
// setDeleted(idObj, fullArray){
|
|
@@ -700,14 +714,14 @@ class SyncedTable {
|
|
|
700
714
|
if ((0, prostgles_types_1.isEmpty)(o))
|
|
701
715
|
return { ...n };
|
|
702
716
|
return Object.keys({ ...o, ...n })
|
|
703
|
-
.filter(k => !this.id_fields.includes(k))
|
|
717
|
+
.filter((k) => !this.id_fields.includes(k))
|
|
704
718
|
.reduce((a, k) => {
|
|
705
719
|
let delta = {};
|
|
706
720
|
if (k in n && n[k] !== o[k]) {
|
|
707
|
-
|
|
721
|
+
const deltaProp = { [k]: n[k] };
|
|
708
722
|
/** If object then compare with stringify */
|
|
709
723
|
if (n[k] && o[k] && typeof o[k] === "object") {
|
|
710
|
-
if (
|
|
724
|
+
if (!(0, prostgles_1.isEqual)(n[k], o[k])) {
|
|
711
725
|
delta = deltaProp;
|
|
712
726
|
}
|
|
713
727
|
}
|
|
@@ -719,7 +733,7 @@ class SyncedTable {
|
|
|
719
733
|
}, {});
|
|
720
734
|
}
|
|
721
735
|
deleteAll() {
|
|
722
|
-
this.getItems().map(d => this.delete(d));
|
|
736
|
+
this.getItems().map((d) => this.delete(d));
|
|
723
737
|
}
|
|
724
738
|
get tableHandler() {
|
|
725
739
|
const tblHandler = this.db[this.name];
|
|
@@ -734,14 +748,14 @@ class SyncedTable {
|
|
|
734
748
|
let d;
|
|
735
749
|
if (this.storageType === STORAGE_TYPES.localStorage) {
|
|
736
750
|
const items = this.getItems();
|
|
737
|
-
d = items.find(d => this.matchesIdObj(d, idObj));
|
|
751
|
+
d = items.find((d) => this.matchesIdObj(d, idObj));
|
|
738
752
|
}
|
|
739
753
|
else if (this.storageType === STORAGE_TYPES.array) {
|
|
740
|
-
d = this.items.find(d => this.matchesIdObj(d, idObj));
|
|
754
|
+
d = this.items.find((d) => this.matchesIdObj(d, idObj));
|
|
741
755
|
}
|
|
742
756
|
else {
|
|
743
|
-
this.itemsObj = this.itemsObj || {};
|
|
744
|
-
d =
|
|
757
|
+
// this.itemsObj = this.itemsObj || {};
|
|
758
|
+
d = { ...this.itemsObj }[this.getIdStr(idObj)];
|
|
745
759
|
}
|
|
746
760
|
return { data: quickClone(d), index };
|
|
747
761
|
}
|
|
@@ -763,7 +777,7 @@ class SyncedTable {
|
|
|
763
777
|
items.push(item);
|
|
764
778
|
}
|
|
765
779
|
else
|
|
766
|
-
items = items.filter(d => !this.matchesIdObj(d, item));
|
|
780
|
+
items = items.filter((d) => !this.matchesIdObj(d, item));
|
|
767
781
|
if (hasWnd)
|
|
768
782
|
window.localStorage.setItem(this.name, JSON.stringify(items));
|
|
769
783
|
}
|
|
@@ -777,10 +791,10 @@ class SyncedTable {
|
|
|
777
791
|
}
|
|
778
792
|
}
|
|
779
793
|
else
|
|
780
|
-
this.items = this.items.filter(d => !this.matchesIdObj(d, item));
|
|
794
|
+
this.items = this.items.filter((d) => !this.matchesIdObj(d, item));
|
|
781
795
|
}
|
|
782
796
|
else {
|
|
783
|
-
this.itemsObj = this.itemsObj || {};
|
|
797
|
+
// this.itemsObj = this.itemsObj || {};
|
|
784
798
|
if (!deleteItem) {
|
|
785
799
|
const existing = this.itemsObj[this.getIdStr(item)] || {};
|
|
786
800
|
this.itemsObj[this.getIdStr(item)] = isFullData ? { ...item } : { ...existing, ...item };
|
|
@@ -800,7 +814,7 @@ function mergeDeep(_target, _source) {
|
|
|
800
814
|
const source = _source ? quickClone(_source) : _source;
|
|
801
815
|
const output = Object.assign({}, target);
|
|
802
816
|
if ((0, prostgles_types_1.isObject)(target) && (0, prostgles_types_1.isObject)(source)) {
|
|
803
|
-
Object.keys(source).forEach(key => {
|
|
817
|
+
Object.keys(source).forEach((key) => {
|
|
804
818
|
if ((0, prostgles_types_1.isObject)(source[key])) {
|
|
805
819
|
if (!(key in target)) {
|
|
806
820
|
Object.assign(output, { [key]: source[key] });
|
|
@@ -822,11 +836,11 @@ function quickClone(obj) {
|
|
|
822
836
|
return window.structuredClone(obj);
|
|
823
837
|
}
|
|
824
838
|
if (Array.isArray(obj)) {
|
|
825
|
-
return obj.slice(0).map(v => quickClone(v));
|
|
839
|
+
return obj.slice(0).map((v) => quickClone(v));
|
|
826
840
|
}
|
|
827
841
|
else if ((0, prostgles_types_1.isObject)(obj)) {
|
|
828
842
|
const result = {};
|
|
829
|
-
(0, prostgles_types_1.getKeys)(obj).map(k => {
|
|
843
|
+
(0, prostgles_types_1.getKeys)(obj).map((k) => {
|
|
830
844
|
result[k] = quickClone(obj[k]);
|
|
831
845
|
});
|
|
832
846
|
return result;
|
|
@@ -837,9 +851,9 @@ exports.quickClone = quickClone;
|
|
|
837
851
|
/**
|
|
838
852
|
* Type tests
|
|
839
853
|
*/
|
|
840
|
-
|
|
854
|
+
const typeTest = async () => {
|
|
841
855
|
const s = 1;
|
|
842
|
-
const sh = s({ a: 1 }, {}, (d) => { d
|
|
856
|
+
const sh = s({ a: 1 }, {}, (d) => ({ d }));
|
|
843
857
|
const syncTyped = 1;
|
|
844
858
|
// const sUntyped: Sync<AnyObject, any> = syncTyped;
|
|
845
|
-
}
|
|
859
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getMultiSyncSubscription.d.ts","sourceRoot":"","sources":["../../lib/SyncedTable/getMultiSyncSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"getMultiSyncSubscription.d.ts","sourceRoot":"","sources":["../../lib/SyncedTable/getMultiSyncSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAEV,mBAAmB,EACnB,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACZ,MAAM,eAAe,CAAC;AAEvB,KAAK,IAAI,GAAG;IACV,QAAQ,EAAE,mBAAmB,CAAC,SAAS,CAAC,CAAC;IACzC,aAAa,EAAE,OAAO,CAAC;CACxB,CAAC;AACF,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE,QAAQ,EAAE,aAAa,EAAE,EAAE,IAAI;;;EA4D5F"}
|
|
@@ -6,24 +6,26 @@ function getMultiSyncSubscription({ onChange, handlesOnData }) {
|
|
|
6
6
|
$unsync: () => {
|
|
7
7
|
return this.unsubscribe(onChange);
|
|
8
8
|
},
|
|
9
|
-
getItems: () => {
|
|
9
|
+
getItems: () => {
|
|
10
|
+
return this.getItems();
|
|
11
|
+
},
|
|
10
12
|
$upsert: (newData) => {
|
|
11
13
|
if (!newData) {
|
|
12
14
|
throw "No data provided for upsert";
|
|
13
15
|
}
|
|
14
16
|
const prepareOne = (d) => {
|
|
15
|
-
return
|
|
17
|
+
return {
|
|
16
18
|
idObj: this.getIdObj(d),
|
|
17
|
-
delta: d
|
|
18
|
-
}
|
|
19
|
+
delta: d,
|
|
20
|
+
};
|
|
19
21
|
};
|
|
20
22
|
if (Array.isArray(newData)) {
|
|
21
|
-
this.upsert(newData.map(d => prepareOne(d)));
|
|
23
|
+
this.upsert(newData.map((d) => prepareOne(d)));
|
|
22
24
|
}
|
|
23
25
|
else {
|
|
24
26
|
this.upsert([prepareOne(newData)]);
|
|
25
27
|
}
|
|
26
|
-
}
|
|
28
|
+
},
|
|
27
29
|
};
|
|
28
30
|
const sub = {
|
|
29
31
|
_onChange: onChange,
|
|
@@ -40,19 +42,19 @@ function getMultiSyncSubscription({ onChange, handlesOnData }) {
|
|
|
40
42
|
$get: () => getItem(this.getItem(idObj).data, idObj),
|
|
41
43
|
$find: (idObject) => getItem(this.getItem(idObject).data, idObject),
|
|
42
44
|
$update: (newData, opts) => {
|
|
43
|
-
return this.upsert([{ idObj, delta: newData, opts }]).then(r => true);
|
|
45
|
+
return this.upsert([{ idObj, delta: newData, opts }]).then((r) => true);
|
|
44
46
|
},
|
|
45
47
|
$delete: async () => {
|
|
46
48
|
return this.delete(idObj);
|
|
47
49
|
},
|
|
48
|
-
$cloneMultiSync: (onChange) => this.sync(onChange, handlesOnData)
|
|
50
|
+
$cloneMultiSync: (onChange) => this.sync(onChange, handlesOnData),
|
|
49
51
|
});
|
|
50
52
|
const idObj = this.getIdObj(item);
|
|
51
53
|
return getItem(item, idObj);
|
|
52
54
|
});
|
|
53
55
|
}
|
|
54
56
|
return onChange(allItems, allDeltas);
|
|
55
|
-
}
|
|
57
|
+
},
|
|
56
58
|
};
|
|
57
59
|
return { sub, handles };
|
|
58
60
|
}
|