spice-js 2.5.26 → 2.5.29
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/models/SpiceModel.js +107 -88
- package/package.json +1 -1
- package/src/models/SpiceModel.js +60 -15
|
@@ -201,6 +201,25 @@ class SpiceModel {
|
|
|
201
201
|
return item.length > 27 && item.indexOf("|") > -1;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
+
propsToBeRemoved(data) {
|
|
205
|
+
var _this = this;
|
|
206
|
+
|
|
207
|
+
return _asyncToGenerator(function* () {
|
|
208
|
+
if (!_.isArray(data)) {
|
|
209
|
+
data = Array.of(data);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
console.log("DAta", data);
|
|
213
|
+
|
|
214
|
+
if (_this[_ctx]) {
|
|
215
|
+
var returned = yield new _this[_ctx].state.process_fields().process(_this[_ctx], data, _this.type);
|
|
216
|
+
return returned;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return [];
|
|
220
|
+
})();
|
|
221
|
+
}
|
|
222
|
+
|
|
204
223
|
createValidationString(_ref) {
|
|
205
224
|
var {
|
|
206
225
|
property_name,
|
|
@@ -418,7 +437,7 @@ class SpiceModel {
|
|
|
418
437
|
}
|
|
419
438
|
|
|
420
439
|
get(args) {
|
|
421
|
-
var
|
|
440
|
+
var _this2 = this;
|
|
422
441
|
|
|
423
442
|
return _asyncToGenerator(function* () {
|
|
424
443
|
try {
|
|
@@ -427,19 +446,19 @@ class SpiceModel {
|
|
|
427
446
|
}
|
|
428
447
|
|
|
429
448
|
if (_.isString(args.id)) {
|
|
430
|
-
yield
|
|
431
|
-
var results = yield
|
|
449
|
+
yield _this2.run_hook(args, "get", "before");
|
|
450
|
+
var results = yield _this2.database.get(args.id);
|
|
432
451
|
|
|
433
|
-
if (results.type !=
|
|
434
|
-
throw new Error(
|
|
452
|
+
if (results.type != _this2.type) {
|
|
453
|
+
throw new Error(_this2.type + " does not exist");
|
|
435
454
|
}
|
|
436
455
|
|
|
437
456
|
if (results.deleted == undefined || results.deleted == false) {
|
|
438
|
-
yield
|
|
439
|
-
results = yield
|
|
457
|
+
yield _this2.run_hook(results, "get", "after");
|
|
458
|
+
results = yield _this2.do_serialize(results, "read", {}, args, (yield _this2.propsToBeRemoved(results)));
|
|
440
459
|
return results;
|
|
441
460
|
} else {
|
|
442
|
-
throw new Error(
|
|
461
|
+
throw new Error(_this2.type + " does not exist");
|
|
443
462
|
}
|
|
444
463
|
}
|
|
445
464
|
} catch (e) {
|
|
@@ -448,12 +467,12 @@ class SpiceModel {
|
|
|
448
467
|
})();
|
|
449
468
|
}
|
|
450
469
|
|
|
451
|
-
query(query) {
|
|
452
|
-
var
|
|
470
|
+
query(query, scope) {
|
|
471
|
+
var _this3 = this;
|
|
453
472
|
|
|
454
473
|
return _asyncToGenerator(function* () {
|
|
455
474
|
try {
|
|
456
|
-
var results = yield
|
|
475
|
+
var results = yield _this3.database.query(query, scope);
|
|
457
476
|
return results;
|
|
458
477
|
} catch (error) {
|
|
459
478
|
throw error;
|
|
@@ -462,7 +481,7 @@ class SpiceModel {
|
|
|
462
481
|
}
|
|
463
482
|
|
|
464
483
|
getMulti(args) {
|
|
465
|
-
var
|
|
484
|
+
var _this4 = this;
|
|
466
485
|
|
|
467
486
|
return _asyncToGenerator(function* () {
|
|
468
487
|
try {
|
|
@@ -470,20 +489,20 @@ class SpiceModel {
|
|
|
470
489
|
args = {};
|
|
471
490
|
}
|
|
472
491
|
|
|
473
|
-
yield
|
|
492
|
+
yield _this4.run_hook(_this4, "list", "before");
|
|
474
493
|
|
|
475
494
|
_.remove(args.ids, o => o == undefined);
|
|
476
495
|
|
|
477
496
|
var results = [];
|
|
478
497
|
|
|
479
498
|
if (args.ids.length > 0) {
|
|
480
|
-
results = yield
|
|
499
|
+
results = yield _this4.database.multi_get(args.ids, true);
|
|
481
500
|
}
|
|
482
501
|
|
|
483
|
-
_.remove(results, o => o.type !=
|
|
502
|
+
_.remove(results, o => o.type != _this4.type);
|
|
484
503
|
|
|
485
|
-
yield
|
|
486
|
-
results = yield
|
|
504
|
+
yield _this4.run_hook(results, "list", "after", args.context);
|
|
505
|
+
results = yield _this4.do_serialize(results, "read", {}, args, (yield _this4.propsToBeRemoved(results)));
|
|
487
506
|
return results;
|
|
488
507
|
} catch (e) {
|
|
489
508
|
console.log(e.stack);
|
|
@@ -493,18 +512,18 @@ class SpiceModel {
|
|
|
493
512
|
}
|
|
494
513
|
|
|
495
514
|
exist(data) {
|
|
496
|
-
var
|
|
515
|
+
var _this5 = this;
|
|
497
516
|
|
|
498
517
|
return _asyncToGenerator(function* () {
|
|
499
518
|
try {
|
|
500
519
|
if (_.isString(data)) {
|
|
501
|
-
var result = yield
|
|
520
|
+
var result = yield _this5.database.get(data);
|
|
502
521
|
|
|
503
|
-
if (result.type !=
|
|
522
|
+
if (result.type != _this5.type) {
|
|
504
523
|
return false;
|
|
505
524
|
}
|
|
506
525
|
} else {
|
|
507
|
-
if (data.type !=
|
|
526
|
+
if (data.type != _this5.type) {
|
|
508
527
|
return false;
|
|
509
528
|
}
|
|
510
529
|
}
|
|
@@ -517,51 +536,51 @@ class SpiceModel {
|
|
|
517
536
|
}
|
|
518
537
|
|
|
519
538
|
update(args) {
|
|
520
|
-
var
|
|
539
|
+
var _this6 = this;
|
|
521
540
|
|
|
522
541
|
return _asyncToGenerator(function* () {
|
|
523
542
|
try {
|
|
524
|
-
|
|
525
|
-
var results = yield
|
|
526
|
-
var item_exist = yield
|
|
543
|
+
_this6.updated_at = new SDate().now();
|
|
544
|
+
var results = yield _this6.database.get(args.id);
|
|
545
|
+
var item_exist = yield _this6.exist(results);
|
|
527
546
|
|
|
528
547
|
if (!item_exist) {
|
|
529
|
-
throw new Error(
|
|
548
|
+
throw new Error(_this6.type + " does not exist.");
|
|
530
549
|
}
|
|
531
550
|
|
|
532
551
|
delete results["id"];
|
|
533
552
|
|
|
534
|
-
_.defaults(
|
|
553
|
+
_.defaults(_this6, results);
|
|
535
554
|
|
|
536
555
|
var cover_obj = {
|
|
537
556
|
old: results,
|
|
538
|
-
new:
|
|
557
|
+
new: _this6,
|
|
539
558
|
id: args.id
|
|
540
559
|
};
|
|
541
560
|
|
|
542
561
|
if (args.skip_hooks != true) {
|
|
543
|
-
yield
|
|
562
|
+
yield _this6.run_hook(cover_obj, "update", "before");
|
|
544
563
|
}
|
|
545
564
|
|
|
546
565
|
var form;
|
|
547
566
|
|
|
548
567
|
if (args.skip_write_serialize != true && args.skip_serialize != true) {
|
|
549
|
-
form = yield
|
|
568
|
+
form = yield _this6.do_serialize(_this6, "write", cover_obj.new, args);
|
|
550
569
|
}
|
|
551
570
|
|
|
552
|
-
yield
|
|
571
|
+
yield _this6.database.update(args.id, form || _this6);
|
|
553
572
|
|
|
554
573
|
if (args.skip_hooks != true) {
|
|
555
|
-
yield
|
|
574
|
+
yield _this6.run_hook(_extends({}, _this6, {
|
|
556
575
|
id: args.id
|
|
557
576
|
}), "update", "after");
|
|
558
577
|
}
|
|
559
578
|
|
|
560
579
|
if (args.skip_read_serialize != true && args.skip_serialize != true) {
|
|
561
|
-
form = yield
|
|
580
|
+
form = yield _this6.do_serialize(form, "read", {}, args, (yield _this6.propsToBeRemoved(form)));
|
|
562
581
|
}
|
|
563
582
|
|
|
564
|
-
|
|
583
|
+
_this6.id = args.id;
|
|
565
584
|
return _extends({}, form, {
|
|
566
585
|
id: args.id
|
|
567
586
|
});
|
|
@@ -573,35 +592,35 @@ class SpiceModel {
|
|
|
573
592
|
}
|
|
574
593
|
|
|
575
594
|
create(args) {
|
|
576
|
-
var
|
|
595
|
+
var _this7 = this;
|
|
577
596
|
|
|
578
597
|
return _asyncToGenerator(function* () {
|
|
579
598
|
try {
|
|
580
599
|
var form;
|
|
581
|
-
|
|
600
|
+
_this7.created_at = new SDate().now();
|
|
582
601
|
|
|
583
602
|
if (args.body) {
|
|
584
603
|
form = _.defaults({}, args.body);
|
|
585
|
-
form.created_at =
|
|
586
|
-
form.updated_at =
|
|
604
|
+
form.created_at = _this7.created_at;
|
|
605
|
+
form.updated_at = _this7.created_at;
|
|
587
606
|
delete form["bucket"];
|
|
588
607
|
}
|
|
589
608
|
|
|
590
|
-
var workingForm = form ||
|
|
591
|
-
|
|
609
|
+
var workingForm = form || _this7;
|
|
610
|
+
_this7.updated_at = new SDate().now();
|
|
592
611
|
var id = args.id_prefix + "-" + UUID.v4();
|
|
593
612
|
|
|
594
613
|
if (args && args.id) {
|
|
595
614
|
id = args.id;
|
|
596
615
|
}
|
|
597
616
|
|
|
598
|
-
yield
|
|
599
|
-
workingForm = yield
|
|
600
|
-
var results = yield
|
|
601
|
-
yield
|
|
617
|
+
yield _this7.run_hook(workingForm, "create", "before");
|
|
618
|
+
workingForm = yield _this7.do_serialize(workingForm, "write", {}, args);
|
|
619
|
+
var results = yield _this7.database.insert(id, workingForm, args.expiry);
|
|
620
|
+
yield _this7.run_hook(_extends({}, results, {
|
|
602
621
|
id
|
|
603
622
|
}), "create", "after");
|
|
604
|
-
results = yield
|
|
623
|
+
results = yield _this7.do_serialize(results, "read", {}, args, (yield _this7.propsToBeRemoved(results)));
|
|
605
624
|
return _extends({}, results, {
|
|
606
625
|
id
|
|
607
626
|
});
|
|
@@ -613,29 +632,29 @@ class SpiceModel {
|
|
|
613
632
|
}
|
|
614
633
|
|
|
615
634
|
delete(args) {
|
|
616
|
-
var
|
|
635
|
+
var _this8 = this;
|
|
617
636
|
|
|
618
637
|
return _asyncToGenerator(function* () {
|
|
619
|
-
var item_exist = yield
|
|
638
|
+
var item_exist = yield _this8.exist(args.id);
|
|
620
639
|
|
|
621
640
|
if (!item_exist) {
|
|
622
|
-
throw new Error(
|
|
641
|
+
throw new Error(_this8.type + " does not exist.");
|
|
623
642
|
}
|
|
624
643
|
|
|
625
644
|
try {
|
|
626
|
-
yield
|
|
645
|
+
yield _this8.run_hook(args, "delete", "before");
|
|
627
646
|
var delete_response = {};
|
|
628
647
|
|
|
629
648
|
if (args.hard) {
|
|
630
|
-
delete_response = yield
|
|
649
|
+
delete_response = yield _this8.database.delete(args.id);
|
|
631
650
|
} else {
|
|
632
|
-
var results = yield
|
|
651
|
+
var results = yield _this8.database.get(args.id);
|
|
633
652
|
delete results["id"];
|
|
634
653
|
results.deleted = true;
|
|
635
|
-
delete_response = yield
|
|
654
|
+
delete_response = yield _this8.database.update(args.id, "");
|
|
636
655
|
}
|
|
637
656
|
|
|
638
|
-
yield
|
|
657
|
+
yield _this8.run_hook(args, "delete", "after");
|
|
639
658
|
return {};
|
|
640
659
|
} catch (e) {
|
|
641
660
|
console.log(e.stack);
|
|
@@ -649,7 +668,7 @@ class SpiceModel {
|
|
|
649
668
|
}
|
|
650
669
|
|
|
651
670
|
list(args) {
|
|
652
|
-
var
|
|
671
|
+
var _this9 = this;
|
|
653
672
|
|
|
654
673
|
return _asyncToGenerator(function* () {
|
|
655
674
|
try {
|
|
@@ -663,7 +682,7 @@ class SpiceModel {
|
|
|
663
682
|
query = args.query;
|
|
664
683
|
} else {
|
|
665
684
|
if (args.filters) {
|
|
666
|
-
query = yield
|
|
685
|
+
query = yield _this9.makeQueryFromFilter(args.filters);
|
|
667
686
|
} else {
|
|
668
687
|
if (args.query) {
|
|
669
688
|
query = args.query + " AND (deleted = false OR deleted IS MISSING) ";
|
|
@@ -685,22 +704,22 @@ class SpiceModel {
|
|
|
685
704
|
args.sort = "created_at DESC";
|
|
686
705
|
}
|
|
687
706
|
|
|
688
|
-
yield
|
|
707
|
+
yield _this9.run_hook(_this9, "list", "before");
|
|
689
708
|
var results;
|
|
690
709
|
|
|
691
710
|
if (args.is_custom_query && args.is_custom_query === "true") {
|
|
692
|
-
results = yield
|
|
711
|
+
results = yield _this9.database.query(query);
|
|
693
712
|
} else {
|
|
694
713
|
if (args.is_full_text && args.is_full_text === "true") {
|
|
695
|
-
results = yield
|
|
714
|
+
results = yield _this9.database.full_text_search(_this9.type, query || "", args.limit, args.offset);
|
|
696
715
|
} else {
|
|
697
|
-
results = yield
|
|
716
|
+
results = yield _this9.database.search(_this9.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent);
|
|
698
717
|
}
|
|
699
718
|
}
|
|
700
719
|
|
|
701
720
|
try {
|
|
702
|
-
yield
|
|
703
|
-
results.data = yield
|
|
721
|
+
yield _this9.run_hook(results.data, "list", "after");
|
|
722
|
+
results.data = yield _this9.do_serialize(results.data, "read", {}, args, (yield _this9.propsToBeRemoved(results.data)));
|
|
704
723
|
} catch (e) {
|
|
705
724
|
console.log(e);
|
|
706
725
|
}
|
|
@@ -724,26 +743,26 @@ class SpiceModel {
|
|
|
724
743
|
}
|
|
725
744
|
|
|
726
745
|
run_hook(data, op, when, old_data) {
|
|
727
|
-
var
|
|
746
|
+
var _this10 = this;
|
|
728
747
|
|
|
729
748
|
return _asyncToGenerator(function* () {
|
|
730
749
|
try {
|
|
731
|
-
if (
|
|
750
|
+
if (_this10[_disable_lifecycle_events] == false) {
|
|
732
751
|
var resourceLifecycleTriggered = new _ResourceLifecycleTriggered.default({
|
|
733
752
|
data: {
|
|
734
753
|
data,
|
|
735
754
|
operation: op,
|
|
736
755
|
when,
|
|
737
756
|
old_data,
|
|
738
|
-
resource:
|
|
739
|
-
ctx:
|
|
757
|
+
resource: _this10.type,
|
|
758
|
+
ctx: _this10[_ctx]
|
|
740
759
|
}
|
|
741
760
|
});
|
|
742
761
|
resourceLifecycleTriggered.dispatch();
|
|
743
762
|
}
|
|
744
763
|
|
|
745
|
-
if (
|
|
746
|
-
for (var i of
|
|
764
|
+
if (_this10[_hooks] && _this10[_hooks][op] && _this10[_hooks][op][when]) {
|
|
765
|
+
for (var i of _this10[_hooks][op][when]) {
|
|
747
766
|
data = yield i(data, old_data);
|
|
748
767
|
}
|
|
749
768
|
}
|
|
@@ -776,7 +795,7 @@ class SpiceModel {
|
|
|
776
795
|
}
|
|
777
796
|
|
|
778
797
|
mapToObject(data, Class, source_property, store_property, property) {
|
|
779
|
-
var
|
|
798
|
+
var _this11 = this;
|
|
780
799
|
|
|
781
800
|
return _asyncToGenerator(function* () {
|
|
782
801
|
var original_is_array = _.isArray(data);
|
|
@@ -795,7 +814,7 @@ class SpiceModel {
|
|
|
795
814
|
});
|
|
796
815
|
|
|
797
816
|
var returned_all = yield Promise.allSettled(_.map(classes, obj => {
|
|
798
|
-
return new obj(
|
|
817
|
+
return new obj(_this11[_args]).getMulti({
|
|
799
818
|
ids: ids
|
|
800
819
|
});
|
|
801
820
|
}));
|
|
@@ -813,7 +832,7 @@ class SpiceModel {
|
|
|
813
832
|
}
|
|
814
833
|
|
|
815
834
|
mapToObjectArray(data, Class, source_property, store_property, property) {
|
|
816
|
-
var
|
|
835
|
+
var _this12 = this;
|
|
817
836
|
|
|
818
837
|
return _asyncToGenerator(function* () {
|
|
819
838
|
var original_is_array = _.isArray(data);
|
|
@@ -842,7 +861,7 @@ class SpiceModel {
|
|
|
842
861
|
|
|
843
862
|
var classes = _.isArray(Class) ? Class : [Class];
|
|
844
863
|
var returned_all = yield Promise.allSettled(_.map(classes, obj => {
|
|
845
|
-
return new obj(
|
|
864
|
+
return new obj(_this12[_args]).getMulti({
|
|
846
865
|
ids: ids
|
|
847
866
|
});
|
|
848
867
|
}));
|
|
@@ -889,7 +908,7 @@ class SpiceModel {
|
|
|
889
908
|
}
|
|
890
909
|
|
|
891
910
|
createMofifier(properties) {
|
|
892
|
-
var
|
|
911
|
+
var _this13 = this;
|
|
893
912
|
|
|
894
913
|
var _loop = function _loop(i) {
|
|
895
914
|
if (properties[i].map) {
|
|
@@ -900,11 +919,11 @@ class SpiceModel {
|
|
|
900
919
|
case String:
|
|
901
920
|
case "string":
|
|
902
921
|
{
|
|
903
|
-
|
|
922
|
+
_this13.addModifier({
|
|
904
923
|
when: properties[i].map.when || "read",
|
|
905
924
|
execute: function () {
|
|
906
925
|
var _execute = _asyncToGenerator(function* (data) {
|
|
907
|
-
return yield
|
|
926
|
+
return yield _this13.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]);
|
|
908
927
|
});
|
|
909
928
|
|
|
910
929
|
function execute(_x) {
|
|
@@ -921,11 +940,11 @@ class SpiceModel {
|
|
|
921
940
|
case Array:
|
|
922
941
|
case "array":
|
|
923
942
|
{
|
|
924
|
-
|
|
943
|
+
_this13.addModifier({
|
|
925
944
|
when: properties[i].map.when || "read",
|
|
926
945
|
execute: function () {
|
|
927
946
|
var _execute2 = _asyncToGenerator(function* (data) {
|
|
928
|
-
return yield
|
|
947
|
+
return yield _this13.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]);
|
|
929
948
|
});
|
|
930
949
|
|
|
931
950
|
function execute(_x2) {
|
|
@@ -956,14 +975,14 @@ class SpiceModel {
|
|
|
956
975
|
}
|
|
957
976
|
}
|
|
958
977
|
|
|
959
|
-
do_serialize(data, type, old_data, args) {
|
|
960
|
-
var
|
|
978
|
+
do_serialize(data, type, old_data, args, path_to_be_removed) {
|
|
979
|
+
var _this14 = this;
|
|
961
980
|
|
|
962
981
|
return _asyncToGenerator(function* () {
|
|
963
982
|
try {
|
|
964
983
|
// run serializers
|
|
965
|
-
if (
|
|
966
|
-
for (var i of
|
|
984
|
+
if (_this14.shouldSerializerRun(args, type)) {
|
|
985
|
+
for (var i of _this14[_serializers][type]["modifiers"]) {
|
|
967
986
|
try {
|
|
968
987
|
data = yield i(data, old_data);
|
|
969
988
|
} catch (e) {
|
|
@@ -981,12 +1000,12 @@ class SpiceModel {
|
|
|
981
1000
|
|
|
982
1001
|
var defaults = {};
|
|
983
1002
|
|
|
984
|
-
for (var _i in
|
|
985
|
-
if (
|
|
986
|
-
defaults[_i] = _.isFunction(
|
|
1003
|
+
for (var _i in _this14.props) {
|
|
1004
|
+
if (_this14.props[_i].defaults != undefined && _this14.props[_i].defaults[type] != undefined && _this14.props[_i].defaults[type] != undefined) {
|
|
1005
|
+
defaults[_i] = _.isFunction(_this14.props[_i].defaults[type]) ? _this14.props[_i].defaults[type]({
|
|
987
1006
|
old_data: data,
|
|
988
1007
|
new_data: old_data
|
|
989
|
-
}) :
|
|
1008
|
+
}) : _this14.props[_i].defaults[type];
|
|
990
1009
|
}
|
|
991
1010
|
} // apply defaults
|
|
992
1011
|
|
|
@@ -997,10 +1016,10 @@ class SpiceModel {
|
|
|
997
1016
|
|
|
998
1017
|
|
|
999
1018
|
if (type == "read") {
|
|
1000
|
-
var props_to_clean = ["deleted", "type"];
|
|
1019
|
+
var props_to_clean = ["deleted", "type", ...path_to_be_removed];
|
|
1001
1020
|
|
|
1002
|
-
for (var _i3 in
|
|
1003
|
-
if (
|
|
1021
|
+
for (var _i3 in _this14.props) {
|
|
1022
|
+
if (_this14.props[_i3].hide) {
|
|
1004
1023
|
props_to_clean.push(_i3);
|
|
1005
1024
|
}
|
|
1006
1025
|
}
|
|
@@ -1008,7 +1027,7 @@ class SpiceModel {
|
|
|
1008
1027
|
data = _.map(data, obj => _.omit(obj, props_to_clean));
|
|
1009
1028
|
}
|
|
1010
1029
|
|
|
1011
|
-
return original_is_array ? data : data
|
|
1030
|
+
return original_is_array ? data : _.first(data);
|
|
1012
1031
|
}
|
|
1013
1032
|
} catch (e) {
|
|
1014
1033
|
console.log(e.stack);
|
package/package.json
CHANGED
package/src/models/SpiceModel.js
CHANGED
|
@@ -123,8 +123,8 @@ export default class SpiceModel {
|
|
|
123
123
|
this[i] = _.isBoolean(args.args[i])
|
|
124
124
|
? args.args[i]
|
|
125
125
|
: args.args[i] == "true" ||
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
args.args[i] == 1 ||
|
|
127
|
+
args.args[i] == "True";
|
|
128
128
|
break;
|
|
129
129
|
}
|
|
130
130
|
case Date:
|
|
@@ -180,6 +180,21 @@ export default class SpiceModel {
|
|
|
180
180
|
return item.length > 27 && item.indexOf("|") > -1;
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
+
async propsToBeRemoved(data) {
|
|
184
|
+
|
|
185
|
+
if (!_.isArray(data)) {
|
|
186
|
+
data = Array.of(data)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
console.log("DAta", data)
|
|
190
|
+
if (this[_ctx]) {
|
|
191
|
+
let returned = await new this[_ctx].state.process_fields().process(this[_ctx], data, this.type);
|
|
192
|
+
return returned;
|
|
193
|
+
|
|
194
|
+
}
|
|
195
|
+
return []
|
|
196
|
+
}
|
|
197
|
+
|
|
183
198
|
createValidationString({ property_name, props, operation, complete }) {
|
|
184
199
|
let operationExempt = {
|
|
185
200
|
PUT: ["required", "unique"],
|
|
@@ -364,7 +379,13 @@ export default class SpiceModel {
|
|
|
364
379
|
}
|
|
365
380
|
if (results.deleted == undefined || results.deleted == false) {
|
|
366
381
|
await this.run_hook(results, "get", "after");
|
|
367
|
-
results = await this.do_serialize(
|
|
382
|
+
results = await this.do_serialize(
|
|
383
|
+
results,
|
|
384
|
+
"read",
|
|
385
|
+
{},
|
|
386
|
+
args,
|
|
387
|
+
await this.propsToBeRemoved(results)
|
|
388
|
+
);
|
|
368
389
|
return results;
|
|
369
390
|
} else {
|
|
370
391
|
throw new Error(`${this.type} does not exist`);
|
|
@@ -375,9 +396,9 @@ export default class SpiceModel {
|
|
|
375
396
|
}
|
|
376
397
|
}
|
|
377
398
|
|
|
378
|
-
async query(query) {
|
|
399
|
+
async query(query, scope) {
|
|
379
400
|
try {
|
|
380
|
-
let results = await this.database.query(query);
|
|
401
|
+
let results = await this.database.query(query, scope);
|
|
381
402
|
return results;
|
|
382
403
|
} catch (error) {
|
|
383
404
|
throw error;
|
|
@@ -399,7 +420,13 @@ export default class SpiceModel {
|
|
|
399
420
|
_.remove(results, (o) => o.type != this.type);
|
|
400
421
|
|
|
401
422
|
await this.run_hook(results, "list", "after", args.context);
|
|
402
|
-
results = await this.do_serialize(
|
|
423
|
+
results = await this.do_serialize(
|
|
424
|
+
results,
|
|
425
|
+
"read",
|
|
426
|
+
{},
|
|
427
|
+
args,
|
|
428
|
+
await this.propsToBeRemoved(results)
|
|
429
|
+
);
|
|
403
430
|
|
|
404
431
|
return results;
|
|
405
432
|
} catch (e) {
|
|
@@ -461,7 +488,13 @@ export default class SpiceModel {
|
|
|
461
488
|
);
|
|
462
489
|
}
|
|
463
490
|
if (args.skip_read_serialize != true && args.skip_serialize != true) {
|
|
464
|
-
form = await this.do_serialize(
|
|
491
|
+
form = await this.do_serialize(
|
|
492
|
+
form,
|
|
493
|
+
"read",
|
|
494
|
+
{},
|
|
495
|
+
args,
|
|
496
|
+
await this.propsToBeRemoved(form)
|
|
497
|
+
);
|
|
465
498
|
}
|
|
466
499
|
this.id = args.id;
|
|
467
500
|
return { ...form, id: args.id };
|
|
@@ -500,7 +533,13 @@ export default class SpiceModel {
|
|
|
500
533
|
"create",
|
|
501
534
|
"after"
|
|
502
535
|
);
|
|
503
|
-
results = await this.do_serialize(
|
|
536
|
+
results = await this.do_serialize(
|
|
537
|
+
results,
|
|
538
|
+
"read",
|
|
539
|
+
{},
|
|
540
|
+
args,
|
|
541
|
+
await this.propsToBeRemoved(results)
|
|
542
|
+
);
|
|
504
543
|
return { ...results, id };
|
|
505
544
|
} catch (e) {
|
|
506
545
|
console.log(e.stack);
|
|
@@ -601,7 +640,13 @@ export default class SpiceModel {
|
|
|
601
640
|
}
|
|
602
641
|
try {
|
|
603
642
|
await this.run_hook(results.data, "list", "after");
|
|
604
|
-
results.data = await this.do_serialize(
|
|
643
|
+
results.data = await this.do_serialize(
|
|
644
|
+
results.data,
|
|
645
|
+
"read",
|
|
646
|
+
{},
|
|
647
|
+
args,
|
|
648
|
+
await this.propsToBeRemoved(results.data)
|
|
649
|
+
);
|
|
605
650
|
} catch (e) {
|
|
606
651
|
console.log(e);
|
|
607
652
|
}
|
|
@@ -830,7 +875,7 @@ export default class SpiceModel {
|
|
|
830
875
|
}
|
|
831
876
|
}
|
|
832
877
|
|
|
833
|
-
async do_serialize(data, type, old_data, args) {
|
|
878
|
+
async do_serialize(data, type, old_data, args, path_to_be_removed) {
|
|
834
879
|
try {
|
|
835
880
|
// run serializers
|
|
836
881
|
|
|
@@ -860,9 +905,9 @@ export default class SpiceModel {
|
|
|
860
905
|
) {
|
|
861
906
|
defaults[i] = _.isFunction(this.props[i].defaults[type])
|
|
862
907
|
? this.props[i].defaults[type]({
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
908
|
+
old_data: data,
|
|
909
|
+
new_data: old_data,
|
|
910
|
+
})
|
|
866
911
|
: this.props[i].defaults[type];
|
|
867
912
|
}
|
|
868
913
|
}
|
|
@@ -873,7 +918,7 @@ export default class SpiceModel {
|
|
|
873
918
|
|
|
874
919
|
// apply cleaners
|
|
875
920
|
if (type == "read") {
|
|
876
|
-
let props_to_clean = ["deleted", "type"];
|
|
921
|
+
let props_to_clean = ["deleted", "type", ...path_to_be_removed];
|
|
877
922
|
for (let i in this.props) {
|
|
878
923
|
if (this.props[i].hide) {
|
|
879
924
|
props_to_clean.push(i);
|
|
@@ -882,7 +927,7 @@ export default class SpiceModel {
|
|
|
882
927
|
data = _.map(data, (obj) => _.omit(obj, props_to_clean));
|
|
883
928
|
}
|
|
884
929
|
|
|
885
|
-
return original_is_array ? data : data
|
|
930
|
+
return original_is_array ? data : _.first(data);
|
|
886
931
|
}
|
|
887
932
|
} catch (e) {
|
|
888
933
|
console.log(e.stack);
|