spice-js 2.6.57 → 2.6.58

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.
@@ -12,7 +12,8 @@ var crypto = require("crypto");
12
12
 
13
13
  function generateCacheKey(input) {
14
14
  // Create an MD5 hash of the input string and return it in hexadecimal format
15
- return crypto.createHash("md5").update(input).digest("hex");
15
+ //return crypto.createHash("md5").update(input).digest("hex");
16
+ return input;
16
17
  }
17
18
 
18
19
  module.exports = class SpiceCache {
@@ -53,8 +54,9 @@ module.exports = class SpiceCache {
53
54
  namespace = ""
54
55
  } = working_options;
55
56
  var workingKey = namespace + key;
57
+ var cacheKey = generateCacheKey(workingKey);
56
58
 
57
- _this2.client.set(generateCacheKey(workingKey), value, ttl == undefined ? 60 : ttl);
59
+ _this2.client.set(cacheKey, value, ttl === undefined ? 60 : ttl);
58
60
  })();
59
61
  }
60
62
 
@@ -79,11 +81,19 @@ module.exports = class SpiceCache {
79
81
  })();
80
82
  }
81
83
 
82
- exists(key) {
84
+ exists(key, options) {
83
85
  var _this4 = this;
84
86
 
85
87
  return _asyncToGenerator(function* () {
86
- var cacheKey = generateCacheKey(key);
88
+ if (options === void 0) {
89
+ options = {};
90
+ }
91
+
92
+ var {
93
+ namespace = ""
94
+ } = options;
95
+ var workingKey = namespace + key;
96
+ var cacheKey = generateCacheKey(workingKey);
87
97
  return yield _this4.client.has(cacheKey);
88
98
  })();
89
99
  }
@@ -573,7 +573,7 @@ class SpiceModel {
573
573
  }
574
574
 
575
575
  if (_.isString(args.id)) {
576
- if (args.skip_hooks != true) {
576
+ if (args.skip_hooks !== true) {
577
577
  yield _this4.run_hook(args, "get", "before");
578
578
  }
579
579
 
@@ -581,34 +581,43 @@ class SpiceModel {
581
581
  var results = {};
582
582
 
583
583
  if (_this4.shouldUseCache(_this4.type)) {
584
- var cached_results = yield _this4.getCacheProviderObject(_this4.type).get(key);
585
- results = cached_results == null ? void 0 : cached_results.value;
584
+ // Retrieve the cached results
585
+ var cached_results = yield _this4.getCacheProviderObject(_this4.type).get(key); // Check if the cache is empty
586
+
587
+ var isCacheEmpty = (cached_results == null ? void 0 : cached_results.value) === undefined; // Check if the cached result should be refreshed
586
588
 
587
- if ((cached_results == null ? void 0 : cached_results.value) === undefined || (yield _this4.shouldForceRefresh(cached_results)) || _this4.type == "workflow") {
589
+ var shouldRefresh = yield _this4.shouldForceRefresh(cached_results); // Force a refresh for "workflow" type if needed
590
+
591
+ if (isCacheEmpty || shouldRefresh) {
592
+ // Retrieve from the database and update cache
588
593
  results = yield _this4.database.get(args.id);
589
594
  yield _this4.getCacheProviderObject(_this4.type).set(key, {
590
595
  value: results,
591
596
  time: new Date().getTime()
592
597
  }, _this4.getCacheConfig(_this4.type));
598
+ } else {
599
+ // Use the cached value
600
+ results = cached_results.value;
593
601
  }
594
602
  } else {
603
+ // Directly fetch from the database if caching is disabled
595
604
  results = yield _this4.database.get(args.id);
596
605
  }
597
606
 
598
- if (results.type != undefined) {
599
- if (results.type != _this4.type) throw new Error(_this4.type + " does not exist type");
607
+ if (results.type !== undefined && results.type !== _this4.type) {
608
+ throw new Error(_this4.type + " does not exist type");
600
609
  }
601
610
 
602
- if (results._type != undefined) {
603
- if (results._type != _this4.type) throw new Error(_this4.type + " does not exist _type");
611
+ if (results._type !== undefined && results._type !== _this4.type) {
612
+ throw new Error(_this4.type + " does not exist _type");
604
613
  }
605
614
 
606
- if (results.deleted == undefined || results.deleted == false) {
607
- if (args.skip_read_serialize != true && args.skip_serialize != true) {
615
+ if (results.deleted === undefined || results.deleted === false) {
616
+ if (args.skip_read_serialize !== true && args.skip_serialize !== true) {
608
617
  results = yield _this4.do_serialize(results, "read", {}, args, (yield _this4.propsToBeRemoved(results)));
609
618
  }
610
619
 
611
- if (args.skip_hooks != true) {
620
+ if (args.skip_hooks !== true) {
612
621
  yield _this4.run_hook(results, "get", "after");
613
622
  }
614
623
 
@@ -995,10 +1004,10 @@ class SpiceModel {
995
1004
  parts[0] = "`" + parts[0] + "`";
996
1005
  return parts.join(" ");
997
1006
  }
998
-
999
- removeSpaceAndSpecialCharacters(str) {
1007
+ /* removeSpaceAndSpecialCharacters(str) {
1000
1008
  return str.replace(/[^a-zA-Z0-9]/g, "");
1001
- }
1009
+ } */
1010
+
1002
1011
 
1003
1012
  fixColumnName(columns) {
1004
1013
  // Guard clause: if columns is not provided or not a string, return it as-is.
@@ -1115,24 +1124,25 @@ class SpiceModel {
1115
1124
  yield _this12.run_hook(_this12, "list", "before");
1116
1125
  }
1117
1126
 
1118
- var cacheKey = _this12.removeSpaceAndSpecialCharacters("list::" + _this12.type + "::" + args._join + "::" + query + "::" + args.limit + "::" + args.offset + "::" + args.sort + "::" + args.do_count + "::" + args.statement_consistent + "::" + args.columns + "::" + args.is_full_text + "::" + args.is_custom_query);
1119
-
1127
+ var cacheKey = "list::" + _this12.type + "::" + args._join + "::" + query + "::" + args.limit + "::" + args.offset + "::" + args.sort + "::" + args.do_count + "::" + args.statement_consistent + "::" + args.columns + "::" + args.is_full_text + "::" + args.is_custom_query;
1120
1128
  var results;
1121
1129
 
1122
1130
  if (_this12.shouldUseCache(_this12.type)) {
1123
1131
  var cachedResults = yield _this12.getCacheProviderObject(_this12.type).get(cacheKey);
1124
- results = cachedResults == null ? void 0 : cachedResults.value;
1132
+ results = cachedResults == null ? void 0 : cachedResults.value; // Check if there is no cached value
1125
1133
 
1126
- if ((cachedResults == null ? void 0 : cachedResults.value) === undefined) {
1134
+ var isCacheEmpty = (cachedResults == null ? void 0 : cachedResults.value) === undefined; // Check if the cached value should be refreshed (e.g., due to the monitor's timestamp)
1135
+
1136
+ var shouldRefresh = yield _this12.shouldForceRefresh(cachedResults); // Always force a refresh for "workflow" type
1137
+
1138
+ if (isCacheEmpty || shouldRefresh) {
1139
+ // Force a database read and cache update
1127
1140
  results = yield _this12.fetchResults(args, query);
1128
- } else {
1129
- if (!results || (yield _this12.shouldForceRefresh(cachedResults))) {
1130
- results = yield _this12.fetchResults(args, query);
1131
- yield _this12.getCacheProviderObject(_this12.type).set(cacheKey, {
1132
- value: results,
1133
- time: new Date().getTime()
1134
- }, _this12.getCacheConfig(_this12.type));
1135
- }
1141
+
1142
+ _this12.getCacheProviderObject(_this12.type).set(cacheKey, {
1143
+ value: results,
1144
+ time: new Date().getTime()
1145
+ }, _this12.getCacheConfig(_this12.type));
1136
1146
  }
1137
1147
  } else {
1138
1148
  results = yield _this12.fetchResults(args, query);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-js",
3
- "version": "2.6.57",
3
+ "version": "2.6.58",
4
4
  "description": "spice",
5
5
  "main": "build/index.js",
6
6
  "repository": {
@@ -3,7 +3,8 @@ const crypto = require("crypto");
3
3
 
4
4
  function generateCacheKey(input) {
5
5
  // Create an MD5 hash of the input string and return it in hexadecimal format
6
- return crypto.createHash("md5").update(input).digest("hex");
6
+ //return crypto.createHash("md5").update(input).digest("hex");
7
+ return input;
7
8
  }
8
9
 
9
10
  module.exports = class SpiceCache {
@@ -20,11 +21,8 @@ module.exports = class SpiceCache {
20
21
  let working_options = { ...this.options, ...options };
21
22
  const { ttl, namespace = "" } = working_options;
22
23
  const workingKey = namespace + key;
23
- this.client.set(
24
- generateCacheKey(workingKey),
25
- value,
26
- ttl == undefined ? 60 : ttl
27
- );
24
+ const cacheKey = generateCacheKey(workingKey);
25
+ this.client.set(cacheKey, value, ttl === undefined ? 60 : ttl);
28
26
  }
29
27
 
30
28
  async get(key, options = {}) {
@@ -38,8 +36,10 @@ module.exports = class SpiceCache {
38
36
  }
39
37
  }
40
38
 
41
- async exists(key) {
42
- const cacheKey = generateCacheKey(key);
39
+ async exists(key, options = {}) {
40
+ const { namespace = "" } = options;
41
+ const workingKey = namespace + key;
42
+ const cacheKey = generateCacheKey(workingKey);
43
43
  return await this.client.has(cacheKey);
44
44
  }
45
45
  };
@@ -489,45 +489,51 @@ export default class SpiceModel {
489
489
  args = {};
490
490
  }
491
491
  if (_.isString(args.id)) {
492
- if (args.skip_hooks != true) {
492
+ if (args.skip_hooks !== true) {
493
493
  await this.run_hook(args, "get", "before");
494
494
  }
495
495
  let key = `get::${this.type}::${args.id}`;
496
496
  let results = {};
497
- if (this.shouldUseCache(this.type)) {
498
- let cached_results = await this.getCacheProviderObject(this.type).get(
499
- key
500
- );
501
497
 
502
- results = cached_results?.value;
503
- if (
504
- cached_results?.value === undefined ||
505
- (await this.shouldForceRefresh(cached_results)) ||
506
- this.type == "workflow"
507
- ) {
498
+ if (this.shouldUseCache(this.type)) {
499
+ // Retrieve the cached results
500
+ const cached_results = await this.getCacheProviderObject(
501
+ this.type
502
+ ).get(key);
503
+ // Check if the cache is empty
504
+ const isCacheEmpty = cached_results?.value === undefined;
505
+ // Check if the cached result should be refreshed
506
+ const shouldRefresh = await this.shouldForceRefresh(cached_results);
507
+ // Force a refresh for "workflow" type if needed
508
+
509
+ if (isCacheEmpty || shouldRefresh) {
510
+ // Retrieve from the database and update cache
508
511
  results = await this.database.get(args.id);
509
512
  await this.getCacheProviderObject(this.type).set(
510
513
  key,
511
514
  { value: results, time: new Date().getTime() },
512
515
  this.getCacheConfig(this.type)
513
516
  );
517
+ } else {
518
+ // Use the cached value
519
+ results = cached_results.value;
514
520
  }
515
521
  } else {
522
+ // Directly fetch from the database if caching is disabled
516
523
  results = await this.database.get(args.id);
517
524
  }
518
525
 
519
- if (results.type != undefined) {
520
- if (results.type != this.type)
521
- throw new Error(`${this.type} does not exist type`);
526
+ if (results.type !== undefined && results.type !== this.type) {
527
+ throw new Error(`${this.type} does not exist type`);
522
528
  }
523
-
524
- if (results._type != undefined) {
525
- if (results._type != this.type)
526
- throw new Error(`${this.type} does not exist _type`);
529
+ if (results._type !== undefined && results._type !== this.type) {
530
+ throw new Error(`${this.type} does not exist _type`);
527
531
  }
528
-
529
- if (results.deleted == undefined || results.deleted == false) {
530
- if (args.skip_read_serialize != true && args.skip_serialize != true) {
532
+ if (results.deleted === undefined || results.deleted === false) {
533
+ if (
534
+ args.skip_read_serialize !== true &&
535
+ args.skip_serialize !== true
536
+ ) {
531
537
  results = await this.do_serialize(
532
538
  results,
533
539
  "read",
@@ -536,7 +542,7 @@ export default class SpiceModel {
536
542
  await this.propsToBeRemoved(results)
537
543
  );
538
544
  }
539
- if (args.skip_hooks != true) {
545
+ if (args.skip_hooks !== true) {
540
546
  await this.run_hook(results, "get", "after");
541
547
  }
542
548
  return results;
@@ -918,9 +924,9 @@ export default class SpiceModel {
918
924
  return parts.join(" ");
919
925
  }
920
926
 
921
- removeSpaceAndSpecialCharacters(str) {
927
+ /* removeSpaceAndSpecialCharacters(str) {
922
928
  return str.replace(/[^a-zA-Z0-9]/g, "");
923
- }
929
+ } */
924
930
 
925
931
  fixColumnName(columns) {
926
932
  // Guard clause: if columns is not provided or not a string, return it as-is.
@@ -1051,10 +1057,7 @@ export default class SpiceModel {
1051
1057
  await this.run_hook(this, "list", "before");
1052
1058
  }
1053
1059
 
1054
- const cacheKey = this.removeSpaceAndSpecialCharacters(
1055
- `list::${this.type}::${args._join}::${query}::${args.limit}::${args.offset}::${args.sort}::${args.do_count}::${args.statement_consistent}::${args.columns}::${args.is_full_text}::${args.is_custom_query}`
1056
- );
1057
-
1060
+ const cacheKey = `list::${this.type}::${args._join}::${query}::${args.limit}::${args.offset}::${args.sort}::${args.do_count}::${args.statement_consistent}::${args.columns}::${args.is_full_text}::${args.is_custom_query}`;
1058
1061
  let results;
1059
1062
  if (this.shouldUseCache(this.type)) {
1060
1063
  const cachedResults = await this.getCacheProviderObject(this.type).get(
@@ -1062,17 +1065,21 @@ export default class SpiceModel {
1062
1065
  );
1063
1066
  results = cachedResults?.value;
1064
1067
 
1065
- if (cachedResults?.value === undefined) {
1068
+ // Check if there is no cached value
1069
+ const isCacheEmpty = cachedResults?.value === undefined;
1070
+
1071
+ // Check if the cached value should be refreshed (e.g., due to the monitor's timestamp)
1072
+ const shouldRefresh = await this.shouldForceRefresh(cachedResults);
1073
+
1074
+ // Always force a refresh for "workflow" type
1075
+ if (isCacheEmpty || shouldRefresh) {
1076
+ // Force a database read and cache update
1066
1077
  results = await this.fetchResults(args, query);
1067
- } else {
1068
- if (!results || (await this.shouldForceRefresh(cachedResults))) {
1069
- results = await this.fetchResults(args, query);
1070
- await this.getCacheProviderObject(this.type).set(
1071
- cacheKey,
1072
- { value: results, time: new Date().getTime() },
1073
- this.getCacheConfig(this.type)
1074
- );
1075
- }
1078
+ this.getCacheProviderObject(this.type).set(
1079
+ cacheKey,
1080
+ { value: results, time: new Date().getTime() },
1081
+ this.getCacheConfig(this.type)
1082
+ );
1076
1083
  }
1077
1084
  } else {
1078
1085
  results = await this.fetchResults(args, query);