@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.cjs.js +150 -164
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +151 -165
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/httpExecutorSingular.e2e.test.ts +72 -0
- package/src/__tests__/normalizeSingularRequest.test.ts +8 -8
- package/src/__tests__/sakila-db/C6.js +1 -1
- package/src/__tests__/sakila-db/C6.ts +1 -1
- package/src/api/executors/HttpExecutor.ts +4 -35
- package/src/api/handlers/ExpressHandler.ts +10 -23
- package/src/api/utils/normalizeSingularRequest.ts +15 -4
package/dist/index.cjs.js
CHANGED
|
@@ -623,122 +623,6 @@ function sortAndSerializeQueryObject(tables, query) {
|
|
|
623
623
|
return tables + ' ' + JSON.stringify(orderedQuery);
|
|
624
624
|
}
|
|
625
625
|
|
|
626
|
-
/**
|
|
627
|
-
* Converts a singular T-shaped request into complex ORM format for GET/PUT/DELETE
|
|
628
|
-
* Enforces that all primary keys are present for singular syntax and that the table has PKs.
|
|
629
|
-
* Optionally accepts a previously removed primary key (key/value) to reconstruct WHERE.
|
|
630
|
-
*/
|
|
631
|
-
function normalizeSingularRequest(requestMethod, request, restModel, removedPrimary) {
|
|
632
|
-
var _a, _b;
|
|
633
|
-
var _c;
|
|
634
|
-
if (request == null || typeof request !== 'object')
|
|
635
|
-
return request;
|
|
636
|
-
var specialKeys = new Set([
|
|
637
|
-
C6Constants.SELECT,
|
|
638
|
-
C6Constants.UPDATE,
|
|
639
|
-
C6Constants.DELETE,
|
|
640
|
-
C6Constants.WHERE,
|
|
641
|
-
C6Constants.JOIN,
|
|
642
|
-
C6Constants.PAGINATION,
|
|
643
|
-
]);
|
|
644
|
-
// Determine if the request is already complex (has any special key besides PAGINATION)
|
|
645
|
-
var keys = Object.keys(request);
|
|
646
|
-
var hasComplexKeys = keys.some(function (k) { return k !== C6Constants.PAGINATION && specialKeys.has(k); });
|
|
647
|
-
if (hasComplexKeys)
|
|
648
|
-
return request; // already complex
|
|
649
|
-
// We treat it as singular when it's not complex.
|
|
650
|
-
// For GET, PUT, DELETE only
|
|
651
|
-
if (!(requestMethod === C6Constants.GET || requestMethod === C6Constants.PUT || requestMethod === C6Constants.DELETE)) {
|
|
652
|
-
return request;
|
|
653
|
-
}
|
|
654
|
-
var pkShorts = Array.isArray(restModel.PRIMARY_SHORT) ? tslib.__spreadArray([], restModel.PRIMARY_SHORT, true) : [];
|
|
655
|
-
var pkFulls = Array.isArray(restModel.PRIMARY) ? tslib.__spreadArray([], restModel.PRIMARY, true) : [];
|
|
656
|
-
var resolveShortKey = function (key) {
|
|
657
|
-
var _a;
|
|
658
|
-
var cols = restModel.COLUMNS || {};
|
|
659
|
-
return (_a = cols[key]) !== null && _a !== void 0 ? _a : key;
|
|
660
|
-
};
|
|
661
|
-
if (!pkShorts.length) {
|
|
662
|
-
// For GET requests, do not enforce primary key presence; treat as a collection query.
|
|
663
|
-
if (requestMethod === C6Constants.GET)
|
|
664
|
-
return request;
|
|
665
|
-
throw new Error("Table (".concat(restModel.TABLE_NAME, ") has no primary key; singular request syntax is not allowed."));
|
|
666
|
-
}
|
|
667
|
-
// Build pk map from request + possibly removed primary key (accept short or fully-qualified keys)
|
|
668
|
-
var pkValues = {};
|
|
669
|
-
var requestObj = request;
|
|
670
|
-
var _loop_1 = function (pkShort) {
|
|
671
|
-
// 1) direct short key
|
|
672
|
-
var value = requestObj[pkShort];
|
|
673
|
-
if (value === undefined) {
|
|
674
|
-
// 2) fully-qualified key matching this short key (from PRIMARY list or by concatenation)
|
|
675
|
-
var fqCandidate_1 = "".concat(restModel.TABLE_NAME, ".").concat(pkShort);
|
|
676
|
-
var fqKey = (_c = pkFulls.find(function (fq) { return fq === fqCandidate_1 || fq.endsWith(".".concat(pkShort)); })) !== null && _c !== void 0 ? _c : fqCandidate_1;
|
|
677
|
-
value = requestObj[fqKey];
|
|
678
|
-
}
|
|
679
|
-
if (value === undefined && removedPrimary) {
|
|
680
|
-
// 3) removedPrimary may provide either short or fully-qualified key
|
|
681
|
-
var removedKeyShort = resolveShortKey(removedPrimary.key);
|
|
682
|
-
if (removedKeyShort === pkShort)
|
|
683
|
-
value = removedPrimary.value;
|
|
684
|
-
}
|
|
685
|
-
if (value !== undefined && value !== null) {
|
|
686
|
-
pkValues[pkShort] = value;
|
|
687
|
-
}
|
|
688
|
-
};
|
|
689
|
-
for (var _i = 0, pkShorts_1 = pkShorts; _i < pkShorts_1.length; _i++) {
|
|
690
|
-
var pkShort = pkShorts_1[_i];
|
|
691
|
-
_loop_1(pkShort);
|
|
692
|
-
}
|
|
693
|
-
var missing = pkShorts.filter(function (pk) { return !(pk in pkValues); });
|
|
694
|
-
if (missing.length) {
|
|
695
|
-
// For GET requests, if not all PKs are provided, treat as a collection query and leave as-is.
|
|
696
|
-
if (requestMethod === C6Constants.GET) {
|
|
697
|
-
return request;
|
|
698
|
-
}
|
|
699
|
-
throw new Error("Singular request requires all primary key(s) [".concat(pkShorts.join(', '), "] for table (").concat(restModel.TABLE_NAME, "). Missing: [").concat(missing.join(', '), "]"));
|
|
700
|
-
}
|
|
701
|
-
// Strip API metadata that should remain at root
|
|
702
|
-
var _d = request, dataInsertMultipleRows = _d.dataInsertMultipleRows, cacheResults = _d.cacheResults, fetchDependencies = _d.fetchDependencies, debug = _d.debug, success = _d.success, error = _d.error, rest = tslib.__rest(_d, ["dataInsertMultipleRows", "cacheResults", "fetchDependencies", "debug", "success", "error"]);
|
|
703
|
-
if (requestMethod === C6Constants.GET) {
|
|
704
|
-
var normalized_1 = {
|
|
705
|
-
WHERE: tslib.__assign({}, pkValues),
|
|
706
|
-
};
|
|
707
|
-
// Preserve pagination if any was added previously
|
|
708
|
-
if (request[C6Constants.PAGINATION]) {
|
|
709
|
-
normalized_1[C6Constants.PAGINATION] = request[C6Constants.PAGINATION];
|
|
710
|
-
}
|
|
711
|
-
return tslib.__assign(tslib.__assign({}, normalized_1), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
712
|
-
}
|
|
713
|
-
if (requestMethod === C6Constants.DELETE) {
|
|
714
|
-
var normalized_2 = (_a = {},
|
|
715
|
-
_a[C6Constants.DELETE] = true,
|
|
716
|
-
_a.WHERE = tslib.__assign({}, pkValues),
|
|
717
|
-
_a);
|
|
718
|
-
return tslib.__assign(tslib.__assign({}, normalized_2), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
719
|
-
}
|
|
720
|
-
// PUT
|
|
721
|
-
var updateBody = {};
|
|
722
|
-
for (var _e = 0, _f = Object.keys(rest); _e < _f.length; _e++) {
|
|
723
|
-
var k = _f[_e];
|
|
724
|
-
// Skip special request keys if any slipped through
|
|
725
|
-
if (specialKeys.has(k))
|
|
726
|
-
continue;
|
|
727
|
-
var shortKey = resolveShortKey(k);
|
|
728
|
-
if (pkShorts.includes(shortKey))
|
|
729
|
-
continue; // don't update PK columns (short or fully qualified)
|
|
730
|
-
updateBody[shortKey] = rest[k];
|
|
731
|
-
}
|
|
732
|
-
if (Object.keys(updateBody).length === 0) {
|
|
733
|
-
throw new Error("Singular PUT request for table (".concat(restModel.TABLE_NAME, ") must include at least one non-primary field to update."));
|
|
734
|
-
}
|
|
735
|
-
var normalized = (_b = {},
|
|
736
|
-
_b[C6Constants.UPDATE] = updateBody,
|
|
737
|
-
_b.WHERE = tslib.__assign({}, pkValues),
|
|
738
|
-
_b);
|
|
739
|
-
return tslib.__assign(tslib.__assign({}, normalized), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
740
|
-
}
|
|
741
|
-
|
|
742
626
|
var HttpExecutor = /** @class */ (function (_super) {
|
|
743
627
|
tslib.__extends(HttpExecutor, _super);
|
|
744
628
|
function HttpExecutor() {
|
|
@@ -845,7 +729,7 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
845
729
|
query[C6.PAGINATION][C6.LIMIT] = query[C6.PAGINATION][C6.LIMIT] || 100;
|
|
846
730
|
}
|
|
847
731
|
apiRequest = function () { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
848
|
-
var _a, debug, _b, cacheResults, dataInsertMultipleRows, success, _c, fetchDependencies, _d, error, querySerialized, cacheResult, cachingConfirmed, cacheCheck, cacheCheck,
|
|
732
|
+
var _a, debug, _b, cacheResults, dataInsertMultipleRows, success, _c, fetchDependencies, _d, error, querySerialized, cacheResult, cachingConfirmed, cacheCheck, cacheCheck, apiResponse, returnGetNextPageFunction, restRequestUri, needsConditionOrPrimaryCheck, TABLES, primaryKey, axiosActiveRequest;
|
|
849
733
|
var _e;
|
|
850
734
|
var _this = this;
|
|
851
735
|
var _f, _g, _h, _j, _k, _l;
|
|
@@ -964,14 +848,7 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
964
848
|
&& undefined !== primaryKey
|
|
965
849
|
&& primaryKey in query) {
|
|
966
850
|
restRequestUri += query[primaryKey] + '/';
|
|
967
|
-
|
|
968
|
-
removedPrimaryKV = { key: primaryKey, value: removedPkValue_1 };
|
|
969
|
-
addBackPK = function () {
|
|
970
|
-
query !== null && query !== void 0 ? query : (query = {});
|
|
971
|
-
query[primaryKey] = removedPkValue_1;
|
|
972
|
-
};
|
|
973
|
-
delete query[primaryKey];
|
|
974
|
-
console.log('query', query, 'primaryKey', primaryKey, 'removedPkValue', removedPkValue_1);
|
|
851
|
+
console.log('query', query, 'primaryKey', primaryKey);
|
|
975
852
|
}
|
|
976
853
|
else {
|
|
977
854
|
console.log('query', query);
|
|
@@ -994,11 +871,9 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
994
871
|
var baseConfig = {
|
|
995
872
|
withCredentials: withCredentials,
|
|
996
873
|
};
|
|
997
|
-
// Normalize singular request (GET/PUT/DELETE) into complex ORM shape
|
|
998
|
-
var normalizedQuery = normalizeSingularRequest(requestMethod, query, restModel, removedPrimaryKV);
|
|
999
874
|
switch (requestMethod) {
|
|
1000
875
|
case GET:
|
|
1001
|
-
return [tslib.__assign(tslib.__assign({}, baseConfig), { params:
|
|
876
|
+
return [tslib.__assign(tslib.__assign({}, baseConfig), { params: query })];
|
|
1002
877
|
case POST:
|
|
1003
878
|
if (dataInsertMultipleRows !== undefined) {
|
|
1004
879
|
return [
|
|
@@ -1008,9 +883,9 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
1008
883
|
}
|
|
1009
884
|
return [convert(query), baseConfig];
|
|
1010
885
|
case PUT:
|
|
1011
|
-
return [convert(
|
|
886
|
+
return [convert(query), baseConfig];
|
|
1012
887
|
case DELETE:
|
|
1013
|
-
return [tslib.__assign(tslib.__assign({}, baseConfig), { data: convert(
|
|
888
|
+
return [tslib.__assign(tslib.__assign({}, baseConfig), { data: convert(query) })];
|
|
1014
889
|
default:
|
|
1015
890
|
throw new Error("The request method (".concat(requestMethod, ") was not recognized."));
|
|
1016
891
|
}
|
|
@@ -1022,9 +897,6 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
1022
897
|
request: axiosActiveRequest
|
|
1023
898
|
});
|
|
1024
899
|
}
|
|
1025
|
-
// todo - wip verify this works
|
|
1026
|
-
// we had removed the value from the request to add to the URI.
|
|
1027
|
-
addBackPK === null || addBackPK === void 0 ? void 0 : addBackPK(); // adding back so post-processing methods work
|
|
1028
900
|
// returning the promise with this then is important for tests. todo - we could make that optional.
|
|
1029
901
|
// https://rapidapi.com/guides/axios-async-await
|
|
1030
902
|
return [2 /*return*/, axiosActiveRequest.then(function (response) { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
@@ -1881,6 +1753,133 @@ var UpdateQueryBuilder = /** @class */ (function (_super) {
|
|
|
1881
1753
|
return UpdateQueryBuilder;
|
|
1882
1754
|
}(PaginationBuilder));
|
|
1883
1755
|
|
|
1756
|
+
/**
|
|
1757
|
+
* Converts a singular T-shaped request into complex ORM format for GET/PUT/DELETE
|
|
1758
|
+
* Enforces that all primary keys are present for singular syntax and that the table has PKs.
|
|
1759
|
+
* Optionally accepts a previously removed primary key (key/value) to reconstruct WHERE.
|
|
1760
|
+
*/
|
|
1761
|
+
function normalizeSingularRequest(requestMethod, request, restModel, removedPrimary) {
|
|
1762
|
+
var _a, _b;
|
|
1763
|
+
var _c;
|
|
1764
|
+
if (request == null || typeof request !== 'object')
|
|
1765
|
+
return request;
|
|
1766
|
+
var specialKeys = new Set([
|
|
1767
|
+
C6Constants.SELECT,
|
|
1768
|
+
C6Constants.UPDATE,
|
|
1769
|
+
C6Constants.DELETE,
|
|
1770
|
+
C6Constants.WHERE,
|
|
1771
|
+
C6Constants.JOIN,
|
|
1772
|
+
C6Constants.PAGINATION,
|
|
1773
|
+
]);
|
|
1774
|
+
// Determine if the request is already complex (has any special key besides PAGINATION)
|
|
1775
|
+
var keys = Object.keys(request);
|
|
1776
|
+
var hasComplexKeys = keys.some(function (k) { return k !== C6Constants.PAGINATION && specialKeys.has(k); });
|
|
1777
|
+
if (hasComplexKeys)
|
|
1778
|
+
return request; // already complex
|
|
1779
|
+
// We treat it as singular when it's not complex.
|
|
1780
|
+
// For GET, PUT, DELETE only
|
|
1781
|
+
if (!(requestMethod === C6Constants.GET || requestMethod === C6Constants.PUT || requestMethod === C6Constants.DELETE)) {
|
|
1782
|
+
return request;
|
|
1783
|
+
}
|
|
1784
|
+
var pkShorts = Array.isArray(restModel.PRIMARY_SHORT) ? tslib.__spreadArray([], restModel.PRIMARY_SHORT, true) : [];
|
|
1785
|
+
var pkFulls = Array.isArray(restModel.PRIMARY) ? tslib.__spreadArray([], restModel.PRIMARY, true) : [];
|
|
1786
|
+
var resolveShortKey = function (key) {
|
|
1787
|
+
var _a;
|
|
1788
|
+
var cols = restModel.COLUMNS || {};
|
|
1789
|
+
return (_a = cols[key]) !== null && _a !== void 0 ? _a : key;
|
|
1790
|
+
};
|
|
1791
|
+
if (!pkShorts.length) {
|
|
1792
|
+
// For GET requests, do not enforce primary key presence; treat as a collection query.
|
|
1793
|
+
if (requestMethod === C6Constants.GET)
|
|
1794
|
+
return request;
|
|
1795
|
+
throw new Error("Table (".concat(restModel.TABLE_NAME, ") has no primary key; singular request syntax is not allowed."));
|
|
1796
|
+
}
|
|
1797
|
+
// Build pk map from request + possibly removed primary key (accept short or fully-qualified keys)
|
|
1798
|
+
var pkValues = {};
|
|
1799
|
+
var requestObj = request;
|
|
1800
|
+
var _loop_1 = function (pkShort) {
|
|
1801
|
+
// 1) direct short key
|
|
1802
|
+
var value = requestObj[pkShort];
|
|
1803
|
+
if (value === undefined) {
|
|
1804
|
+
// 2) fully-qualified key matching this short key (from PRIMARY list or by concatenation)
|
|
1805
|
+
var fqCandidate_1 = "".concat(restModel.TABLE_NAME, ".").concat(pkShort);
|
|
1806
|
+
var fqKey = (_c = pkFulls.find(function (fq) { return fq === fqCandidate_1 || fq.endsWith(".".concat(pkShort)); })) !== null && _c !== void 0 ? _c : fqCandidate_1;
|
|
1807
|
+
value = requestObj[fqKey];
|
|
1808
|
+
}
|
|
1809
|
+
if (value === undefined && removedPrimary) {
|
|
1810
|
+
// 3) removedPrimary may provide either short or fully-qualified key
|
|
1811
|
+
var removedKeyShort = resolveShortKey(removedPrimary.key);
|
|
1812
|
+
if (removedKeyShort === pkShort)
|
|
1813
|
+
value = removedPrimary.value;
|
|
1814
|
+
}
|
|
1815
|
+
if (value !== undefined && value !== null) {
|
|
1816
|
+
pkValues[pkShort] = value;
|
|
1817
|
+
}
|
|
1818
|
+
};
|
|
1819
|
+
for (var _i = 0, pkShorts_1 = pkShorts; _i < pkShorts_1.length; _i++) {
|
|
1820
|
+
var pkShort = pkShorts_1[_i];
|
|
1821
|
+
_loop_1(pkShort);
|
|
1822
|
+
}
|
|
1823
|
+
var missing = pkShorts.filter(function (pk) { return !(pk in pkValues); });
|
|
1824
|
+
if (missing.length) {
|
|
1825
|
+
// For GET requests, if not all PKs are provided, treat as a collection query and leave as-is.
|
|
1826
|
+
if (requestMethod === C6Constants.GET) {
|
|
1827
|
+
return request;
|
|
1828
|
+
}
|
|
1829
|
+
throw new Error("Singular request requires all primary key(s) [".concat(pkShorts.join(', '), "] for table (").concat(restModel.TABLE_NAME, "). Missing: [").concat(missing.join(', '), "]"));
|
|
1830
|
+
}
|
|
1831
|
+
// Strip API metadata that should remain at root
|
|
1832
|
+
var _d = request, dataInsertMultipleRows = _d.dataInsertMultipleRows, cacheResults = _d.cacheResults, fetchDependencies = _d.fetchDependencies, debug = _d.debug, success = _d.success, error = _d.error, rest = tslib.__rest(_d, ["dataInsertMultipleRows", "cacheResults", "fetchDependencies", "debug", "success", "error"]);
|
|
1833
|
+
// Map short primary keys to fully-qualified column names
|
|
1834
|
+
var shortToFull = {};
|
|
1835
|
+
for (var _e = 0, _f = Object.entries(restModel.COLUMNS || {}); _e < _f.length; _e++) {
|
|
1836
|
+
var _g = _f[_e], full = _g[0], short = _g[1];
|
|
1837
|
+
shortToFull[short] = full;
|
|
1838
|
+
}
|
|
1839
|
+
var pkFullValues = Object.fromEntries(Object.entries(pkValues).map(function (_a) {
|
|
1840
|
+
var _b;
|
|
1841
|
+
var k = _a[0], v = _a[1];
|
|
1842
|
+
return [(_b = shortToFull[k]) !== null && _b !== void 0 ? _b : k, v];
|
|
1843
|
+
}));
|
|
1844
|
+
if (requestMethod === C6Constants.GET) {
|
|
1845
|
+
var normalized_1 = {
|
|
1846
|
+
WHERE: tslib.__assign({}, pkFullValues),
|
|
1847
|
+
};
|
|
1848
|
+
// Preserve pagination if any was added previously
|
|
1849
|
+
if (request[C6Constants.PAGINATION]) {
|
|
1850
|
+
normalized_1[C6Constants.PAGINATION] = request[C6Constants.PAGINATION];
|
|
1851
|
+
}
|
|
1852
|
+
return tslib.__assign(tslib.__assign({}, normalized_1), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
1853
|
+
}
|
|
1854
|
+
if (requestMethod === C6Constants.DELETE) {
|
|
1855
|
+
var normalized_2 = (_a = {},
|
|
1856
|
+
_a[C6Constants.DELETE] = true,
|
|
1857
|
+
_a.WHERE = tslib.__assign({}, pkFullValues),
|
|
1858
|
+
_a);
|
|
1859
|
+
return tslib.__assign(tslib.__assign({}, normalized_2), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
1860
|
+
}
|
|
1861
|
+
// PUT
|
|
1862
|
+
var updateBody = {};
|
|
1863
|
+
for (var _h = 0, _j = Object.keys(rest); _h < _j.length; _h++) {
|
|
1864
|
+
var k = _j[_h];
|
|
1865
|
+
// Skip special request keys if any slipped through
|
|
1866
|
+
if (specialKeys.has(k))
|
|
1867
|
+
continue;
|
|
1868
|
+
var shortKey = resolveShortKey(k);
|
|
1869
|
+
if (pkShorts.includes(shortKey))
|
|
1870
|
+
continue; // don't update PK columns (short or fully qualified)
|
|
1871
|
+
updateBody[shortKey] = rest[k];
|
|
1872
|
+
}
|
|
1873
|
+
if (Object.keys(updateBody).length === 0) {
|
|
1874
|
+
throw new Error("Singular PUT request for table (".concat(restModel.TABLE_NAME, ") must include at least one non-primary field to update."));
|
|
1875
|
+
}
|
|
1876
|
+
var normalized = (_b = {},
|
|
1877
|
+
_b[C6Constants.UPDATE] = updateBody,
|
|
1878
|
+
_b.WHERE = tslib.__assign({}, pkFullValues),
|
|
1879
|
+
_b);
|
|
1880
|
+
return tslib.__assign(tslib.__assign({}, normalized), { dataInsertMultipleRows: dataInsertMultipleRows, cacheResults: cacheResults, fetchDependencies: fetchDependencies, debug: debug, success: success, error: error });
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1884
1883
|
var SqlExecutor = /** @class */ (function (_super) {
|
|
1885
1884
|
tslib.__extends(SqlExecutor, _super);
|
|
1886
1885
|
function SqlExecutor() {
|
|
@@ -2068,12 +2067,12 @@ function ExpressHandler(_a) {
|
|
|
2068
2067
|
var _this = this;
|
|
2069
2068
|
var C6 = _a.C6, mysqlPool = _a.mysqlPool;
|
|
2070
2069
|
return function (req, res, next) { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
2071
|
-
var method, table, primary, payload, primaryKeys, primaryKeyName,
|
|
2072
|
-
var _a, _b
|
|
2073
|
-
return tslib.__generator(this, function (
|
|
2074
|
-
switch (
|
|
2070
|
+
var method, table, primary, payload, primaryKeys, primaryKeyName, response, err_1;
|
|
2071
|
+
var _a, _b;
|
|
2072
|
+
return tslib.__generator(this, function (_c) {
|
|
2073
|
+
switch (_c.label) {
|
|
2075
2074
|
case 0:
|
|
2076
|
-
|
|
2075
|
+
_c.trys.push([0, 2, , 3]);
|
|
2077
2076
|
method = req.method.toUpperCase();
|
|
2078
2077
|
table = req.params.table;
|
|
2079
2078
|
primary = req.params.primary;
|
|
@@ -2094,30 +2093,17 @@ function ExpressHandler(_a) {
|
|
|
2094
2093
|
return [2 /*return*/];
|
|
2095
2094
|
}
|
|
2096
2095
|
primaryKeyName = primaryKeys[0];
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
case 'DELETE':
|
|
2109
|
-
if (primary) {
|
|
2110
|
-
payload[C6C.WHERE][primaryKeyName] = primary;
|
|
2111
|
-
}
|
|
2112
|
-
else {
|
|
2113
|
-
res.status(400).json({ error: "Invalid request: ".concat(method, " requires a primary key (").concat(primaryKeyName, ").") });
|
|
2114
|
-
}
|
|
2115
|
-
break;
|
|
2116
|
-
case 'POST':
|
|
2117
|
-
break;
|
|
2118
|
-
default:
|
|
2119
|
-
res.status(405).json({ error: "Method ".concat(method, " not allowed") });
|
|
2120
|
-
return [2 /*return*/];
|
|
2096
|
+
// If a primary key was provided in the URL, merge it into the payload.
|
|
2097
|
+
// Support both complex requests using WHERE and singular requests
|
|
2098
|
+
// where the primary key lives at the root of the payload.
|
|
2099
|
+
if (primary) {
|
|
2100
|
+
if (payload[C6C.WHERE]) {
|
|
2101
|
+
payload[C6C.WHERE][primaryKeyName] =
|
|
2102
|
+
(_a = payload[C6C.WHERE][primaryKeyName]) !== null && _a !== void 0 ? _a : primary;
|
|
2103
|
+
}
|
|
2104
|
+
else {
|
|
2105
|
+
payload[primaryKeyName] =
|
|
2106
|
+
(_b = payload[primaryKeyName]) !== null && _b !== void 0 ? _b : primary;
|
|
2121
2107
|
}
|
|
2122
2108
|
}
|
|
2123
2109
|
return [4 /*yield*/, restRequest({
|
|
@@ -2127,11 +2113,11 @@ function ExpressHandler(_a) {
|
|
|
2127
2113
|
restModel: C6.TABLES[table]
|
|
2128
2114
|
})(payload)];
|
|
2129
2115
|
case 1:
|
|
2130
|
-
response =
|
|
2116
|
+
response = _c.sent();
|
|
2131
2117
|
res.status(200).json(tslib.__assign({ success: true }, response));
|
|
2132
2118
|
return [3 /*break*/, 3];
|
|
2133
2119
|
case 2:
|
|
2134
|
-
err_1 =
|
|
2120
|
+
err_1 = _c.sent();
|
|
2135
2121
|
res.status(500).json({ success: false, error: err_1 });
|
|
2136
2122
|
next(err_1);
|
|
2137
2123
|
return [3 /*break*/, 3];
|