@carbonorm/carbonnode 3.7.11 → 3.7.13
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.cjs.js +119 -100
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +120 -101
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/normalizeSingularRequest.test.ts +30 -0
- package/src/__tests__/sakila-db/C6.js +1 -1
- package/src/__tests__/sakila-db/C6.ts +1 -1
- package/src/api/executors/HttpExecutor.ts +2 -10
- package/src/api/utils/normalizeSingularRequest.ts +25 -11
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
|
|
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';
|
|
@@ -590,102 +590,6 @@ function removeInvalidKeys(request, c6Tables) {
|
|
|
590
590
|
return intersection;
|
|
591
591
|
}
|
|
592
592
|
|
|
593
|
-
/**
|
|
594
|
-
* Converts a singular T-shaped request into complex ORM format for GET/PUT/DELETE
|
|
595
|
-
* Enforces that all primary keys are present for singular syntax and that the table has PKs.
|
|
596
|
-
* Optionally accepts a previously removed primary key (key/value) to reconstruct WHERE.
|
|
597
|
-
*/
|
|
598
|
-
function normalizeSingularRequest(requestMethod, request, restModel, removedPrimary) {
|
|
599
|
-
var _a, _b;
|
|
600
|
-
if (request == null || typeof request !== 'object')
|
|
601
|
-
return request;
|
|
602
|
-
var specialKeys = new Set([
|
|
603
|
-
C6Constants.SELECT,
|
|
604
|
-
C6Constants.UPDATE,
|
|
605
|
-
C6Constants.DELETE,
|
|
606
|
-
C6Constants.WHERE,
|
|
607
|
-
C6Constants.JOIN,
|
|
608
|
-
C6Constants.PAGINATION,
|
|
609
|
-
]);
|
|
610
|
-
// Determine if the request is already complex (has any special key besides PAGINATION)
|
|
611
|
-
var keys = Object.keys(request);
|
|
612
|
-
var hasComplexKeys = keys.some(function (k) { return k !== C6Constants.PAGINATION && specialKeys.has(k); });
|
|
613
|
-
if (hasComplexKeys)
|
|
614
|
-
return request; // already complex
|
|
615
|
-
// We treat it as singular when it's not complex.
|
|
616
|
-
// For GET, PUT, DELETE only
|
|
617
|
-
if (!(requestMethod === C6Constants.GET || requestMethod === C6Constants.PUT || requestMethod === C6Constants.DELETE)) {
|
|
618
|
-
return request;
|
|
619
|
-
}
|
|
620
|
-
var pkShorts = Array.isArray(restModel.PRIMARY_SHORT) ? __spreadArray([], restModel.PRIMARY_SHORT, true) : [];
|
|
621
|
-
if (!pkShorts.length) {
|
|
622
|
-
// For GET requests, do not enforce primary key presence; treat as a collection query.
|
|
623
|
-
if (requestMethod === C6Constants.GET)
|
|
624
|
-
return request;
|
|
625
|
-
throw new Error("Table (".concat(restModel.TABLE_NAME, ") has no primary key; singular request syntax is not allowed."));
|
|
626
|
-
}
|
|
627
|
-
// Build pk map from request + possibly removed primary key
|
|
628
|
-
var pkValues = {};
|
|
629
|
-
for (var _i = 0, pkShorts_1 = pkShorts; _i < pkShorts_1.length; _i++) {
|
|
630
|
-
var pk = pkShorts_1[_i];
|
|
631
|
-
var fromRequest = request[pk];
|
|
632
|
-
if (fromRequest !== undefined && fromRequest !== null) {
|
|
633
|
-
pkValues[pk] = fromRequest;
|
|
634
|
-
continue;
|
|
635
|
-
}
|
|
636
|
-
if (removedPrimary && removedPrimary.key === pk) {
|
|
637
|
-
pkValues[pk] = removedPrimary.value;
|
|
638
|
-
continue;
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
var missing = pkShorts.filter(function (pk) { return !(pk in pkValues); });
|
|
642
|
-
if (missing.length) {
|
|
643
|
-
// For GET requests, if not all PKs are provided, treat as a collection query and leave as-is.
|
|
644
|
-
if (requestMethod === C6Constants.GET) {
|
|
645
|
-
return request;
|
|
646
|
-
}
|
|
647
|
-
throw new Error("Singular request requires all primary key(s) [".concat(pkShorts.join(', '), "] for table (").concat(restModel.TABLE_NAME, "). Missing: [").concat(missing.join(', '), "]"));
|
|
648
|
-
}
|
|
649
|
-
// Strip API metadata that should remain at root
|
|
650
|
-
var _c = request, dataInsertMultipleRows = _c.dataInsertMultipleRows, cacheResults = _c.cacheResults, fetchDependencies = _c.fetchDependencies, debug = _c.debug, success = _c.success, error = _c.error, rest = __rest(_c, ["dataInsertMultipleRows", "cacheResults", "fetchDependencies", "debug", "success", "error"]);
|
|
651
|
-
if (requestMethod === C6Constants.GET) {
|
|
652
|
-
var normalized_1 = {
|
|
653
|
-
WHERE: __assign({}, pkValues),
|
|
654
|
-
};
|
|
655
|
-
// Preserve pagination if any was added previously
|
|
656
|
-
if (request[C6Constants.PAGINATION]) {
|
|
657
|
-
normalized_1[C6Constants.PAGINATION] = request[C6Constants.PAGINATION];
|
|
658
|
-
}
|
|
659
|
-
return __assign(__assign({}, normalized_1), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
660
|
-
}
|
|
661
|
-
if (requestMethod === C6Constants.DELETE) {
|
|
662
|
-
var normalized_2 = (_a = {},
|
|
663
|
-
_a[C6Constants.DELETE] = true,
|
|
664
|
-
_a.WHERE = __assign({}, pkValues),
|
|
665
|
-
_a);
|
|
666
|
-
return __assign(__assign({}, normalized_2), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
667
|
-
}
|
|
668
|
-
// PUT
|
|
669
|
-
var updateBody = {};
|
|
670
|
-
for (var _d = 0, _e = Object.keys(rest); _d < _e.length; _d++) {
|
|
671
|
-
var k = _e[_d];
|
|
672
|
-
if (pkShorts.includes(k))
|
|
673
|
-
continue; // don't update PK columns
|
|
674
|
-
// Skip special request keys if any slipped through
|
|
675
|
-
if (specialKeys.has(k))
|
|
676
|
-
continue;
|
|
677
|
-
updateBody[k] = rest[k];
|
|
678
|
-
}
|
|
679
|
-
if (Object.keys(updateBody).length === 0) {
|
|
680
|
-
throw new Error("Singular PUT request for table (".concat(restModel.TABLE_NAME, ") must include at least one non-primary field to update."));
|
|
681
|
-
}
|
|
682
|
-
var normalized = (_b = {},
|
|
683
|
-
_b[C6Constants.UPDATE] = updateBody,
|
|
684
|
-
_b.WHERE = __assign({}, pkValues),
|
|
685
|
-
_b);
|
|
686
|
-
return __assign(__assign({}, normalized), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
687
|
-
}
|
|
688
|
-
|
|
689
593
|
// do not remove entries from this array. It is used to track the progress of API requests.
|
|
690
594
|
// position in array is important. Do not sort. To not add to begging.
|
|
691
595
|
var apiRequestCache = [];
|
|
@@ -823,7 +727,7 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
823
727
|
query[C6.PAGINATION][C6.LIMIT] = query[C6.PAGINATION][C6.LIMIT] || 100;
|
|
824
728
|
}
|
|
825
729
|
apiRequest = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
826
|
-
var _a, debug, _b, cacheResults, dataInsertMultipleRows, success, _c, fetchDependencies, _d, error, querySerialized, cacheResult, cachingConfirmed, cacheCheck, cacheCheck, addBackPK,
|
|
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;
|
|
827
731
|
var _e;
|
|
828
732
|
var _this = this;
|
|
829
733
|
var _f, _g, _h, _j, _k, _l;
|
|
@@ -943,7 +847,6 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
943
847
|
&& primaryKey in query) {
|
|
944
848
|
restRequestUri += query[primaryKey] + '/';
|
|
945
849
|
removedPkValue_1 = query[primaryKey];
|
|
946
|
-
removedPrimaryKV = { key: primaryKey, value: removedPkValue_1 };
|
|
947
850
|
addBackPK = function () {
|
|
948
851
|
query !== null && query !== void 0 ? query : (query = {});
|
|
949
852
|
query[primaryKey] = removedPkValue_1;
|
|
@@ -972,8 +875,8 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
972
875
|
var baseConfig = {
|
|
973
876
|
withCredentials: withCredentials,
|
|
974
877
|
};
|
|
975
|
-
//
|
|
976
|
-
var normalizedQuery =
|
|
878
|
+
// Pass-through request; singular normalization occurs on the backend (SqlExecutor)
|
|
879
|
+
var normalizedQuery = query;
|
|
977
880
|
switch (requestMethod) {
|
|
978
881
|
case GET:
|
|
979
882
|
return [__assign(__assign({}, baseConfig), { params: normalizedQuery })];
|
|
@@ -1859,6 +1762,122 @@ var UpdateQueryBuilder = /** @class */ (function (_super) {
|
|
|
1859
1762
|
return UpdateQueryBuilder;
|
|
1860
1763
|
}(PaginationBuilder));
|
|
1861
1764
|
|
|
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
|
+
|
|
1862
1881
|
var SqlExecutor = /** @class */ (function (_super) {
|
|
1863
1882
|
__extends(SqlExecutor, _super);
|
|
1864
1883
|
function SqlExecutor() {
|