spice-js 2.6.70 → 2.6.72

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.
@@ -52,6 +52,7 @@ if (!Promise.allSettled) {
52
52
  }
53
53
 
54
54
  class SpiceModel {
55
+ // ⚡ Static caches for performance optimization
55
56
  constructor(args) {
56
57
  if (args === void 0) {
57
58
  args = {};
@@ -565,6 +566,14 @@ class SpiceModel {
565
566
  var _this4 = this;
566
567
 
567
568
  return _asyncToGenerator(function* () {
569
+ var _this4$_ctx, _args6;
570
+
571
+ // Profiling: zero overhead when disabled (single falsy check)
572
+ var p = (_this4$_ctx = _this4[_ctx]) == null ? void 0 : _this4$_ctx.profiler,
573
+ c = p == null ? void 0 : p.start(_this4.type + ".get", {
574
+ id: (_args6 = args) == null ? void 0 : _args6.id
575
+ });
576
+
568
577
  try {
569
578
  if (args.mapping_dept) _this4[_mapping_dept] = args.mapping_dept;
570
579
 
@@ -590,7 +599,15 @@ class SpiceModel {
590
599
 
591
600
  if (isCacheEmpty || shouldRefresh) {
592
601
  // Retrieve from the database and update cache
593
- results = yield _this4.database.get(args.id);
602
+ var pDb = p,
603
+ cDb = pDb == null ? void 0 : pDb.start(_this4.type + ".get.database");
604
+
605
+ try {
606
+ results = yield _this4.database.get(args.id);
607
+ } finally {
608
+ cDb && pDb.end(cDb);
609
+ }
610
+
594
611
  yield _this4.getCacheProviderObject(_this4.type).set(key, {
595
612
  value: results,
596
613
  time: new Date().getTime()
@@ -601,7 +618,14 @@ class SpiceModel {
601
618
  }
602
619
  } else {
603
620
  // Directly fetch from the database if caching is disabled
604
- results = yield _this4.database.get(args.id);
621
+ var _pDb = p,
622
+ _cDb = _pDb == null ? void 0 : _pDb.start(_this4.type + ".get.database");
623
+
624
+ try {
625
+ results = yield _this4.database.get(args.id);
626
+ } finally {
627
+ _cDb && _pDb.end(_cDb);
628
+ }
605
629
  }
606
630
 
607
631
  if (results.type !== undefined && results.type !== _this4.type) {
@@ -629,6 +653,8 @@ class SpiceModel {
629
653
  } catch (e) {
630
654
  console.log(e.message, e);
631
655
  throw e;
656
+ } finally {
657
+ c && p.end(c);
632
658
  }
633
659
  })();
634
660
  }
@@ -734,6 +760,14 @@ class SpiceModel {
734
760
  var _this8 = this;
735
761
 
736
762
  return _asyncToGenerator(function* () {
763
+ var _this8$_ctx;
764
+
765
+ // Profiling: zero overhead when disabled (single falsy check)
766
+ var p = (_this8$_ctx = _this8[_ctx]) == null ? void 0 : _this8$_ctx.profiler,
767
+ c = p == null ? void 0 : p.start(_this8.type + ".update", {
768
+ id: args == null ? void 0 : args.id
769
+ });
770
+
737
771
  try {
738
772
  _this8.updated_at = new SDate().now();
739
773
  var results = yield _this8.database.get(args.id);
@@ -763,7 +797,14 @@ class SpiceModel {
763
797
  }
764
798
 
765
799
  var db_data = cover_obj.new || _this8;
766
- yield _this8.database.update(args.id, db_data, args._ttl);
800
+ var pDb = p,
801
+ cDb = pDb == null ? void 0 : pDb.start(_this8.type + ".update.database");
802
+
803
+ try {
804
+ yield _this8.database.update(args.id, db_data, args._ttl);
805
+ } finally {
806
+ cDb && pDb.end(cDb);
807
+ }
767
808
 
768
809
  _this8.setMonitor();
769
810
 
@@ -784,6 +825,8 @@ class SpiceModel {
784
825
  } catch (e) {
785
826
  console.log("Error on update", e, e.stack);
786
827
  throw e;
828
+ } finally {
829
+ c && p.end(c);
787
830
  }
788
831
  })();
789
832
  }
@@ -792,10 +835,16 @@ class SpiceModel {
792
835
  var _this9 = this;
793
836
 
794
837
  return _asyncToGenerator(function* () {
838
+ var _this9$_ctx;
839
+
795
840
  if (args === void 0) {
796
841
  args = {};
797
842
  }
798
843
 
844
+ // Profiling: zero overhead when disabled (single falsy check)
845
+ var p = (_this9$_ctx = _this9[_ctx]) == null ? void 0 : _this9$_ctx.profiler,
846
+ c = p == null ? void 0 : p.start(_this9.type + ".create");
847
+
799
848
  try {
800
849
  var form;
801
850
  _this9.created_at = new SDate().now();
@@ -826,7 +875,15 @@ class SpiceModel {
826
875
  yield _this9.run_hook(workingForm, "create", "before");
827
876
  }
828
877
 
829
- var results = yield _this9.database.insert(id, workingForm, args._ttl);
878
+ var pDb = p,
879
+ cDb = pDb == null ? void 0 : pDb.start(_this9.type + ".create.database");
880
+ var results;
881
+
882
+ try {
883
+ results = yield _this9.database.insert(id, workingForm, args._ttl);
884
+ } finally {
885
+ cDb && pDb.end(cDb);
886
+ }
830
887
 
831
888
  _this9.setMonitor();
832
889
 
@@ -846,6 +903,8 @@ class SpiceModel {
846
903
  } catch (e) {
847
904
  console.log(e.stack);
848
905
  throw e;
906
+ } finally {
907
+ c && p.end(c);
849
908
  }
850
909
  })();
851
910
  }
@@ -875,29 +934,43 @@ class SpiceModel {
875
934
  var _this11 = this;
876
935
 
877
936
  return _asyncToGenerator(function* () {
878
- var item_exist = yield _this11.exist(args.id);
937
+ var _this11$_ctx;
879
938
 
880
- if (!item_exist) {
881
- throw new Error(_this11.type + " does not exist.");
882
- }
883
-
884
- var results = yield _this11.database.get(args.id);
939
+ // Profiling: zero overhead when disabled (single falsy check)
940
+ var p = (_this11$_ctx = _this11[_ctx]) == null ? void 0 : _this11$_ctx.profiler,
941
+ c = p == null ? void 0 : p.start(_this11.type + ".delete", {
942
+ id: args == null ? void 0 : args.id
943
+ });
885
944
 
886
945
  try {
946
+ var item_exist = yield _this11.exist(args.id);
947
+
948
+ if (!item_exist) {
949
+ throw new Error(_this11.type + " does not exist.");
950
+ }
951
+
952
+ var results = yield _this11.database.get(args.id);
953
+
887
954
  if (args.skip_hooks != true) {
888
955
  yield _this11.run_hook(args, "delete", "before");
889
956
  }
890
957
 
891
958
  var delete_response = {};
959
+ var pDb = p,
960
+ cDb = pDb == null ? void 0 : pDb.start(_this11.type + ".delete.database");
892
961
 
893
- if (args.hard) {
894
- delete_response = yield _this11.database.delete(args.id);
962
+ try {
963
+ if (args.hard) {
964
+ delete_response = yield _this11.database.delete(args.id);
895
965
 
896
- _this11.setMonitor();
897
- } else {
898
- delete results["id"];
899
- results.deleted = true;
900
- delete_response = yield _this11.database.update(args.id, results);
966
+ _this11.setMonitor();
967
+ } else {
968
+ delete results["id"];
969
+ results.deleted = true;
970
+ delete_response = yield _this11.database.update(args.id, results);
971
+ }
972
+ } finally {
973
+ cDb && pDb.end(cDb);
901
974
  }
902
975
 
903
976
  if (args.skip_hooks != true) {
@@ -908,6 +981,8 @@ class SpiceModel {
908
981
  } catch (e) {
909
982
  console.log(e.stack);
910
983
  throw e;
984
+ } finally {
985
+ c && p.end(c);
911
986
  }
912
987
  })();
913
988
  }
@@ -1151,16 +1226,25 @@ class SpiceModel {
1151
1226
  var _this12 = this;
1152
1227
 
1153
1228
  return _asyncToGenerator(function* () {
1229
+ var _this12$_ctx, _args7, _args8;
1230
+
1154
1231
  if (args === void 0) {
1155
1232
  args = {};
1156
1233
  }
1157
1234
 
1235
+ // Profiling: zero overhead when disabled (single falsy check)
1236
+ var p = (_this12$_ctx = _this12[_ctx]) == null ? void 0 : _this12$_ctx.profiler,
1237
+ c = p == null ? void 0 : p.start(_this12.type + ".list", {
1238
+ limit: (_args7 = args) == null ? void 0 : _args7.limit,
1239
+ offset: (_args8 = args) == null ? void 0 : _args8.offset
1240
+ });
1241
+
1158
1242
  try {
1159
- var _args6, _args7, _args8;
1243
+ var _args9, _args10, _args11;
1160
1244
 
1161
1245
  if (args.mapping_dept) _this12[_mapping_dept] = args.mapping_dept; // Find alias tokens from query/columns/sort
1162
1246
 
1163
- var nestings = [..._this12.extractNestings(((_args6 = args) == null ? void 0 : _args6.query) || "", _this12.type), ..._this12.extractNestings(((_args7 = args) == null ? void 0 : _args7.columns) || "", _this12.type), ..._this12.extractNestings(((_args8 = args) == null ? void 0 : _args8.sort) || "", _this12.type)]; // Decide which aliases we can join: only when map.type===MODEL AND reference is a STRING keyspace.
1247
+ var nestings = [..._this12.extractNestings(((_args9 = args) == null ? void 0 : _args9.query) || "", _this12.type), ..._this12.extractNestings(((_args10 = args) == null ? void 0 : _args10.columns) || "", _this12.type), ..._this12.extractNestings(((_args11 = args) == null ? void 0 : _args11.sort) || "", _this12.type)]; // Decide which aliases we can join: only when map.type===MODEL AND reference is a STRING keyspace.
1164
1248
 
1165
1249
  var mappedNestings = _.compact(_.uniq(nestings).map(alias => {
1166
1250
  var prop = _this12.props[alias];
@@ -1251,6 +1335,8 @@ class SpiceModel {
1251
1335
  } catch (e) {
1252
1336
  console.log(e.stack);
1253
1337
  throw e;
1338
+ } finally {
1339
+ c && p.end(c);
1254
1340
  }
1255
1341
  })();
1256
1342
  }
@@ -1259,13 +1345,23 @@ class SpiceModel {
1259
1345
  var _this13 = this;
1260
1346
 
1261
1347
  return _asyncToGenerator(function* () {
1262
- if (args.is_custom_query === "true" && args.ids.length > 0) {
1263
- return yield _this13.database.query(query);
1264
- } else if (args.is_full_text === "true") {
1265
- return yield _this13.database.full_text_search(_this13.type, query || "", args.limit, args.offset, args._join);
1266
- } else {
1267
- var result = yield _this13.database.search(_this13.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent, args._join);
1268
- return result;
1348
+ var _this13$_ctx;
1349
+
1350
+ // Profiling: zero overhead when disabled (single falsy check)
1351
+ var p = (_this13$_ctx = _this13[_ctx]) == null ? void 0 : _this13$_ctx.profiler,
1352
+ c = p == null ? void 0 : p.start(_this13.type + ".fetchResults");
1353
+
1354
+ try {
1355
+ if (args.is_custom_query === "true" && args.ids.length > 0) {
1356
+ return yield _this13.database.query(query);
1357
+ } else if (args.is_full_text === "true") {
1358
+ return yield _this13.database.full_text_search(_this13.type, query || "", args.limit, args.offset, args._join);
1359
+ } else {
1360
+ var result = yield _this13.database.search(_this13.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent, args._join);
1361
+ return result;
1362
+ }
1363
+ } finally {
1364
+ c && p.end(c);
1269
1365
  }
1270
1366
  })();
1271
1367
  }
@@ -1330,6 +1426,54 @@ class SpiceModel {
1330
1426
  }
1331
1427
 
1332
1428
  return true;
1429
+ } // ⚡ OPTIMIZED: Cache defaults metadata per model type
1430
+
1431
+
1432
+ getDefaultsMetadata(type) {
1433
+ var cacheKey = this.type + "::" + type;
1434
+
1435
+ if (!SpiceModel._defaultsCache[cacheKey]) {
1436
+ var staticDefaults = {};
1437
+ var dynamicDefaults = []; // Pre-compute once per model type
1438
+
1439
+ for (var key in this.props) {
1440
+ var _this$props$key, _this$props$key$defau;
1441
+
1442
+ var def = (_this$props$key = this.props[key]) == null ? void 0 : (_this$props$key$defau = _this$props$key.defaults) == null ? void 0 : _this$props$key$defau[type];
1443
+
1444
+ if (def !== undefined) {
1445
+ if (_.isFunction(def)) {
1446
+ dynamicDefaults.push({
1447
+ key,
1448
+ fn: def
1449
+ });
1450
+ } else {
1451
+ staticDefaults[key] = def;
1452
+ }
1453
+ }
1454
+ }
1455
+
1456
+ SpiceModel._defaultsCache[cacheKey] = {
1457
+ staticDefaults,
1458
+ dynamicDefaults,
1459
+ hasDefaults: Object.keys(staticDefaults).length > 0 || dynamicDefaults.length > 0
1460
+ };
1461
+ }
1462
+
1463
+ return SpiceModel._defaultsCache[cacheKey];
1464
+ } // ⚡ OPTIMIZED: Cache hidden props per model type
1465
+
1466
+
1467
+ getHiddenProps() {
1468
+ if (!SpiceModel._hiddenPropsCache[this.type]) {
1469
+ SpiceModel._hiddenPropsCache[this.type] = Object.keys(this.props).filter(key => {
1470
+ var _this$props$key2;
1471
+
1472
+ return (_this$props$key2 = this.props[key]) == null ? void 0 : _this$props$key2.hide;
1473
+ });
1474
+ }
1475
+
1476
+ return SpiceModel._hiddenPropsCache[this.type];
1333
1477
  }
1334
1478
 
1335
1479
  mapToObject(data, Class, source_property, store_property, property) {
@@ -1552,10 +1696,18 @@ class SpiceModel {
1552
1696
  var _this18 = this;
1553
1697
 
1554
1698
  return _asyncToGenerator(function* () {
1699
+ var _this18$_ctx;
1700
+
1555
1701
  if (path_to_be_removed === void 0) {
1556
1702
  path_to_be_removed = [];
1557
1703
  }
1558
1704
 
1705
+ // Profiling: zero overhead when disabled (single falsy check)
1706
+ var p = (_this18$_ctx = _this18[_ctx]) == null ? void 0 : _this18$_ctx.profiler,
1707
+ c = p == null ? void 0 : p.start(_this18.type + ".do_serialize", {
1708
+ type
1709
+ });
1710
+
1559
1711
  try {
1560
1712
  var _this18$_serializers, _this18$_serializers$;
1561
1713
 
@@ -1587,33 +1739,42 @@ class SpiceModel {
1587
1739
 
1588
1740
  if (!originalIsArray) {
1589
1741
  data = [data];
1590
- } // Compute the defaults from properties using reduce.
1591
-
1592
-
1593
- var defaults = Object.keys(_this18.props).reduce((acc, key) => {
1594
- var _this18$props$key, _this18$props$key$def;
1595
-
1596
- var def = (_this18$props$key = _this18.props[key]) == null ? void 0 : (_this18$props$key$def = _this18$props$key.defaults) == null ? void 0 : _this18$props$key$def[type];
1597
-
1598
- if (def !== undefined) {
1599
- acc[key] = _.isFunction(def) ? def({
1600
- old_data: data,
1601
- new_data: old_data
1602
- }) : def;
1603
- }
1742
+ } // OPTIMIZED: Use cached defaults metadata instead of computing every time
1743
+
1744
+
1745
+ var {
1746
+ staticDefaults,
1747
+ dynamicDefaults,
1748
+ hasDefaults
1749
+ } = _this18.getDefaultsMetadata(type);
1750
+
1751
+ if (hasDefaults) {
1752
+ data = data.map(item => {
1753
+ // Apply static defaults first (fast - no function calls)
1754
+ var result = _.defaults({}, item, staticDefaults); // Only compute dynamic defaults if there are any
1755
+
1756
+
1757
+ for (var {
1758
+ key,
1759
+ fn
1760
+ } of dynamicDefaults) {
1761
+ if (result[key] === undefined) {
1762
+ result[key] = fn({
1763
+ old_data: data,
1764
+ new_data: old_data
1765
+ });
1766
+ }
1767
+ }
1604
1768
 
1605
- return acc;
1606
- }, {}); // Merge defaults into each object.
1769
+ return result;
1770
+ });
1771
+ } // If type is "read", clean the data by omitting certain props.
1607
1772
 
1608
- data = data.map(item => _.defaults(item, defaults)); // If type is "read", clean the data by omitting certain props.
1609
1773
 
1610
1774
  if (type === "read") {
1611
- // Collect hidden properties from schema.
1612
- var hiddenProps = Object.keys(_this18.props).filter(key => {
1613
- var _this18$props$key2;
1775
+ // OPTIMIZED: Use cached hidden props instead of computing every time
1776
+ var hiddenProps = _this18.getHiddenProps(); // Combine default props to remove.
1614
1777
 
1615
- return (_this18$props$key2 = _this18.props[key]) == null ? void 0 : _this18$props$key2.hide;
1616
- }); // Combine default props to remove.
1617
1778
 
1618
1779
  var propsToClean = ["deleted", "type", ...path_to_be_removed, ...hiddenProps];
1619
1780
  data = data.map(item => _.omit(item, propsToClean));
@@ -1624,10 +1785,14 @@ class SpiceModel {
1624
1785
  } catch (error) {
1625
1786
  console.error("Error in do_serialize:", error.stack);
1626
1787
  throw error;
1788
+ } finally {
1789
+ c && p.end(c);
1627
1790
  }
1628
1791
  })();
1629
1792
  }
1630
1793
 
1631
1794
  }
1632
1795
 
1633
- exports.default = SpiceModel;
1796
+ exports.default = SpiceModel;
1797
+ SpiceModel._defaultsCache = {};
1798
+ SpiceModel._hiddenPropsCache = {};