spice-js 2.6.71 → 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.
- package/build/models/SpiceModel.js +131 -26
- package/package.json +1 -1
- package/src/models/SpiceModel.js +149 -72
|
@@ -566,6 +566,14 @@ class SpiceModel {
|
|
|
566
566
|
var _this4 = this;
|
|
567
567
|
|
|
568
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
|
+
|
|
569
577
|
try {
|
|
570
578
|
if (args.mapping_dept) _this4[_mapping_dept] = args.mapping_dept;
|
|
571
579
|
|
|
@@ -591,7 +599,15 @@ class SpiceModel {
|
|
|
591
599
|
|
|
592
600
|
if (isCacheEmpty || shouldRefresh) {
|
|
593
601
|
// Retrieve from the database and update cache
|
|
594
|
-
|
|
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
|
+
|
|
595
611
|
yield _this4.getCacheProviderObject(_this4.type).set(key, {
|
|
596
612
|
value: results,
|
|
597
613
|
time: new Date().getTime()
|
|
@@ -602,7 +618,14 @@ class SpiceModel {
|
|
|
602
618
|
}
|
|
603
619
|
} else {
|
|
604
620
|
// Directly fetch from the database if caching is disabled
|
|
605
|
-
|
|
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
|
+
}
|
|
606
629
|
}
|
|
607
630
|
|
|
608
631
|
if (results.type !== undefined && results.type !== _this4.type) {
|
|
@@ -630,6 +653,8 @@ class SpiceModel {
|
|
|
630
653
|
} catch (e) {
|
|
631
654
|
console.log(e.message, e);
|
|
632
655
|
throw e;
|
|
656
|
+
} finally {
|
|
657
|
+
c && p.end(c);
|
|
633
658
|
}
|
|
634
659
|
})();
|
|
635
660
|
}
|
|
@@ -735,6 +760,14 @@ class SpiceModel {
|
|
|
735
760
|
var _this8 = this;
|
|
736
761
|
|
|
737
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
|
+
|
|
738
771
|
try {
|
|
739
772
|
_this8.updated_at = new SDate().now();
|
|
740
773
|
var results = yield _this8.database.get(args.id);
|
|
@@ -764,7 +797,14 @@ class SpiceModel {
|
|
|
764
797
|
}
|
|
765
798
|
|
|
766
799
|
var db_data = cover_obj.new || _this8;
|
|
767
|
-
|
|
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
|
+
}
|
|
768
808
|
|
|
769
809
|
_this8.setMonitor();
|
|
770
810
|
|
|
@@ -785,6 +825,8 @@ class SpiceModel {
|
|
|
785
825
|
} catch (e) {
|
|
786
826
|
console.log("Error on update", e, e.stack);
|
|
787
827
|
throw e;
|
|
828
|
+
} finally {
|
|
829
|
+
c && p.end(c);
|
|
788
830
|
}
|
|
789
831
|
})();
|
|
790
832
|
}
|
|
@@ -793,10 +835,16 @@ class SpiceModel {
|
|
|
793
835
|
var _this9 = this;
|
|
794
836
|
|
|
795
837
|
return _asyncToGenerator(function* () {
|
|
838
|
+
var _this9$_ctx;
|
|
839
|
+
|
|
796
840
|
if (args === void 0) {
|
|
797
841
|
args = {};
|
|
798
842
|
}
|
|
799
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
|
+
|
|
800
848
|
try {
|
|
801
849
|
var form;
|
|
802
850
|
_this9.created_at = new SDate().now();
|
|
@@ -827,7 +875,15 @@ class SpiceModel {
|
|
|
827
875
|
yield _this9.run_hook(workingForm, "create", "before");
|
|
828
876
|
}
|
|
829
877
|
|
|
830
|
-
var
|
|
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
|
+
}
|
|
831
887
|
|
|
832
888
|
_this9.setMonitor();
|
|
833
889
|
|
|
@@ -847,6 +903,8 @@ class SpiceModel {
|
|
|
847
903
|
} catch (e) {
|
|
848
904
|
console.log(e.stack);
|
|
849
905
|
throw e;
|
|
906
|
+
} finally {
|
|
907
|
+
c && p.end(c);
|
|
850
908
|
}
|
|
851
909
|
})();
|
|
852
910
|
}
|
|
@@ -876,29 +934,43 @@ class SpiceModel {
|
|
|
876
934
|
var _this11 = this;
|
|
877
935
|
|
|
878
936
|
return _asyncToGenerator(function* () {
|
|
879
|
-
var
|
|
880
|
-
|
|
881
|
-
if (!item_exist) {
|
|
882
|
-
throw new Error(_this11.type + " does not exist.");
|
|
883
|
-
}
|
|
937
|
+
var _this11$_ctx;
|
|
884
938
|
|
|
885
|
-
|
|
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
|
+
});
|
|
886
944
|
|
|
887
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
|
+
|
|
888
954
|
if (args.skip_hooks != true) {
|
|
889
955
|
yield _this11.run_hook(args, "delete", "before");
|
|
890
956
|
}
|
|
891
957
|
|
|
892
958
|
var delete_response = {};
|
|
959
|
+
var pDb = p,
|
|
960
|
+
cDb = pDb == null ? void 0 : pDb.start(_this11.type + ".delete.database");
|
|
893
961
|
|
|
894
|
-
|
|
895
|
-
|
|
962
|
+
try {
|
|
963
|
+
if (args.hard) {
|
|
964
|
+
delete_response = yield _this11.database.delete(args.id);
|
|
896
965
|
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
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);
|
|
902
974
|
}
|
|
903
975
|
|
|
904
976
|
if (args.skip_hooks != true) {
|
|
@@ -909,6 +981,8 @@ class SpiceModel {
|
|
|
909
981
|
} catch (e) {
|
|
910
982
|
console.log(e.stack);
|
|
911
983
|
throw e;
|
|
984
|
+
} finally {
|
|
985
|
+
c && p.end(c);
|
|
912
986
|
}
|
|
913
987
|
})();
|
|
914
988
|
}
|
|
@@ -1152,16 +1226,25 @@ class SpiceModel {
|
|
|
1152
1226
|
var _this12 = this;
|
|
1153
1227
|
|
|
1154
1228
|
return _asyncToGenerator(function* () {
|
|
1229
|
+
var _this12$_ctx, _args7, _args8;
|
|
1230
|
+
|
|
1155
1231
|
if (args === void 0) {
|
|
1156
1232
|
args = {};
|
|
1157
1233
|
}
|
|
1158
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
|
+
|
|
1159
1242
|
try {
|
|
1160
|
-
var
|
|
1243
|
+
var _args9, _args10, _args11;
|
|
1161
1244
|
|
|
1162
1245
|
if (args.mapping_dept) _this12[_mapping_dept] = args.mapping_dept; // Find alias tokens from query/columns/sort
|
|
1163
1246
|
|
|
1164
|
-
var nestings = [..._this12.extractNestings(((
|
|
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.
|
|
1165
1248
|
|
|
1166
1249
|
var mappedNestings = _.compact(_.uniq(nestings).map(alias => {
|
|
1167
1250
|
var prop = _this12.props[alias];
|
|
@@ -1252,6 +1335,8 @@ class SpiceModel {
|
|
|
1252
1335
|
} catch (e) {
|
|
1253
1336
|
console.log(e.stack);
|
|
1254
1337
|
throw e;
|
|
1338
|
+
} finally {
|
|
1339
|
+
c && p.end(c);
|
|
1255
1340
|
}
|
|
1256
1341
|
})();
|
|
1257
1342
|
}
|
|
@@ -1260,13 +1345,23 @@ class SpiceModel {
|
|
|
1260
1345
|
var _this13 = this;
|
|
1261
1346
|
|
|
1262
1347
|
return _asyncToGenerator(function* () {
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
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);
|
|
1270
1365
|
}
|
|
1271
1366
|
})();
|
|
1272
1367
|
}
|
|
@@ -1601,10 +1696,18 @@ class SpiceModel {
|
|
|
1601
1696
|
var _this18 = this;
|
|
1602
1697
|
|
|
1603
1698
|
return _asyncToGenerator(function* () {
|
|
1699
|
+
var _this18$_ctx;
|
|
1700
|
+
|
|
1604
1701
|
if (path_to_be_removed === void 0) {
|
|
1605
1702
|
path_to_be_removed = [];
|
|
1606
1703
|
}
|
|
1607
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
|
+
|
|
1608
1711
|
try {
|
|
1609
1712
|
var _this18$_serializers, _this18$_serializers$;
|
|
1610
1713
|
|
|
@@ -1682,6 +1785,8 @@ class SpiceModel {
|
|
|
1682
1785
|
} catch (error) {
|
|
1683
1786
|
console.error("Error in do_serialize:", error.stack);
|
|
1684
1787
|
throw error;
|
|
1788
|
+
} finally {
|
|
1789
|
+
c && p.end(c);
|
|
1685
1790
|
}
|
|
1686
1791
|
})();
|
|
1687
1792
|
}
|
package/package.json
CHANGED
package/src/models/SpiceModel.js
CHANGED
|
@@ -45,7 +45,7 @@ export default class SpiceModel {
|
|
|
45
45
|
// ⚡ Static caches for performance optimization
|
|
46
46
|
static _defaultsCache = {};
|
|
47
47
|
static _hiddenPropsCache = {};
|
|
48
|
-
|
|
48
|
+
|
|
49
49
|
constructor(args = {}) {
|
|
50
50
|
try {
|
|
51
51
|
var dbtype =
|
|
@@ -169,15 +169,17 @@ export default class SpiceModel {
|
|
|
169
169
|
}
|
|
170
170
|
case Number:
|
|
171
171
|
case "number": {
|
|
172
|
-
this[i] =
|
|
173
|
-
|
|
172
|
+
this[i] =
|
|
173
|
+
_.isNumber(args.args[i]) ?
|
|
174
|
+
args.args[i]
|
|
174
175
|
: Number(args.args[i]);
|
|
175
176
|
break;
|
|
176
177
|
}
|
|
177
178
|
case Boolean:
|
|
178
179
|
case "boolean": {
|
|
179
|
-
this[i] =
|
|
180
|
-
|
|
180
|
+
this[i] =
|
|
181
|
+
_.isBoolean(args.args[i]) ?
|
|
182
|
+
args.args[i]
|
|
181
183
|
: args.args[i] == "true" ||
|
|
182
184
|
args.args[i] == 1 ||
|
|
183
185
|
args.args[i] == "True";
|
|
@@ -190,15 +192,17 @@ export default class SpiceModel {
|
|
|
190
192
|
}
|
|
191
193
|
case Array:
|
|
192
194
|
case "array": {
|
|
193
|
-
this[i] =
|
|
194
|
-
|
|
195
|
+
this[i] =
|
|
196
|
+
_.isArray(args.args[i]) ?
|
|
197
|
+
args.args[i]
|
|
195
198
|
: JSON.parse(args.args[i]);
|
|
196
199
|
break;
|
|
197
200
|
}
|
|
198
201
|
case Object:
|
|
199
202
|
case "object": {
|
|
200
|
-
this[i] =
|
|
201
|
-
|
|
203
|
+
this[i] =
|
|
204
|
+
_.isObject(args.args[i]) ?
|
|
205
|
+
args.args[i]
|
|
202
206
|
: JSON.parse(args.args[i]);
|
|
203
207
|
break;
|
|
204
208
|
}
|
|
@@ -482,6 +486,9 @@ export default class SpiceModel {
|
|
|
482
486
|
}
|
|
483
487
|
|
|
484
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 });
|
|
485
492
|
try {
|
|
486
493
|
if (args.mapping_dept) this[_mapping_dept] = args.mapping_dept;
|
|
487
494
|
|
|
@@ -508,7 +515,13 @@ export default class SpiceModel {
|
|
|
508
515
|
|
|
509
516
|
if (isCacheEmpty || shouldRefresh) {
|
|
510
517
|
// Retrieve from the database and update cache
|
|
511
|
-
|
|
518
|
+
const pDb = p,
|
|
519
|
+
cDb = pDb?.start(`${this.type}.get.database`);
|
|
520
|
+
try {
|
|
521
|
+
results = await this.database.get(args.id);
|
|
522
|
+
} finally {
|
|
523
|
+
cDb && pDb.end(cDb);
|
|
524
|
+
}
|
|
512
525
|
await this.getCacheProviderObject(this.type).set(
|
|
513
526
|
key,
|
|
514
527
|
{ value: results, time: new Date().getTime() },
|
|
@@ -520,7 +533,13 @@ export default class SpiceModel {
|
|
|
520
533
|
}
|
|
521
534
|
} else {
|
|
522
535
|
// Directly fetch from the database if caching is disabled
|
|
523
|
-
|
|
536
|
+
const pDb = p,
|
|
537
|
+
cDb = pDb?.start(`${this.type}.get.database`);
|
|
538
|
+
try {
|
|
539
|
+
results = await this.database.get(args.id);
|
|
540
|
+
} finally {
|
|
541
|
+
cDb && pDb.end(cDb);
|
|
542
|
+
}
|
|
524
543
|
}
|
|
525
544
|
|
|
526
545
|
if (results.type !== undefined && results.type !== this.type) {
|
|
@@ -553,6 +572,8 @@ export default class SpiceModel {
|
|
|
553
572
|
} catch (e) {
|
|
554
573
|
console.log(e.message, e);
|
|
555
574
|
throw e;
|
|
575
|
+
} finally {
|
|
576
|
+
c && p.end(c);
|
|
556
577
|
}
|
|
557
578
|
}
|
|
558
579
|
|
|
@@ -649,6 +670,9 @@ export default class SpiceModel {
|
|
|
649
670
|
}
|
|
650
671
|
|
|
651
672
|
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 });
|
|
652
676
|
try {
|
|
653
677
|
this.updated_at = new SDate().now();
|
|
654
678
|
let results = await this.database.get(args.id);
|
|
@@ -679,7 +703,13 @@ export default class SpiceModel {
|
|
|
679
703
|
}
|
|
680
704
|
let db_data = cover_obj.new || this;
|
|
681
705
|
|
|
682
|
-
|
|
706
|
+
const pDb = p,
|
|
707
|
+
cDb = pDb?.start(`${this.type}.update.database`);
|
|
708
|
+
try {
|
|
709
|
+
await this.database.update(args.id, db_data, args._ttl);
|
|
710
|
+
} finally {
|
|
711
|
+
cDb && pDb.end(cDb);
|
|
712
|
+
}
|
|
683
713
|
this.setMonitor();
|
|
684
714
|
|
|
685
715
|
if (args.skip_read_serialize != true && args.skip_serialize != true) {
|
|
@@ -708,10 +738,15 @@ export default class SpiceModel {
|
|
|
708
738
|
} catch (e) {
|
|
709
739
|
console.log("Error on update", e, e.stack);
|
|
710
740
|
throw e;
|
|
741
|
+
} finally {
|
|
742
|
+
c && p.end(c);
|
|
711
743
|
}
|
|
712
744
|
}
|
|
713
745
|
|
|
714
746
|
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`);
|
|
715
750
|
try {
|
|
716
751
|
let form;
|
|
717
752
|
this.created_at = new SDate().now();
|
|
@@ -738,7 +773,14 @@ export default class SpiceModel {
|
|
|
738
773
|
await this.run_hook(workingForm, "create", "before");
|
|
739
774
|
}
|
|
740
775
|
|
|
741
|
-
|
|
776
|
+
const pDb = p,
|
|
777
|
+
cDb = pDb?.start(`${this.type}.create.database`);
|
|
778
|
+
let results;
|
|
779
|
+
try {
|
|
780
|
+
results = await this.database.insert(id, workingForm, args._ttl);
|
|
781
|
+
} finally {
|
|
782
|
+
cDb && pDb.end(cDb);
|
|
783
|
+
}
|
|
742
784
|
this.setMonitor();
|
|
743
785
|
|
|
744
786
|
if (args.skip_read_serialize != true && args.skip_serialize != true) {
|
|
@@ -764,6 +806,8 @@ export default class SpiceModel {
|
|
|
764
806
|
} catch (e) {
|
|
765
807
|
console.log(e.stack);
|
|
766
808
|
throw e;
|
|
809
|
+
} finally {
|
|
810
|
+
c && p.end(c);
|
|
767
811
|
}
|
|
768
812
|
}
|
|
769
813
|
|
|
@@ -784,24 +828,33 @@ export default class SpiceModel {
|
|
|
784
828
|
}
|
|
785
829
|
|
|
786
830
|
async delete(args) {
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
throw new Error(`${this.type} does not exist.`);
|
|
791
|
-
}
|
|
792
|
-
let results = await this.database.get(args.id);
|
|
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 });
|
|
793
834
|
try {
|
|
835
|
+
let item_exist = await this.exist(args.id);
|
|
836
|
+
|
|
837
|
+
if (!item_exist) {
|
|
838
|
+
throw new Error(`${this.type} does not exist.`);
|
|
839
|
+
}
|
|
840
|
+
let results = await this.database.get(args.id);
|
|
794
841
|
if (args.skip_hooks != true) {
|
|
795
842
|
await this.run_hook(args, "delete", "before");
|
|
796
843
|
}
|
|
797
844
|
let delete_response = {};
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
845
|
+
const pDb = p,
|
|
846
|
+
cDb = pDb?.start(`${this.type}.delete.database`);
|
|
847
|
+
try {
|
|
848
|
+
if (args.hard) {
|
|
849
|
+
delete_response = await this.database.delete(args.id);
|
|
850
|
+
this.setMonitor();
|
|
851
|
+
} else {
|
|
852
|
+
delete results["id"];
|
|
853
|
+
results.deleted = true;
|
|
854
|
+
delete_response = await this.database.update(args.id, results);
|
|
855
|
+
}
|
|
856
|
+
} finally {
|
|
857
|
+
cDb && pDb.end(cDb);
|
|
805
858
|
}
|
|
806
859
|
if (args.skip_hooks != true) {
|
|
807
860
|
await this.run_hook(results, "delete", "after", results);
|
|
@@ -810,6 +863,8 @@ export default class SpiceModel {
|
|
|
810
863
|
} catch (e) {
|
|
811
864
|
console.log(e.stack);
|
|
812
865
|
throw e;
|
|
866
|
+
} finally {
|
|
867
|
+
c && p.end(c);
|
|
813
868
|
}
|
|
814
869
|
}
|
|
815
870
|
|
|
@@ -1017,9 +1072,8 @@ export default class SpiceModel {
|
|
|
1017
1072
|
const aliasRegex = /\s+AS\s+`?([\w]+)`?$/i;
|
|
1018
1073
|
const aliasMatch = col.match(aliasRegex);
|
|
1019
1074
|
const explicitAlias = aliasMatch ? aliasMatch[1] : null;
|
|
1020
|
-
const colWithoutAlias =
|
|
1021
|
-
? col.replace(aliasRegex, "").trim()
|
|
1022
|
-
: col;
|
|
1075
|
+
const colWithoutAlias =
|
|
1076
|
+
explicitAlias ? col.replace(aliasRegex, "").trim() : col;
|
|
1023
1077
|
|
|
1024
1078
|
// `table`.`col` or table.col
|
|
1025
1079
|
const columnRegex = /^`?([\w]+)`?\.`?([\w]+)`?$/;
|
|
@@ -1053,6 +1107,12 @@ export default class SpiceModel {
|
|
|
1053
1107
|
}
|
|
1054
1108
|
|
|
1055
1109
|
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
|
+
});
|
|
1056
1116
|
try {
|
|
1057
1117
|
if (args.mapping_dept) this[_mapping_dept] = args.mapping_dept;
|
|
1058
1118
|
|
|
@@ -1126,13 +1186,14 @@ export default class SpiceModel {
|
|
|
1126
1186
|
// LIMIT/OFFSET/SORT
|
|
1127
1187
|
args.limit = Number(args.limit) || undefined;
|
|
1128
1188
|
args.offset = Number(args.offset) || 0;
|
|
1129
|
-
args.sort =
|
|
1130
|
-
|
|
1189
|
+
args.sort =
|
|
1190
|
+
args.sort ?
|
|
1191
|
+
args.sort
|
|
1131
1192
|
.split(",")
|
|
1132
1193
|
.map((item) =>
|
|
1133
|
-
item.includes(".")
|
|
1134
|
-
|
|
1135
|
-
|
|
1194
|
+
item.includes(".") ? item : (
|
|
1195
|
+
`\`${this.type}\`.${this.formatSortComponent(item)}`
|
|
1196
|
+
)
|
|
1136
1197
|
)
|
|
1137
1198
|
.join(",")
|
|
1138
1199
|
: `\`${this.type}\`.created_at DESC`;
|
|
@@ -1183,33 +1244,42 @@ export default class SpiceModel {
|
|
|
1183
1244
|
} catch (e) {
|
|
1184
1245
|
console.log(e.stack);
|
|
1185
1246
|
throw e;
|
|
1247
|
+
} finally {
|
|
1248
|
+
c && p.end(c);
|
|
1186
1249
|
}
|
|
1187
1250
|
}
|
|
1188
1251
|
|
|
1189
1252
|
async fetchResults(args, query) {
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1253
|
+
// Profiling: zero overhead when disabled (single falsy check)
|
|
1254
|
+
const p = this[_ctx]?.profiler,
|
|
1255
|
+
c = p?.start(`${this.type}.fetchResults`);
|
|
1256
|
+
try {
|
|
1257
|
+
if (args.is_custom_query === "true" && args.ids.length > 0) {
|
|
1258
|
+
return await this.database.query(query);
|
|
1259
|
+
} else if (args.is_full_text === "true") {
|
|
1260
|
+
return await this.database.full_text_search(
|
|
1261
|
+
this.type,
|
|
1262
|
+
query || "",
|
|
1263
|
+
args.limit,
|
|
1264
|
+
args.offset,
|
|
1265
|
+
args._join
|
|
1266
|
+
);
|
|
1267
|
+
} else {
|
|
1268
|
+
let result = await this.database.search(
|
|
1269
|
+
this.type,
|
|
1270
|
+
args.columns || "",
|
|
1271
|
+
query || "",
|
|
1272
|
+
args.limit,
|
|
1273
|
+
args.offset,
|
|
1274
|
+
args.sort,
|
|
1275
|
+
args.do_count,
|
|
1276
|
+
args.statement_consistent,
|
|
1277
|
+
args._join
|
|
1278
|
+
);
|
|
1279
|
+
return result;
|
|
1280
|
+
}
|
|
1281
|
+
} finally {
|
|
1282
|
+
c && p.end(c);
|
|
1213
1283
|
}
|
|
1214
1284
|
}
|
|
1215
1285
|
|
|
@@ -1264,11 +1334,11 @@ export default class SpiceModel {
|
|
|
1264
1334
|
// ⚡ OPTIMIZED: Cache defaults metadata per model type
|
|
1265
1335
|
getDefaultsMetadata(type) {
|
|
1266
1336
|
const cacheKey = `${this.type}::${type}`;
|
|
1267
|
-
|
|
1337
|
+
|
|
1268
1338
|
if (!SpiceModel._defaultsCache[cacheKey]) {
|
|
1269
1339
|
const staticDefaults = {};
|
|
1270
1340
|
const dynamicDefaults = [];
|
|
1271
|
-
|
|
1341
|
+
|
|
1272
1342
|
// Pre-compute once per model type
|
|
1273
1343
|
for (const key in this.props) {
|
|
1274
1344
|
const def = this.props[key]?.defaults?.[type];
|
|
@@ -1280,14 +1350,15 @@ export default class SpiceModel {
|
|
|
1280
1350
|
}
|
|
1281
1351
|
}
|
|
1282
1352
|
}
|
|
1283
|
-
|
|
1353
|
+
|
|
1284
1354
|
SpiceModel._defaultsCache[cacheKey] = {
|
|
1285
1355
|
staticDefaults,
|
|
1286
1356
|
dynamicDefaults,
|
|
1287
|
-
hasDefaults:
|
|
1357
|
+
hasDefaults:
|
|
1358
|
+
Object.keys(staticDefaults).length > 0 || dynamicDefaults.length > 0,
|
|
1288
1359
|
};
|
|
1289
1360
|
}
|
|
1290
|
-
|
|
1361
|
+
|
|
1291
1362
|
return SpiceModel._defaultsCache[cacheKey];
|
|
1292
1363
|
}
|
|
1293
1364
|
|
|
@@ -1461,9 +1532,9 @@ export default class SpiceModel {
|
|
|
1461
1532
|
execute: async (data) => {
|
|
1462
1533
|
return await this.mapToObject(
|
|
1463
1534
|
data,
|
|
1464
|
-
_.isString(properties[i].map.reference)
|
|
1465
|
-
|
|
1466
|
-
|
|
1535
|
+
_.isString(properties[i].map.reference) ?
|
|
1536
|
+
spice.models[properties[i].map.reference]
|
|
1537
|
+
: properties[i].map.reference,
|
|
1467
1538
|
i,
|
|
1468
1539
|
properties[i].map.destination || i,
|
|
1469
1540
|
properties[i]
|
|
@@ -1479,9 +1550,9 @@ export default class SpiceModel {
|
|
|
1479
1550
|
execute: async (data) => {
|
|
1480
1551
|
return await this.mapToObjectArray(
|
|
1481
1552
|
data,
|
|
1482
|
-
_.isString(properties[i].map.reference)
|
|
1483
|
-
|
|
1484
|
-
|
|
1553
|
+
_.isString(properties[i].map.reference) ?
|
|
1554
|
+
spice.models[properties[i].map.reference]
|
|
1555
|
+
: properties[i].map.reference,
|
|
1485
1556
|
i,
|
|
1486
1557
|
properties[i].map.destination || i,
|
|
1487
1558
|
properties[i]
|
|
@@ -1502,6 +1573,9 @@ export default class SpiceModel {
|
|
|
1502
1573
|
}
|
|
1503
1574
|
|
|
1504
1575
|
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 });
|
|
1505
1579
|
try {
|
|
1506
1580
|
// Early exit if serialization should not run.
|
|
1507
1581
|
if (!this.shouldSerializerRun(args, type)) {
|
|
@@ -1531,20 +1605,21 @@ export default class SpiceModel {
|
|
|
1531
1605
|
}
|
|
1532
1606
|
|
|
1533
1607
|
// ⚡ OPTIMIZED: Use cached defaults metadata instead of computing every time
|
|
1534
|
-
const { staticDefaults, dynamicDefaults, hasDefaults } =
|
|
1535
|
-
|
|
1608
|
+
const { staticDefaults, dynamicDefaults, hasDefaults } =
|
|
1609
|
+
this.getDefaultsMetadata(type);
|
|
1610
|
+
|
|
1536
1611
|
if (hasDefaults) {
|
|
1537
1612
|
data = data.map((item) => {
|
|
1538
1613
|
// Apply static defaults first (fast - no function calls)
|
|
1539
1614
|
const result = _.defaults({}, item, staticDefaults);
|
|
1540
|
-
|
|
1615
|
+
|
|
1541
1616
|
// Only compute dynamic defaults if there are any
|
|
1542
1617
|
for (const { key, fn } of dynamicDefaults) {
|
|
1543
1618
|
if (result[key] === undefined) {
|
|
1544
1619
|
result[key] = fn({ old_data: data, new_data: old_data });
|
|
1545
1620
|
}
|
|
1546
1621
|
}
|
|
1547
|
-
|
|
1622
|
+
|
|
1548
1623
|
return result;
|
|
1549
1624
|
});
|
|
1550
1625
|
}
|
|
@@ -1568,6 +1643,8 @@ export default class SpiceModel {
|
|
|
1568
1643
|
} catch (error) {
|
|
1569
1644
|
console.error("Error in do_serialize:", error.stack);
|
|
1570
1645
|
throw error;
|
|
1646
|
+
} finally {
|
|
1647
|
+
c && p.end(c);
|
|
1571
1648
|
}
|
|
1572
1649
|
}
|
|
1573
1650
|
}
|