@carbonorm/carbonnode 3.7.13 → 3.7.15

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/dist/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import Qs from 'qs';
3
- import { __assign, __awaiter, __generator, __extends, __spreadArray, __rest } from 'tslib';
3
+ import { __assign, __awaiter, __generator, __spreadArray, __rest, __extends } from 'tslib';
4
4
  import { toast } from 'react-toastify';
5
5
  import namedPlaceholders from 'named-placeholders';
6
6
  import { Buffer as Buffer$1 } from 'buffer';
@@ -620,6 +620,122 @@ function sortAndSerializeQueryObject(tables, query) {
620
620
  return tables + ' ' + JSON.stringify(orderedQuery);
621
621
  }
622
622
 
623
+ /**
624
+ * Converts a singular T-shaped request into complex ORM format for GET/PUT/DELETE
625
+ * Enforces that all primary keys are present for singular syntax and that the table has PKs.
626
+ * Optionally accepts a previously removed primary key (key/value) to reconstruct WHERE.
627
+ */
628
+ function normalizeSingularRequest(requestMethod, request, restModel, removedPrimary) {
629
+ var _a, _b;
630
+ var _c;
631
+ if (request == null || typeof request !== 'object')
632
+ return request;
633
+ var specialKeys = new Set([
634
+ C6Constants.SELECT,
635
+ C6Constants.UPDATE,
636
+ C6Constants.DELETE,
637
+ C6Constants.WHERE,
638
+ C6Constants.JOIN,
639
+ C6Constants.PAGINATION,
640
+ ]);
641
+ // Determine if the request is already complex (has any special key besides PAGINATION)
642
+ var keys = Object.keys(request);
643
+ var hasComplexKeys = keys.some(function (k) { return k !== C6Constants.PAGINATION && specialKeys.has(k); });
644
+ if (hasComplexKeys)
645
+ return request; // already complex
646
+ // We treat it as singular when it's not complex.
647
+ // For GET, PUT, DELETE only
648
+ if (!(requestMethod === C6Constants.GET || requestMethod === C6Constants.PUT || requestMethod === C6Constants.DELETE)) {
649
+ return request;
650
+ }
651
+ var pkShorts = Array.isArray(restModel.PRIMARY_SHORT) ? __spreadArray([], restModel.PRIMARY_SHORT, true) : [];
652
+ var pkFulls = Array.isArray(restModel.PRIMARY) ? __spreadArray([], restModel.PRIMARY, true) : [];
653
+ var resolveShortKey = function (key) {
654
+ var _a;
655
+ var cols = restModel.COLUMNS || {};
656
+ return (_a = cols[key]) !== null && _a !== void 0 ? _a : key;
657
+ };
658
+ if (!pkShorts.length) {
659
+ // For GET requests, do not enforce primary key presence; treat as a collection query.
660
+ if (requestMethod === C6Constants.GET)
661
+ return request;
662
+ throw new Error("Table (".concat(restModel.TABLE_NAME, ") has no primary key; singular request syntax is not allowed."));
663
+ }
664
+ // Build pk map from request + possibly removed primary key (accept short or fully-qualified keys)
665
+ var pkValues = {};
666
+ var requestObj = request;
667
+ var _loop_1 = function (pkShort) {
668
+ // 1) direct short key
669
+ var value = requestObj[pkShort];
670
+ if (value === undefined) {
671
+ // 2) fully-qualified key matching this short key (from PRIMARY list or by concatenation)
672
+ var fqCandidate_1 = "".concat(restModel.TABLE_NAME, ".").concat(pkShort);
673
+ var fqKey = (_c = pkFulls.find(function (fq) { return fq === fqCandidate_1 || fq.endsWith(".".concat(pkShort)); })) !== null && _c !== void 0 ? _c : fqCandidate_1;
674
+ value = requestObj[fqKey];
675
+ }
676
+ if (value === undefined && removedPrimary) {
677
+ // 3) removedPrimary may provide either short or fully-qualified key
678
+ var removedKeyShort = resolveShortKey(removedPrimary.key);
679
+ if (removedKeyShort === pkShort)
680
+ value = removedPrimary.value;
681
+ }
682
+ if (value !== undefined && value !== null) {
683
+ pkValues[pkShort] = value;
684
+ }
685
+ };
686
+ for (var _i = 0, pkShorts_1 = pkShorts; _i < pkShorts_1.length; _i++) {
687
+ var pkShort = pkShorts_1[_i];
688
+ _loop_1(pkShort);
689
+ }
690
+ var missing = pkShorts.filter(function (pk) { return !(pk in pkValues); });
691
+ if (missing.length) {
692
+ // For GET requests, if not all PKs are provided, treat as a collection query and leave as-is.
693
+ if (requestMethod === C6Constants.GET) {
694
+ return request;
695
+ }
696
+ throw new Error("Singular request requires all primary key(s) [".concat(pkShorts.join(', '), "] for table (").concat(restModel.TABLE_NAME, "). Missing: [").concat(missing.join(', '), "]"));
697
+ }
698
+ // Strip API metadata that should remain at root
699
+ var _d = request, dataInsertMultipleRows = _d.dataInsertMultipleRows, cacheResults = _d.cacheResults, fetchDependencies = _d.fetchDependencies, debug = _d.debug, success = _d.success, error = _d.error, rest = __rest(_d, ["dataInsertMultipleRows", "cacheResults", "fetchDependencies", "debug", "success", "error"]);
700
+ if (requestMethod === C6Constants.GET) {
701
+ var normalized_1 = {
702
+ WHERE: __assign({}, pkValues),
703
+ };
704
+ // Preserve pagination if any was added previously
705
+ if (request[C6Constants.PAGINATION]) {
706
+ normalized_1[C6Constants.PAGINATION] = request[C6Constants.PAGINATION];
707
+ }
708
+ return __assign(__assign({}, normalized_1), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
709
+ }
710
+ if (requestMethod === C6Constants.DELETE) {
711
+ var normalized_2 = (_a = {},
712
+ _a[C6Constants.DELETE] = true,
713
+ _a.WHERE = __assign({}, pkValues),
714
+ _a);
715
+ return __assign(__assign({}, normalized_2), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
716
+ }
717
+ // PUT
718
+ var updateBody = {};
719
+ for (var _e = 0, _f = Object.keys(rest); _e < _f.length; _e++) {
720
+ var k = _f[_e];
721
+ // Skip special request keys if any slipped through
722
+ if (specialKeys.has(k))
723
+ continue;
724
+ var shortKey = resolveShortKey(k);
725
+ if (pkShorts.includes(shortKey))
726
+ continue; // don't update PK columns (short or fully qualified)
727
+ updateBody[shortKey] = rest[k];
728
+ }
729
+ if (Object.keys(updateBody).length === 0) {
730
+ throw new Error("Singular PUT request for table (".concat(restModel.TABLE_NAME, ") must include at least one non-primary field to update."));
731
+ }
732
+ var normalized = (_b = {},
733
+ _b[C6Constants.UPDATE] = updateBody,
734
+ _b.WHERE = __assign({}, pkValues),
735
+ _b);
736
+ return __assign(__assign({}, normalized), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
737
+ }
738
+
623
739
  var HttpExecutor = /** @class */ (function (_super) {
624
740
  __extends(HttpExecutor, _super);
625
741
  function HttpExecutor() {
@@ -681,7 +797,6 @@ var HttpExecutor = /** @class */ (function (_super) {
681
797
  switch (_b.label) {
682
798
  case 0:
683
799
  _a = this.config, C6 = _a.C6, axios = _a.axios, restURL = _a.restURL, withCredentials = _a.withCredentials, restModel = _a.restModel, reactBootstrap = _a.reactBootstrap, requestMethod = _a.requestMethod, skipPrimaryCheck = _a.skipPrimaryCheck, clearCache = _a.clearCache;
684
- console.log('this.config', this.config);
685
800
  return [4 /*yield*/, this.runLifecycleHooks("beforeProcessing", {
686
801
  config: this.config,
687
802
  request: this.request,
@@ -727,7 +842,7 @@ var HttpExecutor = /** @class */ (function (_super) {
727
842
  query[C6.PAGINATION][C6.LIMIT] = query[C6.PAGINATION][C6.LIMIT] || 100;
728
843
  }
729
844
  apiRequest = function () { return __awaiter(_this, void 0, void 0, function () {
730
- var _a, debug, _b, cacheResults, dataInsertMultipleRows, success, _c, fetchDependencies, _d, error, querySerialized, cacheResult, cachingConfirmed, cacheCheck, cacheCheck, addBackPK, apiResponse, returnGetNextPageFunction, restRequestUri, needsConditionOrPrimaryCheck, TABLES, primaryKey, removedPkValue_1, axiosActiveRequest;
845
+ var _a, debug, _b, cacheResults, dataInsertMultipleRows, success, _c, fetchDependencies, _d, error, querySerialized, cacheResult, cachingConfirmed, cacheCheck, cacheCheck, addBackPK, removedPrimaryKV, apiResponse, returnGetNextPageFunction, restRequestUri, needsConditionOrPrimaryCheck, TABLES, primaryKey, removedPkValue_1, axiosActiveRequest;
731
846
  var _e;
732
847
  var _this = this;
733
848
  var _f, _g, _h, _j, _k, _l;
@@ -847,6 +962,7 @@ var HttpExecutor = /** @class */ (function (_super) {
847
962
  && primaryKey in query) {
848
963
  restRequestUri += query[primaryKey] + '/';
849
964
  removedPkValue_1 = query[primaryKey];
965
+ removedPrimaryKV = { key: primaryKey, value: removedPkValue_1 };
850
966
  addBackPK = function () {
851
967
  query !== null && query !== void 0 ? query : (query = {});
852
968
  query[primaryKey] = removedPkValue_1;
@@ -875,8 +991,8 @@ var HttpExecutor = /** @class */ (function (_super) {
875
991
  var baseConfig = {
876
992
  withCredentials: withCredentials,
877
993
  };
878
- // Pass-through request; singular normalization occurs on the backend (SqlExecutor)
879
- var normalizedQuery = query;
994
+ // Normalize singular request (GET/PUT/DELETE) into complex ORM shape
995
+ var normalizedQuery = normalizeSingularRequest(requestMethod, query, restModel, removedPrimaryKV);
880
996
  switch (requestMethod) {
881
997
  case GET:
882
998
  return [__assign(__assign({}, baseConfig), { params: normalizedQuery })];
@@ -1079,7 +1195,7 @@ var HttpExecutor = /** @class */ (function (_super) {
1079
1195
  accumulator.push(row['entity_tag']);
1080
1196
  }
1081
1197
  return accumulator;
1082
- }, []).map(function (entityTag) { return entityTag.split('\\').pop().toLowerCase(); });
1198
+ }, []).map(function (entityTag) { var _a, _b; return (_b = (_a = entityTag.split('\\')) === null || _a === void 0 ? void 0 : _a.pop()) === null || _b === void 0 ? void 0 : _b.toLowerCase(); });
1083
1199
  var shouldContinue = referencesTables.find(function (referencesTable) { return tableToFetch.endsWith(referencesTable); });
1084
1200
  if (!shouldContinue) {
1085
1201
  console.log('%c C6ENTITY: The constraintTableName (' + tableToFetch + ') did not end with any value in referencesTables', 'color: #c00', referencesTables);
@@ -1762,122 +1878,6 @@ var UpdateQueryBuilder = /** @class */ (function (_super) {
1762
1878
  return UpdateQueryBuilder;
1763
1879
  }(PaginationBuilder));
1764
1880
 
1765
- /**
1766
- * Converts a singular T-shaped request into complex ORM format for GET/PUT/DELETE
1767
- * Enforces that all primary keys are present for singular syntax and that the table has PKs.
1768
- * Optionally accepts a previously removed primary key (key/value) to reconstruct WHERE.
1769
- */
1770
- function normalizeSingularRequest(requestMethod, request, restModel, removedPrimary) {
1771
- var _a, _b;
1772
- var _c;
1773
- if (request == null || typeof request !== 'object')
1774
- return request;
1775
- var specialKeys = new Set([
1776
- C6Constants.SELECT,
1777
- C6Constants.UPDATE,
1778
- C6Constants.DELETE,
1779
- C6Constants.WHERE,
1780
- C6Constants.JOIN,
1781
- C6Constants.PAGINATION,
1782
- ]);
1783
- // Determine if the request is already complex (has any special key besides PAGINATION)
1784
- var keys = Object.keys(request);
1785
- var hasComplexKeys = keys.some(function (k) { return k !== C6Constants.PAGINATION && specialKeys.has(k); });
1786
- if (hasComplexKeys)
1787
- return request; // already complex
1788
- // We treat it as singular when it's not complex.
1789
- // For GET, PUT, DELETE only
1790
- if (!(requestMethod === C6Constants.GET || requestMethod === C6Constants.PUT || requestMethod === C6Constants.DELETE)) {
1791
- return request;
1792
- }
1793
- var pkShorts = Array.isArray(restModel.PRIMARY_SHORT) ? __spreadArray([], restModel.PRIMARY_SHORT, true) : [];
1794
- var pkFulls = Array.isArray(restModel.PRIMARY) ? __spreadArray([], restModel.PRIMARY, true) : [];
1795
- var resolveShortKey = function (key) {
1796
- var _a;
1797
- var cols = restModel.COLUMNS || {};
1798
- return (_a = cols[key]) !== null && _a !== void 0 ? _a : key;
1799
- };
1800
- if (!pkShorts.length) {
1801
- // For GET requests, do not enforce primary key presence; treat as a collection query.
1802
- if (requestMethod === C6Constants.GET)
1803
- return request;
1804
- throw new Error("Table (".concat(restModel.TABLE_NAME, ") has no primary key; singular request syntax is not allowed."));
1805
- }
1806
- // Build pk map from request + possibly removed primary key (accept short or fully-qualified keys)
1807
- var pkValues = {};
1808
- var requestObj = request;
1809
- var _loop_1 = function (pkShort) {
1810
- // 1) direct short key
1811
- var value = requestObj[pkShort];
1812
- if (value === undefined) {
1813
- // 2) fully-qualified key matching this short key (from PRIMARY list or by concatenation)
1814
- var fqCandidate_1 = "".concat(restModel.TABLE_NAME, ".").concat(pkShort);
1815
- var fqKey = (_c = pkFulls.find(function (fq) { return fq === fqCandidate_1 || fq.endsWith(".".concat(pkShort)); })) !== null && _c !== void 0 ? _c : fqCandidate_1;
1816
- value = requestObj[fqKey];
1817
- }
1818
- if (value === undefined && removedPrimary) {
1819
- // 3) removedPrimary may provide either short or fully-qualified key
1820
- var removedKeyShort = resolveShortKey(removedPrimary.key);
1821
- if (removedKeyShort === pkShort)
1822
- value = removedPrimary.value;
1823
- }
1824
- if (value !== undefined && value !== null) {
1825
- pkValues[pkShort] = value;
1826
- }
1827
- };
1828
- for (var _i = 0, pkShorts_1 = pkShorts; _i < pkShorts_1.length; _i++) {
1829
- var pkShort = pkShorts_1[_i];
1830
- _loop_1(pkShort);
1831
- }
1832
- var missing = pkShorts.filter(function (pk) { return !(pk in pkValues); });
1833
- if (missing.length) {
1834
- // For GET requests, if not all PKs are provided, treat as a collection query and leave as-is.
1835
- if (requestMethod === C6Constants.GET) {
1836
- return request;
1837
- }
1838
- throw new Error("Singular request requires all primary key(s) [".concat(pkShorts.join(', '), "] for table (").concat(restModel.TABLE_NAME, "). Missing: [").concat(missing.join(', '), "]"));
1839
- }
1840
- // Strip API metadata that should remain at root
1841
- var _d = request, dataInsertMultipleRows = _d.dataInsertMultipleRows, cacheResults = _d.cacheResults, fetchDependencies = _d.fetchDependencies, debug = _d.debug, success = _d.success, error = _d.error, rest = __rest(_d, ["dataInsertMultipleRows", "cacheResults", "fetchDependencies", "debug", "success", "error"]);
1842
- if (requestMethod === C6Constants.GET) {
1843
- var normalized_1 = {
1844
- WHERE: __assign({}, pkValues),
1845
- };
1846
- // Preserve pagination if any was added previously
1847
- if (request[C6Constants.PAGINATION]) {
1848
- normalized_1[C6Constants.PAGINATION] = request[C6Constants.PAGINATION];
1849
- }
1850
- return __assign(__assign({}, normalized_1), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
1851
- }
1852
- if (requestMethod === C6Constants.DELETE) {
1853
- var normalized_2 = (_a = {},
1854
- _a[C6Constants.DELETE] = true,
1855
- _a.WHERE = __assign({}, pkValues),
1856
- _a);
1857
- return __assign(__assign({}, normalized_2), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
1858
- }
1859
- // PUT
1860
- var updateBody = {};
1861
- for (var _e = 0, _f = Object.keys(rest); _e < _f.length; _e++) {
1862
- var k = _f[_e];
1863
- // Skip special request keys if any slipped through
1864
- if (specialKeys.has(k))
1865
- continue;
1866
- var shortKey = resolveShortKey(k);
1867
- if (pkShorts.includes(shortKey))
1868
- continue; // don't update PK columns (short or fully qualified)
1869
- updateBody[shortKey] = rest[k];
1870
- }
1871
- if (Object.keys(updateBody).length === 0) {
1872
- throw new Error("Singular PUT request for table (".concat(restModel.TABLE_NAME, ") must include at least one non-primary field to update."));
1873
- }
1874
- var normalized = (_b = {},
1875
- _b[C6Constants.UPDATE] = updateBody,
1876
- _b.WHERE = __assign({}, pkValues),
1877
- _b);
1878
- return __assign(__assign({}, normalized), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
1879
- }
1880
-
1881
1881
  var SqlExecutor = /** @class */ (function (_super) {
1882
1882
  __extends(SqlExecutor, _super);
1883
1883
  function SqlExecutor() {