@carbonorm/carbonnode 3.7.16 → 3.7.18
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 +167 -174
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +168 -175
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/scripts/assets/handlebars/C6.test.ts.handlebars +27 -0
- package/src/__tests__/httpExecutorSingular.e2e.test.ts +81 -0
- package/src/__tests__/normalizeSingularRequest.test.ts +8 -8
- package/src/__tests__/sakila-db/C6.js +1 -1
- package/src/__tests__/sakila-db/C6.test.ts +27 -0
- package/src/__tests__/sakila-db/C6.ts +1 -1
- package/src/api/convertForRequestBody.ts +2 -2
- package/src/api/executors/HttpExecutor.ts +22 -42
- package/src/api/handlers/ExpressHandler.ts +10 -23
- package/src/api/utils/normalizeSingularRequest.ts +15 -4
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';
|
|
@@ -331,8 +331,8 @@ function convertForRequestBody (restfulObject, tableName, C6, regexErrorHandler)
|
|
|
331
331
|
}
|
|
332
332
|
}
|
|
333
333
|
}
|
|
334
|
-
else if (Object.
|
|
335
|
-
// Already a fully qualified column name
|
|
334
|
+
else if (Object.keys(tableDefinition.COLUMNS).includes(value)) {
|
|
335
|
+
// Already using a fully qualified column name
|
|
336
336
|
var columnValue = restfulObject[value];
|
|
337
337
|
payload[value] = columnValue;
|
|
338
338
|
var regexValidations = tableDefinition.REGEX_VALIDATION[value];
|
|
@@ -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,
|
|
729
|
+
var _a, debug, _b, cacheResults, dataInsertMultipleRows, success, _c, fetchDependencies, _d, error, querySerialized, cacheResult, cachingConfirmed, cacheCheck, cacheCheck, apiResponse, returnGetNextPageFunction, restRequestUri, needsConditionOrPrimaryCheck, TABLES, primaryKeyList, primaryKeyFullyQualified, primaryKey, providedPrimary, primaryVal, axiosActiveRequest;
|
|
846
730
|
var _e;
|
|
847
731
|
var _this = this;
|
|
848
732
|
var _f, _g, _h, _j, _k, _l;
|
|
@@ -923,7 +807,9 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
923
807
|
needsConditionOrPrimaryCheck = (PUT === requestMethod || DELETE === requestMethod)
|
|
924
808
|
&& false === skipPrimaryCheck;
|
|
925
809
|
TABLES = C6.TABLES;
|
|
926
|
-
|
|
810
|
+
primaryKeyList = structuredClone((_g = TABLES[operatingTable]) === null || _g === void 0 ? void 0 : _g.PRIMARY);
|
|
811
|
+
primaryKeyFullyQualified = primaryKeyList === null || primaryKeyList === void 0 ? void 0 : primaryKeyList.pop();
|
|
812
|
+
primaryKey = (_h = primaryKeyFullyQualified === null || primaryKeyFullyQualified === void 0 ? void 0 : primaryKeyFullyQualified.split('.')) === null || _h === void 0 ? void 0 : _h.pop();
|
|
927
813
|
if (needsConditionOrPrimaryCheck) {
|
|
928
814
|
if (undefined === primaryKey) {
|
|
929
815
|
if (null === query
|
|
@@ -933,20 +819,20 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
933
819
|
|| query[C6.WHERE].length === 0)
|
|
934
820
|
|| (Object.keys(query === null || query === void 0 ? void 0 : query[C6.WHERE]).length === 0)) {
|
|
935
821
|
console.error(query);
|
|
936
|
-
throw Error('Failed to parse primary key information. Query: (' + JSON.stringify(query) + ') Primary Key: (' + JSON.stringify(primaryKey) + ') TABLES[operatingTable]?.PRIMARY: (' + JSON.stringify((
|
|
822
|
+
throw Error('Failed to parse primary key information. Query: (' + JSON.stringify(query) + ') Primary Key: (' + JSON.stringify(primaryKey) + ') TABLES[operatingTable]?.PRIMARY: (' + JSON.stringify((_j = TABLES[operatingTable]) === null || _j === void 0 ? void 0 : _j.PRIMARY) + ') for operatingTable (' + operatingTable + ').');
|
|
937
823
|
}
|
|
938
824
|
}
|
|
939
825
|
else {
|
|
940
826
|
if (undefined === query
|
|
941
827
|
|| null === query
|
|
942
|
-
||
|
|
828
|
+
|| (!(primaryKey in query) && !(primaryKeyFullyQualified && primaryKeyFullyQualified in query))) {
|
|
943
829
|
if (true === debug && isLocal()) {
|
|
944
830
|
toast.error('DEVS: The primary key (' + primaryKey + ') was not provided!!');
|
|
945
831
|
}
|
|
946
832
|
throw Error('You must provide the primary key (' + primaryKey + ') for table (' + operatingTable + '). Request (' + JSON.stringify(this.request, undefined, 4) + ') Query (' + JSON.stringify(query) + ')');
|
|
947
833
|
}
|
|
948
|
-
|
|
949
|
-
|
|
834
|
+
providedPrimary = (_k = query === null || query === void 0 ? void 0 : query[primaryKey]) !== null && _k !== void 0 ? _k : (primaryKeyFullyQualified ? query === null || query === void 0 ? void 0 : query[primaryKeyFullyQualified] : undefined);
|
|
835
|
+
if (undefined === providedPrimary || null === providedPrimary) {
|
|
950
836
|
toast.error('The primary key (' + primaryKey + ') provided is undefined or null explicitly!!');
|
|
951
837
|
throw Error('The primary key (' + primaryKey + ') provided in the request was exactly equal to undefined.');
|
|
952
838
|
}
|
|
@@ -958,17 +844,15 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
958
844
|
if (POST !== requestMethod
|
|
959
845
|
&& undefined !== query
|
|
960
846
|
&& null !== query
|
|
961
|
-
&& undefined !== primaryKey
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
query
|
|
969
|
-
}
|
|
970
|
-
delete query[primaryKey];
|
|
971
|
-
console.log('query', query, 'primaryKey', primaryKey, 'removedPkValue', removedPkValue_1);
|
|
847
|
+
&& undefined !== primaryKey) {
|
|
848
|
+
primaryVal = (_l = query[primaryKey]) !== null && _l !== void 0 ? _l : (primaryKeyFullyQualified ? query[primaryKeyFullyQualified] : undefined);
|
|
849
|
+
if (undefined !== primaryVal) {
|
|
850
|
+
restRequestUri += primaryVal + '/';
|
|
851
|
+
console.log('query', query, 'primaryKey', primaryKey);
|
|
852
|
+
}
|
|
853
|
+
else {
|
|
854
|
+
console.log('query', query);
|
|
855
|
+
}
|
|
972
856
|
}
|
|
973
857
|
else {
|
|
974
858
|
console.log('query', query);
|
|
@@ -991,11 +875,9 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
991
875
|
var baseConfig = {
|
|
992
876
|
withCredentials: withCredentials,
|
|
993
877
|
};
|
|
994
|
-
// Normalize singular request (GET/PUT/DELETE) into complex ORM shape
|
|
995
|
-
var normalizedQuery = normalizeSingularRequest(requestMethod, query, restModel, removedPrimaryKV);
|
|
996
878
|
switch (requestMethod) {
|
|
997
879
|
case GET:
|
|
998
|
-
return [__assign(__assign({}, baseConfig), { params:
|
|
880
|
+
return [__assign(__assign({}, baseConfig), { params: query })];
|
|
999
881
|
case POST:
|
|
1000
882
|
if (dataInsertMultipleRows !== undefined) {
|
|
1001
883
|
return [
|
|
@@ -1005,9 +887,9 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
1005
887
|
}
|
|
1006
888
|
return [convert(query), baseConfig];
|
|
1007
889
|
case PUT:
|
|
1008
|
-
return [convert(
|
|
890
|
+
return [convert(query), baseConfig];
|
|
1009
891
|
case DELETE:
|
|
1010
|
-
return [__assign(__assign({}, baseConfig), { data: convert(
|
|
892
|
+
return [__assign(__assign({}, baseConfig), { data: convert(query) })];
|
|
1011
893
|
default:
|
|
1012
894
|
throw new Error("The request method (".concat(requestMethod, ") was not recognized."));
|
|
1013
895
|
}
|
|
@@ -1019,9 +901,6 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
1019
901
|
request: axiosActiveRequest
|
|
1020
902
|
});
|
|
1021
903
|
}
|
|
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
904
|
// returning the promise with this then is important for tests. todo - we could make that optional.
|
|
1026
905
|
// https://rapidapi.com/guides/axios-async-await
|
|
1027
906
|
return [2 /*return*/, axiosActiveRequest.then(function (response) { return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -1878,6 +1757,133 @@ var UpdateQueryBuilder = /** @class */ (function (_super) {
|
|
|
1878
1757
|
return UpdateQueryBuilder;
|
|
1879
1758
|
}(PaginationBuilder));
|
|
1880
1759
|
|
|
1760
|
+
/**
|
|
1761
|
+
* Converts a singular T-shaped request into complex ORM format for GET/PUT/DELETE
|
|
1762
|
+
* Enforces that all primary keys are present for singular syntax and that the table has PKs.
|
|
1763
|
+
* Optionally accepts a previously removed primary key (key/value) to reconstruct WHERE.
|
|
1764
|
+
*/
|
|
1765
|
+
function normalizeSingularRequest(requestMethod, request, restModel, removedPrimary) {
|
|
1766
|
+
var _a, _b;
|
|
1767
|
+
var _c;
|
|
1768
|
+
if (request == null || typeof request !== 'object')
|
|
1769
|
+
return request;
|
|
1770
|
+
var specialKeys = new Set([
|
|
1771
|
+
C6Constants.SELECT,
|
|
1772
|
+
C6Constants.UPDATE,
|
|
1773
|
+
C6Constants.DELETE,
|
|
1774
|
+
C6Constants.WHERE,
|
|
1775
|
+
C6Constants.JOIN,
|
|
1776
|
+
C6Constants.PAGINATION,
|
|
1777
|
+
]);
|
|
1778
|
+
// Determine if the request is already complex (has any special key besides PAGINATION)
|
|
1779
|
+
var keys = Object.keys(request);
|
|
1780
|
+
var hasComplexKeys = keys.some(function (k) { return k !== C6Constants.PAGINATION && specialKeys.has(k); });
|
|
1781
|
+
if (hasComplexKeys)
|
|
1782
|
+
return request; // already complex
|
|
1783
|
+
// We treat it as singular when it's not complex.
|
|
1784
|
+
// For GET, PUT, DELETE only
|
|
1785
|
+
if (!(requestMethod === C6Constants.GET || requestMethod === C6Constants.PUT || requestMethod === C6Constants.DELETE)) {
|
|
1786
|
+
return request;
|
|
1787
|
+
}
|
|
1788
|
+
var pkShorts = Array.isArray(restModel.PRIMARY_SHORT) ? __spreadArray([], restModel.PRIMARY_SHORT, true) : [];
|
|
1789
|
+
var pkFulls = Array.isArray(restModel.PRIMARY) ? __spreadArray([], restModel.PRIMARY, true) : [];
|
|
1790
|
+
var resolveShortKey = function (key) {
|
|
1791
|
+
var _a;
|
|
1792
|
+
var cols = restModel.COLUMNS || {};
|
|
1793
|
+
return (_a = cols[key]) !== null && _a !== void 0 ? _a : key;
|
|
1794
|
+
};
|
|
1795
|
+
if (!pkShorts.length) {
|
|
1796
|
+
// For GET requests, do not enforce primary key presence; treat as a collection query.
|
|
1797
|
+
if (requestMethod === C6Constants.GET)
|
|
1798
|
+
return request;
|
|
1799
|
+
throw new Error("Table (".concat(restModel.TABLE_NAME, ") has no primary key; singular request syntax is not allowed."));
|
|
1800
|
+
}
|
|
1801
|
+
// Build pk map from request + possibly removed primary key (accept short or fully-qualified keys)
|
|
1802
|
+
var pkValues = {};
|
|
1803
|
+
var requestObj = request;
|
|
1804
|
+
var _loop_1 = function (pkShort) {
|
|
1805
|
+
// 1) direct short key
|
|
1806
|
+
var value = requestObj[pkShort];
|
|
1807
|
+
if (value === undefined) {
|
|
1808
|
+
// 2) fully-qualified key matching this short key (from PRIMARY list or by concatenation)
|
|
1809
|
+
var fqCandidate_1 = "".concat(restModel.TABLE_NAME, ".").concat(pkShort);
|
|
1810
|
+
var fqKey = (_c = pkFulls.find(function (fq) { return fq === fqCandidate_1 || fq.endsWith(".".concat(pkShort)); })) !== null && _c !== void 0 ? _c : fqCandidate_1;
|
|
1811
|
+
value = requestObj[fqKey];
|
|
1812
|
+
}
|
|
1813
|
+
if (value === undefined && removedPrimary) {
|
|
1814
|
+
// 3) removedPrimary may provide either short or fully-qualified key
|
|
1815
|
+
var removedKeyShort = resolveShortKey(removedPrimary.key);
|
|
1816
|
+
if (removedKeyShort === pkShort)
|
|
1817
|
+
value = removedPrimary.value;
|
|
1818
|
+
}
|
|
1819
|
+
if (value !== undefined && value !== null) {
|
|
1820
|
+
pkValues[pkShort] = value;
|
|
1821
|
+
}
|
|
1822
|
+
};
|
|
1823
|
+
for (var _i = 0, pkShorts_1 = pkShorts; _i < pkShorts_1.length; _i++) {
|
|
1824
|
+
var pkShort = pkShorts_1[_i];
|
|
1825
|
+
_loop_1(pkShort);
|
|
1826
|
+
}
|
|
1827
|
+
var missing = pkShorts.filter(function (pk) { return !(pk in pkValues); });
|
|
1828
|
+
if (missing.length) {
|
|
1829
|
+
// For GET requests, if not all PKs are provided, treat as a collection query and leave as-is.
|
|
1830
|
+
if (requestMethod === C6Constants.GET) {
|
|
1831
|
+
return request;
|
|
1832
|
+
}
|
|
1833
|
+
throw new Error("Singular request requires all primary key(s) [".concat(pkShorts.join(', '), "] for table (").concat(restModel.TABLE_NAME, "). Missing: [").concat(missing.join(', '), "]"));
|
|
1834
|
+
}
|
|
1835
|
+
// Strip API metadata that should remain at root
|
|
1836
|
+
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"]);
|
|
1837
|
+
// Map short primary keys to fully-qualified column names
|
|
1838
|
+
var shortToFull = {};
|
|
1839
|
+
for (var _e = 0, _f = Object.entries(restModel.COLUMNS || {}); _e < _f.length; _e++) {
|
|
1840
|
+
var _g = _f[_e], full = _g[0], short = _g[1];
|
|
1841
|
+
shortToFull[short] = full;
|
|
1842
|
+
}
|
|
1843
|
+
var pkFullValues = Object.fromEntries(Object.entries(pkValues).map(function (_a) {
|
|
1844
|
+
var _b;
|
|
1845
|
+
var k = _a[0], v = _a[1];
|
|
1846
|
+
return [(_b = shortToFull[k]) !== null && _b !== void 0 ? _b : k, v];
|
|
1847
|
+
}));
|
|
1848
|
+
if (requestMethod === C6Constants.GET) {
|
|
1849
|
+
var normalized_1 = {
|
|
1850
|
+
WHERE: __assign({}, pkFullValues),
|
|
1851
|
+
};
|
|
1852
|
+
// Preserve pagination if any was added previously
|
|
1853
|
+
if (request[C6Constants.PAGINATION]) {
|
|
1854
|
+
normalized_1[C6Constants.PAGINATION] = request[C6Constants.PAGINATION];
|
|
1855
|
+
}
|
|
1856
|
+
return __assign(__assign({}, normalized_1), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
1857
|
+
}
|
|
1858
|
+
if (requestMethod === C6Constants.DELETE) {
|
|
1859
|
+
var normalized_2 = (_a = {},
|
|
1860
|
+
_a[C6Constants.DELETE] = true,
|
|
1861
|
+
_a.WHERE = __assign({}, pkFullValues),
|
|
1862
|
+
_a);
|
|
1863
|
+
return __assign(__assign({}, normalized_2), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
1864
|
+
}
|
|
1865
|
+
// PUT
|
|
1866
|
+
var updateBody = {};
|
|
1867
|
+
for (var _h = 0, _j = Object.keys(rest); _h < _j.length; _h++) {
|
|
1868
|
+
var k = _j[_h];
|
|
1869
|
+
// Skip special request keys if any slipped through
|
|
1870
|
+
if (specialKeys.has(k))
|
|
1871
|
+
continue;
|
|
1872
|
+
var shortKey = resolveShortKey(k);
|
|
1873
|
+
if (pkShorts.includes(shortKey))
|
|
1874
|
+
continue; // don't update PK columns (short or fully qualified)
|
|
1875
|
+
updateBody[shortKey] = rest[k];
|
|
1876
|
+
}
|
|
1877
|
+
if (Object.keys(updateBody).length === 0) {
|
|
1878
|
+
throw new Error("Singular PUT request for table (".concat(restModel.TABLE_NAME, ") must include at least one non-primary field to update."));
|
|
1879
|
+
}
|
|
1880
|
+
var normalized = (_b = {},
|
|
1881
|
+
_b[C6Constants.UPDATE] = updateBody,
|
|
1882
|
+
_b.WHERE = __assign({}, pkFullValues),
|
|
1883
|
+
_b);
|
|
1884
|
+
return __assign(__assign({}, normalized), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1881
1887
|
var SqlExecutor = /** @class */ (function (_super) {
|
|
1882
1888
|
__extends(SqlExecutor, _super);
|
|
1883
1889
|
function SqlExecutor() {
|
|
@@ -2065,12 +2071,12 @@ function ExpressHandler(_a) {
|
|
|
2065
2071
|
var _this = this;
|
|
2066
2072
|
var C6 = _a.C6, mysqlPool = _a.mysqlPool;
|
|
2067
2073
|
return function (req, res, next) { return __awaiter(_this, void 0, void 0, function () {
|
|
2068
|
-
var method, table, primary, payload, primaryKeys, primaryKeyName,
|
|
2069
|
-
var _a, _b
|
|
2070
|
-
return __generator(this, function (
|
|
2071
|
-
switch (
|
|
2074
|
+
var method, table, primary, payload, primaryKeys, primaryKeyName, response, err_1;
|
|
2075
|
+
var _a, _b;
|
|
2076
|
+
return __generator(this, function (_c) {
|
|
2077
|
+
switch (_c.label) {
|
|
2072
2078
|
case 0:
|
|
2073
|
-
|
|
2079
|
+
_c.trys.push([0, 2, , 3]);
|
|
2074
2080
|
method = req.method.toUpperCase();
|
|
2075
2081
|
table = req.params.table;
|
|
2076
2082
|
primary = req.params.primary;
|
|
@@ -2091,30 +2097,17 @@ function ExpressHandler(_a) {
|
|
|
2091
2097
|
return [2 /*return*/];
|
|
2092
2098
|
}
|
|
2093
2099
|
primaryKeyName = primaryKeys[0];
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
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*/];
|
|
2100
|
+
// If a primary key was provided in the URL, merge it into the payload.
|
|
2101
|
+
// Support both complex requests using WHERE and singular requests
|
|
2102
|
+
// where the primary key lives at the root of the payload.
|
|
2103
|
+
if (primary) {
|
|
2104
|
+
if (payload[C6C.WHERE]) {
|
|
2105
|
+
payload[C6C.WHERE][primaryKeyName] =
|
|
2106
|
+
(_a = payload[C6C.WHERE][primaryKeyName]) !== null && _a !== void 0 ? _a : primary;
|
|
2107
|
+
}
|
|
2108
|
+
else {
|
|
2109
|
+
payload[primaryKeyName] =
|
|
2110
|
+
(_b = payload[primaryKeyName]) !== null && _b !== void 0 ? _b : primary;
|
|
2118
2111
|
}
|
|
2119
2112
|
}
|
|
2120
2113
|
return [4 /*yield*/, restRequest({
|
|
@@ -2124,11 +2117,11 @@ function ExpressHandler(_a) {
|
|
|
2124
2117
|
restModel: C6.TABLES[table]
|
|
2125
2118
|
})(payload)];
|
|
2126
2119
|
case 1:
|
|
2127
|
-
response =
|
|
2120
|
+
response = _c.sent();
|
|
2128
2121
|
res.status(200).json(__assign({ success: true }, response));
|
|
2129
2122
|
return [3 /*break*/, 3];
|
|
2130
2123
|
case 2:
|
|
2131
|
-
err_1 =
|
|
2124
|
+
err_1 = _c.sent();
|
|
2132
2125
|
res.status(500).json({ success: false, error: err_1 });
|
|
2133
2126
|
next(err_1);
|
|
2134
2127
|
return [3 /*break*/, 3];
|