@carbonorm/carbonnode 3.0.8 → 3.0.10
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/api/executors/SqlExecutor.d.ts +8 -12
- package/dist/api/handlers/ExpressHandler.d.ts +7 -0
- package/dist/api/restOrm.d.ts +1 -4
- package/dist/api/restRequest.d.ts +1 -1
- package/dist/api/types/ormInterfaces.d.ts +1 -1
- package/dist/index.cjs.js +544 -453
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +544 -454
- package/dist/index.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/api/executors/HttpExecutor.ts +5 -3
- package/src/api/executors/SqlExecutor.ts +60 -57
- package/src/api/handlers/ExpressHandler.ts +55 -0
- package/src/api/restOrm.ts +20 -57
- package/src/api/restRequest.ts +6 -2
- package/src/api/types/ormInterfaces.ts +1 -1
- package/src/index.ts +1 -0
package/dist/index.esm.js
CHANGED
|
@@ -314,14 +314,14 @@ var isNode = typeof process !== 'undefined' && !!((_a = process.versions) === nu
|
|
|
314
314
|
/**
|
|
315
315
|
* Facade: routes API calls to SQL or HTTP executors based on runtime context.
|
|
316
316
|
*/
|
|
317
|
-
function restRequest(
|
|
317
|
+
function restRequest(configX) {
|
|
318
318
|
var _this = this;
|
|
319
319
|
return function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
320
320
|
var config, SqlExecutor, executor, HttpExecutor, http;
|
|
321
321
|
return __generator(this, function (_a) {
|
|
322
322
|
switch (_a.label) {
|
|
323
323
|
case 0:
|
|
324
|
-
config =
|
|
324
|
+
config = typeof configX === "function" ? configX() : configX;
|
|
325
325
|
if (!(isNode && config.mysqlPool)) return [3 /*break*/, 2];
|
|
326
326
|
return [4 /*yield*/, Promise.resolve().then(function () { return SqlExecutor$1; })];
|
|
327
327
|
case 1:
|
|
@@ -339,12 +339,11 @@ function restRequest(configFn) {
|
|
|
339
339
|
}
|
|
340
340
|
|
|
341
341
|
function restOrm(config) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
};
|
|
342
|
+
var methods = ["GET", "PUT", "POST", "DELETE"];
|
|
343
|
+
return Object.fromEntries(methods.map(function (method) { return [
|
|
344
|
+
method[0] + method.slice(1).toLowerCase(), // Capitalize e.g. "Get"
|
|
345
|
+
restRequest(function () { return (__assign(__assign({}, config()), { requestMethod: method })); })
|
|
346
|
+
]; }));
|
|
348
347
|
}
|
|
349
348
|
|
|
350
349
|
function timeout(shouldContinueAfterTimeout, cb, timeoutMs) {
|
|
@@ -828,458 +827,466 @@ var HttpExecutor = /** @class */ (function (_super) {
|
|
|
828
827
|
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;
|
|
829
828
|
var _e;
|
|
830
829
|
var _this = this;
|
|
831
|
-
var _f, _g, _h, _j, _k, _l;
|
|
832
|
-
return __generator(this, function (
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
cacheResult = apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
|
|
844
|
-
cachingConfirmed = false;
|
|
845
|
-
// determine if we need to paginate.
|
|
846
|
-
if (requestMethod === C6.GET) {
|
|
847
|
-
if (undefined === (query === null || query === void 0 ? void 0 : query[C6.PAGINATION])) {
|
|
848
|
-
if (undefined === query || null === query) {
|
|
849
|
-
query = {};
|
|
830
|
+
var _f, _g, _h, _j, _k, _l, _m, _o;
|
|
831
|
+
return __generator(this, function (_p) {
|
|
832
|
+
switch (_p.label) {
|
|
833
|
+
case 0:
|
|
834
|
+
_a = this.request, debug = _a.debug, _b = _a.cacheResults, cacheResults = _b === void 0 ? (C6.GET === requestMethod) : _b, dataInsertMultipleRows = _a.dataInsertMultipleRows, success = _a.success, _c = _a.fetchDependencies, fetchDependencies = _c === void 0 ? eFetchDependencies.NONE : _c, _d = _a.error, error = _d === void 0 ? "An unexpected API error occurred!" : _d;
|
|
835
|
+
if (C6.GET === requestMethod
|
|
836
|
+
&& undefined !== ((_f = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _f === void 0 ? void 0 : _f[C6.PAGE])
|
|
837
|
+
&& 1 !== query[C6.PAGINATION][C6.PAGE]) {
|
|
838
|
+
console.groupCollapsed('Request on table (' + tableName + ') is firing for page (' + query[C6.PAGINATION][C6.PAGE] + '), please wait!');
|
|
839
|
+
console.log('Request Data (note you may see the success and/or error prompt):', this.request);
|
|
840
|
+
console.trace();
|
|
841
|
+
console.groupEnd();
|
|
850
842
|
}
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
if (undefined !== cacheResult) {
|
|
859
|
-
do {
|
|
860
|
-
cacheCheck = checkCache(cacheResult, requestMethod, tableName, this.request);
|
|
861
|
-
if (false !== cacheCheck) {
|
|
862
|
-
return [2 /*return*/, cacheCheck];
|
|
863
|
-
}
|
|
864
|
-
// this line incrementing page is why we return recursively
|
|
865
|
-
++query[C6.PAGINATION][C6.PAGE];
|
|
866
|
-
// this json stringify is to capture the new page number
|
|
867
|
-
querySerialized = sortAndSerializeQueryObject(tables, query !== null && query !== void 0 ? query : {});
|
|
868
|
-
cacheResult = apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
|
|
869
|
-
} while (undefined !== cacheResult);
|
|
870
|
-
if (debug && isDevelopment) {
|
|
871
|
-
toast.warning("DEVS: Request in cache. (" + apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; }) + "). Returning function to request page (" + query[C6.PAGINATION][C6.PAGE] + ")", toastOptionsDevs);
|
|
843
|
+
querySerialized = sortAndSerializeQueryObject(tables, query !== null && query !== void 0 ? query : {});
|
|
844
|
+
cacheResult = apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
|
|
845
|
+
cachingConfirmed = false;
|
|
846
|
+
if (!(requestMethod === C6.GET)) return [3 /*break*/, 9];
|
|
847
|
+
if (undefined === (query === null || query === void 0 ? void 0 : query[C6.PAGINATION])) {
|
|
848
|
+
if (undefined === query || null === query) {
|
|
849
|
+
query = {};
|
|
872
850
|
}
|
|
873
|
-
|
|
874
|
-
|
|
851
|
+
query[C6.PAGINATION] = {};
|
|
852
|
+
}
|
|
853
|
+
query[C6.PAGINATION][C6.PAGE] = query[C6.PAGINATION][C6.PAGE] || 1;
|
|
854
|
+
query[C6.PAGINATION][C6.LIMIT] = query[C6.PAGINATION][C6.LIMIT] || 100;
|
|
855
|
+
if (!(true === cacheResults)) return [3 /*break*/, 7];
|
|
856
|
+
if (!(undefined !== cacheResult)) return [3 /*break*/, 6];
|
|
857
|
+
_p.label = 1;
|
|
858
|
+
case 1:
|
|
859
|
+
cacheCheck = checkCache(cacheResult, requestMethod, tableName, this.request);
|
|
860
|
+
if (!(false !== cacheCheck)) return [3 /*break*/, 3];
|
|
861
|
+
return [4 /*yield*/, cacheCheck];
|
|
862
|
+
case 2: return [2 /*return*/, (_g = (_p.sent())) === null || _g === void 0 ? void 0 : _g.data];
|
|
863
|
+
case 3:
|
|
864
|
+
// this line incrementing page is why we return recursively
|
|
865
|
+
++query[C6.PAGINATION][C6.PAGE];
|
|
866
|
+
// this json stringify is to capture the new page number
|
|
867
|
+
querySerialized = sortAndSerializeQueryObject(tables, query !== null && query !== void 0 ? query : {});
|
|
868
|
+
cacheResult = apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
|
|
869
|
+
_p.label = 4;
|
|
870
|
+
case 4:
|
|
871
|
+
if (undefined !== cacheResult) return [3 /*break*/, 1];
|
|
872
|
+
_p.label = 5;
|
|
873
|
+
case 5:
|
|
874
|
+
if (debug && isDevelopment) {
|
|
875
|
+
toast.warning("DEVS: Request in cache. (" + apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; }) + "). Returning function to request page (" + query[C6.PAGINATION][C6.PAGE] + ")", toastOptionsDevs);
|
|
875
876
|
}
|
|
877
|
+
// @ts-ignore - this is an incorrect warning on TS, it's well typed
|
|
878
|
+
return [2 /*return*/, apiRequest];
|
|
879
|
+
case 6:
|
|
876
880
|
cachingConfirmed = true;
|
|
877
|
-
|
|
878
|
-
|
|
881
|
+
return [3 /*break*/, 8];
|
|
882
|
+
case 7:
|
|
879
883
|
if (debug && isDevelopment) {
|
|
880
884
|
toast.info("DEVS: Ignore cache was set to true.", toastOptionsDevs);
|
|
881
885
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
}
|
|
887
|
-
else if (cacheResults) { // if we are not getting, we are updating, deleting, or inserting
|
|
888
|
-
if (cacheResult) {
|
|
889
|
-
cacheCheck = checkCache(cacheResult, requestMethod, tableName, this.request);
|
|
890
|
-
if (false !== cacheCheck) {
|
|
891
|
-
return [2 /*return*/, cacheCheck];
|
|
892
|
-
}
|
|
893
|
-
}
|
|
894
|
-
cachingConfirmed = true;
|
|
895
|
-
// push to cache so we do not repeat the request
|
|
896
|
-
}
|
|
897
|
-
returnGetNextPageFunction = false;
|
|
898
|
-
restRequestUri = restURL + operatingTable + '/';
|
|
899
|
-
needsConditionOrPrimaryCheck = (PUT === requestMethod || DELETE === requestMethod)
|
|
900
|
-
&& false === skipPrimaryCheck;
|
|
901
|
-
TABLES = C6.TABLES;
|
|
902
|
-
primaryKey = (_k = (_j = (_h = structuredClone((_g = TABLES[operatingTable]) === null || _g === void 0 ? void 0 : _g.PRIMARY)) === null || _h === void 0 ? void 0 : _h.pop()) === null || _j === void 0 ? void 0 : _j.split('.')) === null || _k === void 0 ? void 0 : _k.pop();
|
|
903
|
-
if (needsConditionOrPrimaryCheck) {
|
|
904
|
-
if (undefined === primaryKey) {
|
|
905
|
-
if (null === query
|
|
906
|
-
|| undefined === query
|
|
907
|
-
|| undefined === (query === null || query === void 0 ? void 0 : query[C6.WHERE])
|
|
908
|
-
|| (true === Array.isArray(query[C6.WHERE])
|
|
909
|
-
|| query[C6.WHERE].length === 0)
|
|
910
|
-
|| (Object.keys(query === null || query === void 0 ? void 0 : query[C6.WHERE]).length === 0)) {
|
|
911
|
-
console.error(query);
|
|
912
|
-
throw Error('Failed to parse primary key information. Query: (' + JSON.stringify(query) + ') Primary Key: (' + JSON.stringify(primaryKey) + ') TABLES[operatingTable]?.PRIMARY: (' + JSON.stringify((_l = TABLES[operatingTable]) === null || _l === void 0 ? void 0 : _l.PRIMARY) + ') for operatingTable (' + operatingTable + ').');
|
|
886
|
+
_p.label = 8;
|
|
887
|
+
case 8:
|
|
888
|
+
if (debug && isDevelopment) {
|
|
889
|
+
toast.success("DEVS: Request not in cache." + (requestMethod === C6.GET ? "Page (" + query[C6.PAGINATION][C6.PAGE] + ")." : '') + " Logging cache 2 console.", toastOptionsDevs);
|
|
913
890
|
}
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
if (
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
891
|
+
return [3 /*break*/, 12];
|
|
892
|
+
case 9:
|
|
893
|
+
if (!cacheResults) return [3 /*break*/, 12];
|
|
894
|
+
if (!cacheResult) return [3 /*break*/, 11];
|
|
895
|
+
cacheCheck = checkCache(cacheResult, requestMethod, tableName, this.request);
|
|
896
|
+
if (!(false !== cacheCheck)) return [3 /*break*/, 11];
|
|
897
|
+
return [4 /*yield*/, cacheCheck];
|
|
898
|
+
case 10: return [2 /*return*/, (_h = (_p.sent())) === null || _h === void 0 ? void 0 : _h.data];
|
|
899
|
+
case 11:
|
|
900
|
+
cachingConfirmed = true;
|
|
901
|
+
_p.label = 12;
|
|
902
|
+
case 12:
|
|
903
|
+
returnGetNextPageFunction = false;
|
|
904
|
+
restRequestUri = restURL + operatingTable + '/';
|
|
905
|
+
needsConditionOrPrimaryCheck = (PUT === requestMethod || DELETE === requestMethod)
|
|
906
|
+
&& false === skipPrimaryCheck;
|
|
907
|
+
TABLES = C6.TABLES;
|
|
908
|
+
primaryKey = (_m = (_l = (_k = structuredClone((_j = TABLES[operatingTable]) === null || _j === void 0 ? void 0 : _j.PRIMARY)) === null || _k === void 0 ? void 0 : _k.pop()) === null || _l === void 0 ? void 0 : _l.split('.')) === null || _m === void 0 ? void 0 : _m.pop();
|
|
909
|
+
if (needsConditionOrPrimaryCheck) {
|
|
910
|
+
if (undefined === primaryKey) {
|
|
911
|
+
if (null === query
|
|
912
|
+
|| undefined === query
|
|
913
|
+
|| undefined === (query === null || query === void 0 ? void 0 : query[C6.WHERE])
|
|
914
|
+
|| (true === Array.isArray(query[C6.WHERE])
|
|
915
|
+
|| query[C6.WHERE].length === 0)
|
|
916
|
+
|| (Object.keys(query === null || query === void 0 ? void 0 : query[C6.WHERE]).length === 0)) {
|
|
917
|
+
console.error(query);
|
|
918
|
+
throw Error('Failed to parse primary key information. Query: (' + JSON.stringify(query) + ') Primary Key: (' + JSON.stringify(primaryKey) + ') TABLES[operatingTable]?.PRIMARY: (' + JSON.stringify((_o = TABLES[operatingTable]) === null || _o === void 0 ? void 0 : _o.PRIMARY) + ') for operatingTable (' + operatingTable + ').');
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
else {
|
|
922
|
+
if (undefined === query
|
|
923
|
+
|| null === query
|
|
924
|
+
|| false === primaryKey in query) {
|
|
925
|
+
if (true === debug && isDevelopment) {
|
|
926
|
+
toast.error('DEVS: The primary key (' + primaryKey + ') was not provided!!');
|
|
927
|
+
}
|
|
928
|
+
throw Error('You must provide the primary key (' + primaryKey + ') for table (' + operatingTable + '). Request (' + JSON.stringify(this.request, undefined, 4) + ') Query (' + JSON.stringify(query) + ')');
|
|
929
|
+
}
|
|
930
|
+
if (undefined === (query === null || query === void 0 ? void 0 : query[primaryKey])
|
|
931
|
+
|| null === (query === null || query === void 0 ? void 0 : query[primaryKey])) {
|
|
932
|
+
toast.error('The primary key (' + primaryKey + ') provided is undefined or null explicitly!!');
|
|
933
|
+
throw Error('The primary key (' + primaryKey + ') provided in the request was exactly equal to undefined.');
|
|
934
|
+
}
|
|
921
935
|
}
|
|
922
|
-
throw Error('You must provide the primary key (' + primaryKey + ') for table (' + operatingTable + '). Request (' + JSON.stringify(this.request, undefined, 4) + ') Query (' + JSON.stringify(query) + ')');
|
|
923
936
|
}
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
937
|
+
// A part of me exists that wants to remove this, but it's a good feature
|
|
938
|
+
// this allows developers the ability to cache requests based on primary key
|
|
939
|
+
// for tables like `photos` this can be a huge performance boost
|
|
940
|
+
if (undefined !== query
|
|
941
|
+
&& null !== query
|
|
942
|
+
&& undefined !== primaryKey
|
|
943
|
+
&& primaryKey in query) {
|
|
944
|
+
restRequestUri += query[primaryKey] + '/';
|
|
945
|
+
removedPkValue_1 = query[primaryKey];
|
|
946
|
+
addBackPK = function () {
|
|
947
|
+
query !== null && query !== void 0 ? query : (query = {});
|
|
948
|
+
query[primaryKey] = removedPkValue_1;
|
|
949
|
+
};
|
|
950
|
+
delete query[primaryKey];
|
|
951
|
+
console.log('query', query, 'primaryKey', primaryKey, 'removedPkValue', removedPkValue_1);
|
|
928
952
|
}
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
// A part of me exists that wants to remove this, but it's a good feature
|
|
932
|
-
// this allows developers the ability to cache requests based on primary key
|
|
933
|
-
// for tables like `photos` this can be a huge performance boost
|
|
934
|
-
if (undefined !== query
|
|
935
|
-
&& null !== query
|
|
936
|
-
&& undefined !== primaryKey
|
|
937
|
-
&& primaryKey in query) {
|
|
938
|
-
restRequestUri += query[primaryKey] + '/';
|
|
939
|
-
removedPkValue_1 = query[primaryKey];
|
|
940
|
-
addBackPK = function () {
|
|
941
|
-
query !== null && query !== void 0 ? query : (query = {});
|
|
942
|
-
query[primaryKey] = removedPkValue_1;
|
|
943
|
-
};
|
|
944
|
-
delete query[primaryKey];
|
|
945
|
-
console.log('query', query, 'primaryKey', primaryKey, 'removedPkValue', removedPkValue_1);
|
|
946
|
-
}
|
|
947
|
-
else {
|
|
948
|
-
console.log('query', query);
|
|
949
|
-
}
|
|
950
|
-
try {
|
|
951
|
-
console.groupCollapsed('%c API: (' + requestMethod + ') Request Query for (' + operatingTable + ') is about to fire, will return with promise!', 'color: #A020F0');
|
|
952
|
-
console.log(this.request);
|
|
953
|
-
console.log('%c If this is the first request for this datatype; thus the value being set is currently undefined, please remember to update the state to null.', 'color: #A020F0');
|
|
954
|
-
console.log('%c Remember undefined indicated the request has not fired, null indicates the request is firing, an empty array would signal no data was returned for the sql stmt.', 'color: #A020F0');
|
|
955
|
-
console.trace();
|
|
956
|
-
console.groupEnd();
|
|
957
|
-
this.runLifecycleHooks("beforeExecution", {
|
|
958
|
-
config: this.config,
|
|
959
|
-
request: this.request
|
|
960
|
-
});
|
|
961
|
-
axiosActiveRequest = (_e = axios)[requestMethod.toLowerCase()].apply(_e, __spreadArray([restRequestUri], (function () {
|
|
962
|
-
var convert = function (data) {
|
|
963
|
-
return convertForRequestBody(data, fullTableList, C6, function (message) { return toast.error(message, toastOptions); });
|
|
964
|
-
};
|
|
965
|
-
var baseConfig = {
|
|
966
|
-
withCredentials: withCredentials,
|
|
967
|
-
};
|
|
968
|
-
switch (requestMethod) {
|
|
969
|
-
case GET:
|
|
970
|
-
return [__assign(__assign({}, baseConfig), { params: query })];
|
|
971
|
-
case POST:
|
|
972
|
-
if (dataInsertMultipleRows !== undefined) {
|
|
973
|
-
return [
|
|
974
|
-
dataInsertMultipleRows.map(convert),
|
|
975
|
-
baseConfig
|
|
976
|
-
];
|
|
977
|
-
}
|
|
978
|
-
return [convert(query), baseConfig];
|
|
979
|
-
case PUT:
|
|
980
|
-
return [convert(query), baseConfig];
|
|
981
|
-
case DELETE:
|
|
982
|
-
return [__assign(__assign({}, baseConfig), { data: convert(query) })];
|
|
983
|
-
default:
|
|
984
|
-
throw new Error("The request method (".concat(requestMethod, ") was not recognized."));
|
|
953
|
+
else {
|
|
954
|
+
console.log('query', query);
|
|
985
955
|
}
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
switch (
|
|
1005
|
-
case
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
}
|
|
1014
|
-
if (cachingConfirmed) {
|
|
1015
|
-
cacheIndex = apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
|
|
1016
|
-
apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction;
|
|
1017
|
-
// only cache get method requests
|
|
1018
|
-
apiRequestCache[cacheIndex].response = response;
|
|
1019
|
-
}
|
|
1020
|
-
this.runLifecycleHooks("afterExecution", {
|
|
1021
|
-
config: this.config,
|
|
1022
|
-
request: this.request,
|
|
1023
|
-
response: response
|
|
1024
|
-
});
|
|
1025
|
-
// todo - this feels dumb now, but i digress
|
|
1026
|
-
apiResponse = TestRestfulResponse(response, success, error);
|
|
1027
|
-
if (false === apiResponse) {
|
|
1028
|
-
if (debug && isDevelopment) {
|
|
1029
|
-
toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
|
|
1030
|
-
}
|
|
1031
|
-
return [2 /*return*/, response];
|
|
1032
|
-
}
|
|
1033
|
-
callback = function () { return _this.runLifecycleHooks("afterCommit", {
|
|
1034
|
-
config: _this.config,
|
|
1035
|
-
request: _this.request,
|
|
1036
|
-
response: response
|
|
1037
|
-
}); };
|
|
1038
|
-
if (undefined !== reactBootstrap && response) {
|
|
1039
|
-
switch (requestMethod) {
|
|
1040
|
-
case GET:
|
|
1041
|
-
reactBootstrap.updateRestfulObjectArrays({
|
|
1042
|
-
dataOrCallback: Array.isArray(response.data.rest) ? response.data.rest : [response.data.rest],
|
|
1043
|
-
stateKey: this.config.restModel.TABLE_NAME,
|
|
1044
|
-
uniqueObjectId: this.config.restModel.PRIMARY_SHORT,
|
|
1045
|
-
callback: callback
|
|
1046
|
-
});
|
|
1047
|
-
break;
|
|
1048
|
-
case POST:
|
|
1049
|
-
this.postState(response, this.request, callback);
|
|
1050
|
-
break;
|
|
1051
|
-
case PUT:
|
|
1052
|
-
this.putState(response, this.request, callback);
|
|
1053
|
-
break;
|
|
1054
|
-
case DELETE:
|
|
1055
|
-
this.deleteState(response, this.request, callback);
|
|
1056
|
-
break;
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
else {
|
|
1060
|
-
callback();
|
|
1061
|
-
}
|
|
1062
|
-
if (!(C6.GET === requestMethod)) return [3 /*break*/, 6];
|
|
1063
|
-
responseData_1 = response.data;
|
|
1064
|
-
returnGetNextPageFunction = 1 !== ((_d = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _d === void 0 ? void 0 : _d[C6.LIMIT]) &&
|
|
1065
|
-
((_e = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _e === void 0 ? void 0 : _e[C6.LIMIT]) === responseData_1.rest.length;
|
|
1066
|
-
if (false === isTest || true === isVerbose) {
|
|
1067
|
-
console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + ((_f = responseData_1.rest) === null || _f === void 0 ? void 0 : _f.length) + ') of possible (' + ((_g = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _g === void 0 ? void 0 : _g[C6.LIMIT]) + ') limit!', 'color: #0c0');
|
|
1068
|
-
console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0');
|
|
1069
|
-
console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', this.request);
|
|
1070
|
-
console.log('%c Response Data:', 'color: #0c0', responseData_1.rest);
|
|
1071
|
-
console.log('%c Will return get next page function:' + (1 !== ((_h = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _h === void 0 ? void 0 : _h[C6.LIMIT]) ? '' : ' (Will not return with explicit limit 1 set)'), 'color: #0c0', true === returnGetNextPageFunction);
|
|
1072
|
-
console.trace();
|
|
1073
|
-
console.groupEnd();
|
|
1074
|
-
}
|
|
1075
|
-
if (false === returnGetNextPageFunction
|
|
1076
|
-
&& true === debug
|
|
1077
|
-
&& isDevelopment) {
|
|
1078
|
-
toast.success("DEVS: Response returned length (" + ((_j = responseData_1.rest) === null || _j === void 0 ? void 0 : _j.length) + ") less than limit (" + ((_k = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _k === void 0 ? void 0 : _k[C6.LIMIT]) + ").", toastOptionsDevs);
|
|
956
|
+
try {
|
|
957
|
+
console.groupCollapsed('%c API: (' + requestMethod + ') Request Query for (' + operatingTable + ') is about to fire, will return with promise!', 'color: #A020F0');
|
|
958
|
+
console.log(this.request);
|
|
959
|
+
console.log('%c If this is the first request for this datatype; thus the value being set is currently undefined, please remember to update the state to null.', 'color: #A020F0');
|
|
960
|
+
console.log('%c Remember undefined indicated the request has not fired, null indicates the request is firing, an empty array would signal no data was returned for the sql stmt.', 'color: #A020F0');
|
|
961
|
+
console.trace();
|
|
962
|
+
console.groupEnd();
|
|
963
|
+
this.runLifecycleHooks("beforeExecution", {
|
|
964
|
+
config: this.config,
|
|
965
|
+
request: this.request
|
|
966
|
+
});
|
|
967
|
+
axiosActiveRequest = (_e = axios)[requestMethod.toLowerCase()].apply(_e, __spreadArray([restRequestUri], (function () {
|
|
968
|
+
var convert = function (data) {
|
|
969
|
+
return convertForRequestBody(data, fullTableList, C6, function (message) { return toast.error(message, toastOptions); });
|
|
970
|
+
};
|
|
971
|
+
var baseConfig = {
|
|
972
|
+
withCredentials: withCredentials,
|
|
973
|
+
};
|
|
974
|
+
switch (requestMethod) {
|
|
975
|
+
case GET:
|
|
976
|
+
return [__assign(__assign({}, baseConfig), { params: query })];
|
|
977
|
+
case POST:
|
|
978
|
+
if (dataInsertMultipleRows !== undefined) {
|
|
979
|
+
return [
|
|
980
|
+
dataInsertMultipleRows.map(convert),
|
|
981
|
+
baseConfig
|
|
982
|
+
];
|
|
1079
983
|
}
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
984
|
+
return [convert(query), baseConfig];
|
|
985
|
+
case PUT:
|
|
986
|
+
return [convert(query), baseConfig];
|
|
987
|
+
case DELETE:
|
|
988
|
+
return [__assign(__assign({}, baseConfig), { data: convert(query) })];
|
|
989
|
+
default:
|
|
990
|
+
throw new Error("The request method (".concat(requestMethod, ") was not recognized."));
|
|
991
|
+
}
|
|
992
|
+
})(), false));
|
|
993
|
+
if (cachingConfirmed) {
|
|
994
|
+
// push to cache so we do not repeat the request
|
|
995
|
+
apiRequestCache.push({
|
|
996
|
+
requestArgumentsSerialized: querySerialized,
|
|
997
|
+
request: axiosActiveRequest
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
// todo - wip verify this works
|
|
1001
|
+
// we had removed the value from the request to add to the URI.
|
|
1002
|
+
addBackPK === null || addBackPK === void 0 ? void 0 : addBackPK(); // adding back so post-processing methods work
|
|
1003
|
+
// returning the promise with this then is important for tests. todo - we could make that optional.
|
|
1004
|
+
// https://rapidapi.com/guides/axios-async-await
|
|
1005
|
+
return [2 /*return*/, axiosActiveRequest.then(function (response) { return __awaiter(_this, void 0, void 0, function () {
|
|
1006
|
+
var cacheIndex, callback, responseData_1, dependencies_1, fetchReferences_1, apiRequestPromises, _loop_1, _a, _b, _c, _i, tableToFetch;
|
|
1007
|
+
var _this = this;
|
|
1008
|
+
var _d, _e, _f, _g, _h, _j, _k;
|
|
1009
|
+
return __generator(this, function (_l) {
|
|
1010
|
+
switch (_l.label) {
|
|
1011
|
+
case 0:
|
|
1012
|
+
// noinspection SuspiciousTypeOfGuard
|
|
1013
|
+
if (typeof response.data === 'string') {
|
|
1014
|
+
if (isTest) {
|
|
1015
|
+
console.trace();
|
|
1016
|
+
throw new Error('The response data was a string this typically indicated html was sent. Make sure all cookies (' + JSON.stringify(response.config.headers) + ') needed are present! (' + response.data + ')');
|
|
1102
1017
|
}
|
|
1103
|
-
return
|
|
1104
|
-
}
|
|
1105
|
-
)
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1018
|
+
return [2 /*return*/, Promise.reject(response)];
|
|
1019
|
+
}
|
|
1020
|
+
if (cachingConfirmed) {
|
|
1021
|
+
cacheIndex = apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
|
|
1022
|
+
apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction;
|
|
1023
|
+
// only cache get method requests
|
|
1024
|
+
apiRequestCache[cacheIndex].response = response;
|
|
1025
|
+
}
|
|
1026
|
+
this.runLifecycleHooks("afterExecution", {
|
|
1027
|
+
config: this.config,
|
|
1028
|
+
request: this.request,
|
|
1029
|
+
response: response
|
|
1030
|
+
});
|
|
1031
|
+
// todo - this feels dumb now, but i digress
|
|
1032
|
+
apiResponse = TestRestfulResponse(response, success, error);
|
|
1033
|
+
if (false === apiResponse) {
|
|
1034
|
+
if (debug && isDevelopment) {
|
|
1035
|
+
toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
|
|
1109
1036
|
}
|
|
1110
|
-
return
|
|
1111
|
-
}, {})
|
|
1112
|
-
: {});
|
|
1113
|
-
}
|
|
1114
|
-
else {
|
|
1115
|
-
// this is the natural mysql context
|
|
1116
|
-
dependencies_1 = __assign(__assign({}, fetchDependencies & eFetchDependencies.REFERENCED // REFERENCED === CHILDREN
|
|
1117
|
-
? C6.TABLES[operatingTable].TABLE_REFERENCED_BY
|
|
1118
|
-
: {}), fetchDependencies & eFetchDependencies.REFERENCES // REFERENCES === PARENTS
|
|
1119
|
-
? C6.TABLES[operatingTable].TABLE_REFERENCES
|
|
1120
|
-
: {});
|
|
1121
|
-
}
|
|
1122
|
-
fetchReferences_1 = {};
|
|
1123
|
-
apiRequestPromises = [];
|
|
1124
|
-
console.log('%c Dependencies', 'color: #005555', dependencies_1);
|
|
1125
|
-
Object.keys(dependencies_1)
|
|
1126
|
-
.forEach(function (column) { return dependencies_1[column]
|
|
1127
|
-
.forEach(function (constraint) {
|
|
1128
|
-
var _a, _b, _c, _d;
|
|
1129
|
-
var _e, _f, _g;
|
|
1130
|
-
var columnValues = (_b = (_a = responseData_1.rest[column]) !== null && _a !== void 0 ? _a : responseData_1.rest.map(function (row) {
|
|
1131
|
-
if (operatingTable.endsWith("carbons")
|
|
1132
|
-
&& 'entity_tag' in row
|
|
1133
|
-
&& !constraint.TABLE.endsWith(row['entity_tag'].split('\\').pop().toLowerCase())) {
|
|
1134
|
-
return false; // map
|
|
1037
|
+
return [2 /*return*/, response];
|
|
1135
1038
|
}
|
|
1136
|
-
|
|
1137
|
-
|
|
1039
|
+
callback = function () { return _this.runLifecycleHooks("afterCommit", {
|
|
1040
|
+
config: _this.config,
|
|
1041
|
+
request: _this.request,
|
|
1042
|
+
response: response
|
|
1043
|
+
}); };
|
|
1044
|
+
if (undefined !== reactBootstrap && response) {
|
|
1045
|
+
switch (requestMethod) {
|
|
1046
|
+
case GET:
|
|
1047
|
+
reactBootstrap.updateRestfulObjectArrays({
|
|
1048
|
+
dataOrCallback: Array.isArray(response.data.rest) ? response.data.rest : [response.data.rest],
|
|
1049
|
+
stateKey: this.config.restModel.TABLE_NAME,
|
|
1050
|
+
uniqueObjectId: this.config.restModel.PRIMARY_SHORT,
|
|
1051
|
+
callback: callback
|
|
1052
|
+
});
|
|
1053
|
+
break;
|
|
1054
|
+
case POST:
|
|
1055
|
+
this.postState(response, this.request, callback);
|
|
1056
|
+
break;
|
|
1057
|
+
case PUT:
|
|
1058
|
+
this.putState(response, this.request, callback);
|
|
1059
|
+
break;
|
|
1060
|
+
case DELETE:
|
|
1061
|
+
this.deleteState(response, this.request, callback);
|
|
1062
|
+
break;
|
|
1063
|
+
}
|
|
1138
1064
|
}
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1065
|
+
else {
|
|
1066
|
+
callback();
|
|
1067
|
+
}
|
|
1068
|
+
if (!(C6.GET === requestMethod)) return [3 /*break*/, 6];
|
|
1069
|
+
responseData_1 = response.data;
|
|
1070
|
+
returnGetNextPageFunction = 1 !== ((_d = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _d === void 0 ? void 0 : _d[C6.LIMIT]) &&
|
|
1071
|
+
((_e = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _e === void 0 ? void 0 : _e[C6.LIMIT]) === responseData_1.rest.length;
|
|
1072
|
+
if (false === isTest || true === isVerbose) {
|
|
1073
|
+
console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + ((_f = responseData_1.rest) === null || _f === void 0 ? void 0 : _f.length) + ') of possible (' + ((_g = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _g === void 0 ? void 0 : _g[C6.LIMIT]) + ') limit!', 'color: #0c0');
|
|
1074
|
+
console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0');
|
|
1075
|
+
console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', this.request);
|
|
1076
|
+
console.log('%c Response Data:', 'color: #0c0', responseData_1.rest);
|
|
1077
|
+
console.log('%c Will return get next page function:' + (1 !== ((_h = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _h === void 0 ? void 0 : _h[C6.LIMIT]) ? '' : ' (Will not return with explicit limit 1 set)'), 'color: #0c0', true === returnGetNextPageFunction);
|
|
1078
|
+
console.trace();
|
|
1079
|
+
console.groupEnd();
|
|
1080
|
+
}
|
|
1081
|
+
if (false === returnGetNextPageFunction
|
|
1082
|
+
&& true === debug
|
|
1083
|
+
&& isDevelopment) {
|
|
1084
|
+
toast.success("DEVS: Response returned length (" + ((_j = responseData_1.rest) === null || _j === void 0 ? void 0 : _j.length) + ") less than limit (" + ((_k = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _k === void 0 ? void 0 : _k[C6.LIMIT]) + ").", toastOptionsDevs);
|
|
1085
|
+
}
|
|
1086
|
+
if (!(fetchDependencies
|
|
1087
|
+
&& 'number' === typeof fetchDependencies
|
|
1088
|
+
&& responseData_1.rest.length > 0)) return [3 /*break*/, 6];
|
|
1089
|
+
console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')'
|
|
1090
|
+
+ (fetchDependencies & eFetchDependencies.CHILDREN ? ' | (CHILDREN|REFERENCED) ' : '')
|
|
1091
|
+
+ (fetchDependencies & eFetchDependencies.PARENTS ? ' | (PARENTS|REFERENCED_BY)' : '')
|
|
1092
|
+
+ (fetchDependencies & eFetchDependencies.C6ENTITY ? ' | (C6ENTITY)' : '')
|
|
1093
|
+
+ (fetchDependencies & eFetchDependencies.RECURSIVE ? ' | (RECURSIVE)' : ''), 'color: #33ccff');
|
|
1094
|
+
console.groupCollapsed('Collapsed JS Trace');
|
|
1095
|
+
console.trace(); // hidden in collapsed group
|
|
1096
|
+
console.groupEnd();
|
|
1097
|
+
dependencies_1 = {};
|
|
1098
|
+
if (fetchDependencies & eFetchDependencies.C6ENTITY) {
|
|
1099
|
+
dependencies_1 = operatingTable.endsWith("carbon_carbons")
|
|
1100
|
+
? __assign(__assign({}, fetchDependencies & eFetchDependencies.CHILDREN // REFERENCED === CHILDREN
|
|
1101
|
+
? C6.TABLES[operatingTable].TABLE_REFERENCED_BY
|
|
1102
|
+
: {}), fetchDependencies & eFetchDependencies.PARENTS // REFERENCES === PARENTS
|
|
1103
|
+
? C6.TABLES[operatingTable].TABLE_REFERENCES
|
|
1104
|
+
: {}) : __assign(__assign({}, fetchDependencies & eFetchDependencies.CHILDREN // REFERENCED === CHILDREN
|
|
1105
|
+
? __assign(__assign({}, Object.keys(C6.TABLES[operatingTable].TABLE_REFERENCES).reduce(function (accumulator, columnName) {
|
|
1106
|
+
if (!C6.TABLES[operatingTable].PRIMARY_SHORT.includes(columnName)) {
|
|
1107
|
+
accumulator[columnName] = C6.TABLES[operatingTable].TABLE_REFERENCES[columnName];
|
|
1170
1108
|
}
|
|
1171
|
-
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
console.log('%c Fetch Dependencies will select (' + tableToFetch + ') using GET request', 'color: #33ccff');
|
|
1178
|
-
nextFetchDependencies = eFetchDependencies.NONE;
|
|
1179
|
-
if (fetchDependencies & eFetchDependencies.RECURSIVE) {
|
|
1180
|
-
if (fetchDependencies & eFetchDependencies.ALL) {
|
|
1181
|
-
throw Error('Recursive fetch dependencies with both PARENT and CHILD reference will result in an infin1ite loop. As there is not real ending condition, this is not supported.');
|
|
1109
|
+
return accumulator;
|
|
1110
|
+
}, {})), C6.TABLES[operatingTable].TABLE_REFERENCED_BY // it is unlikely that a C6 table will have any TABLE_REFERENCED_BY
|
|
1111
|
+
) : {}), fetchDependencies & eFetchDependencies.PARENTS // REFERENCES === PARENTS
|
|
1112
|
+
? C6.TABLES[operatingTable].PRIMARY_SHORT.reduce(function (accumulator, primaryKey) {
|
|
1113
|
+
if (primaryKey in C6.TABLES[operatingTable].TABLE_REFERENCES) {
|
|
1114
|
+
accumulator[primaryKey] = C6.TABLES[operatingTable].TABLE_REFERENCES[primaryKey];
|
|
1182
1115
|
}
|
|
1183
|
-
|
|
1116
|
+
return accumulator;
|
|
1117
|
+
}, {})
|
|
1118
|
+
: {});
|
|
1119
|
+
}
|
|
1120
|
+
else {
|
|
1121
|
+
// this is the natural mysql context
|
|
1122
|
+
dependencies_1 = __assign(__assign({}, fetchDependencies & eFetchDependencies.REFERENCED // REFERENCED === CHILDREN
|
|
1123
|
+
? C6.TABLES[operatingTable].TABLE_REFERENCED_BY
|
|
1124
|
+
: {}), fetchDependencies & eFetchDependencies.REFERENCES // REFERENCES === PARENTS
|
|
1125
|
+
? C6.TABLES[operatingTable].TABLE_REFERENCES
|
|
1126
|
+
: {});
|
|
1127
|
+
}
|
|
1128
|
+
fetchReferences_1 = {};
|
|
1129
|
+
apiRequestPromises = [];
|
|
1130
|
+
console.log('%c Dependencies', 'color: #005555', dependencies_1);
|
|
1131
|
+
Object.keys(dependencies_1)
|
|
1132
|
+
.forEach(function (column) { return dependencies_1[column]
|
|
1133
|
+
.forEach(function (constraint) {
|
|
1134
|
+
var _a, _b, _c, _d;
|
|
1135
|
+
var _e, _f, _g;
|
|
1136
|
+
var columnValues = (_b = (_a = responseData_1.rest[column]) !== null && _a !== void 0 ? _a : responseData_1.rest.map(function (row) {
|
|
1137
|
+
if (operatingTable.endsWith("carbons")
|
|
1138
|
+
&& 'entity_tag' in row
|
|
1139
|
+
&& !constraint.TABLE.endsWith(row['entity_tag'].split('\\').pop().toLowerCase())) {
|
|
1140
|
+
return false; // map
|
|
1184
1141
|
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
nextFetchDependencies = fetchDependencies;
|
|
1188
|
-
}
|
|
1189
|
-
else {
|
|
1190
|
-
nextFetchDependencies = fetchDependencies ^ eFetchDependencies.C6ENTITY;
|
|
1191
|
-
}
|
|
1142
|
+
if (!(column in row)) {
|
|
1143
|
+
return false;
|
|
1192
1144
|
}
|
|
1193
|
-
|
|
1194
|
-
//
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1145
|
+
// todo - row[column] is a FK value, we should optionally remove values that are already in state
|
|
1146
|
+
// this could be any column in the table constraint.TABLE, not just the primary key
|
|
1147
|
+
return row[column];
|
|
1148
|
+
}).filter(function (n) { return n; })) !== null && _b !== void 0 ? _b : [];
|
|
1149
|
+
if (columnValues.length === 0) {
|
|
1150
|
+
return; // forEach
|
|
1151
|
+
}
|
|
1152
|
+
(_c = fetchReferences_1[_e = constraint.TABLE]) !== null && _c !== void 0 ? _c : (fetchReferences_1[_e] = {});
|
|
1153
|
+
(_d = (_f = fetchReferences_1[constraint.TABLE])[_g = constraint.COLUMN]) !== null && _d !== void 0 ? _d : (_f[_g] = []);
|
|
1154
|
+
fetchReferences_1[constraint.TABLE][constraint.COLUMN].push(columnValues);
|
|
1155
|
+
}); });
|
|
1156
|
+
console.log('fetchReferences', fetchReferences_1);
|
|
1157
|
+
_loop_1 = function (tableToFetch) {
|
|
1158
|
+
var referencesTables, shouldContinue, fetchTable, RestApi, nextFetchDependencies;
|
|
1159
|
+
var _m;
|
|
1160
|
+
return __generator(this, function (_o) {
|
|
1161
|
+
switch (_o.label) {
|
|
1162
|
+
case 0:
|
|
1163
|
+
if (fetchDependencies & eFetchDependencies.C6ENTITY
|
|
1164
|
+
&& 'string' === typeof tableName
|
|
1165
|
+
&& tableName.endsWith("carbon_carbons")) {
|
|
1166
|
+
referencesTables = responseData_1.rest.reduce(function (accumulator, row) {
|
|
1167
|
+
if ('entity_tag' in row && !accumulator.includes(row['entity_tag'])) {
|
|
1168
|
+
accumulator.push(row['entity_tag']);
|
|
1169
|
+
}
|
|
1170
|
+
return accumulator;
|
|
1171
|
+
}, []).map(function (entityTag) { return entityTag.split('\\').pop().toLowerCase(); });
|
|
1172
|
+
shouldContinue = referencesTables.find(function (referencesTable) { return tableToFetch.endsWith(referencesTable); });
|
|
1173
|
+
if (!shouldContinue) {
|
|
1174
|
+
console.log('%c C6ENTITY: The constraintTableName (' + tableToFetch + ') did not end with any value in referencesTables', 'color: #c00', referencesTables);
|
|
1175
|
+
return [2 /*return*/, "continue"];
|
|
1205
1176
|
}
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1177
|
+
console.log('%c C6ENTITY: The constraintTableName (' + tableToFetch + ') will be fetched.', 'color: #0c0');
|
|
1178
|
+
}
|
|
1179
|
+
return [4 /*yield*/, C6.IMPORT(tableToFetch)];
|
|
1180
|
+
case 1:
|
|
1181
|
+
fetchTable = _o.sent();
|
|
1182
|
+
RestApi = fetchTable.default;
|
|
1183
|
+
console.log('%c Fetch Dependencies will select (' + tableToFetch + ') using GET request', 'color: #33ccff');
|
|
1184
|
+
nextFetchDependencies = eFetchDependencies.NONE;
|
|
1185
|
+
if (fetchDependencies & eFetchDependencies.RECURSIVE) {
|
|
1186
|
+
if (fetchDependencies & eFetchDependencies.ALL) {
|
|
1187
|
+
throw Error('Recursive fetch dependencies with both PARENT and CHILD reference will result in an infin1ite loop. As there is not real ending condition, this is not supported.');
|
|
1188
|
+
}
|
|
1189
|
+
nextFetchDependencies = fetchDependencies;
|
|
1190
|
+
}
|
|
1191
|
+
else if (fetchDependencies & eFetchDependencies.C6ENTITY) {
|
|
1192
|
+
if (tableToFetch === "carbon_carbons") {
|
|
1193
|
+
nextFetchDependencies = fetchDependencies;
|
|
1194
|
+
}
|
|
1195
|
+
else {
|
|
1196
|
+
nextFetchDependencies = fetchDependencies ^ eFetchDependencies.C6ENTITY;
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
console.log('fetchReferences', fetchReferences_1[tableToFetch], "Current fetchDependencies for (" + operatingTable + "):", fetchDependencies, "New fetchDependencies for (" + tableToFetch + "): ", nextFetchDependencies);
|
|
1200
|
+
// todo - filter out ids that exist in state?!? note - remember that this does not necessarily mean the pk, but only known is its an FK to somewhere
|
|
1201
|
+
// it not certain that they are using carbons' entities either
|
|
1202
|
+
// this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
|
|
1203
|
+
// todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
|
|
1204
|
+
apiRequestPromises.push(RestApi.Get((_m = {},
|
|
1205
|
+
_m[C6.WHERE] = {
|
|
1206
|
+
0: Object.keys(fetchReferences_1[tableToFetch]).reduce(function (sum, column) {
|
|
1207
|
+
fetchReferences_1[tableToFetch][column] = fetchReferences_1[tableToFetch][column].flat(Infinity);
|
|
1208
|
+
if (0 === fetchReferences_1[tableToFetch][column].length) {
|
|
1209
|
+
console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData_1);
|
|
1210
|
+
return false;
|
|
1211
|
+
}
|
|
1212
|
+
sum[column] = fetchReferences_1[tableToFetch][column].length === 1
|
|
1213
|
+
? fetchReferences_1[tableToFetch][column][0]
|
|
1214
|
+
: [
|
|
1215
|
+
C6.IN, fetchReferences_1[tableToFetch][column]
|
|
1216
|
+
];
|
|
1217
|
+
return sum;
|
|
1218
|
+
}, {})
|
|
1219
|
+
},
|
|
1220
|
+
_m.fetchDependencies = nextFetchDependencies,
|
|
1221
|
+
_m)));
|
|
1222
|
+
return [2 /*return*/];
|
|
1251
1223
|
}
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1224
|
+
});
|
|
1225
|
+
};
|
|
1226
|
+
_a = fetchReferences_1;
|
|
1227
|
+
_b = [];
|
|
1228
|
+
for (_c in _a)
|
|
1229
|
+
_b.push(_c);
|
|
1230
|
+
_i = 0;
|
|
1231
|
+
_l.label = 1;
|
|
1232
|
+
case 1:
|
|
1233
|
+
if (!(_i < _b.length)) return [3 /*break*/, 4];
|
|
1234
|
+
_c = _b[_i];
|
|
1235
|
+
if (!(_c in _a)) return [3 /*break*/, 3];
|
|
1236
|
+
tableToFetch = _c;
|
|
1237
|
+
return [5 /*yield**/, _loop_1(tableToFetch)];
|
|
1238
|
+
case 2:
|
|
1239
|
+
_l.sent();
|
|
1240
|
+
_l.label = 3;
|
|
1241
|
+
case 3:
|
|
1242
|
+
_i++;
|
|
1243
|
+
return [3 /*break*/, 1];
|
|
1244
|
+
case 4:
|
|
1245
|
+
console.groupEnd();
|
|
1246
|
+
return [4 /*yield*/, Promise.all(apiRequestPromises)];
|
|
1247
|
+
case 5:
|
|
1248
|
+
_l.sent();
|
|
1249
|
+
apiRequestPromises.map(function (promise) { return __awaiter(_this, void 0, void 0, function () {
|
|
1250
|
+
var _a, _b;
|
|
1251
|
+
return __generator(this, function (_c) {
|
|
1252
|
+
switch (_c.label) {
|
|
1253
|
+
case 0:
|
|
1254
|
+
if (!Array.isArray(this.request.fetchDependencies)) {
|
|
1255
|
+
// to reassign value we must ref the root
|
|
1256
|
+
this.request.fetchDependencies = [];
|
|
1257
|
+
}
|
|
1258
|
+
_b = (_a = this.request.fetchDependencies).push;
|
|
1259
|
+
return [4 /*yield*/, promise];
|
|
1260
|
+
case 1:
|
|
1261
|
+
_b.apply(_a, [_c.sent()]);
|
|
1262
|
+
return [2 /*return*/];
|
|
1263
|
+
}
|
|
1264
|
+
});
|
|
1265
|
+
}); });
|
|
1266
|
+
_l.label = 6;
|
|
1267
|
+
case 6:
|
|
1268
|
+
if (debug && isDevelopment) {
|
|
1269
|
+
toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
|
|
1257
1270
|
}
|
|
1258
|
-
|
|
1259
|
-
}); });
|
|
1260
|
-
_l.label = 6;
|
|
1261
|
-
case 6:
|
|
1262
|
-
if (debug && isDevelopment) {
|
|
1263
|
-
toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
|
|
1271
|
+
return [2 /*return*/, response];
|
|
1264
1272
|
}
|
|
1265
|
-
|
|
1266
|
-
}
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1273
|
+
});
|
|
1274
|
+
}); }).then(function (response) { return response.data; })];
|
|
1275
|
+
}
|
|
1276
|
+
catch (throwableError) {
|
|
1277
|
+
if (isTest) {
|
|
1278
|
+
throw new Error(JSON.stringify(throwableError));
|
|
1279
|
+
}
|
|
1280
|
+
console.groupCollapsed('%c API: An error occurred in the try catch block. returning null!', 'color: #ff0000');
|
|
1281
|
+
console.log('%c ' + requestMethod + ' ' + tableName, 'color: #A020F0');
|
|
1282
|
+
console.warn(throwableError);
|
|
1283
|
+
console.trace();
|
|
1284
|
+
console.groupEnd();
|
|
1285
|
+
TestRestfulResponse(throwableError, success, error);
|
|
1286
|
+
return [2 /*return*/, null];
|
|
1287
|
+
}
|
|
1288
|
+
return [2 /*return*/];
|
|
1281
1289
|
}
|
|
1282
|
-
return [2 /*return*/];
|
|
1283
1290
|
});
|
|
1284
1291
|
}); };
|
|
1285
1292
|
return [4 /*yield*/, apiRequest()];
|
|
@@ -1302,16 +1309,43 @@ var SqlExecutor = /** @class */ (function (_super) {
|
|
|
1302
1309
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
1303
1310
|
}
|
|
1304
1311
|
SqlExecutor.prototype.execute = function () {
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1312
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1313
|
+
var _a, TABLE_NAME, PRIMARY, _b, rest, result, created, result, updated, result, deleted;
|
|
1314
|
+
return __generator(this, function (_c) {
|
|
1315
|
+
switch (_c.label) {
|
|
1316
|
+
case 0:
|
|
1317
|
+
_a = this.config.restModel, TABLE_NAME = _a.TABLE_NAME, PRIMARY = _a.PRIMARY;
|
|
1318
|
+
_b = this.config.requestMethod;
|
|
1319
|
+
switch (_b) {
|
|
1320
|
+
case 'GET': return [3 /*break*/, 1];
|
|
1321
|
+
case 'POST': return [3 /*break*/, 3];
|
|
1322
|
+
case 'PUT': return [3 /*break*/, 5];
|
|
1323
|
+
case 'DELETE': return [3 /*break*/, 7];
|
|
1324
|
+
}
|
|
1325
|
+
return [3 /*break*/, 9];
|
|
1326
|
+
case 1: return [4 /*yield*/, this.select(TABLE_NAME, undefined, this.request)];
|
|
1327
|
+
case 2:
|
|
1328
|
+
rest = _c.sent();
|
|
1329
|
+
return [2 /*return*/, { rest: rest }];
|
|
1330
|
+
case 3: return [4 /*yield*/, this.insert(TABLE_NAME, this.request)];
|
|
1331
|
+
case 4:
|
|
1332
|
+
result = _c.sent();
|
|
1333
|
+
created = { rest: result, created: true };
|
|
1334
|
+
return [2 /*return*/, created];
|
|
1335
|
+
case 5: return [4 /*yield*/, this.update(TABLE_NAME, PRIMARY, this.request)];
|
|
1336
|
+
case 6:
|
|
1337
|
+
result = _c.sent();
|
|
1338
|
+
updated = { rest: result, updated: true, rowCount: result.affectedRows };
|
|
1339
|
+
return [2 /*return*/, updated];
|
|
1340
|
+
case 7: return [4 /*yield*/, this.delete(TABLE_NAME, PRIMARY, this.request)];
|
|
1341
|
+
case 8:
|
|
1342
|
+
result = _c.sent();
|
|
1343
|
+
deleted = { rest: result, deleted: true, rowCount: result.affectedRows };
|
|
1344
|
+
return [2 /*return*/, deleted];
|
|
1345
|
+
case 9: throw new Error("Unsupported request method: ".concat(this.config.requestMethod));
|
|
1346
|
+
}
|
|
1347
|
+
});
|
|
1348
|
+
});
|
|
1315
1349
|
};
|
|
1316
1350
|
SqlExecutor.prototype.withConnection = function (cb) {
|
|
1317
1351
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -1394,14 +1428,13 @@ var SqlExecutor = /** @class */ (function (_super) {
|
|
|
1394
1428
|
return __generator(this, function (_a) {
|
|
1395
1429
|
switch (_a.label) {
|
|
1396
1430
|
case 0:
|
|
1397
|
-
if (!primary)
|
|
1431
|
+
if (!(primary === null || primary === void 0 ? void 0 : primary.length))
|
|
1398
1432
|
throw new Error('Primary key is required for update');
|
|
1399
|
-
}
|
|
1400
1433
|
keys = Object.keys(data);
|
|
1401
1434
|
values = keys.map(function (k) { return data[k]; });
|
|
1402
1435
|
updates = keys.map(function (k) { return "`".concat(k, "` = ?"); }).join(', ');
|
|
1403
|
-
sql = "UPDATE `".concat(table, "` SET ").concat(updates, " WHERE `").concat(primary, "` = ?");
|
|
1404
|
-
values.push(data[primary]);
|
|
1436
|
+
sql = "UPDATE `".concat(table, "` SET ").concat(updates, " WHERE `").concat(primary[0], "` = ?");
|
|
1437
|
+
values.push(data[primary[0]]);
|
|
1405
1438
|
return [4 /*yield*/, this.withConnection(function (conn) { return __awaiter(_this, void 0, void 0, function () {
|
|
1406
1439
|
var result;
|
|
1407
1440
|
return __generator(this, function (_a) {
|
|
@@ -1420,20 +1453,20 @@ var SqlExecutor = /** @class */ (function (_super) {
|
|
|
1420
1453
|
};
|
|
1421
1454
|
SqlExecutor.prototype.delete = function (table, primary, args) {
|
|
1422
1455
|
return __awaiter(this, void 0, void 0, function () {
|
|
1423
|
-
var sql;
|
|
1456
|
+
var key, sql;
|
|
1424
1457
|
var _this = this;
|
|
1425
1458
|
return __generator(this, function (_a) {
|
|
1426
1459
|
switch (_a.label) {
|
|
1427
1460
|
case 0:
|
|
1428
|
-
|
|
1461
|
+
key = primary === null || primary === void 0 ? void 0 : primary[0];
|
|
1462
|
+
if (!key || !(args === null || args === void 0 ? void 0 : args[key]))
|
|
1429
1463
|
throw new Error('Primary key and value required for delete');
|
|
1430
|
-
|
|
1431
|
-
sql = "DELETE FROM `".concat(table, "` WHERE `").concat(primary, "` = ?");
|
|
1464
|
+
sql = "DELETE FROM `".concat(table, "` WHERE `").concat(key, "` = ?");
|
|
1432
1465
|
return [4 /*yield*/, this.withConnection(function (conn) { return __awaiter(_this, void 0, void 0, function () {
|
|
1433
1466
|
var result;
|
|
1434
1467
|
return __generator(this, function (_a) {
|
|
1435
1468
|
switch (_a.label) {
|
|
1436
|
-
case 0: return [4 /*yield*/, conn.execute(sql, [args[
|
|
1469
|
+
case 0: return [4 /*yield*/, conn.execute(sql, [args[key]])];
|
|
1437
1470
|
case 1:
|
|
1438
1471
|
result = (_a.sent())[0];
|
|
1439
1472
|
return [2 /*return*/, result];
|
|
@@ -1453,6 +1486,63 @@ var SqlExecutor$1 = /*#__PURE__*/Object.freeze({
|
|
|
1453
1486
|
SqlExecutor: SqlExecutor
|
|
1454
1487
|
});
|
|
1455
1488
|
|
|
1489
|
+
// TODO - WE MUST make this a generic - optional, but helpful
|
|
1490
|
+
// note sure how it would help anyone actually...
|
|
1491
|
+
function ExpressHandler(_a) {
|
|
1492
|
+
var _this = this;
|
|
1493
|
+
var C6 = _a.C6, mysqlPool = _a.mysqlPool;
|
|
1494
|
+
return function (req, res, next) { return __awaiter(_this, void 0, void 0, function () {
|
|
1495
|
+
var method, table, primary, payload, response, err_1;
|
|
1496
|
+
return __generator(this, function (_a) {
|
|
1497
|
+
switch (_a.label) {
|
|
1498
|
+
case 0:
|
|
1499
|
+
_a.trys.push([0, 2, , 3]);
|
|
1500
|
+
method = req.method.toUpperCase();
|
|
1501
|
+
table = req.params.table;
|
|
1502
|
+
primary = req.params.primary;
|
|
1503
|
+
payload = method === 'GET' ? req.query : req.body;
|
|
1504
|
+
if (!(table in C6.TABLES)) {
|
|
1505
|
+
res.status(400).json({ error: "Invalid table: ".concat(table) });
|
|
1506
|
+
return [2 /*return*/];
|
|
1507
|
+
}
|
|
1508
|
+
// 👇 Call restRequest for the resolved method
|
|
1509
|
+
// TODO - add primary conditionally based on method signature
|
|
1510
|
+
switch (method) {
|
|
1511
|
+
case 'GET':
|
|
1512
|
+
if (primary) {
|
|
1513
|
+
payload['primary'] = primary;
|
|
1514
|
+
}
|
|
1515
|
+
break;
|
|
1516
|
+
case 'POST':
|
|
1517
|
+
case 'PUT':
|
|
1518
|
+
case 'DELETE':
|
|
1519
|
+
payload['primary'] = primary;
|
|
1520
|
+
break;
|
|
1521
|
+
default:
|
|
1522
|
+
res.status(405).json({ error: "Method ".concat(method, " not allowed") });
|
|
1523
|
+
return [2 /*return*/];
|
|
1524
|
+
}
|
|
1525
|
+
return [4 /*yield*/, restRequest({
|
|
1526
|
+
C6: C6,
|
|
1527
|
+
mysqlPool: mysqlPool,
|
|
1528
|
+
requestMethod: method,
|
|
1529
|
+
restModel: C6.TABLES[table]
|
|
1530
|
+
})(payload)];
|
|
1531
|
+
case 1:
|
|
1532
|
+
response = _a.sent();
|
|
1533
|
+
console.log('response', JSON.stringify(response));
|
|
1534
|
+
res.status(200).json(__assign({ success: true }, response));
|
|
1535
|
+
return [3 /*break*/, 3];
|
|
1536
|
+
case 2:
|
|
1537
|
+
err_1 = _a.sent();
|
|
1538
|
+
next(err_1);
|
|
1539
|
+
return [3 /*break*/, 3];
|
|
1540
|
+
case 3: return [2 /*return*/];
|
|
1541
|
+
}
|
|
1542
|
+
});
|
|
1543
|
+
}); };
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1456
1546
|
function determineRuntimeJsType(mysqlType) {
|
|
1457
1547
|
var base = mysqlType.toLowerCase().split('(')[0];
|
|
1458
1548
|
if ([
|
|
@@ -1551,5 +1641,5 @@ function onError(message) {
|
|
|
1551
1641
|
toast.error(message, isDevelopment ? toastOptionsDevs : toastOptions);
|
|
1552
1642
|
}
|
|
1553
1643
|
|
|
1554
|
-
export { C6Constants, DELETE, Executor, GET, HttpExecutor, POST, PUT, SqlExecutor, TestRestfulResponse, apiRequestCache, axiosInstance, buildAggregateField, buildBooleanJoinedConditions, buildSelectQuery, checkAllRequestsComplete, checkCache, clearCache, convertForRequestBody, determineRuntimeJsType, eFetchDependencies, error, getEnvVar, getPrimaryKeyTypes, group, info, isDevelopment as isLocal, isNode, isTest, isVerbose, onError, onSuccess, removeInvalidKeys, removePrefixIfExists, restOrm, restRequest, sortAndSerializeQueryObject, timeout, toastOptions, toastOptionsDevs, userCustomClearCache, warn };
|
|
1644
|
+
export { C6Constants, DELETE, Executor, ExpressHandler, GET, HttpExecutor, POST, PUT, SqlExecutor, TestRestfulResponse, apiRequestCache, axiosInstance, buildAggregateField, buildBooleanJoinedConditions, buildSelectQuery, checkAllRequestsComplete, checkCache, clearCache, convertForRequestBody, determineRuntimeJsType, eFetchDependencies, error, getEnvVar, getPrimaryKeyTypes, group, info, isDevelopment as isLocal, isNode, isTest, isVerbose, onError, onSuccess, removeInvalidKeys, removePrefixIfExists, restOrm, restRequest, sortAndSerializeQueryObject, timeout, toastOptions, toastOptionsDevs, userCustomClearCache, warn };
|
|
1555
1645
|
//# sourceMappingURL=index.esm.js.map
|