@carbonorm/carbonnode 1.6.33 → 1.6.34

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.
@@ -781,246 +781,286 @@ export default function restApi<
781
781
  // https://rapidapi.com/guides/axios-async-await
782
782
  return axiosActiveRequest.then(async (response): Promise<AxiosResponse<ResponseDataType, any>> => {
783
783
 
784
- if (typeof response.data === 'string') {
784
+ if (typeof response.data === 'string') {
785
785
 
786
- if (isTest) {
786
+ if (isTest) {
787
787
 
788
- console.trace()
788
+ console.trace()
789
789
 
790
- 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 + ')')
790
+ 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 + ')')
791
791
 
792
- }
792
+ }
793
793
 
794
- return Promise.reject(response);
794
+ return Promise.reject(response);
795
795
 
796
- }
796
+ }
797
797
 
798
- if (cachingConfirmed) {
798
+ if (cachingConfirmed) {
799
799
 
800
- const cacheIndex = apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized);
800
+ const cacheIndex = apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized);
801
801
 
802
- apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction
802
+ apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction
803
803
 
804
- // only cache get method requests
805
- apiRequestCache[cacheIndex].response = response
804
+ // only cache get method requests
805
+ apiRequestCache[cacheIndex].response = response
806
806
 
807
- }
807
+ }
808
808
 
809
- apiResponse = TestRestfulResponse(response, request?.success, request?.error ?? "An unexpected API error occurred!")
809
+ apiResponse = TestRestfulResponse(response, request?.success, request?.error ?? "An unexpected API error occurred!")
810
810
 
811
- if (false === apiResponse) {
811
+ if (false === apiResponse) {
812
812
 
813
- if (request.debug && isLocal) {
813
+ if (request.debug && isLocal) {
814
814
 
815
- toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
815
+ toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
816
816
 
817
- }
817
+ }
818
818
 
819
- return response;
819
+ return response;
820
820
 
821
- }
821
+ }
822
822
 
823
- // stateful operations are done in the response callback - its leverages rest generated functions
824
- responseCallback(response, request, apiResponse)
823
+ // stateful operations are done in the response callback - its leverages rest generated functions
824
+ responseCallback(response, request, apiResponse)
825
825
 
826
- if (C6.GET === requestMethod) {
826
+ if (C6.GET === requestMethod) {
827
827
 
828
- const responseData = response.data as iGetC6RestResponse<any>;
828
+ const responseData = response.data as iGetC6RestResponse<any>;
829
829
 
830
- returnGetNextPageFunction = 1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] &&
831
- query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
830
+ returnGetNextPageFunction = 1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] &&
831
+ query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
832
832
 
833
- if (false === isTest || true === isVerbose) {
833
+ if (false === isTest || true === isVerbose) {
834
834
 
835
- console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + responseData.rest?.length + ') of possible (' + query?.[C6.PAGINATION]?.[C6.LIMIT] + ') limit!', 'color: #0c0')
835
+ console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + responseData.rest?.length + ') of possible (' + query?.[C6.PAGINATION]?.[C6.LIMIT] + ') limit!', 'color: #0c0')
836
836
 
837
- console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
837
+ console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
838
838
 
839
- console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
839
+ console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
840
840
 
841
- console.log('%c Response Data:', 'color: #0c0', responseData.rest)
841
+ console.log('%c Response Data:', 'color: #0c0', responseData.rest)
842
842
 
843
- console.log('%c Will return get next page function:' + (1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] ? '' : ' (Will not return with explicit limit 1 set)'), 'color: #0c0', true === returnGetNextPageFunction)
843
+ console.log('%c Will return get next page function:' + (1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] ? '' : ' (Will not return with explicit limit 1 set)'), 'color: #0c0', true === returnGetNextPageFunction)
844
844
 
845
- console.trace();
845
+ console.trace();
846
846
 
847
- console.groupEnd()
847
+ console.groupEnd()
848
848
 
849
- }
849
+ }
850
850
 
851
- if (false === returnGetNextPageFunction
852
- && true === request.debug
853
- && isLocal) {
851
+ if (false === returnGetNextPageFunction
852
+ && true === request.debug
853
+ && isLocal) {
854
854
 
855
- toast.success("DEVS: Response returned length (" + responseData.rest?.length + ") less than limit (" + query?.[C6.PAGINATION]?.[C6.LIMIT] + ").", toastOptionsDevs);
855
+ toast.success("DEVS: Response returned length (" + responseData.rest?.length + ") less than limit (" + query?.[C6.PAGINATION]?.[C6.LIMIT] + ").", toastOptionsDevs);
856
856
 
857
- }
857
+ }
858
858
 
859
- request.fetchDependencies ??= eFetchDependencies.NONE;
859
+ request.fetchDependencies ??= eFetchDependencies.NONE;
860
+
861
+ if (request.fetchDependencies
862
+ && 'number' === typeof request.fetchDependencies
863
+ && responseData.rest.length > 0) {
864
+
865
+ const fetchDependencies = request.fetchDependencies as number;
866
+
867
+ console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')'
868
+ + (fetchDependencies & eFetchDependencies.CHILDREN ? ' | (CHILDREN|REFERENCED) ' : '')
869
+ + (fetchDependencies & eFetchDependencies.PARENTS ? ' | (PARENTS|REFRENCED_BY)' : '')
870
+ + (fetchDependencies & eFetchDependencies.C6ENTITY ? ' | (C6ENTITY)' : '')
871
+ + (fetchDependencies & eFetchDependencies.RECURSIVE ? ' | (RECURSIVE)' : ''), 'color: #33ccff')
872
+
873
+ console.groupCollapsed('Collapsed JS Trace');
874
+ console.trace(); // hidden in collapsed group
875
+ console.groupEnd();
876
+
877
+ // noinspection JSBitwiseOperatorUsage
878
+ let dependencies: {
879
+ [key: string]: iConstraint[]
880
+ } = {};
881
+
882
+ if (fetchDependencies & eFetchDependencies.C6ENTITY) {
883
+
884
+ // if we get PARENTS in a C6 context we need to remove all entities that are not the operatingTables primary key
885
+
886
+ dependencies = operatingTable.endsWith("carbon_carbons")
887
+ ? {
888
+ // the context of the entity system is a bit different
889
+ ...fetchDependencies & eFetchDependencies.CHILDREN // REFERENCED === CHILDREN
890
+ ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY
891
+ : {},
892
+ ...fetchDependencies & eFetchDependencies.PARENTS // REFERENCES === PARENTS
893
+ ? C6.TABLES[operatingTable].TABLE_REFERENCES
894
+ : {}
895
+ } : {
896
+ // the context of the entity system is a bit different
897
+ ...fetchDependencies & eFetchDependencies.CHILDREN // REFERENCED === CHILDREN
898
+ ? C6.TABLES[operatingTable].TABLE_REFERENCES
899
+ : {},
900
+ ...fetchDependencies & eFetchDependencies.PARENTS // REFERENCES === PARENTS
901
+ ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY
902
+ : {}
903
+ }
860
904
 
861
- if (request.fetchDependencies
862
- && 'number' === typeof request.fetchDependencies
863
- && responseData.rest.length > 0) {
905
+ } else {
864
906
 
865
- const fetchDependencies = request.fetchDependencies as number;
907
+ // this is the natural mysql context
908
+ dependencies = {
909
+ ...fetchDependencies & eFetchDependencies.REFERENCED // REFERENCED === CHILDREN
910
+ ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY
911
+ : {},
912
+ ...fetchDependencies & eFetchDependencies.REFERENCES // REFERENCES === PARENTS
913
+ ? C6.TABLES[operatingTable].TABLE_REFERENCES
914
+ : {}
915
+ };
866
916
 
867
- console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')'
868
- + (fetchDependencies & eFetchDependencies.CHILDREN ? ' | (CHILDREN|REFERENCED) ' : '')
869
- + (fetchDependencies & eFetchDependencies.PARENTS ? ' | (PARENTS|REFRENCED_BY)' : '')
870
- + (fetchDependencies & eFetchDependencies.C6ENTITY ? ' | (C6ENTITY)' : '')
871
- + (fetchDependencies & eFetchDependencies.RECURSIVE ? ' | (RECURSIVE)' : ''), 'color: #33ccff')
917
+ }
872
918
 
873
- console.groupCollapsed('Collapsed JS Trace');
874
- console.trace(); // hidden in collapsed group
875
- console.groupEnd();
919
+ let fetchReferences: {
920
+ [externalTable: string]: {
921
+ [column: string]: string[]
922
+ }
923
+ } = {}
876
924
 
877
- // noinspection JSBitwiseOperatorUsage
878
- let dependencies: { [key: string]: iConstraint[] } = {
879
- ...fetchDependencies & eFetchDependencies.REFERENCED ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY : {},
880
- ...fetchDependencies & eFetchDependencies.REFERENCES ? C6.TABLES[operatingTable].TABLE_REFERENCES : {}
881
- };
925
+ let apiRequestPromises: Array<Awaited<apiReturn<iGetC6RestResponse<any>>>> = []
882
926
 
883
- let fetchReferences: {
884
- [externalTable: string]: {
885
- [column: string]: string[]
886
- }
887
- } = {}
927
+ console.log('dependencies', dependencies)
928
+ Object.keys(
929
+ dependencies
930
+ ).forEach(column => dependencies[column].forEach((constraint) => {
931
+ fetchReferences[constraint.TABLE] ??= {};
932
+ fetchReferences[constraint.TABLE][constraint.COLUMN] ??= []
933
+ // carbon_users -> user_id | user_photo -> carbon_carbons -> entity_id
934
+ fetchReferences[constraint.TABLE][constraint.COLUMN].push(responseData.rest[column] ?? responseData.rest.map((row) => row[column]).filter(n => n)) // todo - we are mapping all carbons received to every fk reference despite its tag name
935
+ console.log('fetchReferences[constraint.TABLE][constraint.COLUMN]', constraint.TABLE, constraint.COLUMN, fetchReferences[constraint.TABLE][constraint.COLUMN], responseData.rest)
936
+ }));
888
937
 
889
- let apiRequestPromises: Array<Awaited<apiReturn<iGetC6RestResponse<any>>>> = []
938
+ console.log('fetchReferences', fetchReferences)
890
939
 
891
- console.log('dependencies', dependencies)
892
- Object.keys(
893
- dependencies
894
- ).forEach(column => dependencies[column].forEach((constraint) => {
895
- fetchReferences[constraint.TABLE] ??= {};
896
- fetchReferences[constraint.TABLE][constraint.COLUMN] ??= []
897
- // carbon_users -> user_id | user_photo -> carbon_carbons -> entity_id
898
- fetchReferences[constraint.TABLE][constraint.COLUMN].push(responseData.rest[column] ?? responseData.rest.map((row) => row[column]).filter(n => n)) // todo - we are mapping all carbons received to every fk reference despite its tag name
899
- console.log('fetchReferences[constraint.TABLE][constraint.COLUMN]', constraint.TABLE, constraint.COLUMN, fetchReferences[constraint.TABLE][constraint.COLUMN], responseData.rest)
900
- }));
940
+ for (const tableToFetch in fetchReferences) {
901
941
 
902
- console.log('fetchReferences', fetchReferences)
942
+ if (fetchDependencies & eFetchDependencies.C6ENTITY
943
+ && 'string' === typeof tableName
944
+ && tableName.endsWith("carbon_carbons")) {
903
945
 
904
- for (const table in fetchReferences) {
946
+ // this more or less implies
947
+ // todo - rethink the table ref entity system - when tables are renamed? no hooks exist in mysql
948
+ // since were already filtering on column, we can assume the first row constraint is the same as the rest
905
949
 
906
- if (fetchDependencies & eFetchDependencies.C6ENTITY
907
- && tableName === "carbon_carbons") {
950
+ const referencesTables: string[] = responseData.rest.reduce((accumulator: string[], row) => {
951
+ if ('entity_tag' in row && !accumulator.includes(row['entity_tag'])) {
952
+ accumulator.push(row['entity_tag']);
953
+ }
954
+ return accumulator;
955
+ }, []).map((entityTag) => entityTag.split('\\').pop().toLowerCase());
908
956
 
909
- // this more or less implies
910
- // todo - rethink the table ref entity system - when tables are renamed? no hooks exist in mysql
911
- // since were already filtering on column, we can assume the first row constraint is the same as the rest
957
+ const shouldContinue = referencesTables.find((referencesTable) => tableToFetch.endsWith(referencesTable))
912
958
 
913
- const referencesTables: string[] = responseData.rest.reduce((accumulator: string[], row) => {
914
- if ('entity_tag' in row && !accumulator.includes(row['entity_tag'])) {
915
- accumulator.push(row['entity_tag']);
916
- }
917
- return accumulator;
918
- }, []).map((entityTag) => entityTag.split('\\').pop().toLowerCase());
959
+ if (!shouldContinue) {
919
960
 
920
- const shouldContinue = referencesTables.find((referencesTable) => table.endsWith(referencesTable))
961
+ console.log('%c C6ENTITY: The constraintTableName (' + tableToFetch + ') did not end with any value in referencesTables', 'color: #c00', referencesTables)
921
962
 
922
- if (!shouldContinue) {
963
+ continue;
923
964
 
924
- console.log('%c C6ENTITY: The constraintTableName (' + table + ') did not end with any value in referencesTables', 'color: #c00', referencesTables)
965
+ }
925
966
 
926
- continue;
967
+ console.log('%c C6ENTITY: The constraintTableName (' + tableToFetch + ') will be fetched.', 'color: #0c0')
927
968
 
928
969
  }
929
970
 
930
- console.log('%c C6ENTITY: The constraintTableName (' + table + ') will be fetched.', 'color: #0c0')
971
+ const fetchTable = await C6.IMPORT(tableToFetch)
931
972
 
932
- }
933
-
934
- const fetchTable = await C6.IMPORT(table)
973
+ const RestApi = fetchTable.default
935
974
 
936
- const RestApi = fetchTable.default
975
+ console.log('%c Fetch Dependencies will select (' + tableToFetch + ') using GET request', 'color: #33ccff')
937
976
 
938
- console.log('%c Fetch Dependencies will select (' + table + ') using GET request', 'color: #33ccff')
977
+ let nextFetchDependencies = eFetchDependencies.NONE
939
978
 
940
- let nextFetchDependencies = eFetchDependencies.NONE
979
+ if (fetchDependencies & eFetchDependencies.RECURSIVE) {
941
980
 
942
- if (fetchDependencies & eFetchDependencies.RECURSIVE) {
981
+ if (fetchDependencies & eFetchDependencies.ALL) {
943
982
 
944
- if (fetchDependencies & eFetchDependencies.ALL) {
983
+ 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.')
945
984
 
946
- 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.')
985
+ }
947
986
 
948
- }
987
+ nextFetchDependencies = fetchDependencies
949
988
 
950
- nextFetchDependencies = fetchDependencies
989
+ } else if (fetchDependencies & eFetchDependencies.C6ENTITY) {
951
990
 
952
- } else if (fetchDependencies & eFetchDependencies.C6ENTITY) {
991
+ if (tableToFetch === "carbon_carbons") {
953
992
 
954
- if (table === "carbon_carbons") {
993
+ nextFetchDependencies = fetchDependencies
955
994
 
956
- nextFetchDependencies = fetchDependencies
995
+ } else {
957
996
 
958
- } else {
997
+ nextFetchDependencies = fetchDependencies ^ eFetchDependencies.C6ENTITY
959
998
 
960
- nextFetchDependencies = fetchDependencies ^ eFetchDependencies.C6ENTITY
999
+ }
961
1000
 
962
1001
  }
963
1002
 
964
- }
1003
+ console.log('fetchReferences', fetchReferences[tableToFetch], "Current fetchDependencies for (" + operatingTable + "):", fetchDependencies, "New fetchDependencies for (" + tableToFetch + "): ", nextFetchDependencies)
965
1004
 
966
- console.log('fetchReferences', fetchReferences[table], "Current fetchDependencies for (" + operatingTable + "):", fetchDependencies, "New fetchDependencies for (" + table + "): ", nextFetchDependencies)
1005
+ // 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
1006
+ // it not certain that they are using carbons' entities either
967
1007
 
968
- // 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
969
- // it not certain that they are using carbons' entities either
1008
+ // this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
1009
+ // todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
1010
+ apiRequestPromises.push(await RestApi.Get({
1011
+ [C6.WHERE]: {
1012
+ 0: Object.keys(fetchReferences[tableToFetch]).reduce((sum, column) => {
970
1013
 
971
- // this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
972
- // todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
973
- apiRequestPromises.push(await RestApi.Get({
974
- [C6.WHERE]: {
975
- 0: Object.keys(fetchReferences[table]).reduce((sum, column) => {
1014
+ fetchReferences[tableToFetch][column] = fetchReferences[tableToFetch][column].flat(Infinity)
976
1015
 
977
- fetchReferences[table][column] = fetchReferences[table][column].flat(Infinity)
1016
+ if (0 === fetchReferences[tableToFetch][column].length) {
978
1017
 
979
- if (0 === fetchReferences[table][column].length) {
1018
+ console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData)
980
1019
 
981
- console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData)
1020
+ return false;
982
1021
 
983
- return false;
1022
+ }
984
1023
 
985
- }
1024
+ sum[column] = fetchReferences[tableToFetch][column].length === 1
1025
+ ? fetchReferences[tableToFetch][column][0]
1026
+ : [
1027
+ C6.IN, fetchReferences[tableToFetch][column]
1028
+ ]
986
1029
 
987
- sum[column] = fetchReferences[table][column].length === 1
988
- ? fetchReferences[table][column][0]
989
- : [
990
- C6.IN, fetchReferences[table][column]
991
- ]
1030
+ return sum
1031
+ }, {})
1032
+ },
1033
+ fetchDependencies: nextFetchDependencies
1034
+ }
1035
+ ));
992
1036
 
993
- return sum
994
- }, {})
995
- },
996
- fetchDependencies: nextFetchDependencies
997
- }
998
- ));
1037
+ }
999
1038
 
1000
- }
1039
+ await Promise.all(apiRequestPromises)
1040
+
1041
+ request.fetchDependencies = apiRequestPromises
1001
1042
 
1002
- await Promise.all(apiRequestPromises)
1043
+ console.groupEnd()
1003
1044
 
1004
- request.fetchDependencies = apiRequestPromises
1045
+ }
1005
1046
 
1006
- console.groupEnd()
1007
1047
 
1008
1048
  }
1009
1049
 
1050
+ if (request.debug && isLocal) {
1010
1051
 
1011
- }
1052
+ toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
1012
1053
 
1013
- if (request.debug && isLocal) {
1054
+ }
1014
1055
 
1015
- toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
1056
+ return response;
1016
1057
 
1017
1058
  }
1059
+ )
1060
+ ;
1018
1061
 
1019
- return response;
1020
-
1021
- });
1022
-
1023
- } catch (error) {
1062
+ } catch
1063
+ (error) {
1024
1064
 
1025
1065
  if (isTest) {
1026
1066