spice-js 2.5.35 → 2.5.37

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/index.js CHANGED
@@ -142,7 +142,16 @@ class Spice {
142
142
  }));
143
143
  app.use(convert(Validate()));
144
144
  spice.app = app;
145
- spice.config = yield require("./loaders/config")(); //io.attach(app);
145
+ spice.mofifiers = {};
146
+ spice.config = yield require("./loaders/config")();
147
+
148
+ spice.addModifier = function (resource, modifier) {
149
+ spice.mofifiers[resource.toLowerCase()] = spice.mofifiers[resource] == undefined ? [modifier] : [...spice.mofifiers[resource], modifier];
150
+ };
151
+
152
+ spice.getModifiers = function (resource) {
153
+ return spice.mofifiers[resource.toLowerCase()] || [];
154
+ }; //io.attach(app);
146
155
  //spice.io = io; */
147
156
 
148
157
  /* io.on("message", (ctx, data) => {
@@ -153,6 +162,7 @@ class Spice {
153
162
  console.log("Connection Up", sock);
154
163
  }); */
155
164
 
165
+
156
166
  app.use((0, _koa2SwaggerUi.koaSwagger)({
157
167
  hideTopbar: true,
158
168
  title: "Spice JS",
@@ -28,7 +28,9 @@ var SDate = require("sonover-date"),
28
28
  _args = Symbol(),
29
29
  _hooks = Symbol(),
30
30
  _disable_lifecycle_events = Symbol(),
31
- _serializers = Symbol();
31
+ _external_modifier_loaded = Symbol(),
32
+ _serializers = Symbol(); //const _type = Symbol("type");
33
+
32
34
 
33
35
  var that;
34
36
 
@@ -56,7 +58,10 @@ class SpiceModel {
56
58
  var Database = require("spice-" + dbtype);
57
59
 
58
60
  this.type = "";
61
+ this.collection = args.connection; //console.log("Args -- Type::", args.type);
62
+
59
63
  this[_args] = args.args;
64
+ this[_external_modifier_loaded] = false;
60
65
  this[_disable_lifecycle_events] = ((_args$args = args.args) == null ? void 0 : _args$args.disable_lifecycle_events) || false;
61
66
  this[_ctx] = (_args2 = args) == null ? void 0 : (_args2$args = _args2.args) == null ? void 0 : _args2$args.ctx;
62
67
  this[_hooks] = {
@@ -174,6 +179,19 @@ class SpiceModel {
174
179
  } // }
175
180
 
176
181
  }
182
+ /* get type() {
183
+ return this._type;
184
+ }
185
+ // Setter for 'type'
186
+ set type(value) {
187
+ if (typeof value !== "string") {
188
+ // example validation
189
+ throw new Error("Type must be a string.");
190
+ }
191
+ this._type = value;
192
+ //console.log("Type Setted::", value);
193
+ } */
194
+
177
195
 
178
196
  get database() {
179
197
  return this[_database];
@@ -441,8 +459,12 @@ class SpiceModel {
441
459
  yield _this2.run_hook(args, "get", "before");
442
460
  var results = yield _this2.database.get(args.id);
443
461
 
444
- if (results.type != _this2.type) {
445
- throw new Error(_this2.type + " does not exist");
462
+ if (results.type != undefined) {
463
+ if (results.type != _this2.type) throw new Error(_this2.type + " does not exist type");
464
+ }
465
+
466
+ if (results._type != undefined) {
467
+ if (results._type != _this2.type) throw new Error(_this2.type + " does not exist _type");
446
468
  }
447
469
 
448
470
  if (results.deleted == undefined || results.deleted == false) {
@@ -510,12 +532,17 @@ class SpiceModel {
510
532
  try {
511
533
  if (_.isString(data)) {
512
534
  var result = yield _this5.database.get(data);
513
-
514
- if (result.type != _this5.type) {
535
+ if (result.type) if (result.type != _this5.type) {
536
+ return false;
537
+ }
538
+ if (result._type) if (result._type != _this5.type) {
515
539
  return false;
516
540
  }
517
541
  } else {
518
- if (data.type != _this5.type) {
542
+ if (data.type) if (data.type != _this5.type) {
543
+ return false;
544
+ }
545
+ if (data._type) if (data._type != _this5.type) {
519
546
  return false;
520
547
  }
521
548
  }
@@ -534,10 +561,12 @@ class SpiceModel {
534
561
  try {
535
562
  _this6.updated_at = new SDate().now();
536
563
  var results = yield _this6.database.get(args.id);
564
+ console.log("Results", results);
537
565
  var item_exist = yield _this6.exist(results);
566
+ console.log("Item Exist", item_exist);
538
567
 
539
568
  if (!item_exist) {
540
- throw new Error(_this6.type + " does not exist.");
569
+ throw new Error(_this6.type + " does not exist. in update");
541
570
  }
542
571
 
543
572
  delete results["id"];
@@ -560,8 +589,12 @@ class SpiceModel {
560
589
  form = yield _this6.do_serialize(_this6, "write", cover_obj.new, args);
561
590
  }
562
591
 
563
- yield _this6.database.update(args.id, form || _this6);
564
- console.log("Old Data Inside Update::::", results);
592
+ var db_data = form || _this6; //console.log("New Data Inside Update::::", db_data);
593
+
594
+ _this6.implementTypeFix(db_data); // console.log("After ITF::::", db_data);
595
+
596
+
597
+ yield _this6.database.update(args.id, db_data); //console.log("Old Data Inside Update::::", results);
565
598
 
566
599
  if (args.skip_hooks != true) {
567
600
  yield _this6.run_hook(_extends({}, _this6, {
@@ -584,6 +617,18 @@ class SpiceModel {
584
617
  })();
585
618
  }
586
619
 
620
+ implementTypeFix(item) {
621
+ /* console.log("Type Before Fix", item.type, item._type);
622
+ let _new_type = item.type || item._type;
623
+ //item.type = type;
624
+ console.log("New type", _new_type);
625
+ item = { ...item, type: _new_type };
626
+ delete item._type; */
627
+ //console.log("Type After Fix", item.type, item._type);
628
+ //if (item._type) delete item["_type"];
629
+ //console.log("Type After Fix", item.type);
630
+ }
631
+
587
632
  create(args) {
588
633
  var _this7 = this;
589
634
 
@@ -608,7 +653,7 @@ class SpiceModel {
608
653
  }
609
654
 
610
655
  yield _this7.run_hook(workingForm, "create", "before");
611
- workingForm = yield _this7.do_serialize(workingForm, "write", {}, args);
656
+ workingForm = _this7.implementTypeFix((yield _this7.do_serialize(workingForm, "write", {}, args)));
612
657
  var results = yield _this7.database.insert(id, workingForm, args.expiry);
613
658
  yield _this7.run_hook(_extends({}, results, {
614
659
  id
@@ -913,6 +958,19 @@ class SpiceModel {
913
958
 
914
959
  createModifiersFromProps() {
915
960
  this.createMofifier(this.props);
961
+ this.addExternalModifiers();
962
+ }
963
+
964
+ addExternalModifiers(scope) {
965
+ if (scope === void 0) {
966
+ scope = "*";
967
+ }
968
+
969
+ var that = this;
970
+
971
+ _.each([...spice.getModifiers(scope)], function (modifier) {
972
+ that.addModifier(modifier);
973
+ });
916
974
  }
917
975
 
918
976
  createMofifier(properties) {
@@ -987,6 +1045,7 @@ class SpiceModel {
987
1045
  var _this14 = this;
988
1046
 
989
1047
  return _asyncToGenerator(function* () {
1048
+ //console.log("CTX INside Model DO", this[_ctx]);
990
1049
  try {
991
1050
  // run serializers
992
1051
  if (!path_to_be_removed) {
@@ -994,9 +1053,15 @@ class SpiceModel {
994
1053
  }
995
1054
 
996
1055
  if (_this14.shouldSerializerRun(args, type)) {
1056
+ if (_this14.type != "" && _this14.type != undefined && _this14[_external_modifier_loaded] != true) {
1057
+ _this14.addExternalModifiers(_this14.type);
1058
+
1059
+ _this14[_external_modifier_loaded] = true;
1060
+ }
1061
+
997
1062
  for (var i of _this14[_serializers][type]["modifiers"]) {
998
1063
  try {
999
- data = yield i(data, old_data);
1064
+ data = yield i(data, old_data, _this14[_ctx], _this14.type);
1000
1065
  } catch (e) {
1001
1066
  console.log(e.stack);
1002
1067
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-js",
3
- "version": "2.5.35",
3
+ "version": "2.5.37",
4
4
  "description": "spice",
5
5
  "main": "build/index.js",
6
6
  "repository": {
package/src/index.js CHANGED
@@ -71,8 +71,19 @@ export default class Spice {
71
71
  );
72
72
  app.use(convert(Validate()));
73
73
  spice.app = app;
74
+ spice.mofifiers = {};
74
75
  spice.config = await require("./loaders/config")();
75
76
 
77
+ spice.addModifier = function (resource, modifier) {
78
+ spice.mofifiers[resource.toLowerCase()] =
79
+ spice.mofifiers[resource] == undefined
80
+ ? [modifier]
81
+ : [...spice.mofifiers[resource], modifier];
82
+ };
83
+
84
+ spice.getModifiers = function (resource) {
85
+ return spice.mofifiers[resource.toLowerCase()] || [];
86
+ };
76
87
  //io.attach(app);
77
88
  //spice.io = io; */
78
89
  /* io.on("message", (ctx, data) => {
@@ -14,7 +14,10 @@ var SDate = require("sonover-date"),
14
14
  _args = Symbol(),
15
15
  _hooks = Symbol(),
16
16
  _disable_lifecycle_events = Symbol(),
17
+ _external_modifier_loaded = Symbol(),
17
18
  _serializers = Symbol();
19
+
20
+ //const _type = Symbol("type");
18
21
  let that;
19
22
  if (!Promise.allSettled) {
20
23
  Promise.allSettled = (promises) =>
@@ -39,7 +42,10 @@ export default class SpiceModel {
39
42
  spice.config.database.connections[args.connection].type || "couchbase";
40
43
  let Database = require(`spice-${dbtype}`);
41
44
  this.type = "";
45
+ this.collection = args.connection;
46
+ //console.log("Args -- Type::", args.type);
42
47
  this[_args] = args.args;
48
+ this[_external_modifier_loaded] = false;
43
49
  this[_disable_lifecycle_events] =
44
50
  args.args?.disable_lifecycle_events || false;
45
51
  this[_ctx] = args?.args?.ctx;
@@ -154,6 +160,19 @@ export default class SpiceModel {
154
160
  }
155
161
  // }
156
162
  }
163
+ /* get type() {
164
+ return this._type;
165
+ }
166
+
167
+ // Setter for 'type'
168
+ set type(value) {
169
+ if (typeof value !== "string") {
170
+ // example validation
171
+ throw new Error("Type must be a string.");
172
+ }
173
+ this._type = value;
174
+ //console.log("Type Setted::", value);
175
+ } */
157
176
 
158
177
  get database() {
159
178
  return this[_database];
@@ -369,9 +388,16 @@ export default class SpiceModel {
369
388
  await this.run_hook(args, "get", "before");
370
389
  let results = await this.database.get(args.id);
371
390
 
372
- if (results.type != this.type) {
373
- throw new Error(`${this.type} does not exist`);
391
+ if (results.type != undefined) {
392
+ if (results.type != this.type)
393
+ throw new Error(`${this.type} does not exist type`);
394
+ }
395
+
396
+ if (results._type != undefined) {
397
+ if (results._type != this.type)
398
+ throw new Error(`${this.type} does not exist _type`);
374
399
  }
400
+
375
401
  if (results.deleted == undefined || results.deleted == false) {
376
402
  await this.run_hook(results, "get", "after");
377
403
  results = await this.do_serialize(
@@ -434,13 +460,24 @@ export default class SpiceModel {
434
460
  try {
435
461
  if (_.isString(data)) {
436
462
  let result = await this.database.get(data);
437
- if (result.type != this.type) {
438
- return false;
439
- }
463
+ if (result.type)
464
+ if (result.type != this.type) {
465
+ return false;
466
+ }
467
+ if (result._type)
468
+ if (result._type != this.type) {
469
+ return false;
470
+ }
440
471
  } else {
441
- if (data.type != this.type) {
442
- return false;
443
- }
472
+ if (data.type)
473
+ if (data.type != this.type) {
474
+ return false;
475
+ }
476
+
477
+ if (data._type)
478
+ if (data._type != this.type) {
479
+ return false;
480
+ }
444
481
  }
445
482
  return true;
446
483
  } catch (e) {
@@ -452,9 +489,11 @@ export default class SpiceModel {
452
489
  try {
453
490
  this.updated_at = new SDate().now();
454
491
  let results = await this.database.get(args.id);
492
+ console.log("Results", results);
455
493
  let item_exist = await this.exist(results);
494
+ console.log("Item Exist", item_exist);
456
495
  if (!item_exist) {
457
- throw new Error(`${this.type} does not exist.`);
496
+ throw new Error(`${this.type} does not exist. in update`);
458
497
  }
459
498
  delete results["id"];
460
499
  _.defaults(this, results);
@@ -471,8 +510,14 @@ export default class SpiceModel {
471
510
  if (args.skip_write_serialize != true && args.skip_serialize != true) {
472
511
  form = await this.do_serialize(this, "write", cover_obj.new, args);
473
512
  }
474
- await this.database.update(args.id, form || this);
475
- console.log("Old Data Inside Update::::", results);
513
+ let db_data = form || this;
514
+ //console.log("New Data Inside Update::::", db_data);
515
+
516
+ this.implementTypeFix(db_data);
517
+ // console.log("After ITF::::", db_data);
518
+
519
+ await this.database.update(args.id, db_data);
520
+ //console.log("Old Data Inside Update::::", results);
476
521
  if (args.skip_hooks != true) {
477
522
  await this.run_hook(
478
523
  {
@@ -501,6 +546,19 @@ export default class SpiceModel {
501
546
  }
502
547
  }
503
548
 
549
+ implementTypeFix(item) {
550
+ /* console.log("Type Before Fix", item.type, item._type);
551
+ let _new_type = item.type || item._type;
552
+ //item.type = type;
553
+ console.log("New type", _new_type);
554
+
555
+ item = { ...item, type: _new_type };
556
+ delete item._type; */
557
+ //console.log("Type After Fix", item.type, item._type);
558
+ //if (item._type) delete item["_type"];
559
+ //console.log("Type After Fix", item.type);
560
+ }
561
+
504
562
  async create(args) {
505
563
  try {
506
564
  let form;
@@ -519,7 +577,9 @@ export default class SpiceModel {
519
577
  id = args.id;
520
578
  }
521
579
  await this.run_hook(workingForm, "create", "before");
522
- workingForm = await this.do_serialize(workingForm, "write", {}, args);
580
+ workingForm = this.implementTypeFix(
581
+ await this.do_serialize(workingForm, "write", {}, args)
582
+ );
523
583
 
524
584
  let results = await this.database.insert(id, workingForm, args.expiry);
525
585
  await this.run_hook(
@@ -830,6 +890,14 @@ export default class SpiceModel {
830
890
 
831
891
  createModifiersFromProps() {
832
892
  this.createMofifier(this.props);
893
+ this.addExternalModifiers();
894
+ }
895
+
896
+ addExternalModifiers(scope = "*") {
897
+ let that = this;
898
+ _.each([...spice.getModifiers(scope)], function (modifier) {
899
+ that.addModifier(modifier);
900
+ });
833
901
  }
834
902
 
835
903
  createMofifier(properties) {
@@ -886,6 +954,7 @@ export default class SpiceModel {
886
954
  }
887
955
 
888
956
  async do_serialize(data, type, old_data, args, path_to_be_removed) {
957
+ //console.log("CTX INside Model DO", this[_ctx]);
889
958
  try {
890
959
  // run serializers
891
960
 
@@ -894,9 +963,17 @@ export default class SpiceModel {
894
963
  }
895
964
 
896
965
  if (this.shouldSerializerRun(args, type)) {
966
+ if (
967
+ this.type != "" &&
968
+ this.type != undefined &&
969
+ this[_external_modifier_loaded] != true
970
+ ) {
971
+ this.addExternalModifiers(this.type);
972
+ this[_external_modifier_loaded] = true;
973
+ }
897
974
  for (let i of this[_serializers][type]["modifiers"]) {
898
975
  try {
899
- data = await i(data, old_data);
976
+ data = await i(data, old_data, this[_ctx], this.type);
900
977
  } catch (e) {
901
978
  console.log(e.stack);
902
979
  }