@epam/uui-db 4.9.0-alpha.0 → 4.9.0-rc.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.
- package/bundle-stats.json +1 -1
- package/index.js +434 -480
- package/index.js.map +1 -1
- package/package.json +4 -4
- package/src/DbTable.d.ts.map +1 -1
- package/src/Loader.d.ts +1 -1
- package/src/Loader.d.ts.map +1 -1
- package/src/types.d.ts +2 -1
- package/src/types.d.ts.map +1 -1
package/index.js
CHANGED
|
@@ -383,7 +383,7 @@ function __classPrivateFieldIn(state, receiver) {
|
|
|
383
383
|
"use strict";
|
|
384
384
|
|
|
385
385
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
386
|
-
|
|
386
|
+
const tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.js");
|
|
387
387
|
(0, tslib_1.__exportStar)(__webpack_require__(/*! ./src */ "./src/index.ts"), exports);
|
|
388
388
|
|
|
389
389
|
|
|
@@ -400,11 +400,11 @@ var tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.
|
|
|
400
400
|
|
|
401
401
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
402
402
|
exports.Db = void 0;
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
403
|
+
const tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.js");
|
|
404
|
+
const utils_1 = __webpack_require__(/*! ./utils */ "./src/utils.ts");
|
|
405
|
+
const lodash_isequal_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.isequal */ "lodash.isequal"));
|
|
406
|
+
class Db {
|
|
407
|
+
constructor(tables, reusedCache) {
|
|
408
408
|
this.tables = tables;
|
|
409
409
|
this.viewCache = new Map();
|
|
410
410
|
/* Views (cached db projections) */
|
|
@@ -416,29 +416,29 @@ var Db = /** @class */ (function () {
|
|
|
416
416
|
this.viewCache = new Map();
|
|
417
417
|
}
|
|
418
418
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
clone.tables =
|
|
419
|
+
update(mutate) {
|
|
420
|
+
const clone = new this.constructor(this.tables, this.viewCache);
|
|
421
|
+
clone.tables = Object.assign({}, this.tables);
|
|
422
422
|
mutate(clone);
|
|
423
423
|
return clone;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
return this.update(
|
|
427
|
-
(0, utils_1.objectKeys)(db.tables).forEach(
|
|
424
|
+
}
|
|
425
|
+
with(patch) {
|
|
426
|
+
return this.update(db => {
|
|
427
|
+
(0, utils_1.objectKeys)(db.tables).forEach(entityName => {
|
|
428
428
|
if (patch[entityName]) {
|
|
429
429
|
db.tables[entityName] = db.tables[entityName].with(patch[entityName]);
|
|
430
430
|
}
|
|
431
431
|
});
|
|
432
432
|
});
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
|
|
433
|
+
}
|
|
434
|
+
runView(view, params) {
|
|
435
|
+
let cache = this.viewCache.get(view);
|
|
436
436
|
if (!cache) {
|
|
437
437
|
cache = new Map();
|
|
438
438
|
this.viewCache.set(view, cache);
|
|
439
439
|
}
|
|
440
|
-
|
|
441
|
-
|
|
440
|
+
const key = view.getKey ? view.getKey(params) : JSON.stringify(params);
|
|
441
|
+
let cacheItem = cache.get(key);
|
|
442
442
|
if (!cacheItem) {
|
|
443
443
|
cacheItem = {
|
|
444
444
|
dependencies: this.valueAbsent,
|
|
@@ -447,19 +447,19 @@ var Db = /** @class */ (function () {
|
|
|
447
447
|
};
|
|
448
448
|
cache.set(key, cacheItem);
|
|
449
449
|
}
|
|
450
|
-
|
|
451
|
-
|
|
450
|
+
let shouldUpdate = this !== cacheItem.computedOnDb;
|
|
451
|
+
let dependencies = this.valueAbsent;
|
|
452
452
|
if (view.getDependencies) {
|
|
453
|
-
|
|
453
|
+
const compare = view.compareDependencies || utils_1.defaultCompareViewDependencies;
|
|
454
454
|
dependencies = view.getDependencies(this, params);
|
|
455
455
|
shouldUpdate = !compare(cacheItem.dependencies, dependencies);
|
|
456
456
|
}
|
|
457
|
-
|
|
457
|
+
let previousValue = cacheItem.currentValue;
|
|
458
458
|
if (shouldUpdate) {
|
|
459
459
|
cacheItem.dependencies = dependencies;
|
|
460
|
-
|
|
460
|
+
let newValue = view.compute(this, params, dependencies);
|
|
461
461
|
cacheItem.computedOnDb = this;
|
|
462
|
-
|
|
462
|
+
let compareResults = view.compareResults || lodash_isequal_1.default;
|
|
463
463
|
if (!compareResults(previousValue, newValue)) {
|
|
464
464
|
if (view.traceDiff) {
|
|
465
465
|
console.debug((0, utils_1.difference)(previousValue, newValue));
|
|
@@ -468,9 +468,8 @@ var Db = /** @class */ (function () {
|
|
|
468
468
|
}
|
|
469
469
|
}
|
|
470
470
|
return cacheItem.currentValue;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
}());
|
|
471
|
+
}
|
|
472
|
+
}
|
|
474
473
|
exports.Db = Db;
|
|
475
474
|
|
|
476
475
|
|
|
@@ -487,18 +486,17 @@ exports.Db = Db;
|
|
|
487
486
|
|
|
488
487
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
489
488
|
exports.DbRef = void 0;
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
var _this = this;
|
|
489
|
+
const tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.js");
|
|
490
|
+
const patchHelpers_1 = __webpack_require__(/*! ./patchHelpers */ "./src/patchHelpers.ts");
|
|
491
|
+
const tempIds_1 = __webpack_require__(/*! ./tempIds */ "./src/tempIds.tsx");
|
|
492
|
+
const utils_1 = __webpack_require__(/*! ./utils */ "./src/utils.ts");
|
|
493
|
+
const lodash_isempty_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.isempty */ "lodash.isempty"));
|
|
494
|
+
const Loader_1 = __webpack_require__(/*! ./Loader */ "./src/Loader.ts");
|
|
495
|
+
const uui_core_1 = __webpack_require__(/*! @epam/uui-core */ "@epam/uui-core");
|
|
496
|
+
const SimpleLoadingTracker_1 = __webpack_require__(/*! ./SimpleLoadingTracker */ "./src/SimpleLoadingTracker.ts");
|
|
497
|
+
const ListLoadingTracker_1 = __webpack_require__(/*! ./ListLoadingTracker */ "./src/ListLoadingTracker.ts");
|
|
498
|
+
class DbRef {
|
|
499
|
+
constructor(blank) {
|
|
502
500
|
this.blank = blank;
|
|
503
501
|
this.log = [];
|
|
504
502
|
this.autoSave = true;
|
|
@@ -511,36 +509,25 @@ var DbRef = /** @class */ (function () {
|
|
|
511
509
|
/* Error Subscriptions */
|
|
512
510
|
this.saveErrorHandlers = [];
|
|
513
511
|
/* Save scheduling */
|
|
514
|
-
this.enqueueSave = (0, uui_core_1.batch)(
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
case 3:
|
|
534
|
-
error_1 = _a.sent();
|
|
535
|
-
this.saveError(cumulativePatch, error_1);
|
|
536
|
-
this.update();
|
|
537
|
-
throw error_1;
|
|
538
|
-
case 4: return [2 /*return*/];
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
}); });
|
|
542
|
-
this.handleBeforeUnload = function (e) {
|
|
543
|
-
if (_this.enqueueSave.isBusy) {
|
|
512
|
+
this.enqueueSave = (0, uui_core_1.batch)(() => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
|
|
513
|
+
const cumulativePatch = this.makeCumulativePatch();
|
|
514
|
+
const lastLogEntry = this.log.length;
|
|
515
|
+
if ((0, lodash_isempty_1.default)(cumulativePatch)) {
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
try {
|
|
519
|
+
const response = yield this.savePatch(cumulativePatch);
|
|
520
|
+
this.applyResponse(response, lastLogEntry);
|
|
521
|
+
this.update();
|
|
522
|
+
}
|
|
523
|
+
catch (error) {
|
|
524
|
+
this.saveError(cumulativePatch, error);
|
|
525
|
+
this.update();
|
|
526
|
+
throw error;
|
|
527
|
+
}
|
|
528
|
+
}));
|
|
529
|
+
this.handleBeforeUnload = (e) => {
|
|
530
|
+
if (this.enqueueSave.isBusy) {
|
|
544
531
|
e.returnValue = false;
|
|
545
532
|
return false;
|
|
546
533
|
}
|
|
@@ -553,143 +540,139 @@ var DbRef = /** @class */ (function () {
|
|
|
553
540
|
window.addEventListener('beforeunload', this.handleBeforeUnload);
|
|
554
541
|
}
|
|
555
542
|
/** Saves all committed patches. Useful only if autoSave is off. */
|
|
556
|
-
|
|
543
|
+
save() {
|
|
557
544
|
return this.enqueueSave();
|
|
558
|
-
}
|
|
559
|
-
|
|
545
|
+
}
|
|
546
|
+
getAutoSave() {
|
|
560
547
|
return this.autoSave;
|
|
561
|
-
}
|
|
562
|
-
|
|
548
|
+
}
|
|
549
|
+
setAutoSave(val) {
|
|
563
550
|
if (this.autoSave != val) {
|
|
564
551
|
this.autoSave = val;
|
|
565
552
|
this.autoSave && this.enqueueSave();
|
|
566
553
|
this.update();
|
|
567
554
|
}
|
|
568
|
-
}
|
|
569
|
-
|
|
555
|
+
}
|
|
556
|
+
revert() {
|
|
570
557
|
this.db = this.base;
|
|
571
558
|
this.log = this.log.slice(0, this.savedPoint);
|
|
572
559
|
this.update();
|
|
573
|
-
}
|
|
560
|
+
}
|
|
574
561
|
/** Concrete DbRef instances should override and implement their save logic in this method */
|
|
575
|
-
|
|
562
|
+
savePatch(patch) {
|
|
576
563
|
return Promise.resolve({ submit: {} });
|
|
577
|
-
}
|
|
564
|
+
}
|
|
578
565
|
/* Db Update logic */
|
|
579
|
-
|
|
580
|
-
|
|
566
|
+
commit(patch) {
|
|
567
|
+
const dbBeforeUpdate = this.db.with(patch);
|
|
581
568
|
patch = this.beforeUpdate(patch, dbBeforeUpdate.tables, this.db.tables);
|
|
582
569
|
patch = (0, patchHelpers_1.mergeEntityPatches)(this.db.tables, patch);
|
|
583
|
-
this.log.push({ patch
|
|
570
|
+
this.log.push({ patch });
|
|
584
571
|
this.db = this.db.with(patch);
|
|
585
572
|
this.autoSave && this.enqueueSave(null, { throttleMs: this.throttleSaveMs });
|
|
586
573
|
this.update();
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
(0, utils_1.objectKeys)(tables).filter(function (entityName) { return patch[entityName] && !tables[entityName].schema.beforeUpdate; }).forEach(function (entityName) {
|
|
574
|
+
}
|
|
575
|
+
beforeUpdate(patch, tables, prevTables) {
|
|
576
|
+
let patchAndDependencies = {};
|
|
577
|
+
(0, utils_1.objectKeys)(tables).filter(entityName => patch[entityName] && !tables[entityName].schema.beforeUpdate).forEach(entityName => {
|
|
592
578
|
patchAndDependencies[entityName] = patch[entityName];
|
|
593
579
|
});
|
|
594
|
-
(0, utils_1.objectKeys)(tables).filter(
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
prevTables: prevTables,
|
|
580
|
+
(0, utils_1.objectKeys)(tables).filter(entityName => patch[entityName] && tables[entityName].schema.beforeUpdate).forEach(entityName => {
|
|
581
|
+
const schema = tables[entityName].schema;
|
|
582
|
+
const { beforeUpdate } = schema;
|
|
583
|
+
let context = {
|
|
584
|
+
clientIdsMap: this.tempIdMap,
|
|
585
|
+
schema,
|
|
586
|
+
tables,
|
|
587
|
+
prevTables,
|
|
603
588
|
};
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
589
|
+
const results = patch[entityName].map(e => beforeUpdate(e, context));
|
|
590
|
+
const updatedPatch = { [entityName]: results.map(result => result.entity) };
|
|
591
|
+
const dependentEntityPatchesForUpdate = (0, patchHelpers_1.unionPatches)(results.filter(result => result.dependentEntities).map(result => result.dependentEntities));
|
|
592
|
+
const dependentEntityPatches = this.beforeUpdate(dependentEntityPatchesForUpdate, tables, prevTables);
|
|
608
593
|
patchAndDependencies = (0, patchHelpers_1.mergeEntityPatches)(tables, (0, patchHelpers_1.unionPatches)([patchAndDependencies, updatedPatch, dependentEntityPatches]));
|
|
609
594
|
});
|
|
610
595
|
return patchAndDependencies;
|
|
611
|
-
}
|
|
612
|
-
|
|
596
|
+
}
|
|
597
|
+
commitFetch(patch) {
|
|
613
598
|
patch = this.tempIdMap.serverToClientPatch(patch);
|
|
614
599
|
this.rebase(this.base.with(patch));
|
|
615
600
|
this.update();
|
|
616
|
-
}
|
|
617
|
-
|
|
601
|
+
}
|
|
602
|
+
rebase(newBase) {
|
|
618
603
|
this.base = newBase;
|
|
619
604
|
this.db = newBase;
|
|
620
|
-
for (
|
|
605
|
+
for (let n = this.savedPoint; n < this.log.length; n++) {
|
|
621
606
|
this.db = this.db.with(this.log[n].patch);
|
|
622
607
|
}
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
608
|
+
}
|
|
609
|
+
makeCumulativePatch() {
|
|
610
|
+
const lastLogEntry = this.log.length;
|
|
611
|
+
const logEntriesToSave = this.log.slice(this.savedPoint, lastLogEntry);
|
|
612
|
+
let cumulativePatch = (0, patchHelpers_1.makeCumulativePatch)(this.db, logEntriesToSave.map(t => t.patch), this.tempIdMap);
|
|
628
613
|
cumulativePatch = this.tempIdMap.clientToServerPatch(cumulativePatch);
|
|
629
614
|
return cumulativePatch;
|
|
630
|
-
}
|
|
631
|
-
|
|
615
|
+
}
|
|
616
|
+
applyResponse(response, newSavedPoint) {
|
|
632
617
|
this.tempIdMap.appendServerMapping(response);
|
|
633
|
-
|
|
618
|
+
let serverPatch = (0, patchHelpers_1.flattenResponse)(response.submit, this.db.tables);
|
|
634
619
|
serverPatch = (0, patchHelpers_1.mergeEntityPatches)(this.db.tables, serverPatch);
|
|
635
620
|
this.savedPoint = newSavedPoint;
|
|
636
621
|
this.base = this.db;
|
|
637
622
|
this.commitFetch(serverPatch);
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
update: function (params) {
|
|
623
|
+
}
|
|
624
|
+
subscribe(view, params, onUpdate) {
|
|
625
|
+
const id = this.lastSubscriptionId++;
|
|
626
|
+
const subscription = {
|
|
627
|
+
update: (params) => {
|
|
644
628
|
subscription.currentParams = params;
|
|
645
|
-
subscription.currentValue =
|
|
629
|
+
subscription.currentValue = this.db.runView(view, params);
|
|
646
630
|
return subscription.currentValue;
|
|
647
631
|
},
|
|
648
632
|
currentParams: null,
|
|
649
633
|
currentValue: null,
|
|
650
|
-
onUpdate
|
|
651
|
-
unsubscribe:
|
|
634
|
+
onUpdate,
|
|
635
|
+
unsubscribe: () => { this.subscriptions.delete(id); },
|
|
652
636
|
};
|
|
653
637
|
subscription.update(params);
|
|
654
638
|
this.subscriptions.set(id, subscription);
|
|
655
639
|
return subscription;
|
|
656
|
-
}
|
|
657
|
-
|
|
640
|
+
}
|
|
641
|
+
onUpdate(handler) {
|
|
658
642
|
this.subscribe({
|
|
659
|
-
compute:
|
|
660
|
-
compareResults:
|
|
643
|
+
compute: db => db,
|
|
644
|
+
compareResults: (a, b) => a === b,
|
|
661
645
|
}, null, handler);
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
this.subscriptions.forEach(
|
|
665
|
-
|
|
646
|
+
}
|
|
647
|
+
update() {
|
|
648
|
+
this.subscriptions.forEach(subscription => {
|
|
649
|
+
const previousValue = subscription.currentValue;
|
|
666
650
|
subscription.update(subscription.currentParams);
|
|
667
651
|
if (subscription.currentValue !== previousValue && subscription.onUpdate) {
|
|
668
652
|
subscription.onUpdate(subscription.currentValue);
|
|
669
653
|
}
|
|
670
654
|
});
|
|
671
|
-
}
|
|
672
|
-
|
|
655
|
+
}
|
|
656
|
+
onSaveError(handler) {
|
|
673
657
|
this.saveErrorHandlers.push(handler);
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
this.saveErrorHandlers.forEach(
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
|
|
658
|
+
}
|
|
659
|
+
saveError(request, error) {
|
|
660
|
+
this.saveErrorHandlers.forEach(handler => handler(request, error));
|
|
661
|
+
}
|
|
662
|
+
makeLoader(options) {
|
|
663
|
+
const loader = new Loader_1.Loader(this, () => new SimpleLoadingTracker_1.SimpleLoadingTracker(), options);
|
|
680
664
|
this.loaders.push(loader);
|
|
681
665
|
return loader;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
|
|
666
|
+
}
|
|
667
|
+
makeListLoader(options) {
|
|
668
|
+
const loader = new Loader_1.Loader(this, () => new ListLoadingTracker_1.ListLoadingTracker(), options);
|
|
685
669
|
this.loaders.push(loader);
|
|
686
670
|
return loader;
|
|
687
|
-
}
|
|
688
|
-
|
|
671
|
+
}
|
|
672
|
+
reload() {
|
|
689
673
|
this.loaders = [];
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
}());
|
|
674
|
+
}
|
|
675
|
+
}
|
|
693
676
|
exports.DbRef = DbRef;
|
|
694
677
|
|
|
695
678
|
|
|
@@ -706,151 +689,144 @@ exports.DbRef = DbRef;
|
|
|
706
689
|
|
|
707
690
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
708
691
|
exports.DbTable = void 0;
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
692
|
+
const tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.js");
|
|
693
|
+
const I = (0, tslib_1.__importStar)(__webpack_require__(/*! immutable */ "immutable"));
|
|
694
|
+
const uui_core_1 = __webpack_require__(/*! @epam/uui-core */ "@epam/uui-core");
|
|
695
|
+
const immutable_1 = __webpack_require__(/*! immutable */ "immutable");
|
|
696
|
+
class DbTable {
|
|
697
|
+
constructor(schema, state, q) {
|
|
715
698
|
this.schema = schema;
|
|
716
699
|
this.state = state;
|
|
717
700
|
this.q = q;
|
|
718
701
|
if (!state) {
|
|
719
|
-
|
|
702
|
+
const indexes = (schema.indexes || []).map(indexDef => {
|
|
720
703
|
return {
|
|
721
704
|
field: indexDef,
|
|
722
705
|
map: I.Map(),
|
|
723
706
|
};
|
|
724
707
|
});
|
|
725
|
-
this.state = { pk: I.Map(), indexes
|
|
708
|
+
this.state = { pk: I.Map(), indexes };
|
|
726
709
|
}
|
|
727
710
|
if (!q) {
|
|
728
711
|
this.q = {};
|
|
729
712
|
}
|
|
730
713
|
}
|
|
731
|
-
|
|
714
|
+
keyToImmutable(id) {
|
|
732
715
|
if (Array.isArray(id)) {
|
|
733
716
|
return I.List(id);
|
|
734
717
|
}
|
|
735
718
|
else {
|
|
736
719
|
return id;
|
|
737
720
|
}
|
|
738
|
-
}
|
|
739
|
-
|
|
721
|
+
}
|
|
722
|
+
byId(id) {
|
|
740
723
|
return this.state.pk.get(this.keyToImmutable(id));
|
|
741
|
-
}
|
|
742
|
-
|
|
724
|
+
}
|
|
725
|
+
getId(entity) {
|
|
743
726
|
if (Array.isArray(this.schema.primaryKey)) {
|
|
744
|
-
return this.schema.primaryKey.map(
|
|
727
|
+
return this.schema.primaryKey.map((key) => entity[key]);
|
|
745
728
|
}
|
|
746
729
|
return entity[this.schema.primaryKey];
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
|
|
730
|
+
}
|
|
731
|
+
update(mutate) {
|
|
732
|
+
const clone = new this.constructor(this.schema, this.state, this.q);
|
|
750
733
|
mutate(clone);
|
|
751
734
|
return clone;
|
|
752
|
-
}
|
|
735
|
+
}
|
|
753
736
|
/* Mutation */
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
var updated = entityPatch;
|
|
737
|
+
with(patch) {
|
|
738
|
+
const updates = patch.map(entityPatch => {
|
|
739
|
+
const id = this.getId(entityPatch);
|
|
740
|
+
const immId = this.keyToImmutable(id);
|
|
741
|
+
const existing = this.state.pk.get(immId);
|
|
742
|
+
let isDeleted = false;
|
|
743
|
+
let updated = entityPatch;
|
|
762
744
|
if (existing) {
|
|
763
|
-
updated =
|
|
764
|
-
if (
|
|
765
|
-
isDeleted = updated[
|
|
745
|
+
updated = Object.assign(Object.assign({}, existing), entityPatch);
|
|
746
|
+
if (this.schema.deleteFlag) {
|
|
747
|
+
isDeleted = updated[this.schema.deleteFlag] === true;
|
|
766
748
|
}
|
|
767
749
|
}
|
|
768
|
-
return { id
|
|
750
|
+
return { id, immId, patch: entityPatch, existing, updated, isNew: !existing, isDeleted };
|
|
769
751
|
});
|
|
770
|
-
|
|
771
|
-
|
|
752
|
+
const idVal = immutable_1.Seq.Keyed(updates.map(entity => [entity.immId, entity.updated]));
|
|
753
|
+
let newPk = this.state.pk.merge(idVal);
|
|
772
754
|
// TBD: replace with deleteAll after migrating to immutable 4
|
|
773
|
-
updates.filter(
|
|
755
|
+
updates.filter(u => u.isDeleted).forEach(u => {
|
|
774
756
|
newPk = newPk.delete(u.immId);
|
|
775
757
|
});
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
758
|
+
const newIndexes = this.state.indexes.map(index => {
|
|
759
|
+
let newMap = index.map;
|
|
760
|
+
for (let n = 0; n < updates.length; n++) {
|
|
761
|
+
let update = updates[n];
|
|
762
|
+
const newKey = update.updated[index.field];
|
|
781
763
|
if (update.existing) {
|
|
782
|
-
|
|
764
|
+
const oldKey = update.existing[index.field];
|
|
783
765
|
if (oldKey !== newKey || update.isDeleted) {
|
|
784
|
-
newMap = newMap.update(oldKey,
|
|
766
|
+
newMap = newMap.update(oldKey, set => set.remove(update.immId));
|
|
785
767
|
}
|
|
786
768
|
}
|
|
787
769
|
if (!update.isDeleted) {
|
|
788
|
-
newMap = newMap.update(newKey, I.Set(),
|
|
770
|
+
newMap = newMap.update(newKey, I.Set(), set => set.add(update.immId));
|
|
789
771
|
}
|
|
790
|
-
};
|
|
791
|
-
for (var n = 0; n < updates.length; n++) {
|
|
792
|
-
_loop_1(n);
|
|
793
772
|
}
|
|
794
|
-
return
|
|
773
|
+
return Object.assign(Object.assign({}, index), { map: newMap });
|
|
795
774
|
});
|
|
796
|
-
|
|
797
|
-
return this.update(
|
|
798
|
-
}
|
|
775
|
+
const newState = { pk: newPk, indexes: newIndexes };
|
|
776
|
+
return this.update(t => t.state = newState);
|
|
777
|
+
}
|
|
799
778
|
/* Query */
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
};
|
|
814
|
-
DbTable.prototype.search = function (text) {
|
|
779
|
+
find(filter) {
|
|
780
|
+
return this.update(t => t.q = Object.assign(Object.assign({}, t.q), { filter: Object.assign(Object.assign({}, this.q.filter), filter) }));
|
|
781
|
+
}
|
|
782
|
+
order(order) {
|
|
783
|
+
return this.update(t => t.q = Object.assign(Object.assign({}, t.q), { sorting: order }));
|
|
784
|
+
}
|
|
785
|
+
orderBy(field, direction = 'asc') {
|
|
786
|
+
return this.update(t => t.q = Object.assign(Object.assign({}, t.q), { sorting: [{ field, direction }] }));
|
|
787
|
+
}
|
|
788
|
+
thenBy(field, direction) {
|
|
789
|
+
return this.update(t => t.q = Object.assign(Object.assign({}, t.q), { sorting: [...t.q.sorting, { field, direction }] }));
|
|
790
|
+
}
|
|
791
|
+
search(text) {
|
|
815
792
|
if (!this.schema.searchBy) {
|
|
816
|
-
throw new Error(
|
|
793
|
+
throw new Error(`Can't search in the ${this.schema.typeName} table - searchBy is not defined in the schema.`);
|
|
817
794
|
}
|
|
818
|
-
return this.update(
|
|
819
|
-
}
|
|
795
|
+
return this.update(t => t.q = Object.assign(Object.assign({}, t.q), { search: text }));
|
|
796
|
+
}
|
|
820
797
|
/* Materializing */
|
|
821
|
-
|
|
822
|
-
return this.runQuery(
|
|
823
|
-
}
|
|
824
|
-
|
|
798
|
+
range(from, count) {
|
|
799
|
+
return this.runQuery(Object.assign(Object.assign({}, this.q), { range: { from, count } }));
|
|
800
|
+
}
|
|
801
|
+
count() {
|
|
825
802
|
return this.runQuery(this.q).length;
|
|
826
|
-
}
|
|
827
|
-
|
|
803
|
+
}
|
|
804
|
+
one() {
|
|
828
805
|
return this.runQuery(this.q)[0] || null;
|
|
829
|
-
}
|
|
830
|
-
|
|
806
|
+
}
|
|
807
|
+
toArray() {
|
|
831
808
|
return this.runQuery(this.q);
|
|
832
|
-
}
|
|
833
|
-
|
|
809
|
+
}
|
|
810
|
+
map(fn) {
|
|
834
811
|
return this.toArray().map(fn);
|
|
835
|
-
}
|
|
812
|
+
}
|
|
836
813
|
/* Query implementation */
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
var filter = q.filter;
|
|
814
|
+
runQuery(q) {
|
|
815
|
+
let result = null;
|
|
816
|
+
let filter = q.filter;
|
|
841
817
|
if (filter) {
|
|
842
|
-
|
|
818
|
+
let indexes = this.state.indexes;
|
|
843
819
|
// Try to use indexes to fulfill filter conditions
|
|
844
|
-
for (
|
|
845
|
-
|
|
820
|
+
for (let n = 0; n < indexes.length; n++) {
|
|
821
|
+
const index = indexes[n];
|
|
846
822
|
if (index.field in filter) {
|
|
847
|
-
|
|
848
|
-
|
|
823
|
+
const _a = filter, _b = index.field, condition = _a[_b], rest = (0, tslib_1.__rest)(_a, [typeof _b === "symbol" ? _b : _b + ""]);
|
|
824
|
+
let conditionValues = null;
|
|
849
825
|
if (condition != null && typeof condition === "object") {
|
|
850
826
|
// Attempt to use index for 'in' and 'isNull' criteria.
|
|
851
827
|
// We need to be very conservative here, indexed field should work the same way as getPatternPredicate. So
|
|
852
828
|
// - it's better to leave corner cases to getPatternPredicate
|
|
853
|
-
|
|
829
|
+
const _c = condition, { in: inCriteria, isNull: nullCriteria } = _c, restCriteria = (0, tslib_1.__rest)(_c, ["in", "isNull"]);
|
|
854
830
|
if (inCriteria && Array.isArray(inCriteria)) {
|
|
855
831
|
conditionValues = inCriteria;
|
|
856
832
|
}
|
|
@@ -877,12 +853,12 @@ var DbTable = /** @class */ (function () {
|
|
|
877
853
|
}
|
|
878
854
|
if (conditionValues) {
|
|
879
855
|
result = [];
|
|
880
|
-
for (
|
|
881
|
-
|
|
856
|
+
for (let i = 0; i < conditionValues.length; i++) {
|
|
857
|
+
const idsSet = index.map.get(conditionValues[i]);
|
|
882
858
|
if (idsSet) {
|
|
883
|
-
|
|
884
|
-
for (
|
|
885
|
-
|
|
859
|
+
const idsArray = idsSet.toArray();
|
|
860
|
+
for (let j = 0; j < idsArray.length; j++) {
|
|
861
|
+
const item = this.state.pk.get(idsArray[j]);
|
|
886
862
|
result.push(item);
|
|
887
863
|
}
|
|
888
864
|
}
|
|
@@ -897,24 +873,23 @@ var DbTable = /** @class */ (function () {
|
|
|
897
873
|
result = this.state.pk.toArray();
|
|
898
874
|
}
|
|
899
875
|
if (filter) {
|
|
900
|
-
|
|
876
|
+
let predicate = (0, uui_core_1.getFilterPredicate)(q.filter);
|
|
901
877
|
result = result.filter(predicate);
|
|
902
878
|
}
|
|
903
879
|
if (q.search) {
|
|
904
|
-
|
|
905
|
-
result = result.filter(
|
|
880
|
+
let searchFilter = (0, uui_core_1.getSearchFilter)(q.search);
|
|
881
|
+
result = result.filter(item => searchFilter(this.schema.searchBy.map(field => item[field])));
|
|
906
882
|
}
|
|
907
883
|
if (q.sorting) {
|
|
908
|
-
|
|
884
|
+
let comparer = (0, uui_core_1.getOrderComparer)(q.sorting);
|
|
909
885
|
result = result.sort(comparer);
|
|
910
886
|
}
|
|
911
887
|
if (q.range) {
|
|
912
888
|
result = result.slice(q.range.from, q.range.from + q.range.count);
|
|
913
889
|
}
|
|
914
890
|
return result;
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
}());
|
|
891
|
+
}
|
|
892
|
+
}
|
|
918
893
|
exports.DbTable = DbTable;
|
|
919
894
|
|
|
920
895
|
|
|
@@ -931,16 +906,16 @@ exports.DbTable = DbTable;
|
|
|
931
906
|
|
|
932
907
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
933
908
|
exports.ListLoadingTracker = void 0;
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
909
|
+
const tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.js");
|
|
910
|
+
class ListLoadingTracker {
|
|
911
|
+
constructor(options) {
|
|
937
912
|
this.options = options;
|
|
938
913
|
this.cache = new Map();
|
|
939
|
-
this.extractList = (options === null || options === void 0 ? void 0 : options.extractList) || (
|
|
914
|
+
this.extractList = (options === null || options === void 0 ? void 0 : options.extractList) || ((r) => r);
|
|
940
915
|
}
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
916
|
+
prepare(request) {
|
|
917
|
+
const { filter, sorting, search, range } = request, unknownFields = (0, tslib_1.__rest)(request, ["filter", "sorting", "search", "range"]);
|
|
918
|
+
const options = {};
|
|
944
919
|
if (filter) {
|
|
945
920
|
options.filter = filter;
|
|
946
921
|
}
|
|
@@ -950,17 +925,17 @@ var ListLoadingTracker = /** @class */ (function () {
|
|
|
950
925
|
if (search) {
|
|
951
926
|
options.search = search;
|
|
952
927
|
}
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
928
|
+
let keyObject = Object.assign(Object.assign({}, options), unknownFields);
|
|
929
|
+
const key = JSON.stringify(keyObject);
|
|
930
|
+
let entry = this.cache.get(key);
|
|
956
931
|
if (!entry) {
|
|
957
|
-
entry = { options
|
|
932
|
+
entry = { options, count: 0, maxCount: null, isComplete: false };
|
|
958
933
|
this.cache.set(key, entry);
|
|
959
934
|
}
|
|
960
|
-
return { options
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
|
|
935
|
+
return { options, range, unknownFields, entry };
|
|
936
|
+
}
|
|
937
|
+
diff(request) {
|
|
938
|
+
const { options, range, unknownFields, entry } = this.prepare(request);
|
|
964
939
|
if (entry.isComplete) {
|
|
965
940
|
return null;
|
|
966
941
|
}
|
|
@@ -968,27 +943,27 @@ var ListLoadingTracker = /** @class */ (function () {
|
|
|
968
943
|
return request;
|
|
969
944
|
}
|
|
970
945
|
else {
|
|
971
|
-
|
|
972
|
-
|
|
946
|
+
const requestedMax = request.range.from + request.range.count;
|
|
947
|
+
let missingCount = requestedMax - entry.count;
|
|
973
948
|
if (entry.maxCount) {
|
|
974
949
|
missingCount = Math.min(missingCount, entry.maxCount);
|
|
975
950
|
}
|
|
976
951
|
if (missingCount > 0) {
|
|
977
|
-
return
|
|
952
|
+
return Object.assign(Object.assign(Object.assign({}, options), unknownFields), { range: { from: entry.count, count: requestedMax - entry.count } });
|
|
978
953
|
}
|
|
979
954
|
else {
|
|
980
955
|
return null;
|
|
981
956
|
}
|
|
982
957
|
}
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
|
|
958
|
+
}
|
|
959
|
+
count(request) {
|
|
960
|
+
const { entry } = this.prepare(request);
|
|
986
961
|
return { knownCount: Math.max(entry.count, entry.maxCount), exactCount: entry.maxCount };
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
|
|
962
|
+
}
|
|
963
|
+
append(request, response) {
|
|
964
|
+
const { options, range, unknownFields, entry } = this.prepare(request);
|
|
990
965
|
if (response) {
|
|
991
|
-
|
|
966
|
+
const list = this.extractList(response);
|
|
992
967
|
if (list.count) {
|
|
993
968
|
entry.maxCount = list.count;
|
|
994
969
|
}
|
|
@@ -998,7 +973,7 @@ var ListLoadingTracker = /** @class */ (function () {
|
|
|
998
973
|
entry.isComplete = true;
|
|
999
974
|
}
|
|
1000
975
|
else {
|
|
1001
|
-
|
|
976
|
+
const from = (list.from == null) ? range.from : list.from;
|
|
1002
977
|
if (list.items.length > 0) {
|
|
1003
978
|
entry.count = Math.max(entry.count, from + list.items.length);
|
|
1004
979
|
}
|
|
@@ -1017,9 +992,8 @@ var ListLoadingTracker = /** @class */ (function () {
|
|
|
1017
992
|
entry.isComplete = true;
|
|
1018
993
|
}
|
|
1019
994
|
}
|
|
1020
|
-
}
|
|
1021
|
-
|
|
1022
|
-
}());
|
|
995
|
+
}
|
|
996
|
+
}
|
|
1023
997
|
exports.ListLoadingTracker = ListLoadingTracker;
|
|
1024
998
|
|
|
1025
999
|
|
|
@@ -1036,25 +1010,24 @@ exports.ListLoadingTracker = ListLoadingTracker;
|
|
|
1036
1010
|
|
|
1037
1011
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1038
1012
|
exports.Loader = void 0;
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
var _this = this;
|
|
1013
|
+
class Loader {
|
|
1014
|
+
constructor(dbRef, getTracker, options) {
|
|
1042
1015
|
this.dbRef = dbRef;
|
|
1043
1016
|
this.getTracker = getTracker;
|
|
1044
1017
|
this.options = options;
|
|
1045
1018
|
this.currentRequests = new Set();
|
|
1046
|
-
this.load =
|
|
1047
|
-
|
|
1048
|
-
isComplete:
|
|
1019
|
+
this.load = (inputRequest) => {
|
|
1020
|
+
const result = {
|
|
1021
|
+
isComplete: this.loaded.diff(inputRequest) == null,
|
|
1049
1022
|
isLoading: false,
|
|
1050
1023
|
request: inputRequest,
|
|
1051
|
-
missing:
|
|
1024
|
+
missing: this.loadedAndLoading.diff(inputRequest),
|
|
1052
1025
|
promise: null,
|
|
1053
1026
|
reload: null,
|
|
1054
1027
|
};
|
|
1055
|
-
result.reload =
|
|
1028
|
+
result.reload = () => {
|
|
1056
1029
|
result.missing = inputRequest;
|
|
1057
|
-
|
|
1030
|
+
this.loadImpl(result);
|
|
1058
1031
|
};
|
|
1059
1032
|
if (result.isComplete) {
|
|
1060
1033
|
// Everything requested is already loaded
|
|
@@ -1070,12 +1043,12 @@ var Loader = /** @class */ (function () {
|
|
|
1070
1043
|
result.isLoading = true;
|
|
1071
1044
|
// There's no good way to tell which of current requests is querying requested data - there actually can be 2 of them.
|
|
1072
1045
|
// So we track all running requests, and await all of them in such case.
|
|
1073
|
-
result.promise = Promise.all(
|
|
1046
|
+
result.promise = Promise.all(this.currentRequests.entries()).then(() => { });
|
|
1074
1047
|
return result;
|
|
1075
1048
|
}
|
|
1076
1049
|
else {
|
|
1077
1050
|
// Something requested is not loaded, nor being loaded. Need to perform a new request.
|
|
1078
|
-
|
|
1051
|
+
this.loadImpl(result);
|
|
1079
1052
|
return result;
|
|
1080
1053
|
}
|
|
1081
1054
|
}
|
|
@@ -1083,33 +1056,31 @@ var Loader = /** @class */ (function () {
|
|
|
1083
1056
|
this.loaded = getTracker();
|
|
1084
1057
|
this.loadedAndLoading = getTracker();
|
|
1085
1058
|
}
|
|
1086
|
-
|
|
1087
|
-
var _this = this;
|
|
1059
|
+
loadImpl(state) {
|
|
1088
1060
|
this.loadedAndLoading.append(state.missing);
|
|
1089
1061
|
state.isLoading = true;
|
|
1090
|
-
|
|
1062
|
+
let serverRequest = state.missing;
|
|
1091
1063
|
if (this.options.clientToServerRequest) {
|
|
1092
1064
|
serverRequest = this.options.clientToServerRequest(serverRequest);
|
|
1093
1065
|
}
|
|
1094
|
-
state.promise = this.options.api(serverRequest).then(
|
|
1095
|
-
|
|
1096
|
-
|
|
1066
|
+
state.promise = this.options.api(serverRequest).then(result => {
|
|
1067
|
+
this.loaded.append(state.missing, result);
|
|
1068
|
+
this.loadedAndLoading.append(state.missing, result);
|
|
1097
1069
|
state.missing = null;
|
|
1098
1070
|
state.isLoading = false;
|
|
1099
1071
|
state.isComplete = true;
|
|
1100
1072
|
state.promise = Promise.resolve();
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1073
|
+
const flatten = this.options.convertToPatch(result);
|
|
1074
|
+
this.options.postProcess && this.options.postProcess(flatten);
|
|
1075
|
+
this.dbRef.commitFetch(flatten);
|
|
1104
1076
|
});
|
|
1105
1077
|
this.currentRequests.add(state.promise);
|
|
1106
|
-
state.promise.finally(
|
|
1107
|
-
|
|
1078
|
+
state.promise.finally(() => {
|
|
1079
|
+
this.currentRequests.delete(state.promise);
|
|
1108
1080
|
});
|
|
1109
1081
|
return state.promise;
|
|
1110
|
-
}
|
|
1111
|
-
|
|
1112
|
-
}());
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1113
1084
|
exports.Loader = Loader;
|
|
1114
1085
|
|
|
1115
1086
|
|
|
@@ -1126,21 +1097,20 @@ exports.Loader = Loader;
|
|
|
1126
1097
|
|
|
1127
1098
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1128
1099
|
exports.SimpleLoadingTracker = void 0;
|
|
1129
|
-
|
|
1130
|
-
|
|
1100
|
+
class SimpleLoadingTracker {
|
|
1101
|
+
constructor() {
|
|
1131
1102
|
this.set = new Set();
|
|
1132
1103
|
}
|
|
1133
|
-
|
|
1104
|
+
getKey(request) {
|
|
1134
1105
|
return JSON.stringify(request);
|
|
1135
|
-
}
|
|
1136
|
-
|
|
1106
|
+
}
|
|
1107
|
+
diff(request) {
|
|
1137
1108
|
return this.set.has(this.getKey(request)) ? null : request;
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1109
|
+
}
|
|
1110
|
+
append(request, result) {
|
|
1140
1111
|
this.set.add(this.getKey(request));
|
|
1141
|
-
}
|
|
1142
|
-
|
|
1143
|
-
}());
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1144
1114
|
exports.SimpleLoadingTracker = SimpleLoadingTracker;
|
|
1145
1115
|
|
|
1146
1116
|
|
|
@@ -1156,7 +1126,7 @@ exports.SimpleLoadingTracker = SimpleLoadingTracker;
|
|
|
1156
1126
|
"use strict";
|
|
1157
1127
|
|
|
1158
1128
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1159
|
-
|
|
1129
|
+
const tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.js");
|
|
1160
1130
|
(0, tslib_1.__exportStar)(__webpack_require__(/*! ./types */ "./src/types.ts"), exports);
|
|
1161
1131
|
(0, tslib_1.__exportStar)(__webpack_require__(/*! ./Db */ "./src/Db.ts"), exports);
|
|
1162
1132
|
(0, tslib_1.__exportStar)(__webpack_require__(/*! ./DbTable */ "./src/DbTable.ts"), exports);
|
|
@@ -1180,42 +1150,38 @@ var tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.
|
|
|
1180
1150
|
|
|
1181
1151
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1182
1152
|
exports.getEntityParentDependencies = exports.getParentEntities = exports.hasServerFields = exports.isNew = exports.getId = exports.isClientOnly = exports.makeCumulativePatch = exports.isPatchBlank = exports.flattenResponse = exports.mergeEntityPatches = exports.unionPatches = void 0;
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1153
|
+
const tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.js");
|
|
1154
|
+
const types_1 = __webpack_require__(/*! ./types */ "./src/types.ts");
|
|
1155
|
+
const I = (0, tslib_1.__importStar)(__webpack_require__(/*! immutable */ "immutable"));
|
|
1156
|
+
const utils_1 = __webpack_require__(/*! ./utils */ "./src/utils.ts");
|
|
1157
|
+
const lodash_groupby_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.groupby */ "lodash.groupby"));
|
|
1158
|
+
const lodash_map_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.map */ "lodash.map"));
|
|
1159
|
+
const lodash_merge_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.merge */ "lodash.merge"));
|
|
1160
|
+
const lodash_countby_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.countby */ "lodash.countby"));
|
|
1161
|
+
const lodash_filter_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.filter */ "lodash.filter"));
|
|
1162
|
+
const lodash_foreach_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.foreach */ "lodash.foreach"));
|
|
1163
|
+
const lodash_pick_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.pick */ "lodash.pick"));
|
|
1194
1164
|
function unionPatches(patches) {
|
|
1195
|
-
|
|
1196
|
-
patches.forEach(
|
|
1197
|
-
(0, utils_1.objectKeys)(patch).forEach(
|
|
1198
|
-
result[tableName] = result[tableName] ?
|
|
1165
|
+
const result = {};
|
|
1166
|
+
patches.forEach(patch => {
|
|
1167
|
+
(0, utils_1.objectKeys)(patch).forEach(tableName => {
|
|
1168
|
+
result[tableName] = result[tableName] ? [...result[tableName], ...patch[tableName]] : patch[tableName];
|
|
1199
1169
|
});
|
|
1200
1170
|
});
|
|
1201
1171
|
return result;
|
|
1202
1172
|
}
|
|
1203
1173
|
exports.unionPatches = unionPatches;
|
|
1204
1174
|
function mergeEntityPatches(tables, patch) {
|
|
1205
|
-
|
|
1206
|
-
(0, utils_1.objectKeys)(patch).forEach(
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1175
|
+
const result = {};
|
|
1176
|
+
(0, utils_1.objectKeys)(patch).forEach(key => {
|
|
1177
|
+
const table = tables[key];
|
|
1178
|
+
const groupedEntities = (0, lodash_groupby_1.default)(patch[key], item => table.getId(item));
|
|
1179
|
+
const tablePatch = (0, lodash_map_1.default)(groupedEntities, entities => {
|
|
1180
|
+
const result = (0, lodash_merge_1.default)({}, ...entities);
|
|
1211
1181
|
if (true) {
|
|
1212
|
-
|
|
1213
|
-
return Object.keys((0, lodash_countby_1.default)(entities.filter(function (entity) {
|
|
1214
|
-
return entity[prop] !== undefined;
|
|
1215
|
-
}), function (entity) { return entity[prop]; })).length > 1;
|
|
1216
|
-
});
|
|
1182
|
+
const props = (0, utils_1.objectKeys)(result).filter(prop => Object.keys((0, lodash_countby_1.default)(entities.filter(entity => entity[prop] !== undefined), entity => entity[prop])).length > 1);
|
|
1217
1183
|
if (props.length) {
|
|
1218
|
-
console.warn
|
|
1184
|
+
console.warn('mergeEntityPatches: Different property values found for the same entity.\nProperty names:\n', props, '\nEntities:\n', ...entities);
|
|
1219
1185
|
}
|
|
1220
1186
|
}
|
|
1221
1187
|
return result;
|
|
@@ -1227,26 +1193,26 @@ function mergeEntityPatches(tables, patch) {
|
|
|
1227
1193
|
exports.mergeEntityPatches = mergeEntityPatches;
|
|
1228
1194
|
function flattenResponseHelper(result, object, tablesByTypeName) {
|
|
1229
1195
|
if (Array.isArray(object)) {
|
|
1230
|
-
object.forEach(
|
|
1196
|
+
object.forEach(item => flattenResponseHelper(result, item, tablesByTypeName));
|
|
1231
1197
|
}
|
|
1232
1198
|
else if (typeof object === 'object') {
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1199
|
+
const clone = {};
|
|
1200
|
+
let tableKey;
|
|
1201
|
+
let fields = {};
|
|
1236
1202
|
if ('__typename' in object) {
|
|
1237
|
-
|
|
1203
|
+
const table = tablesByTypeName[object.__typename];
|
|
1238
1204
|
if (table) {
|
|
1239
1205
|
tableKey = table.schema.tableName;
|
|
1240
1206
|
fields = table.schema.fields;
|
|
1241
1207
|
}
|
|
1242
1208
|
}
|
|
1243
|
-
for (
|
|
1244
|
-
|
|
1245
|
-
|
|
1209
|
+
for (let key in object) {
|
|
1210
|
+
const value = object[key];
|
|
1211
|
+
const field = fields[key];
|
|
1246
1212
|
if ((field && !!field.disableFlatten)
|
|
1247
1213
|
|| typeof value !== 'object'
|
|
1248
1214
|
|| value === null
|
|
1249
|
-
|| (Array.isArray(value) && value.every(
|
|
1215
|
+
|| (Array.isArray(value) && value.every(item => typeof item !== 'object'))) {
|
|
1250
1216
|
clone[key] = value;
|
|
1251
1217
|
}
|
|
1252
1218
|
else {
|
|
@@ -1259,35 +1225,35 @@ function flattenResponseHelper(result, object, tablesByTypeName) {
|
|
|
1259
1225
|
}
|
|
1260
1226
|
}
|
|
1261
1227
|
function flattenResponse(response, tables) {
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
Object.values(tables).forEach(
|
|
1265
|
-
(0, utils_1.objectKeys)(tables).forEach(
|
|
1266
|
-
(0, utils_1.objectKeys)(tables).forEach(
|
|
1228
|
+
const result = {};
|
|
1229
|
+
let tablesByTypeName = {};
|
|
1230
|
+
Object.values(tables).forEach(table => tablesByTypeName[table.schema.typeName] = table);
|
|
1231
|
+
(0, utils_1.objectKeys)(tables).forEach(tableName => tablesByTypeName);
|
|
1232
|
+
(0, utils_1.objectKeys)(tables).forEach(key => result[key] = []);
|
|
1267
1233
|
flattenResponseHelper(result, response, tablesByTypeName);
|
|
1268
1234
|
return result;
|
|
1269
1235
|
}
|
|
1270
1236
|
exports.flattenResponse = flattenResponse;
|
|
1271
1237
|
function isPatchBlank(patch) {
|
|
1272
|
-
return !(0, utils_1.objectKeys)(patch).some(
|
|
1238
|
+
return !(0, utils_1.objectKeys)(patch).some(key => patch[key].length > 0);
|
|
1273
1239
|
}
|
|
1274
1240
|
exports.isPatchBlank = isPatchBlank;
|
|
1275
1241
|
/**
|
|
1276
1242
|
* Given several patches, creates a new single patch with all fields from all entities in all patches.
|
|
1277
1243
|
*/
|
|
1278
1244
|
function makeCumulativePatch(updatedDb, patches, clientIdsMap) {
|
|
1279
|
-
|
|
1280
|
-
(0, utils_1.objectKeys)(updatedDb.tables).forEach(
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
patches.forEach(
|
|
1285
|
-
|
|
1286
|
-
ids = ids.union(filteredPatch.map(
|
|
1245
|
+
const result = {};
|
|
1246
|
+
(0, utils_1.objectKeys)(updatedDb.tables).forEach(entityName => {
|
|
1247
|
+
const table = updatedDb.tables[entityName];
|
|
1248
|
+
const schema = table.schema;
|
|
1249
|
+
let ids = I.Set();
|
|
1250
|
+
patches.forEach(patch => {
|
|
1251
|
+
let filteredPatch = (patch[entityName] || []).filter(entity => hasServerFields(entity, schema, clientIdsMap, updatedDb.tables));
|
|
1252
|
+
ids = ids.union(filteredPatch.map(e => table.getId(e)));
|
|
1287
1253
|
});
|
|
1288
|
-
|
|
1289
|
-
.map(
|
|
1290
|
-
.filter(
|
|
1254
|
+
const entities = ids
|
|
1255
|
+
.map(id => table.byId(id))
|
|
1256
|
+
.filter(e => !isClientOnly(e, schema))
|
|
1291
1257
|
.toArray();
|
|
1292
1258
|
if (entities.length > 0) {
|
|
1293
1259
|
result[entityName] = entities;
|
|
@@ -1307,43 +1273,43 @@ function getId(entity, schema) {
|
|
|
1307
1273
|
if (typeof schema.primaryKey == 'string') {
|
|
1308
1274
|
return entity[schema.primaryKey];
|
|
1309
1275
|
}
|
|
1310
|
-
|
|
1311
|
-
(0, lodash_foreach_1.default)((0, lodash_pick_1.default)(entity, schema.primaryKey),
|
|
1276
|
+
const result = [];
|
|
1277
|
+
(0, lodash_foreach_1.default)((0, lodash_pick_1.default)(entity, schema.primaryKey), (value) => {
|
|
1312
1278
|
result.push(value);
|
|
1313
1279
|
});
|
|
1314
1280
|
return result;
|
|
1315
1281
|
}
|
|
1316
1282
|
exports.getId = getId;
|
|
1317
1283
|
function isNew(entity, schema, clientIdsMap) {
|
|
1318
|
-
|
|
1284
|
+
const id = getId(entity, schema);
|
|
1319
1285
|
return !clientIdsMap.clientToServer(id);
|
|
1320
1286
|
}
|
|
1321
1287
|
exports.isNew = isNew;
|
|
1322
1288
|
function hasServerFields(entity, schema, clientIdsMap, tables) {
|
|
1323
|
-
|
|
1289
|
+
const fullEntity = Object.assign(Object.assign({}, tables[schema.tableName].byId(entity.id) || {}), entity);
|
|
1324
1290
|
if (isClientOnly(fullEntity, schema)) {
|
|
1325
1291
|
return false;
|
|
1326
1292
|
}
|
|
1327
|
-
return (0, utils_1.objectKeys)(entity).some(
|
|
1328
|
-
|
|
1293
|
+
return (0, utils_1.objectKeys)(entity).some(fieldName => {
|
|
1294
|
+
const fieldSchema = schema.fields[fieldName];
|
|
1329
1295
|
return !fieldSchema || !(fieldSchema.isClientOnly || fieldSchema.isReadOnly || (fieldSchema.isGenerated && !isNew(entity, schema, clientIdsMap)));
|
|
1330
1296
|
});
|
|
1331
1297
|
}
|
|
1332
1298
|
exports.hasServerFields = hasServerFields;
|
|
1333
1299
|
function getParentEntities(patch, tables) {
|
|
1334
|
-
|
|
1335
|
-
(0, utils_1.objectKeys)(patch).forEach(
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
fields.forEach(
|
|
1341
|
-
|
|
1342
|
-
tablePatch.forEach(
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1300
|
+
const dependenciesPatch = {};
|
|
1301
|
+
(0, utils_1.objectKeys)(patch).forEach(key => {
|
|
1302
|
+
const table = tables[key];
|
|
1303
|
+
const tableSchema = table.schema;
|
|
1304
|
+
const fields = (0, lodash_filter_1.default)((0, lodash_map_1.default)(tableSchema.fields, (field, name) => (Object.assign(Object.assign({}, field), { name }))), field => field.fk && field.fk.relationType == types_1.DbRelationType.Aggregation);
|
|
1305
|
+
const tablePatch = patch[key];
|
|
1306
|
+
fields.forEach(field => {
|
|
1307
|
+
const { fk: { tableName }, name } = field;
|
|
1308
|
+
tablePatch.forEach(entity => {
|
|
1309
|
+
const actualTableName = (typeof tableName == 'function' ? tableName(entity) : tableName);
|
|
1310
|
+
const dbEntity = table.byId(table.getId(entity));
|
|
1311
|
+
const parentEntity = tables[actualTableName].byId(dbEntity[name]);
|
|
1312
|
+
const idFields = (0, lodash_pick_1.default)(parentEntity, tables[actualTableName].schema.primaryKey);
|
|
1347
1313
|
if (parentEntity) {
|
|
1348
1314
|
dependenciesPatch[actualTableName] = dependenciesPatch[actualTableName] || [];
|
|
1349
1315
|
dependenciesPatch[actualTableName].push(idFields);
|
|
@@ -1355,18 +1321,17 @@ function getParentEntities(patch, tables) {
|
|
|
1355
1321
|
}
|
|
1356
1322
|
exports.getParentEntities = getParentEntities;
|
|
1357
1323
|
function getEntityParentDependencies(entity, entityName, tables) {
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
var resultPatches = [];
|
|
1324
|
+
let patch = { [entityName]: [entity] };
|
|
1325
|
+
const resultPatches = [];
|
|
1361
1326
|
while (true) {
|
|
1362
|
-
|
|
1327
|
+
let parents = getParentEntities(patch, tables);
|
|
1363
1328
|
if (isPatchBlank(parents)) {
|
|
1364
1329
|
break;
|
|
1365
1330
|
}
|
|
1366
1331
|
resultPatches.push(parents);
|
|
1367
1332
|
patch = parents;
|
|
1368
1333
|
}
|
|
1369
|
-
|
|
1334
|
+
let result = unionPatches([patch, ...resultPatches]);
|
|
1370
1335
|
result = mergeEntityPatches(tables, result);
|
|
1371
1336
|
return result;
|
|
1372
1337
|
}
|
|
@@ -1386,20 +1351,19 @@ exports.getEntityParentDependencies = getEntityParentDependencies;
|
|
|
1386
1351
|
|
|
1387
1352
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1388
1353
|
exports.useDbView = exports.useDbRef = exports.DbContext = void 0;
|
|
1389
|
-
|
|
1390
|
-
var react_1 = __webpack_require__(/*! react */ "react");
|
|
1354
|
+
const react_1 = __webpack_require__(/*! react */ "react");
|
|
1391
1355
|
exports.DbContext = (0, react_1.createContext)(null);
|
|
1392
1356
|
function useDbRef() {
|
|
1393
1357
|
return (0, react_1.useContext)(exports.DbContext);
|
|
1394
1358
|
}
|
|
1395
1359
|
exports.useDbRef = useDbRef;
|
|
1396
1360
|
function useDbView(fn, params, options) {
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1361
|
+
const dbRef = useDbRef();
|
|
1362
|
+
const [state, setState] = (0, react_1.useState)();
|
|
1363
|
+
const subscription = (0, react_1.useMemo)(() => dbRef.subscribe(Object.assign({ compute: fn }, options), params, setState), []);
|
|
1400
1364
|
subscription.update(params);
|
|
1401
|
-
(0, react_1.useEffect)(
|
|
1402
|
-
return
|
|
1365
|
+
(0, react_1.useEffect)(() => {
|
|
1366
|
+
return () => subscription.unsubscribe();
|
|
1403
1367
|
}, []);
|
|
1404
1368
|
return subscription.currentValue;
|
|
1405
1369
|
}
|
|
@@ -1419,9 +1383,8 @@ exports.useDbView = useDbView;
|
|
|
1419
1383
|
|
|
1420
1384
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1421
1385
|
exports.TempIdMap = exports.isTempId = exports.getTempId = void 0;
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
var lastTempId = -1;
|
|
1386
|
+
const utils_1 = __webpack_require__(/*! ./utils */ "./src/utils.ts");
|
|
1387
|
+
let lastTempId = -1;
|
|
1425
1388
|
function getTempId() {
|
|
1426
1389
|
return --lastTempId;
|
|
1427
1390
|
}
|
|
@@ -1430,28 +1393,27 @@ function isTempId(val) {
|
|
|
1430
1393
|
return typeof val === 'number' && val < 0;
|
|
1431
1394
|
}
|
|
1432
1395
|
exports.isTempId = isTempId;
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
var _this = this;
|
|
1396
|
+
class TempIdMap {
|
|
1397
|
+
constructor(db) {
|
|
1436
1398
|
this.db = db;
|
|
1437
1399
|
this.serverToClientIds = new Map();
|
|
1438
1400
|
this.clientToServerIds = new Map();
|
|
1439
|
-
this.clientToServer =
|
|
1440
|
-
this.serverToClient =
|
|
1441
|
-
this.clientToServerRequest =
|
|
1442
|
-
request =
|
|
1401
|
+
this.clientToServer = (id) => this.clientToServerIds.get(id);
|
|
1402
|
+
this.serverToClient = (tableName, id) => this.getServerToClientMap(tableName).get(id);
|
|
1403
|
+
this.clientToServerRequest = (request) => {
|
|
1404
|
+
request = Object.assign({}, request);
|
|
1443
1405
|
if (request.filter) {
|
|
1444
|
-
request.filter =
|
|
1406
|
+
request.filter = this.clientToServerDataFilter(request.filter);
|
|
1445
1407
|
}
|
|
1446
1408
|
return request;
|
|
1447
1409
|
};
|
|
1448
|
-
this.clientToServerDataFilter =
|
|
1449
|
-
filter =
|
|
1450
|
-
|
|
1410
|
+
this.clientToServerDataFilter = (filter) => {
|
|
1411
|
+
filter = Object.assign({}, filter);
|
|
1412
|
+
const keys = Object.keys(filter);
|
|
1451
1413
|
// I hope that we can rely that there won't be any negative numbers, which are not IDs, in queries.
|
|
1452
1414
|
// We need to invent better heuristics or a schema otherwise.
|
|
1453
|
-
|
|
1454
|
-
|
|
1415
|
+
const clientToServer = (id) => {
|
|
1416
|
+
const found = this.clientToServer(id);
|
|
1455
1417
|
if (found) {
|
|
1456
1418
|
return found;
|
|
1457
1419
|
}
|
|
@@ -1459,11 +1421,11 @@ var TempIdMap = /** @class */ (function () {
|
|
|
1459
1421
|
return id;
|
|
1460
1422
|
}
|
|
1461
1423
|
};
|
|
1462
|
-
for (
|
|
1463
|
-
|
|
1464
|
-
|
|
1424
|
+
for (let n = 0; n < keys.length; n++) {
|
|
1425
|
+
const key = keys[n];
|
|
1426
|
+
let condition = filter[key];
|
|
1465
1427
|
if (condition != null && typeof condition === "object") {
|
|
1466
|
-
condition =
|
|
1428
|
+
condition = Object.assign({}, condition);
|
|
1467
1429
|
if ('in' in condition && Array.isArray(condition.in) && condition.in.length) {
|
|
1468
1430
|
condition.in = condition.in.map(clientToServer);
|
|
1469
1431
|
}
|
|
@@ -1476,76 +1438,74 @@ var TempIdMap = /** @class */ (function () {
|
|
|
1476
1438
|
return filter;
|
|
1477
1439
|
};
|
|
1478
1440
|
}
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
(0, utils_1.objectKeys)(patch).forEach(
|
|
1482
|
-
|
|
1441
|
+
copyPatch(patch) {
|
|
1442
|
+
const newPatch = Object.assign({}, patch);
|
|
1443
|
+
(0, utils_1.objectKeys)(patch).forEach(tableName => {
|
|
1444
|
+
let tablePatch = patch[tableName].map(entityPatch => (Object.assign({}, entityPatch)));
|
|
1483
1445
|
newPatch[tableName] = tablePatch;
|
|
1484
1446
|
});
|
|
1485
1447
|
return newPatch;
|
|
1486
|
-
}
|
|
1448
|
+
}
|
|
1487
1449
|
// Iterate thru every field in every patch for every table, matching certain field criteria. The patch is mutated!
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
.
|
|
1494
|
-
var fieldSchema = schema.fields[fieldName];
|
|
1450
|
+
mapFields(patch, filter, callback) {
|
|
1451
|
+
(0, utils_1.objectKeys)(patch).forEach(tableName => {
|
|
1452
|
+
const schema = this.db.tables[tableName].schema;
|
|
1453
|
+
const fields = (0, utils_1.objectKeys)(schema.fields || [])
|
|
1454
|
+
.map(fieldName => {
|
|
1455
|
+
const fieldSchema = schema.fields[fieldName];
|
|
1495
1456
|
if (filter(fieldSchema, fieldName)) {
|
|
1496
1457
|
return { name: fieldName, schema: fieldSchema };
|
|
1497
1458
|
}
|
|
1498
1459
|
})
|
|
1499
|
-
.filter(
|
|
1460
|
+
.filter(f => !!f);
|
|
1500
1461
|
if (!fields.length) {
|
|
1501
1462
|
return;
|
|
1502
1463
|
}
|
|
1503
|
-
patch[tableName].forEach(
|
|
1504
|
-
for (
|
|
1505
|
-
|
|
1506
|
-
|
|
1464
|
+
patch[tableName].forEach(entityPatch => {
|
|
1465
|
+
for (let n = 0; n < fields.length; n++) {
|
|
1466
|
+
const field = fields[n];
|
|
1467
|
+
const fieldValue = entityPatch[field.name];
|
|
1507
1468
|
callback(field.name, field.schema, fieldValue, entityPatch, tableName);
|
|
1508
1469
|
}
|
|
1509
1470
|
});
|
|
1510
1471
|
});
|
|
1511
|
-
}
|
|
1512
|
-
|
|
1513
|
-
|
|
1472
|
+
}
|
|
1473
|
+
getServerToClientMap(tableName) {
|
|
1474
|
+
const existing = this.serverToClientIds.get(tableName);
|
|
1514
1475
|
if (existing) {
|
|
1515
1476
|
return existing;
|
|
1516
1477
|
}
|
|
1517
1478
|
else {
|
|
1518
|
-
|
|
1479
|
+
const newMap = new Map();
|
|
1519
1480
|
this.serverToClientIds.set(tableName, newMap);
|
|
1520
1481
|
return newMap;
|
|
1521
1482
|
}
|
|
1522
|
-
}
|
|
1523
|
-
|
|
1524
|
-
var _this = this;
|
|
1483
|
+
}
|
|
1484
|
+
serverToClientPatch(patch) {
|
|
1525
1485
|
patch = this.copyPatch(patch);
|
|
1526
1486
|
// We map PK in a separate pass, as we need to remap all PKs before re-mapping FKs
|
|
1527
|
-
this.mapFields(patch,
|
|
1528
|
-
|
|
1487
|
+
this.mapFields(patch, f => f.isGenerated, (fieldName, fieldSchema, fieldValue, entityPatch, tableName) => {
|
|
1488
|
+
let clientId = this.getServerToClientMap(tableName).get(fieldValue);
|
|
1529
1489
|
if (!clientId) {
|
|
1530
1490
|
clientId = getTempId();
|
|
1531
|
-
|
|
1532
|
-
|
|
1491
|
+
this.clientToServerIds.set(clientId, fieldValue);
|
|
1492
|
+
this.getServerToClientMap(tableName).set(fieldValue, clientId);
|
|
1533
1493
|
}
|
|
1534
1494
|
entityPatch[fieldName] = clientId;
|
|
1535
1495
|
});
|
|
1536
|
-
this.mapFields(patch,
|
|
1537
|
-
|
|
1496
|
+
this.mapFields(patch, f => !!(f.fk || (f.default != null) || f.toClient), (fieldName, fieldSchema, fieldValue, entityPatch, tableName) => {
|
|
1497
|
+
let isUndefined = typeof (fieldValue) === 'undefined';
|
|
1538
1498
|
if (fieldSchema.default != null && fieldValue == null) {
|
|
1539
1499
|
fieldValue = fieldSchema.default;
|
|
1540
1500
|
isUndefined = false;
|
|
1541
1501
|
}
|
|
1542
1502
|
if (fieldSchema.fk && fieldValue != null) {
|
|
1543
|
-
|
|
1544
|
-
|
|
1503
|
+
const fkTableName = typeof fieldSchema.fk.tableName === 'function' ? fieldSchema.fk.tableName(entityPatch) : fieldSchema.fk.tableName;
|
|
1504
|
+
let clientId = this.getServerToClientMap(fkTableName).get(fieldValue);
|
|
1545
1505
|
if (!clientId) {
|
|
1546
1506
|
clientId = getTempId();
|
|
1547
|
-
|
|
1548
|
-
|
|
1507
|
+
this.clientToServerIds.set(clientId, fieldValue);
|
|
1508
|
+
this.getServerToClientMap(fkTableName).set(fieldValue, clientId);
|
|
1549
1509
|
}
|
|
1550
1510
|
fieldValue = clientId;
|
|
1551
1511
|
}
|
|
@@ -1557,48 +1517,45 @@ var TempIdMap = /** @class */ (function () {
|
|
|
1557
1517
|
}
|
|
1558
1518
|
});
|
|
1559
1519
|
return patch;
|
|
1560
|
-
}
|
|
1561
|
-
|
|
1562
|
-
var _this = this;
|
|
1520
|
+
}
|
|
1521
|
+
clientToServerPatch(patch) {
|
|
1563
1522
|
patch = this.copyPatch(patch);
|
|
1564
|
-
this.mapFields(patch,
|
|
1523
|
+
this.mapFields(patch, f => f.isGenerated || !!f.fk, (fieldName, fieldSchema, fieldValue, entityPatch, tableName) => {
|
|
1565
1524
|
if (fieldValue != null) {
|
|
1566
|
-
|
|
1525
|
+
const existingId = this.clientToServerIds.get(fieldValue);
|
|
1567
1526
|
if (existingId) {
|
|
1568
1527
|
entityPatch[fieldName] = existingId;
|
|
1569
1528
|
}
|
|
1570
1529
|
}
|
|
1571
1530
|
});
|
|
1572
|
-
this.mapFields(patch,
|
|
1531
|
+
this.mapFields(patch, f => !(f.isClientOnly || f.isReadOnly) && !!f.toServer, (fieldName, fieldSchema, fieldValue, entityPatch, tableName) => {
|
|
1573
1532
|
entityPatch[fieldName] = fieldSchema.toServer(entityPatch[fieldName]);
|
|
1574
1533
|
});
|
|
1575
|
-
this.mapFields(patch,
|
|
1534
|
+
this.mapFields(patch, meta => meta.isClientOnly || meta.isReadOnly, (fieldName, fieldSchema, fieldValue, entityPatch, tableName) => {
|
|
1576
1535
|
delete entityPatch[fieldName];
|
|
1577
1536
|
});
|
|
1578
1537
|
return patch;
|
|
1579
|
-
}
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
var tablePatch = response.submit[tableName];
|
|
1538
|
+
}
|
|
1539
|
+
appendServerMapping(response) {
|
|
1540
|
+
(0, utils_1.objectKeys)(response.submit).forEach(tableName => {
|
|
1541
|
+
if (this.db.tables[tableName]) {
|
|
1542
|
+
const tablePatch = response.submit[tableName];
|
|
1585
1543
|
if (tablePatch) {
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
tablePatch.forEach(
|
|
1544
|
+
const serverToClientIds = this.getServerToClientMap(tableName);
|
|
1545
|
+
const table = this.db.tables[tableName];
|
|
1546
|
+
tablePatch.forEach(patch => {
|
|
1589
1547
|
if (isTempId(patch.id)) {
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1548
|
+
const clientId = patch.id;
|
|
1549
|
+
const serverId = table.getId(patch.payload);
|
|
1550
|
+
serverToClientIds.set(serverId, clientId);
|
|
1551
|
+
this.clientToServerIds.set(clientId, serverId);
|
|
1594
1552
|
}
|
|
1595
1553
|
});
|
|
1596
1554
|
}
|
|
1597
1555
|
}
|
|
1598
1556
|
});
|
|
1599
|
-
}
|
|
1600
|
-
|
|
1601
|
-
}());
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1602
1559
|
exports.TempIdMap = TempIdMap;
|
|
1603
1560
|
|
|
1604
1561
|
|
|
@@ -1635,20 +1592,17 @@ var DbRelationType;
|
|
|
1635
1592
|
|
|
1636
1593
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1637
1594
|
exports.difference = exports.defaultCompareViewDependencies = exports.objectKeys = void 0;
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1595
|
+
const tslib_1 = __webpack_require__(/*! tslib */ "../node_modules/tslib/tslib.es6.js");
|
|
1596
|
+
const shallowequal_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! shallowequal */ "shallowequal"));
|
|
1597
|
+
const lodash_zip_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.zip */ "lodash.zip"));
|
|
1598
|
+
const lodash_transform_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.transform */ "lodash.transform"));
|
|
1599
|
+
const lodash_isequal_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.isequal */ "lodash.isequal"));
|
|
1643
1600
|
// TBD: New typescript would contain better typing for this. Remove this hack after update.
|
|
1644
1601
|
exports.objectKeys = Object.keys;
|
|
1645
1602
|
function defaultCompareViewDependencies(prev, next) {
|
|
1646
1603
|
if (Array.isArray(prev) && Array.isArray(next)) {
|
|
1647
1604
|
if (prev.length === next.length) {
|
|
1648
|
-
return (0, lodash_zip_1.default)(prev, next).every(
|
|
1649
|
-
var p = _a[0], n = _a[1];
|
|
1650
|
-
return (0, shallowequal_1.default)(p, n);
|
|
1651
|
-
});
|
|
1605
|
+
return (0, lodash_zip_1.default)(prev, next).every(([p, n]) => (0, shallowequal_1.default)(p, n));
|
|
1652
1606
|
}
|
|
1653
1607
|
return false;
|
|
1654
1608
|
}
|