spice-js 2.5.24 → 2.5.27

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.
@@ -23,8 +23,11 @@ var SDate = require("sonover-date"),
23
23
  UUID = require("uuid"),
24
24
  _ = require("lodash"),
25
25
  _database = Symbol(),
26
+ _ctx = Symbol(),
26
27
  _props = Symbol(),
28
+ _args = Symbol(),
27
29
  _hooks = Symbol(),
30
+ _disable_lifecycle_events = Symbol(),
28
31
  _serializers = Symbol();
29
32
 
30
33
  var that;
@@ -46,11 +49,16 @@ class SpiceModel {
46
49
  }
47
50
 
48
51
  try {
52
+ var _args$args, _args2, _args2$args;
53
+
49
54
  var dbtype = spice.config.database.connections[args.connection].type || "couchbase";
50
55
 
51
56
  var Database = require("spice-" + dbtype);
52
57
 
53
58
  this.type = "";
59
+ this[_args] = args.args;
60
+ this[_disable_lifecycle_events] = ((_args$args = args.args) == null ? void 0 : _args$args.disable_lifecycle_events) || false;
61
+ this[_ctx] = (_args2 = args) == null ? void 0 : (_args2$args = _args2.args) == null ? void 0 : _args2$args.ctx;
54
62
  this[_hooks] = {
55
63
  create: {
56
64
  before: [],
@@ -84,10 +92,11 @@ class SpiceModel {
84
92
  }
85
93
  };
86
94
  this.deleted = false;
87
-
95
+ /*
88
96
  if (args.args) {
89
97
  delete args.args["ctx"];
90
98
  }
99
+ */
91
100
 
92
101
  this[_database] = new Database(args.connection || "default", {
93
102
  collection: args.collection,
@@ -719,16 +728,19 @@ class SpiceModel {
719
728
 
720
729
  return _asyncToGenerator(function* () {
721
730
  try {
722
- var resourceLifecycleTriggered = new _ResourceLifecycleTriggered.default({
723
- data: {
724
- data,
725
- operation: op,
726
- when,
727
- old_data,
728
- resource: _this9.type
729
- }
730
- });
731
- resourceLifecycleTriggered.dispatch();
731
+ if (_this9[_disable_lifecycle_events] == false) {
732
+ var resourceLifecycleTriggered = new _ResourceLifecycleTriggered.default({
733
+ data: {
734
+ data,
735
+ operation: op,
736
+ when,
737
+ old_data,
738
+ resource: _this9.type,
739
+ ctx: _this9[_ctx]
740
+ }
741
+ });
742
+ resourceLifecycleTriggered.dispatch();
743
+ }
732
744
 
733
745
  if (_this9[_hooks] && _this9[_hooks][op] && _this9[_hooks][op][when]) {
734
746
  for (var i of _this9[_hooks][op][when]) {
@@ -764,6 +776,8 @@ class SpiceModel {
764
776
  }
765
777
 
766
778
  mapToObject(data, Class, source_property, store_property, property) {
779
+ var _this10 = this;
780
+
767
781
  return _asyncToGenerator(function* () {
768
782
  var original_is_array = _.isArray(data);
769
783
 
@@ -781,7 +795,7 @@ class SpiceModel {
781
795
  });
782
796
 
783
797
  var returned_all = yield Promise.allSettled(_.map(classes, obj => {
784
- return new obj().getMulti({
798
+ return new obj(_this10[_args]).getMulti({
785
799
  ids: ids
786
800
  });
787
801
  }));
@@ -799,6 +813,8 @@ class SpiceModel {
799
813
  }
800
814
 
801
815
  mapToObjectArray(data, Class, source_property, store_property, property) {
816
+ var _this11 = this;
817
+
802
818
  return _asyncToGenerator(function* () {
803
819
  var original_is_array = _.isArray(data);
804
820
 
@@ -826,7 +842,7 @@ class SpiceModel {
826
842
 
827
843
  var classes = _.isArray(Class) ? Class : [Class];
828
844
  var returned_all = yield Promise.allSettled(_.map(classes, obj => {
829
- return new obj().getMulti({
845
+ return new obj(_this11[_args]).getMulti({
830
846
  ids: ids
831
847
  });
832
848
  }));
@@ -873,7 +889,7 @@ class SpiceModel {
873
889
  }
874
890
 
875
891
  createMofifier(properties) {
876
- var _this10 = this;
892
+ var _this12 = this;
877
893
 
878
894
  var _loop = function _loop(i) {
879
895
  if (properties[i].map) {
@@ -884,11 +900,11 @@ class SpiceModel {
884
900
  case String:
885
901
  case "string":
886
902
  {
887
- _this10.addModifier({
903
+ _this12.addModifier({
888
904
  when: properties[i].map.when || "read",
889
905
  execute: function () {
890
906
  var _execute = _asyncToGenerator(function* (data) {
891
- return yield _this10.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]);
907
+ return yield _this12.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]);
892
908
  });
893
909
 
894
910
  function execute(_x) {
@@ -905,11 +921,11 @@ class SpiceModel {
905
921
  case Array:
906
922
  case "array":
907
923
  {
908
- _this10.addModifier({
924
+ _this12.addModifier({
909
925
  when: properties[i].map.when || "read",
910
926
  execute: function () {
911
927
  var _execute2 = _asyncToGenerator(function* (data) {
912
- return yield _this10.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]);
928
+ return yield _this12.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]);
913
929
  });
914
930
 
915
931
  function execute(_x2) {
@@ -941,13 +957,13 @@ class SpiceModel {
941
957
  }
942
958
 
943
959
  do_serialize(data, type, old_data, args) {
944
- var _this11 = this;
960
+ var _this13 = this;
945
961
 
946
962
  return _asyncToGenerator(function* () {
947
963
  try {
948
964
  // run serializers
949
- if (_this11.shouldSerializerRun(args, type)) {
950
- for (var i of _this11[_serializers][type]["modifiers"]) {
965
+ if (_this13.shouldSerializerRun(args, type)) {
966
+ for (var i of _this13[_serializers][type]["modifiers"]) {
951
967
  try {
952
968
  data = yield i(data, old_data);
953
969
  } catch (e) {
@@ -965,12 +981,12 @@ class SpiceModel {
965
981
 
966
982
  var defaults = {};
967
983
 
968
- for (var _i in _this11.props) {
969
- if (_this11.props[_i].defaults != undefined && _this11.props[_i].defaults[type] != undefined && _this11.props[_i].defaults[type] != undefined) {
970
- defaults[_i] = _.isFunction(_this11.props[_i].defaults[type]) ? _this11.props[_i].defaults[type]({
984
+ for (var _i in _this13.props) {
985
+ if (_this13.props[_i].defaults != undefined && _this13.props[_i].defaults[type] != undefined && _this13.props[_i].defaults[type] != undefined) {
986
+ defaults[_i] = _.isFunction(_this13.props[_i].defaults[type]) ? _this13.props[_i].defaults[type]({
971
987
  old_data: data,
972
988
  new_data: old_data
973
- }) : _this11.props[_i].defaults[type];
989
+ }) : _this13.props[_i].defaults[type];
974
990
  }
975
991
  } // apply defaults
976
992
 
@@ -983,8 +999,8 @@ class SpiceModel {
983
999
  if (type == "read") {
984
1000
  var props_to_clean = ["deleted", "type"];
985
1001
 
986
- for (var _i3 in _this11.props) {
987
- if (_this11.props[_i3].hide) {
1002
+ for (var _i3 in _this13.props) {
1003
+ if (_this13.props[_i3].hide) {
988
1004
  props_to_clean.push(_i3);
989
1005
  }
990
1006
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-js",
3
- "version": "2.5.24",
3
+ "version": "2.5.27",
4
4
  "description": "spice",
5
5
  "main": "build/index.js",
6
6
  "repository": {
@@ -9,8 +9,11 @@ var SDate = require("sonover-date"),
9
9
  UUID = require("uuid"),
10
10
  _ = require("lodash"),
11
11
  _database = Symbol(),
12
+ _ctx = Symbol(),
12
13
  _props = Symbol(),
14
+ _args = Symbol(),
13
15
  _hooks = Symbol(),
16
+ _disable_lifecycle_events = Symbol(),
14
17
  _serializers = Symbol();
15
18
  let that;
16
19
  if (!Promise.allSettled) {
@@ -36,7 +39,10 @@ export default class SpiceModel {
36
39
  spice.config.database.connections[args.connection].type || "couchbase";
37
40
  let Database = require(`spice-${dbtype}`);
38
41
  this.type = "";
39
-
42
+ this[_args] = args.args;
43
+ this[_disable_lifecycle_events] =
44
+ args.args?.disable_lifecycle_events || false;
45
+ this[_ctx] = args?.args?.ctx;
40
46
  this[_hooks] = {
41
47
  create: {
42
48
  before: [],
@@ -71,10 +77,12 @@ export default class SpiceModel {
71
77
  };
72
78
 
73
79
  this.deleted = false;
80
+
81
+ /*
74
82
  if (args.args) {
75
83
  delete args.args["ctx"];
76
84
  }
77
-
85
+ */
78
86
  this[_database] = new Database(args.connection || "default", {
79
87
  collection: args.collection,
80
88
  scope: args.scope,
@@ -367,9 +375,9 @@ export default class SpiceModel {
367
375
  }
368
376
  }
369
377
 
370
- async query(query) {
378
+ async query(query, scope) {
371
379
  try {
372
- let results = await this.database.query(query);
380
+ let results = await this.database.query(query, scope);
373
381
  return results;
374
382
  } catch (error) {
375
383
  throw error;
@@ -611,10 +619,19 @@ export default class SpiceModel {
611
619
 
612
620
  async run_hook(data, op, when, old_data) {
613
621
  try {
614
- let resourceLifecycleTriggered = new ResourceLifecycleTriggered({
615
- data: { data, operation: op, when, old_data, resource: this.type },
616
- });
617
- resourceLifecycleTriggered.dispatch();
622
+ if (this[_disable_lifecycle_events] == false) {
623
+ let resourceLifecycleTriggered = new ResourceLifecycleTriggered({
624
+ data: {
625
+ data,
626
+ operation: op,
627
+ when,
628
+ old_data,
629
+ resource: this.type,
630
+ ctx: this[_ctx],
631
+ },
632
+ });
633
+ resourceLifecycleTriggered.dispatch();
634
+ }
618
635
  if (this[_hooks] && this[_hooks][op] && this[_hooks][op][when]) {
619
636
  for (let i of this[_hooks][op][when]) {
620
637
  data = await i(data, old_data);
@@ -661,7 +678,7 @@ export default class SpiceModel {
661
678
 
662
679
  var returned_all = await Promise.allSettled(
663
680
  _.map(classes, (obj) => {
664
- return new obj().getMulti({
681
+ return new obj(this[_args]).getMulti({
665
682
  ids: ids,
666
683
  });
667
684
  })
@@ -712,7 +729,7 @@ export default class SpiceModel {
712
729
  let classes = _.isArray(Class) ? Class : [Class];
713
730
  var returned_all = await Promise.allSettled(
714
731
  _.map(classes, (obj) => {
715
- return new obj().getMulti({
732
+ return new obj(this[_args]).getMulti({
716
733
  ids: ids,
717
734
  });
718
735
  })