spice-js 2.6.5 → 2.6.7
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/build/bootstrap/cache.js +1 -1
- package/build/models/SpiceModel.js +172 -140
- package/package.json +1 -2
- package/src/bootstrap/cache.js +1 -1
- package/src/models/SpiceModel.js +41 -17
package/build/bootstrap/cache.js
CHANGED
|
@@ -12,7 +12,7 @@ function _connect() {
|
|
|
12
12
|
_connect = _asyncToGenerator(function* () {
|
|
13
13
|
try {
|
|
14
14
|
spice.cache_providers = {};
|
|
15
|
-
spice.monitor =
|
|
15
|
+
spice.monitor = new Map();
|
|
16
16
|
|
|
17
17
|
for (var key of Object.keys(spice.config.cache.providers)) {
|
|
18
18
|
spice.cache_providers[key] = new spice.config.cache.providers[key](spice.config.cache.drivers[key] || {});
|
|
@@ -29,6 +29,7 @@ var SDate = require("sonover-date"),
|
|
|
29
29
|
_hooks = Symbol(),
|
|
30
30
|
_disable_lifecycle_events = Symbol(),
|
|
31
31
|
_external_modifier_loaded = Symbol(),
|
|
32
|
+
_skip_cache = Symbol(),
|
|
32
33
|
_serializers = Symbol(); //const _type = Symbol("type");
|
|
33
34
|
|
|
34
35
|
|
|
@@ -51,7 +52,7 @@ class SpiceModel {
|
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
try {
|
|
54
|
-
var _args$args, _args2, _args2$args;
|
|
55
|
+
var _args$args, _args2, _args2$args, _args3, _args3$args;
|
|
55
56
|
|
|
56
57
|
var dbtype = spice.config.database.connections[args.connection].type || "couchbase";
|
|
57
58
|
|
|
@@ -63,6 +64,7 @@ class SpiceModel {
|
|
|
63
64
|
this[_external_modifier_loaded] = false;
|
|
64
65
|
this[_disable_lifecycle_events] = ((_args$args = args.args) == null ? void 0 : _args$args.disable_lifecycle_events) || false;
|
|
65
66
|
this[_ctx] = (_args2 = args) == null ? void 0 : (_args2$args = _args2.args) == null ? void 0 : _args2$args.ctx;
|
|
67
|
+
this[_skip_cache] = ((_args3 = args) == null ? void 0 : (_args3$args = _args3.args) == null ? void 0 : _args3$args.skip_cache) || false;
|
|
66
68
|
this[_hooks] = {
|
|
67
69
|
create: {
|
|
68
70
|
before: [],
|
|
@@ -432,7 +434,22 @@ class SpiceModel {
|
|
|
432
434
|
return return_string;
|
|
433
435
|
}
|
|
434
436
|
|
|
435
|
-
|
|
437
|
+
shouldUseCache(resource_type) {
|
|
438
|
+
var _spice$config$cache;
|
|
439
|
+
|
|
440
|
+
// If '_skip_cache' property of this object is true, then we shouldn't cache.
|
|
441
|
+
if (this[_skip_cache] == true) {
|
|
442
|
+
return false;
|
|
443
|
+
} // If the system configuration for spice has a cache status set to "disable",
|
|
444
|
+
// then we shouldn't cache, so return false.
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
if (((_spice$config$cache = spice.config.cache) == null ? void 0 : _spice$config$cache.status) == "disabled") {
|
|
448
|
+
return false;
|
|
449
|
+
} // If 'spice.cache[resource_type]' is not undefined,
|
|
450
|
+
// it implies that this resource type is already in the cache or is cacheable.
|
|
451
|
+
|
|
452
|
+
|
|
436
453
|
return spice.cache[resource_type] != undefined;
|
|
437
454
|
}
|
|
438
455
|
|
|
@@ -441,7 +458,7 @@ class SpiceModel {
|
|
|
441
458
|
}
|
|
442
459
|
|
|
443
460
|
getCacheProviderObject(resource_type) {
|
|
444
|
-
return spice.cache_providers[this.getCacheConfig(resource_type).driver || spice.config.cache.default_driver];
|
|
461
|
+
return spice.cache_providers[this.getCacheConfig(resource_type).driver || this.getCacheConfig(resource_type).provider || spice.config.cache.default_driver];
|
|
445
462
|
}
|
|
446
463
|
|
|
447
464
|
exists(item_type, key) {
|
|
@@ -456,26 +473,37 @@ class SpiceModel {
|
|
|
456
473
|
}
|
|
457
474
|
|
|
458
475
|
shouldForceRefresh(response) {
|
|
459
|
-
var
|
|
476
|
+
var _this3 = this;
|
|
460
477
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
}
|
|
478
|
+
return _asyncToGenerator(function* () {
|
|
479
|
+
var obj = _this3.getCacheProviderObject();
|
|
464
480
|
|
|
465
|
-
|
|
466
|
-
return true;
|
|
467
|
-
}
|
|
481
|
+
var monitor_record = yield obj.get("monitor::" + _this3.type);
|
|
468
482
|
|
|
469
|
-
|
|
483
|
+
if (monitor_record == undefined) {
|
|
484
|
+
return false;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (monitor_record.time > (response == null ? void 0 : response.time)) {
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return false;
|
|
492
|
+
})();
|
|
470
493
|
}
|
|
471
494
|
|
|
472
495
|
setMonitor() {
|
|
473
496
|
var current_time = new Date().getTime();
|
|
474
|
-
|
|
497
|
+
var obj = this.getCacheProviderObject();
|
|
498
|
+
obj.set("monitor::" + this.type, {
|
|
499
|
+
time: current_time
|
|
500
|
+
}, {
|
|
501
|
+
ttl: 0
|
|
502
|
+
}); //console.log("Monitor Set", spice.monitor);
|
|
475
503
|
}
|
|
476
504
|
|
|
477
505
|
get(args) {
|
|
478
|
-
var
|
|
506
|
+
var _this4 = this;
|
|
479
507
|
|
|
480
508
|
return _asyncToGenerator(function* () {
|
|
481
509
|
try {
|
|
@@ -484,39 +512,39 @@ class SpiceModel {
|
|
|
484
512
|
}
|
|
485
513
|
|
|
486
514
|
if (_.isString(args.id)) {
|
|
487
|
-
yield
|
|
488
|
-
var key =
|
|
515
|
+
yield _this4.run_hook(args, "get", "before");
|
|
516
|
+
var key = _this4.type + "::" + args.id;
|
|
489
517
|
var results = {};
|
|
490
518
|
|
|
491
|
-
if (
|
|
492
|
-
var cached_results = yield
|
|
519
|
+
if (_this4.shouldUseCache(_this4.type)) {
|
|
520
|
+
var cached_results = yield _this4.getCacheProviderObject(_this4.type).get(key);
|
|
493
521
|
results = cached_results == null ? void 0 : cached_results.value;
|
|
494
522
|
|
|
495
|
-
if ((cached_results == null ? void 0 : cached_results.value) == undefined ||
|
|
496
|
-
results = yield
|
|
497
|
-
yield
|
|
523
|
+
if ((cached_results == null ? void 0 : cached_results.value) == undefined || (yield _this4.shouldForceRefresh(cached_results))) {
|
|
524
|
+
results = yield _this4.database.get(args.id);
|
|
525
|
+
yield _this4.getCacheProviderObject(_this4.type).set(key, {
|
|
498
526
|
value: results,
|
|
499
527
|
time: new Date().getTime()
|
|
500
|
-
},
|
|
528
|
+
}, _this4.getCacheConfig(_this4.type));
|
|
501
529
|
}
|
|
502
530
|
} else {
|
|
503
|
-
results = yield
|
|
531
|
+
results = yield _this4.database.get(args.id);
|
|
504
532
|
}
|
|
505
533
|
|
|
506
534
|
if (results.type != undefined) {
|
|
507
|
-
if (results.type !=
|
|
535
|
+
if (results.type != _this4.type) throw new Error(_this4.type + " does not exist type");
|
|
508
536
|
}
|
|
509
537
|
|
|
510
538
|
if (results._type != undefined) {
|
|
511
|
-
if (results._type !=
|
|
539
|
+
if (results._type != _this4.type) throw new Error(_this4.type + " does not exist _type");
|
|
512
540
|
}
|
|
513
541
|
|
|
514
542
|
if (results.deleted == undefined || results.deleted == false) {
|
|
515
|
-
yield
|
|
516
|
-
results = yield
|
|
543
|
+
yield _this4.run_hook(results, "get", "after");
|
|
544
|
+
results = yield _this4.do_serialize(results, "read", {}, args, (yield _this4.propsToBeRemoved(results)));
|
|
517
545
|
return results;
|
|
518
546
|
} else {
|
|
519
|
-
throw new Error(
|
|
547
|
+
throw new Error(_this4.type + " does not exist");
|
|
520
548
|
}
|
|
521
549
|
}
|
|
522
550
|
} catch (e) {
|
|
@@ -526,11 +554,11 @@ class SpiceModel {
|
|
|
526
554
|
}
|
|
527
555
|
|
|
528
556
|
query(query, scope) {
|
|
529
|
-
var
|
|
557
|
+
var _this5 = this;
|
|
530
558
|
|
|
531
559
|
return _asyncToGenerator(function* () {
|
|
532
560
|
try {
|
|
533
|
-
var results = yield
|
|
561
|
+
var results = yield _this5.database.query(query, scope);
|
|
534
562
|
return results;
|
|
535
563
|
} catch (error) {
|
|
536
564
|
throw error;
|
|
@@ -539,7 +567,7 @@ class SpiceModel {
|
|
|
539
567
|
}
|
|
540
568
|
|
|
541
569
|
getMulti(args) {
|
|
542
|
-
var
|
|
570
|
+
var _this6 = this;
|
|
543
571
|
|
|
544
572
|
return _asyncToGenerator(function* () {
|
|
545
573
|
try {
|
|
@@ -547,36 +575,36 @@ class SpiceModel {
|
|
|
547
575
|
args = {};
|
|
548
576
|
}
|
|
549
577
|
|
|
550
|
-
yield
|
|
578
|
+
yield _this6.run_hook(_this6, "list", "before");
|
|
551
579
|
|
|
552
580
|
_.remove(args.ids, o => o == undefined);
|
|
553
581
|
|
|
554
|
-
var key =
|
|
582
|
+
var key = _this6.type + "::" + _.join(args.ids, "|");
|
|
555
583
|
|
|
556
584
|
var results = [];
|
|
557
585
|
|
|
558
586
|
if (args.ids.length > 0) {
|
|
559
|
-
if (
|
|
560
|
-
var cached_results = yield
|
|
587
|
+
if (_this6.shouldUseCache(_this6.type)) {
|
|
588
|
+
var cached_results = yield _this6.getCacheProviderObject(_this6.type).get(key);
|
|
561
589
|
results = cached_results == null ? void 0 : cached_results.value;
|
|
562
590
|
|
|
563
|
-
if ((cached_results == null ? void 0 : cached_results.value) == undefined ||
|
|
564
|
-
results = yield
|
|
591
|
+
if ((cached_results == null ? void 0 : cached_results.value) == undefined || (yield _this6.shouldForceRefresh(cached_results))) {
|
|
592
|
+
results = yield _this6.database.multi_get(args.ids, true);
|
|
565
593
|
|
|
566
|
-
|
|
594
|
+
_this6.getCacheProviderObject(_this6.type).set(key, {
|
|
567
595
|
value: results,
|
|
568
596
|
time: new Date().getTime()
|
|
569
|
-
},
|
|
597
|
+
}, _this6.getCacheConfig(_this6.type));
|
|
570
598
|
}
|
|
571
599
|
} else {
|
|
572
|
-
results = yield
|
|
600
|
+
results = yield _this6.database.multi_get(args.ids, true);
|
|
573
601
|
}
|
|
574
602
|
}
|
|
575
603
|
|
|
576
|
-
_.remove(results, o => o.type !=
|
|
604
|
+
_.remove(results, o => o.type != _this6.type);
|
|
577
605
|
|
|
578
|
-
yield
|
|
579
|
-
results = yield
|
|
606
|
+
yield _this6.run_hook(results, "list", "after", args.context);
|
|
607
|
+
results = yield _this6.do_serialize(results, "read", {}, args, (yield _this6.propsToBeRemoved(results)));
|
|
580
608
|
return results;
|
|
581
609
|
} catch (e) {
|
|
582
610
|
console.log(e.stack);
|
|
@@ -586,23 +614,23 @@ class SpiceModel {
|
|
|
586
614
|
}
|
|
587
615
|
|
|
588
616
|
exist(data) {
|
|
589
|
-
var
|
|
617
|
+
var _this7 = this;
|
|
590
618
|
|
|
591
619
|
return _asyncToGenerator(function* () {
|
|
592
620
|
try {
|
|
593
621
|
if (_.isString(data)) {
|
|
594
|
-
var result = yield
|
|
595
|
-
if (result.type) if (result.type !=
|
|
622
|
+
var result = yield _this7.database.get(data);
|
|
623
|
+
if (result.type) if (result.type != _this7.type) {
|
|
596
624
|
return false;
|
|
597
625
|
}
|
|
598
|
-
if (result._type) if (result._type !=
|
|
626
|
+
if (result._type) if (result._type != _this7.type) {
|
|
599
627
|
return false;
|
|
600
628
|
}
|
|
601
629
|
} else {
|
|
602
|
-
if (data.type) if (data.type !=
|
|
630
|
+
if (data.type) if (data.type != _this7.type) {
|
|
603
631
|
return false;
|
|
604
632
|
}
|
|
605
|
-
if (data._type) if (data._type !=
|
|
633
|
+
if (data._type) if (data._type != _this7.type) {
|
|
606
634
|
return false;
|
|
607
635
|
}
|
|
608
636
|
}
|
|
@@ -615,54 +643,54 @@ class SpiceModel {
|
|
|
615
643
|
}
|
|
616
644
|
|
|
617
645
|
update(args) {
|
|
618
|
-
var
|
|
646
|
+
var _this8 = this;
|
|
619
647
|
|
|
620
648
|
return _asyncToGenerator(function* () {
|
|
621
649
|
try {
|
|
622
|
-
|
|
623
|
-
var results = yield
|
|
624
|
-
var item_exist = yield
|
|
650
|
+
_this8.updated_at = new SDate().now();
|
|
651
|
+
var results = yield _this8.database.get(args.id);
|
|
652
|
+
var item_exist = yield _this8.exist(results);
|
|
625
653
|
|
|
626
654
|
if (!item_exist) {
|
|
627
|
-
throw new Error(
|
|
655
|
+
throw new Error(_this8.type + " does not exist. in update");
|
|
628
656
|
}
|
|
629
657
|
|
|
630
658
|
delete results["id"];
|
|
631
659
|
|
|
632
|
-
_.defaults(
|
|
660
|
+
_.defaults(_this8, results);
|
|
633
661
|
|
|
634
662
|
var cover_obj = {
|
|
635
663
|
old: results,
|
|
636
|
-
new:
|
|
664
|
+
new: _this8,
|
|
637
665
|
id: args.id
|
|
638
666
|
};
|
|
639
667
|
|
|
640
668
|
if (args.skip_hooks != true) {
|
|
641
|
-
yield
|
|
669
|
+
yield _this8.run_hook(cover_obj, "update", "before", results);
|
|
642
670
|
}
|
|
643
671
|
|
|
644
672
|
var form;
|
|
645
673
|
|
|
646
674
|
if (args.skip_write_serialize != true && args.skip_serialize != true) {
|
|
647
|
-
form = yield
|
|
675
|
+
form = yield _this8.do_serialize(_this8, "write", cover_obj.new, args);
|
|
648
676
|
}
|
|
649
677
|
|
|
650
|
-
var db_data = form ||
|
|
651
|
-
yield
|
|
678
|
+
var db_data = form || _this8;
|
|
679
|
+
yield _this8.database.update(args.id, db_data);
|
|
652
680
|
|
|
653
|
-
|
|
681
|
+
_this8.setMonitor();
|
|
654
682
|
|
|
655
683
|
if (args.skip_hooks != true) {
|
|
656
|
-
yield
|
|
684
|
+
yield _this8.run_hook(_extends({}, _this8, {
|
|
657
685
|
id: args.id
|
|
658
686
|
}), "update", "after", results);
|
|
659
687
|
}
|
|
660
688
|
|
|
661
689
|
if (args.skip_read_serialize != true && args.skip_serialize != true) {
|
|
662
|
-
form = yield
|
|
690
|
+
form = yield _this8.do_serialize(form, "read", {}, args, (yield _this8.propsToBeRemoved(form)));
|
|
663
691
|
}
|
|
664
692
|
|
|
665
|
-
|
|
693
|
+
_this8.id = args.id;
|
|
666
694
|
return _extends({}, form, {
|
|
667
695
|
id: args.id
|
|
668
696
|
});
|
|
@@ -674,38 +702,38 @@ class SpiceModel {
|
|
|
674
702
|
}
|
|
675
703
|
|
|
676
704
|
create(args) {
|
|
677
|
-
var
|
|
705
|
+
var _this9 = this;
|
|
678
706
|
|
|
679
707
|
return _asyncToGenerator(function* () {
|
|
680
708
|
try {
|
|
681
709
|
var form;
|
|
682
|
-
|
|
710
|
+
_this9.created_at = new SDate().now();
|
|
683
711
|
|
|
684
712
|
if (args.body) {
|
|
685
713
|
form = _.defaults({}, args.body);
|
|
686
|
-
form.created_at =
|
|
687
|
-
form.updated_at =
|
|
714
|
+
form.created_at = _this9.created_at;
|
|
715
|
+
form.updated_at = _this9.created_at;
|
|
688
716
|
delete form["bucket"];
|
|
689
717
|
}
|
|
690
718
|
|
|
691
|
-
var workingForm = form ||
|
|
692
|
-
|
|
719
|
+
var workingForm = form || _this9;
|
|
720
|
+
_this9.updated_at = new SDate().now();
|
|
693
721
|
var id = args.id_prefix + "-" + UUID.v4();
|
|
694
722
|
|
|
695
723
|
if (args && args.id) {
|
|
696
724
|
id = args.id;
|
|
697
725
|
}
|
|
698
726
|
|
|
699
|
-
yield
|
|
700
|
-
workingForm = yield
|
|
701
|
-
var results = yield
|
|
727
|
+
yield _this9.run_hook(workingForm, "create", "before");
|
|
728
|
+
workingForm = yield _this9.do_serialize(workingForm, "write", {}, args);
|
|
729
|
+
var results = yield _this9.database.insert(id, workingForm, args.expiry);
|
|
702
730
|
|
|
703
|
-
|
|
731
|
+
_this9.setMonitor();
|
|
704
732
|
|
|
705
|
-
yield
|
|
733
|
+
yield _this9.run_hook(_extends({}, results, {
|
|
706
734
|
id
|
|
707
735
|
}), "create", "after");
|
|
708
|
-
results = yield
|
|
736
|
+
results = yield _this9.do_serialize(results, "read", {}, args, (yield _this9.propsToBeRemoved(results)));
|
|
709
737
|
return _extends({}, results, {
|
|
710
738
|
id
|
|
711
739
|
});
|
|
@@ -717,32 +745,32 @@ class SpiceModel {
|
|
|
717
745
|
}
|
|
718
746
|
|
|
719
747
|
delete(args) {
|
|
720
|
-
var
|
|
748
|
+
var _this10 = this;
|
|
721
749
|
|
|
722
750
|
return _asyncToGenerator(function* () {
|
|
723
|
-
var item_exist = yield
|
|
751
|
+
var item_exist = yield _this10.exist(args.id);
|
|
724
752
|
|
|
725
753
|
if (!item_exist) {
|
|
726
|
-
throw new Error(
|
|
754
|
+
throw new Error(_this10.type + " does not exist.");
|
|
727
755
|
}
|
|
728
756
|
|
|
729
|
-
var results = yield
|
|
757
|
+
var results = yield _this10.database.get(args.id);
|
|
730
758
|
|
|
731
759
|
try {
|
|
732
|
-
yield
|
|
760
|
+
yield _this10.run_hook(args, "delete", "before");
|
|
733
761
|
var delete_response = {};
|
|
734
762
|
|
|
735
763
|
if (args.hard) {
|
|
736
|
-
delete_response = yield
|
|
764
|
+
delete_response = yield _this10.database.delete(args.id);
|
|
737
765
|
|
|
738
|
-
|
|
766
|
+
_this10.setMonitor();
|
|
739
767
|
} else {
|
|
740
768
|
delete results["id"];
|
|
741
769
|
results.deleted = true;
|
|
742
|
-
delete_response = yield
|
|
770
|
+
delete_response = yield _this10.database.update(args.id, "");
|
|
743
771
|
}
|
|
744
772
|
|
|
745
|
-
yield
|
|
773
|
+
yield _this10.run_hook(results, "delete", "after", results);
|
|
746
774
|
return {};
|
|
747
775
|
} catch (e) {
|
|
748
776
|
console.log(e.stack);
|
|
@@ -756,7 +784,7 @@ class SpiceModel {
|
|
|
756
784
|
}
|
|
757
785
|
|
|
758
786
|
list(args) {
|
|
759
|
-
var
|
|
787
|
+
var _this11 = this;
|
|
760
788
|
|
|
761
789
|
return _asyncToGenerator(function* () {
|
|
762
790
|
function filterResultsByColumns(data, columns) {
|
|
@@ -784,7 +812,7 @@ class SpiceModel {
|
|
|
784
812
|
query = args.query;
|
|
785
813
|
} else {
|
|
786
814
|
if (args.filters) {
|
|
787
|
-
query =
|
|
815
|
+
query = _this11.makeQueryFromFilter(args.filters);
|
|
788
816
|
} else {
|
|
789
817
|
if (args.query) {
|
|
790
818
|
query = args.query + " AND (deleted = false OR deleted IS MISSING) ";
|
|
@@ -806,69 +834,69 @@ class SpiceModel {
|
|
|
806
834
|
args.sort = "created_at DESC";
|
|
807
835
|
}
|
|
808
836
|
|
|
809
|
-
yield
|
|
837
|
+
yield _this11.run_hook(_this11, "list", "before");
|
|
810
838
|
|
|
811
839
|
function removeSpaceAndSpecialCharacters(str) {
|
|
812
840
|
return str.replace(/[^a-zA-Z0-9]/g, "");
|
|
813
841
|
}
|
|
814
842
|
|
|
815
|
-
var key = removeSpaceAndSpecialCharacters(
|
|
843
|
+
var key = removeSpaceAndSpecialCharacters(_this11.type + "::" + query + "::" + args.limit + "::" + args.offset + "::" + args.sort + "::" + args.do_count + "::" + args.statement_consistent + "::" + args.columns + "::" + args.is_full_text + "::" + args.is_custom_query);
|
|
816
844
|
var results;
|
|
817
845
|
|
|
818
846
|
if (args.is_custom_query && args.is_custom_query === "true") {
|
|
819
847
|
if (args.ids.length > 0) {
|
|
820
|
-
if (
|
|
821
|
-
var cached_results = yield
|
|
848
|
+
if (_this11.shouldUseCache(_this11.type)) {
|
|
849
|
+
var cached_results = yield _this11.getCacheProviderObject(_this11.type).get(key);
|
|
822
850
|
results = cached_results == null ? void 0 : cached_results.value;
|
|
823
851
|
|
|
824
|
-
if ((cached_results == null ? void 0 : cached_results.value) == undefined ||
|
|
825
|
-
results = yield
|
|
852
|
+
if ((cached_results == null ? void 0 : cached_results.value) == undefined || (yield _this11.shouldForceRefresh(cached_results))) {
|
|
853
|
+
results = yield _this11.database.query(query);
|
|
826
854
|
|
|
827
|
-
|
|
855
|
+
_this11.getCacheProviderObject(_this11.type).set(key, {
|
|
828
856
|
value: results,
|
|
829
857
|
time: new Date().getTime()
|
|
830
|
-
},
|
|
858
|
+
}, _this11.getCacheConfig(_this11.type));
|
|
831
859
|
}
|
|
832
860
|
} else {
|
|
833
|
-
results = yield
|
|
861
|
+
results = yield _this11.database.query(query);
|
|
834
862
|
}
|
|
835
863
|
}
|
|
836
864
|
} else {
|
|
837
865
|
if (args.is_full_text && args.is_full_text === "true") {
|
|
838
|
-
if (
|
|
839
|
-
var _cached_results = yield
|
|
866
|
+
if (_this11.shouldUseCache(_this11.type)) {
|
|
867
|
+
var _cached_results = yield _this11.getCacheProviderObject(_this11.type).get(key);
|
|
840
868
|
|
|
841
869
|
results = _cached_results == null ? void 0 : _cached_results.value;
|
|
842
870
|
|
|
843
|
-
if ((_cached_results == null ? void 0 : _cached_results.value) == undefined ||
|
|
844
|
-
results = yield
|
|
871
|
+
if ((_cached_results == null ? void 0 : _cached_results.value) == undefined || (yield _this11.shouldForceRefresh(_cached_results))) {
|
|
872
|
+
results = yield _this11.database.full_text_search(_this11.type, query || "", args.limit, args.offset);
|
|
845
873
|
|
|
846
|
-
|
|
874
|
+
_this11.getCacheProviderObject(_this11.type).set(key, {
|
|
847
875
|
value: results,
|
|
848
876
|
time: new Date().getTime()
|
|
849
|
-
},
|
|
877
|
+
}, _this11.getCacheConfig(_this11.type));
|
|
850
878
|
}
|
|
851
879
|
} else {
|
|
852
|
-
results = yield
|
|
880
|
+
results = yield _this11.database.full_text_search(_this11.type, query || "", args.limit, args.offset);
|
|
853
881
|
}
|
|
854
882
|
} else {
|
|
855
|
-
if (
|
|
856
|
-
var _cached_results2 = yield
|
|
883
|
+
if (_this11.shouldUseCache(_this11.type)) {
|
|
884
|
+
var _cached_results2 = yield _this11.getCacheProviderObject(_this11.type).get(key);
|
|
857
885
|
|
|
858
886
|
results = _cached_results2 == null ? void 0 : _cached_results2.value;
|
|
859
887
|
|
|
860
|
-
if ((_cached_results2 == null ? void 0 : _cached_results2.value) == undefined ||
|
|
861
|
-
results = yield
|
|
888
|
+
if ((_cached_results2 == null ? void 0 : _cached_results2.value) == undefined || (yield _this11.shouldForceRefresh(_cached_results2))) {
|
|
889
|
+
results = yield _this11.database.search(_this11.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent);
|
|
862
890
|
|
|
863
|
-
|
|
891
|
+
_this11.getCacheProviderObject(_this11.type).set(key, {
|
|
864
892
|
value: results,
|
|
865
893
|
time: new Date().getTime()
|
|
866
|
-
},
|
|
894
|
+
}, _this11.getCacheConfig(_this11.type));
|
|
867
895
|
}
|
|
868
896
|
} else {
|
|
869
|
-
results = yield
|
|
897
|
+
results = yield _this11.database.search(_this11.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent);
|
|
870
898
|
|
|
871
|
-
if (
|
|
899
|
+
if (_this11.type == "user") {
|
|
872
900
|
console.log("results from DB No Chache available", results);
|
|
873
901
|
}
|
|
874
902
|
}
|
|
@@ -876,8 +904,8 @@ class SpiceModel {
|
|
|
876
904
|
}
|
|
877
905
|
|
|
878
906
|
try {
|
|
879
|
-
yield
|
|
880
|
-
results.data = yield
|
|
907
|
+
yield _this11.run_hook(results.data, "list", "after");
|
|
908
|
+
results.data = yield _this11.do_serialize(results.data, "read", {}, args, (yield _this11.propsToBeRemoved(results.data)));
|
|
881
909
|
} catch (e) {
|
|
882
910
|
console.log(e);
|
|
883
911
|
}
|
|
@@ -903,26 +931,26 @@ class SpiceModel {
|
|
|
903
931
|
}
|
|
904
932
|
|
|
905
933
|
run_hook(data, op, when, old_data) {
|
|
906
|
-
var
|
|
934
|
+
var _this12 = this;
|
|
907
935
|
|
|
908
936
|
return _asyncToGenerator(function* () {
|
|
909
937
|
try {
|
|
910
|
-
if (
|
|
938
|
+
if (_this12[_disable_lifecycle_events] == false) {
|
|
911
939
|
var resourceLifecycleTriggered = new _ResourceLifecycleTriggered.default({
|
|
912
940
|
data: {
|
|
913
941
|
data,
|
|
914
942
|
operation: op,
|
|
915
943
|
when,
|
|
916
944
|
old_data,
|
|
917
|
-
resource:
|
|
918
|
-
ctx:
|
|
945
|
+
resource: _this12.type,
|
|
946
|
+
ctx: _this12[_ctx]
|
|
919
947
|
}
|
|
920
948
|
});
|
|
921
949
|
resourceLifecycleTriggered.dispatch();
|
|
922
950
|
}
|
|
923
951
|
|
|
924
|
-
if (
|
|
925
|
-
for (var i of
|
|
952
|
+
if (_this12[_hooks] && _this12[_hooks][op] && _this12[_hooks][op][when]) {
|
|
953
|
+
for (var i of _this12[_hooks][op][when]) {
|
|
926
954
|
data = yield i(data, old_data);
|
|
927
955
|
}
|
|
928
956
|
}
|
|
@@ -955,7 +983,7 @@ class SpiceModel {
|
|
|
955
983
|
}
|
|
956
984
|
|
|
957
985
|
mapToObject(data, Class, source_property, store_property, property) {
|
|
958
|
-
var
|
|
986
|
+
var _this13 = this;
|
|
959
987
|
|
|
960
988
|
return _asyncToGenerator(function* () {
|
|
961
989
|
var original_is_array = _.isArray(data);
|
|
@@ -984,7 +1012,9 @@ class SpiceModel {
|
|
|
984
1012
|
});
|
|
985
1013
|
|
|
986
1014
|
var returned_all = yield Promise.allSettled(_.map(classes, obj => {
|
|
987
|
-
return new obj(
|
|
1015
|
+
return new obj(_extends({}, _this13[_args], {
|
|
1016
|
+
skip_cache: _this13[_skip_cache]
|
|
1017
|
+
})).getMulti({
|
|
988
1018
|
ids: ids
|
|
989
1019
|
});
|
|
990
1020
|
}));
|
|
@@ -1006,7 +1036,7 @@ class SpiceModel {
|
|
|
1006
1036
|
}
|
|
1007
1037
|
|
|
1008
1038
|
mapToObjectArray(data, Class, source_property, store_property, property) {
|
|
1009
|
-
var
|
|
1039
|
+
var _this14 = this;
|
|
1010
1040
|
|
|
1011
1041
|
return _asyncToGenerator(function* () {
|
|
1012
1042
|
var original_is_array = _.isArray(data);
|
|
@@ -1035,7 +1065,9 @@ class SpiceModel {
|
|
|
1035
1065
|
|
|
1036
1066
|
var classes = _.isArray(Class) ? Class : [Class];
|
|
1037
1067
|
var returned_all = yield Promise.allSettled(_.map(classes, obj => {
|
|
1038
|
-
return new obj(
|
|
1068
|
+
return new obj(_extends({}, _this14[_args], {
|
|
1069
|
+
skip_cache: _this14[_skip_cache]
|
|
1070
|
+
})).getMulti({
|
|
1039
1071
|
ids: ids
|
|
1040
1072
|
});
|
|
1041
1073
|
}));
|
|
@@ -1095,7 +1127,7 @@ class SpiceModel {
|
|
|
1095
1127
|
}
|
|
1096
1128
|
|
|
1097
1129
|
createMofifier(properties) {
|
|
1098
|
-
var
|
|
1130
|
+
var _this15 = this;
|
|
1099
1131
|
|
|
1100
1132
|
var _loop = function _loop(i) {
|
|
1101
1133
|
if (properties[i].map) {
|
|
@@ -1106,11 +1138,11 @@ class SpiceModel {
|
|
|
1106
1138
|
case String:
|
|
1107
1139
|
case "string":
|
|
1108
1140
|
{
|
|
1109
|
-
|
|
1141
|
+
_this15.addModifier({
|
|
1110
1142
|
when: properties[i].map.when || "read",
|
|
1111
1143
|
execute: function () {
|
|
1112
1144
|
var _execute = _asyncToGenerator(function* (data) {
|
|
1113
|
-
return yield
|
|
1145
|
+
return yield _this15.mapToObject(data, _.isString(properties[i].map.reference) ? spice.models[properties[i].map.reference] : properties[i].map.reference, i, properties[i].map.destination || i, properties[i]);
|
|
1114
1146
|
});
|
|
1115
1147
|
|
|
1116
1148
|
function execute(_x) {
|
|
@@ -1127,11 +1159,11 @@ class SpiceModel {
|
|
|
1127
1159
|
case Array:
|
|
1128
1160
|
case "array":
|
|
1129
1161
|
{
|
|
1130
|
-
|
|
1162
|
+
_this15.addModifier({
|
|
1131
1163
|
when: properties[i].map.when || "read",
|
|
1132
1164
|
execute: function () {
|
|
1133
1165
|
var _execute2 = _asyncToGenerator(function* (data) {
|
|
1134
|
-
return yield
|
|
1166
|
+
return yield _this15.mapToObjectArray(data, _.isString(properties[i].map.reference) ? spice.models[properties[i].map.reference] : properties[i].map.reference, i, properties[i].map.destination || i, properties[i]);
|
|
1135
1167
|
});
|
|
1136
1168
|
|
|
1137
1169
|
function execute(_x2) {
|
|
@@ -1163,7 +1195,7 @@ class SpiceModel {
|
|
|
1163
1195
|
}
|
|
1164
1196
|
|
|
1165
1197
|
do_serialize(data, type, old_data, args, path_to_be_removed) {
|
|
1166
|
-
var
|
|
1198
|
+
var _this16 = this;
|
|
1167
1199
|
|
|
1168
1200
|
return _asyncToGenerator(function* () {
|
|
1169
1201
|
//console.log("CTX INside Model DO", this[_ctx]);
|
|
@@ -1173,16 +1205,16 @@ class SpiceModel {
|
|
|
1173
1205
|
path_to_be_removed = [];
|
|
1174
1206
|
}
|
|
1175
1207
|
|
|
1176
|
-
if (
|
|
1177
|
-
if (
|
|
1178
|
-
|
|
1208
|
+
if (_this16.shouldSerializerRun(args, type)) {
|
|
1209
|
+
if (_this16.type != "" && _this16.type != undefined && _this16[_external_modifier_loaded] != true) {
|
|
1210
|
+
_this16.addExternalModifiers(_this16.type);
|
|
1179
1211
|
|
|
1180
|
-
|
|
1212
|
+
_this16[_external_modifier_loaded] = true;
|
|
1181
1213
|
}
|
|
1182
1214
|
|
|
1183
|
-
for (var i of
|
|
1215
|
+
for (var i of _this16[_serializers][type]["modifiers"]) {
|
|
1184
1216
|
try {
|
|
1185
|
-
data = yield i(data, old_data,
|
|
1217
|
+
data = yield i(data, old_data, _this16[_ctx], _this16.type);
|
|
1186
1218
|
} catch (e) {
|
|
1187
1219
|
console.log(e.stack);
|
|
1188
1220
|
}
|
|
@@ -1198,12 +1230,12 @@ class SpiceModel {
|
|
|
1198
1230
|
|
|
1199
1231
|
var defaults = {};
|
|
1200
1232
|
|
|
1201
|
-
for (var _i in
|
|
1202
|
-
if (
|
|
1203
|
-
defaults[_i] = _.isFunction(
|
|
1233
|
+
for (var _i in _this16.props) {
|
|
1234
|
+
if (_this16.props[_i].defaults != undefined && _this16.props[_i].defaults[type] != undefined && _this16.props[_i].defaults[type] != undefined) {
|
|
1235
|
+
defaults[_i] = _.isFunction(_this16.props[_i].defaults[type]) ? _this16.props[_i].defaults[type]({
|
|
1204
1236
|
old_data: data,
|
|
1205
1237
|
new_data: old_data
|
|
1206
|
-
}) :
|
|
1238
|
+
}) : _this16.props[_i].defaults[type];
|
|
1207
1239
|
}
|
|
1208
1240
|
} // apply defaults
|
|
1209
1241
|
|
|
@@ -1216,8 +1248,8 @@ class SpiceModel {
|
|
|
1216
1248
|
if (type == "read") {
|
|
1217
1249
|
var props_to_clean = ["deleted", "type", ...path_to_be_removed];
|
|
1218
1250
|
|
|
1219
|
-
for (var _i3 in
|
|
1220
|
-
if (
|
|
1251
|
+
for (var _i3 in _this16.props) {
|
|
1252
|
+
if (_this16.props[_i3].hide) {
|
|
1221
1253
|
props_to_clean.push(_i3);
|
|
1222
1254
|
}
|
|
1223
1255
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "spice-js",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.7",
|
|
4
4
|
"description": "spice",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -39,7 +39,6 @@
|
|
|
39
39
|
"regenerator-runtime": "^0.13.7",
|
|
40
40
|
"socket.io-client": "^4.0.1",
|
|
41
41
|
"sonover-date": "^1.0.1",
|
|
42
|
-
"spice-couchbase": "^2.0.14",
|
|
43
42
|
"uuid": "^3.3.3",
|
|
44
43
|
"winston": "^3.2.1"
|
|
45
44
|
},
|
package/src/bootstrap/cache.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
async function connect() {
|
|
4
4
|
try {
|
|
5
5
|
spice.cache_providers = {};
|
|
6
|
-
spice.monitor =
|
|
6
|
+
spice.monitor = new Map();
|
|
7
7
|
for (let key of Object.keys(spice.config.cache.providers)) {
|
|
8
8
|
spice.cache_providers[key] = new spice.config.cache.providers[key](
|
|
9
9
|
spice.config.cache.drivers[key] || {}
|
package/src/models/SpiceModel.js
CHANGED
|
@@ -15,6 +15,7 @@ var SDate = require("sonover-date"),
|
|
|
15
15
|
_hooks = Symbol(),
|
|
16
16
|
_disable_lifecycle_events = Symbol(),
|
|
17
17
|
_external_modifier_loaded = Symbol(),
|
|
18
|
+
_skip_cache = Symbol(),
|
|
18
19
|
_serializers = Symbol();
|
|
19
20
|
|
|
20
21
|
//const _type = Symbol("type");
|
|
@@ -48,6 +49,7 @@ export default class SpiceModel {
|
|
|
48
49
|
this[_disable_lifecycle_events] =
|
|
49
50
|
args.args?.disable_lifecycle_events || false;
|
|
50
51
|
this[_ctx] = args?.args?.ctx;
|
|
52
|
+
this[_skip_cache] = args?.args?.skip_cache || false;
|
|
51
53
|
this[_hooks] = {
|
|
52
54
|
create: {
|
|
53
55
|
before: [],
|
|
@@ -365,7 +367,20 @@ export default class SpiceModel {
|
|
|
365
367
|
return return_string;
|
|
366
368
|
}
|
|
367
369
|
|
|
368
|
-
|
|
370
|
+
shouldUseCache(resource_type) {
|
|
371
|
+
// If '_skip_cache' property of this object is true, then we shouldn't cache.
|
|
372
|
+
if (this[_skip_cache] == true) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// If the system configuration for spice has a cache status set to "disable",
|
|
377
|
+
// then we shouldn't cache, so return false.
|
|
378
|
+
if (spice.config.cache?.status == "disabled") {
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// If 'spice.cache[resource_type]' is not undefined,
|
|
383
|
+
// it implies that this resource type is already in the cache or is cacheable.
|
|
369
384
|
return spice.cache[resource_type] != undefined;
|
|
370
385
|
}
|
|
371
386
|
|
|
@@ -376,6 +391,7 @@ export default class SpiceModel {
|
|
|
376
391
|
getCacheProviderObject(resource_type) {
|
|
377
392
|
return spice.cache_providers[
|
|
378
393
|
this.getCacheConfig(resource_type).driver ||
|
|
394
|
+
this.getCacheConfig(resource_type).provider ||
|
|
379
395
|
spice.config.cache.default_driver
|
|
380
396
|
];
|
|
381
397
|
}
|
|
@@ -386,12 +402,13 @@ export default class SpiceModel {
|
|
|
386
402
|
return false;
|
|
387
403
|
}
|
|
388
404
|
|
|
389
|
-
shouldForceRefresh(response) {
|
|
390
|
-
let
|
|
405
|
+
async shouldForceRefresh(response) {
|
|
406
|
+
let obj = this.getCacheProviderObject();
|
|
407
|
+
let monitor_record = await obj.get(`monitor::${this.type}`);
|
|
391
408
|
if (monitor_record == undefined) {
|
|
392
409
|
return false;
|
|
393
410
|
}
|
|
394
|
-
if (monitor_record > response?.time) {
|
|
411
|
+
if (monitor_record.time > response?.time) {
|
|
395
412
|
return true;
|
|
396
413
|
}
|
|
397
414
|
return false;
|
|
@@ -399,7 +416,8 @@ export default class SpiceModel {
|
|
|
399
416
|
|
|
400
417
|
setMonitor() {
|
|
401
418
|
let current_time = new Date().getTime();
|
|
402
|
-
|
|
419
|
+
let obj = this.getCacheProviderObject();
|
|
420
|
+
obj.set(`monitor::${this.type}`, { time: current_time }, { ttl: 0 });
|
|
403
421
|
//console.log("Monitor Set", spice.monitor);
|
|
404
422
|
}
|
|
405
423
|
|
|
@@ -412,14 +430,14 @@ export default class SpiceModel {
|
|
|
412
430
|
await this.run_hook(args, "get", "before");
|
|
413
431
|
let key = `${this.type}::${args.id}`;
|
|
414
432
|
let results = {};
|
|
415
|
-
if (this.
|
|
433
|
+
if (this.shouldUseCache(this.type)) {
|
|
416
434
|
let cached_results = await this.getCacheProviderObject(this.type).get(
|
|
417
435
|
key
|
|
418
436
|
);
|
|
419
437
|
results = cached_results?.value;
|
|
420
438
|
if (
|
|
421
439
|
cached_results?.value == undefined ||
|
|
422
|
-
this.shouldForceRefresh(cached_results)
|
|
440
|
+
(await this.shouldForceRefresh(cached_results))
|
|
423
441
|
) {
|
|
424
442
|
results = await this.database.get(args.id);
|
|
425
443
|
await this.getCacheProviderObject(this.type).set(
|
|
@@ -481,14 +499,14 @@ export default class SpiceModel {
|
|
|
481
499
|
let key = `${this.type}::${_.join(args.ids, "|")}`;
|
|
482
500
|
let results = [];
|
|
483
501
|
if (args.ids.length > 0) {
|
|
484
|
-
if (this.
|
|
502
|
+
if (this.shouldUseCache(this.type)) {
|
|
485
503
|
let cached_results = await this.getCacheProviderObject(this.type).get(
|
|
486
504
|
key
|
|
487
505
|
);
|
|
488
506
|
results = cached_results?.value;
|
|
489
507
|
if (
|
|
490
508
|
cached_results?.value == undefined ||
|
|
491
|
-
this.shouldForceRefresh(cached_results)
|
|
509
|
+
(await this.shouldForceRefresh(cached_results))
|
|
492
510
|
) {
|
|
493
511
|
results = await this.database.multi_get(args.ids, true);
|
|
494
512
|
this.getCacheProviderObject(this.type).set(
|
|
@@ -736,14 +754,14 @@ export default class SpiceModel {
|
|
|
736
754
|
let results;
|
|
737
755
|
if (args.is_custom_query && args.is_custom_query === "true") {
|
|
738
756
|
if (args.ids.length > 0) {
|
|
739
|
-
if (this.
|
|
757
|
+
if (this.shouldUseCache(this.type)) {
|
|
740
758
|
let cached_results = await this.getCacheProviderObject(
|
|
741
759
|
this.type
|
|
742
760
|
).get(key);
|
|
743
761
|
results = cached_results?.value;
|
|
744
762
|
if (
|
|
745
763
|
cached_results?.value == undefined ||
|
|
746
|
-
this.shouldForceRefresh(cached_results)
|
|
764
|
+
(await this.shouldForceRefresh(cached_results))
|
|
747
765
|
) {
|
|
748
766
|
results = await this.database.query(query);
|
|
749
767
|
this.getCacheProviderObject(this.type).set(
|
|
@@ -758,14 +776,14 @@ export default class SpiceModel {
|
|
|
758
776
|
}
|
|
759
777
|
} else {
|
|
760
778
|
if (args.is_full_text && args.is_full_text === "true") {
|
|
761
|
-
if (this.
|
|
779
|
+
if (this.shouldUseCache(this.type)) {
|
|
762
780
|
let cached_results = await this.getCacheProviderObject(
|
|
763
781
|
this.type
|
|
764
782
|
).get(key);
|
|
765
783
|
results = cached_results?.value;
|
|
766
784
|
if (
|
|
767
785
|
cached_results?.value == undefined ||
|
|
768
|
-
this.shouldForceRefresh(cached_results)
|
|
786
|
+
(await this.shouldForceRefresh(cached_results))
|
|
769
787
|
) {
|
|
770
788
|
results = await this.database.full_text_search(
|
|
771
789
|
this.type,
|
|
@@ -788,14 +806,14 @@ export default class SpiceModel {
|
|
|
788
806
|
);
|
|
789
807
|
}
|
|
790
808
|
} else {
|
|
791
|
-
if (this.
|
|
809
|
+
if (this.shouldUseCache(this.type)) {
|
|
792
810
|
let cached_results = await this.getCacheProviderObject(
|
|
793
811
|
this.type
|
|
794
812
|
).get(key);
|
|
795
813
|
results = cached_results?.value;
|
|
796
814
|
if (
|
|
797
815
|
cached_results?.value == undefined ||
|
|
798
|
-
this.shouldForceRefresh(cached_results)
|
|
816
|
+
(await this.shouldForceRefresh(cached_results))
|
|
799
817
|
) {
|
|
800
818
|
results = await this.database.search(
|
|
801
819
|
this.type,
|
|
@@ -925,7 +943,10 @@ export default class SpiceModel {
|
|
|
925
943
|
|
|
926
944
|
var returned_all = await Promise.allSettled(
|
|
927
945
|
_.map(classes, (obj) => {
|
|
928
|
-
return new obj(
|
|
946
|
+
return new obj({
|
|
947
|
+
...this[_args],
|
|
948
|
+
skip_cache: this[_skip_cache],
|
|
949
|
+
}).getMulti({
|
|
929
950
|
ids: ids,
|
|
930
951
|
});
|
|
931
952
|
})
|
|
@@ -980,7 +1001,10 @@ export default class SpiceModel {
|
|
|
980
1001
|
let classes = _.isArray(Class) ? Class : [Class];
|
|
981
1002
|
var returned_all = await Promise.allSettled(
|
|
982
1003
|
_.map(classes, (obj) => {
|
|
983
|
-
return new obj(
|
|
1004
|
+
return new obj({
|
|
1005
|
+
...this[_args],
|
|
1006
|
+
skip_cache: this[_skip_cache],
|
|
1007
|
+
}).getMulti({
|
|
984
1008
|
ids: ids,
|
|
985
1009
|
});
|
|
986
1010
|
})
|