@carbonorm/carbonnode 3.7.16 → 3.7.17

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, __spreadArray, __rest, __extends } from 'tslib';
3
+ import { __assign, __awaiter, __generator, __extends, __spreadArray, __rest } 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,122 +620,6 @@ 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
-
739
623
  var HttpExecutor = /** @class */ (function (_super) {
740
624
  __extends(HttpExecutor, _super);
741
625
  function HttpExecutor() {
@@ -842,7 +726,7 @@ var HttpExecutor = /** @class */ (function (_super) {
842
726
  query[C6.PAGINATION][C6.LIMIT] = query[C6.PAGINATION][C6.LIMIT] || 100;
843
727
  }
844
728
  apiRequest = function () { return __awaiter(_this, void 0, void 0, function () {
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;
729
+ var _a, debug, _b, cacheResults, dataInsertMultipleRows, success, _c, fetchDependencies, _d, error, querySerialized, cacheResult, cachingConfirmed, cacheCheck, cacheCheck, apiResponse, returnGetNextPageFunction, restRequestUri, needsConditionOrPrimaryCheck, TABLES, primaryKey, axiosActiveRequest;
846
730
  var _e;
847
731
  var _this = this;
848
732
  var _f, _g, _h, _j, _k, _l;
@@ -961,14 +845,7 @@ var HttpExecutor = /** @class */ (function (_super) {
961
845
  && undefined !== primaryKey
962
846
  && primaryKey in query) {
963
847
  restRequestUri += query[primaryKey] + '/';
964
- removedPkValue_1 = query[primaryKey];
965
- removedPrimaryKV = { key: primaryKey, value: removedPkValue_1 };
966
- addBackPK = function () {
967
- query !== null && query !== void 0 ? query : (query = {});
968
- query[primaryKey] = removedPkValue_1;
969
- };
970
- delete query[primaryKey];
971
- console.log('query', query, 'primaryKey', primaryKey, 'removedPkValue', removedPkValue_1);
848
+ console.log('query', query, 'primaryKey', primaryKey);
972
849
  }
973
850
  else {
974
851
  console.log('query', query);
@@ -991,11 +868,9 @@ var HttpExecutor = /** @class */ (function (_super) {
991
868
  var baseConfig = {
992
869
  withCredentials: withCredentials,
993
870
  };
994
- // Normalize singular request (GET/PUT/DELETE) into complex ORM shape
995
- var normalizedQuery = normalizeSingularRequest(requestMethod, query, restModel, removedPrimaryKV);
996
871
  switch (requestMethod) {
997
872
  case GET:
998
- return [__assign(__assign({}, baseConfig), { params: normalizedQuery })];
873
+ return [__assign(__assign({}, baseConfig), { params: query })];
999
874
  case POST:
1000
875
  if (dataInsertMultipleRows !== undefined) {
1001
876
  return [
@@ -1005,9 +880,9 @@ var HttpExecutor = /** @class */ (function (_super) {
1005
880
  }
1006
881
  return [convert(query), baseConfig];
1007
882
  case PUT:
1008
- return [convert(normalizedQuery), baseConfig];
883
+ return [convert(query), baseConfig];
1009
884
  case DELETE:
1010
- return [__assign(__assign({}, baseConfig), { data: convert(normalizedQuery) })];
885
+ return [__assign(__assign({}, baseConfig), { data: convert(query) })];
1011
886
  default:
1012
887
  throw new Error("The request method (".concat(requestMethod, ") was not recognized."));
1013
888
  }
@@ -1019,9 +894,6 @@ var HttpExecutor = /** @class */ (function (_super) {
1019
894
  request: axiosActiveRequest
1020
895
  });
1021
896
  }
1022
- // todo - wip verify this works
1023
- // we had removed the value from the request to add to the URI.
1024
- addBackPK === null || addBackPK === void 0 ? void 0 : addBackPK(); // adding back so post-processing methods work
1025
897
  // returning the promise with this then is important for tests. todo - we could make that optional.
1026
898
  // https://rapidapi.com/guides/axios-async-await
1027
899
  return [2 /*return*/, axiosActiveRequest.then(function (response) { return __awaiter(_this, void 0, void 0, function () {
@@ -1878,6 +1750,133 @@ var UpdateQueryBuilder = /** @class */ (function (_super) {
1878
1750
  return UpdateQueryBuilder;
1879
1751
  }(PaginationBuilder));
1880
1752
 
1753
+ /**
1754
+ * Converts a singular T-shaped request into complex ORM format for GET/PUT/DELETE
1755
+ * Enforces that all primary keys are present for singular syntax and that the table has PKs.
1756
+ * Optionally accepts a previously removed primary key (key/value) to reconstruct WHERE.
1757
+ */
1758
+ function normalizeSingularRequest(requestMethod, request, restModel, removedPrimary) {
1759
+ var _a, _b;
1760
+ var _c;
1761
+ if (request == null || typeof request !== 'object')
1762
+ return request;
1763
+ var specialKeys = new Set([
1764
+ C6Constants.SELECT,
1765
+ C6Constants.UPDATE,
1766
+ C6Constants.DELETE,
1767
+ C6Constants.WHERE,
1768
+ C6Constants.JOIN,
1769
+ C6Constants.PAGINATION,
1770
+ ]);
1771
+ // Determine if the request is already complex (has any special key besides PAGINATION)
1772
+ var keys = Object.keys(request);
1773
+ var hasComplexKeys = keys.some(function (k) { return k !== C6Constants.PAGINATION && specialKeys.has(k); });
1774
+ if (hasComplexKeys)
1775
+ return request; // already complex
1776
+ // We treat it as singular when it's not complex.
1777
+ // For GET, PUT, DELETE only
1778
+ if (!(requestMethod === C6Constants.GET || requestMethod === C6Constants.PUT || requestMethod === C6Constants.DELETE)) {
1779
+ return request;
1780
+ }
1781
+ var pkShorts = Array.isArray(restModel.PRIMARY_SHORT) ? __spreadArray([], restModel.PRIMARY_SHORT, true) : [];
1782
+ var pkFulls = Array.isArray(restModel.PRIMARY) ? __spreadArray([], restModel.PRIMARY, true) : [];
1783
+ var resolveShortKey = function (key) {
1784
+ var _a;
1785
+ var cols = restModel.COLUMNS || {};
1786
+ return (_a = cols[key]) !== null && _a !== void 0 ? _a : key;
1787
+ };
1788
+ if (!pkShorts.length) {
1789
+ // For GET requests, do not enforce primary key presence; treat as a collection query.
1790
+ if (requestMethod === C6Constants.GET)
1791
+ return request;
1792
+ throw new Error("Table (".concat(restModel.TABLE_NAME, ") has no primary key; singular request syntax is not allowed."));
1793
+ }
1794
+ // Build pk map from request + possibly removed primary key (accept short or fully-qualified keys)
1795
+ var pkValues = {};
1796
+ var requestObj = request;
1797
+ var _loop_1 = function (pkShort) {
1798
+ // 1) direct short key
1799
+ var value = requestObj[pkShort];
1800
+ if (value === undefined) {
1801
+ // 2) fully-qualified key matching this short key (from PRIMARY list or by concatenation)
1802
+ var fqCandidate_1 = "".concat(restModel.TABLE_NAME, ".").concat(pkShort);
1803
+ var fqKey = (_c = pkFulls.find(function (fq) { return fq === fqCandidate_1 || fq.endsWith(".".concat(pkShort)); })) !== null && _c !== void 0 ? _c : fqCandidate_1;
1804
+ value = requestObj[fqKey];
1805
+ }
1806
+ if (value === undefined && removedPrimary) {
1807
+ // 3) removedPrimary may provide either short or fully-qualified key
1808
+ var removedKeyShort = resolveShortKey(removedPrimary.key);
1809
+ if (removedKeyShort === pkShort)
1810
+ value = removedPrimary.value;
1811
+ }
1812
+ if (value !== undefined && value !== null) {
1813
+ pkValues[pkShort] = value;
1814
+ }
1815
+ };
1816
+ for (var _i = 0, pkShorts_1 = pkShorts; _i < pkShorts_1.length; _i++) {
1817
+ var pkShort = pkShorts_1[_i];
1818
+ _loop_1(pkShort);
1819
+ }
1820
+ var missing = pkShorts.filter(function (pk) { return !(pk in pkValues); });
1821
+ if (missing.length) {
1822
+ // For GET requests, if not all PKs are provided, treat as a collection query and leave as-is.
1823
+ if (requestMethod === C6Constants.GET) {
1824
+ return request;
1825
+ }
1826
+ throw new Error("Singular request requires all primary key(s) [".concat(pkShorts.join(', '), "] for table (").concat(restModel.TABLE_NAME, "). Missing: [").concat(missing.join(', '), "]"));
1827
+ }
1828
+ // Strip API metadata that should remain at root
1829
+ 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"]);
1830
+ // Map short primary keys to fully-qualified column names
1831
+ var shortToFull = {};
1832
+ for (var _e = 0, _f = Object.entries(restModel.COLUMNS || {}); _e < _f.length; _e++) {
1833
+ var _g = _f[_e], full = _g[0], short = _g[1];
1834
+ shortToFull[short] = full;
1835
+ }
1836
+ var pkFullValues = Object.fromEntries(Object.entries(pkValues).map(function (_a) {
1837
+ var _b;
1838
+ var k = _a[0], v = _a[1];
1839
+ return [(_b = shortToFull[k]) !== null && _b !== void 0 ? _b : k, v];
1840
+ }));
1841
+ if (requestMethod === C6Constants.GET) {
1842
+ var normalized_1 = {
1843
+ WHERE: __assign({}, pkFullValues),
1844
+ };
1845
+ // Preserve pagination if any was added previously
1846
+ if (request[C6Constants.PAGINATION]) {
1847
+ normalized_1[C6Constants.PAGINATION] = request[C6Constants.PAGINATION];
1848
+ }
1849
+ return __assign(__assign({}, normalized_1), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
1850
+ }
1851
+ if (requestMethod === C6Constants.DELETE) {
1852
+ var normalized_2 = (_a = {},
1853
+ _a[C6Constants.DELETE] = true,
1854
+ _a.WHERE = __assign({}, pkFullValues),
1855
+ _a);
1856
+ return __assign(__assign({}, normalized_2), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
1857
+ }
1858
+ // PUT
1859
+ var updateBody = {};
1860
+ for (var _h = 0, _j = Object.keys(rest); _h < _j.length; _h++) {
1861
+ var k = _j[_h];
1862
+ // Skip special request keys if any slipped through
1863
+ if (specialKeys.has(k))
1864
+ continue;
1865
+ var shortKey = resolveShortKey(k);
1866
+ if (pkShorts.includes(shortKey))
1867
+ continue; // don't update PK columns (short or fully qualified)
1868
+ updateBody[shortKey] = rest[k];
1869
+ }
1870
+ if (Object.keys(updateBody).length === 0) {
1871
+ throw new Error("Singular PUT request for table (".concat(restModel.TABLE_NAME, ") must include at least one non-primary field to update."));
1872
+ }
1873
+ var normalized = (_b = {},
1874
+ _b[C6Constants.UPDATE] = updateBody,
1875
+ _b.WHERE = __assign({}, pkFullValues),
1876
+ _b);
1877
+ return __assign(__assign({}, normalized), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
1878
+ }
1879
+
1881
1880
  var SqlExecutor = /** @class */ (function (_super) {
1882
1881
  __extends(SqlExecutor, _super);
1883
1882
  function SqlExecutor() {
@@ -2065,12 +2064,12 @@ function ExpressHandler(_a) {
2065
2064
  var _this = this;
2066
2065
  var C6 = _a.C6, mysqlPool = _a.mysqlPool;
2067
2066
  return function (req, res, next) { return __awaiter(_this, void 0, void 0, function () {
2068
- var method, table, primary, payload, primaryKeys, primaryKeyName, primaryKeyShort, response, err_1;
2069
- var _a, _b, _c, _d;
2070
- return __generator(this, function (_e) {
2071
- switch (_e.label) {
2067
+ var method, table, primary, payload, primaryKeys, primaryKeyName, response, err_1;
2068
+ var _a, _b;
2069
+ return __generator(this, function (_c) {
2070
+ switch (_c.label) {
2072
2071
  case 0:
2073
- _e.trys.push([0, 2, , 3]);
2072
+ _c.trys.push([0, 2, , 3]);
2074
2073
  method = req.method.toUpperCase();
2075
2074
  table = req.params.table;
2076
2075
  primary = req.params.primary;
@@ -2091,30 +2090,17 @@ function ExpressHandler(_a) {
2091
2090
  return [2 /*return*/];
2092
2091
  }
2093
2092
  primaryKeyName = primaryKeys[0];
2094
- primaryKeyShort = primaryKeyName.split('.')[1];
2095
- if (!((_b = (_a = payload[C6C.WHERE]) === null || _a === void 0 ? void 0 : _a[primaryKeyName]) !== null && _b !== void 0 ? _b : undefined)
2096
- && !((_d = (_c = payload[C6C.WHERE]) === null || _c === void 0 ? void 0 : _c[primaryKeyShort]) !== null && _d !== void 0 ? _d : undefined)) {
2097
- // 👇 Call restRequest for the resolved method
2098
- switch (method) {
2099
- case 'GET':
2100
- if (primary) {
2101
- payload[C6C.WHERE][primaryKeyName] = primary;
2102
- }
2103
- break;
2104
- case 'PUT':
2105
- case 'DELETE':
2106
- if (primary) {
2107
- payload[C6C.WHERE][primaryKeyName] = primary;
2108
- }
2109
- else {
2110
- res.status(400).json({ error: "Invalid request: ".concat(method, " requires a primary key (").concat(primaryKeyName, ").") });
2111
- }
2112
- break;
2113
- case 'POST':
2114
- break;
2115
- default:
2116
- res.status(405).json({ error: "Method ".concat(method, " not allowed") });
2117
- return [2 /*return*/];
2093
+ // If a primary key was provided in the URL, merge it into the payload.
2094
+ // Support both complex requests using WHERE and singular requests
2095
+ // where the primary key lives at the root of the payload.
2096
+ if (primary) {
2097
+ if (payload[C6C.WHERE]) {
2098
+ payload[C6C.WHERE][primaryKeyName] =
2099
+ (_a = payload[C6C.WHERE][primaryKeyName]) !== null && _a !== void 0 ? _a : primary;
2100
+ }
2101
+ else {
2102
+ payload[primaryKeyName] =
2103
+ (_b = payload[primaryKeyName]) !== null && _b !== void 0 ? _b : primary;
2118
2104
  }
2119
2105
  }
2120
2106
  return [4 /*yield*/, restRequest({
@@ -2124,11 +2110,11 @@ function ExpressHandler(_a) {
2124
2110
  restModel: C6.TABLES[table]
2125
2111
  })(payload)];
2126
2112
  case 1:
2127
- response = _e.sent();
2113
+ response = _c.sent();
2128
2114
  res.status(200).json(__assign({ success: true }, response));
2129
2115
  return [3 /*break*/, 3];
2130
2116
  case 2:
2131
- err_1 = _e.sent();
2117
+ err_1 = _c.sent();
2132
2118
  res.status(500).json({ success: false, error: err_1 });
2133
2119
  next(err_1);
2134
2120
  return [3 /*break*/, 3];