spice-js 2.6.72 → 2.6.73

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-js",
3
- "version": "2.6.72",
3
+ "version": "2.6.73",
4
4
  "description": "spice",
5
5
  "main": "build/index.js",
6
6
  "repository": {
@@ -486,10 +486,10 @@ export default class SpiceModel {
486
486
  }
487
487
 
488
488
  async get(args) {
489
- // Profiling: zero overhead when disabled (single falsy check)
490
- const p = this[_ctx]?.profiler,
491
- c = p?.start(`${this.type}.get`, { id: args?.id });
492
- try {
489
+ // Profiling: use track() for proper async context forking
490
+ const p = this[_ctx]?.profiler;
491
+
492
+ const doGet = async () => {
493
493
  if (args.mapping_dept) this[_mapping_dept] = args.mapping_dept;
494
494
 
495
495
  if (!args) {
@@ -515,12 +515,12 @@ export default class SpiceModel {
515
515
 
516
516
  if (isCacheEmpty || shouldRefresh) {
517
517
  // Retrieve from the database and update cache
518
- const pDb = p,
519
- cDb = pDb?.start(`${this.type}.get.database`);
520
- try {
518
+ if (p) {
519
+ results = await p.track(`${this.type}.get.database`, async () => {
520
+ return await this.database.get(args.id);
521
+ });
522
+ } else {
521
523
  results = await this.database.get(args.id);
522
- } finally {
523
- cDb && pDb.end(cDb);
524
524
  }
525
525
  await this.getCacheProviderObject(this.type).set(
526
526
  key,
@@ -533,12 +533,12 @@ export default class SpiceModel {
533
533
  }
534
534
  } else {
535
535
  // Directly fetch from the database if caching is disabled
536
- const pDb = p,
537
- cDb = pDb?.start(`${this.type}.get.database`);
538
- try {
536
+ if (p) {
537
+ results = await p.track(`${this.type}.get.database`, async () => {
538
+ return await this.database.get(args.id);
539
+ });
540
+ } else {
539
541
  results = await this.database.get(args.id);
540
- } finally {
541
- cDb && pDb.end(cDb);
542
542
  }
543
543
  }
544
544
 
@@ -569,11 +569,16 @@ export default class SpiceModel {
569
569
  throw new Error(`${this.type} does not exist`);
570
570
  }
571
571
  }
572
+ };
573
+
574
+ try {
575
+ if (p) {
576
+ return await p.track(`${this.type}.get`, doGet, { id: args?.id });
577
+ }
578
+ return await doGet();
572
579
  } catch (e) {
573
580
  console.log(e.message, e);
574
581
  throw e;
575
- } finally {
576
- c && p.end(c);
577
582
  }
578
583
  }
579
584
 
@@ -670,10 +675,10 @@ export default class SpiceModel {
670
675
  }
671
676
 
672
677
  async update(args) {
673
- // Profiling: zero overhead when disabled (single falsy check)
674
- const p = this[_ctx]?.profiler,
675
- c = p?.start(`${this.type}.update`, { id: args?.id });
676
- try {
678
+ // Profiling: use track() for proper async context forking
679
+ const p = this[_ctx]?.profiler;
680
+
681
+ const doUpdate = async () => {
677
682
  this.updated_at = new SDate().now();
678
683
  let results = await this.database.get(args.id);
679
684
  let item_exist = await this.exist(results);
@@ -703,12 +708,12 @@ export default class SpiceModel {
703
708
  }
704
709
  let db_data = cover_obj.new || this;
705
710
 
706
- const pDb = p,
707
- cDb = pDb?.start(`${this.type}.update.database`);
708
- try {
711
+ if (p) {
712
+ await p.track(`${this.type}.update.database`, async () => {
713
+ await this.database.update(args.id, db_data, args._ttl);
714
+ });
715
+ } else {
709
716
  await this.database.update(args.id, db_data, args._ttl);
710
- } finally {
711
- cDb && pDb.end(cDb);
712
717
  }
713
718
  this.setMonitor();
714
719
 
@@ -735,19 +740,24 @@ export default class SpiceModel {
735
740
  }
736
741
  this.id = args.id;
737
742
  return { ...cover_obj.new, id: args.id };
743
+ };
744
+
745
+ try {
746
+ if (p) {
747
+ return await p.track(`${this.type}.update`, doUpdate, { id: args?.id });
748
+ }
749
+ return await doUpdate();
738
750
  } catch (e) {
739
751
  console.log("Error on update", e, e.stack);
740
752
  throw e;
741
- } finally {
742
- c && p.end(c);
743
753
  }
744
754
  }
745
755
 
746
756
  async create(args = {}) {
747
- // Profiling: zero overhead when disabled (single falsy check)
748
- const p = this[_ctx]?.profiler,
749
- c = p?.start(`${this.type}.create`);
750
- try {
757
+ // Profiling: use track() for proper async context forking
758
+ const p = this[_ctx]?.profiler;
759
+
760
+ const doCreate = async () => {
751
761
  let form;
752
762
  this.created_at = new SDate().now();
753
763
  args = _.defaults(args, { id_prefix: this.type });
@@ -773,13 +783,13 @@ export default class SpiceModel {
773
783
  await this.run_hook(workingForm, "create", "before");
774
784
  }
775
785
 
776
- const pDb = p,
777
- cDb = pDb?.start(`${this.type}.create.database`);
778
786
  let results;
779
- try {
787
+ if (p) {
788
+ results = await p.track(`${this.type}.create.database`, async () => {
789
+ return await this.database.insert(id, workingForm, args._ttl);
790
+ });
791
+ } else {
780
792
  results = await this.database.insert(id, workingForm, args._ttl);
781
- } finally {
782
- cDb && pDb.end(cDb);
783
793
  }
784
794
  this.setMonitor();
785
795
 
@@ -803,11 +813,16 @@ export default class SpiceModel {
803
813
  );
804
814
  }
805
815
  return { ...results, id };
816
+ };
817
+
818
+ try {
819
+ if (p) {
820
+ return await p.track(`${this.type}.create`, doCreate);
821
+ }
822
+ return await doCreate();
806
823
  } catch (e) {
807
824
  console.log(e.stack);
808
825
  throw e;
809
- } finally {
810
- c && p.end(c);
811
826
  }
812
827
  }
813
828
 
@@ -828,10 +843,10 @@ export default class SpiceModel {
828
843
  }
829
844
 
830
845
  async delete(args) {
831
- // Profiling: zero overhead when disabled (single falsy check)
832
- const p = this[_ctx]?.profiler,
833
- c = p?.start(`${this.type}.delete`, { id: args?.id });
834
- try {
846
+ // Profiling: use track() for proper async context forking
847
+ const p = this[_ctx]?.profiler;
848
+
849
+ const doDelete = async () => {
835
850
  let item_exist = await this.exist(args.id);
836
851
 
837
852
  if (!item_exist) {
@@ -842,9 +857,8 @@ export default class SpiceModel {
842
857
  await this.run_hook(args, "delete", "before");
843
858
  }
844
859
  let delete_response = {};
845
- const pDb = p,
846
- cDb = pDb?.start(`${this.type}.delete.database`);
847
- try {
860
+
861
+ const dbOperation = async () => {
848
862
  if (args.hard) {
849
863
  delete_response = await this.database.delete(args.id);
850
864
  this.setMonitor();
@@ -853,18 +867,28 @@ export default class SpiceModel {
853
867
  results.deleted = true;
854
868
  delete_response = await this.database.update(args.id, results);
855
869
  }
856
- } finally {
857
- cDb && pDb.end(cDb);
870
+ };
871
+
872
+ if (p) {
873
+ await p.track(`${this.type}.delete.database`, dbOperation);
874
+ } else {
875
+ await dbOperation();
858
876
  }
877
+
859
878
  if (args.skip_hooks != true) {
860
879
  await this.run_hook(results, "delete", "after", results);
861
880
  }
862
881
  return {};
882
+ };
883
+
884
+ try {
885
+ if (p) {
886
+ return await p.track(`${this.type}.delete`, doDelete, { id: args?.id });
887
+ }
888
+ return await doDelete();
863
889
  } catch (e) {
864
890
  console.log(e.stack);
865
891
  throw e;
866
- } finally {
867
- c && p.end(c);
868
892
  }
869
893
  }
870
894
 
@@ -1107,13 +1131,10 @@ export default class SpiceModel {
1107
1131
  }
1108
1132
 
1109
1133
  async list(args = {}) {
1110
- // Profiling: zero overhead when disabled (single falsy check)
1111
- const p = this[_ctx]?.profiler,
1112
- c = p?.start(`${this.type}.list`, {
1113
- limit: args?.limit,
1114
- offset: args?.offset,
1115
- });
1116
- try {
1134
+ // Profiling: use track() for proper async context forking
1135
+ const p = this[_ctx]?.profiler;
1136
+
1137
+ const doList = async () => {
1117
1138
  if (args.mapping_dept) this[_mapping_dept] = args.mapping_dept;
1118
1139
 
1119
1140
  // Find alias tokens from query/columns/sort
@@ -1241,19 +1262,27 @@ export default class SpiceModel {
1241
1262
 
1242
1263
  results.data = this.filterResultsByColumns(results.data, args.columns);
1243
1264
  return results;
1265
+ };
1266
+
1267
+ try {
1268
+ if (p) {
1269
+ return await p.track(`${this.type}.list`, doList, {
1270
+ limit: args?.limit,
1271
+ offset: args?.offset,
1272
+ });
1273
+ }
1274
+ return await doList();
1244
1275
  } catch (e) {
1245
1276
  console.log(e.stack);
1246
1277
  throw e;
1247
- } finally {
1248
- c && p.end(c);
1249
1278
  }
1250
1279
  }
1251
1280
 
1252
1281
  async fetchResults(args, query) {
1253
- // Profiling: zero overhead when disabled (single falsy check)
1254
- const p = this[_ctx]?.profiler,
1255
- c = p?.start(`${this.type}.fetchResults`);
1256
- try {
1282
+ // Profiling: use track() for proper async context forking
1283
+ const p = this[_ctx]?.profiler;
1284
+
1285
+ const doFetch = async () => {
1257
1286
  if (args.is_custom_query === "true" && args.ids.length > 0) {
1258
1287
  return await this.database.query(query);
1259
1288
  } else if (args.is_full_text === "true") {
@@ -1278,9 +1307,12 @@ export default class SpiceModel {
1278
1307
  );
1279
1308
  return result;
1280
1309
  }
1281
- } finally {
1282
- c && p.end(c);
1310
+ };
1311
+
1312
+ if (p) {
1313
+ return await p.track(`${this.type}.fetchResults`, doFetch);
1283
1314
  }
1315
+ return await doFetch();
1284
1316
  }
1285
1317
 
1286
1318
  addHook({ operation, when, execute }) {
@@ -1573,10 +1605,10 @@ export default class SpiceModel {
1573
1605
  }
1574
1606
 
1575
1607
  async do_serialize(data, type, old_data, args, path_to_be_removed = []) {
1576
- // Profiling: zero overhead when disabled (single falsy check)
1577
- const p = this[_ctx]?.profiler,
1578
- c = p?.start(`${this.type}.do_serialize`, { type });
1579
- try {
1608
+ // Profiling: use track() for proper async context forking
1609
+ const p = this[_ctx]?.profiler;
1610
+
1611
+ const doSerialize = async () => {
1580
1612
  // Early exit if serialization should not run.
1581
1613
  if (!this.shouldSerializerRun(args, type)) {
1582
1614
  return data;
@@ -1640,11 +1672,18 @@ export default class SpiceModel {
1640
1672
 
1641
1673
  // Return in the original format (array or single object).
1642
1674
  return originalIsArray ? data : data[0];
1675
+ };
1676
+
1677
+ try {
1678
+ if (p) {
1679
+ return await p.track(`${this.type}.do_serialize`, doSerialize, {
1680
+ type,
1681
+ });
1682
+ }
1683
+ return await doSerialize();
1643
1684
  } catch (error) {
1644
1685
  console.error("Error in do_serialize:", error.stack);
1645
1686
  throw error;
1646
- } finally {
1647
- c && p.end(c);
1648
1687
  }
1649
1688
  }
1650
1689
  }