@carbonorm/carbonnode 1.6.11 → 1.6.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -121,7 +121,7 @@ export type iAPI<RestTableInterfaces extends { [key: string]: any }> = RestTable
121
121
  dataInsertMultipleRows?: RestTableInterfaces[],
122
122
  cacheResults?: boolean, // aka ignoreCache
123
123
  // todo - this should really only be used for get requests - add this to the Get interface or throw error (im actually inclined to ts ignore the function and add to iGetC6 atm; back later)
124
- fetchDependencies?: number | eFetchDependencies | Promise<apiReturn<iGetC6RestResponse<any>>>[],
124
+ fetchDependencies?: number | eFetchDependencies | apiReturn<iGetC6RestResponse<any>>[],
125
125
  debug?: boolean,
126
126
  success?: string | ((r: AxiosResponse) => (string | void)),
127
127
  error?: string | ((r: AxiosResponse) => (string | void)),
@@ -779,7 +779,7 @@ export default function restApi<
779
779
 
780
780
  // returning the promise with this then is important for tests. todo - we could make that optional.
781
781
  // https://rapidapi.com/guides/axios-async-await
782
- return axiosActiveRequest.then(async (response) => {
782
+ return axiosActiveRequest.then(async (response): Promise<AxiosResponse<ResponseDataType, any>> => {
783
783
 
784
784
  if (typeof response.data === 'string') {
785
785
 
@@ -855,117 +855,109 @@ export default function restApi<
855
855
 
856
856
  }
857
857
 
858
- if ((request.fetchDependencies ??= eFetchDependencies.NONE)
859
- && 'number' === typeof request.fetchDependencies) {
860
-
861
- console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')', 'color: #33ccff')
862
-
863
- const fetchDependencies = async (fetchData: {
864
- [key: string]: iConstraint[]
865
- }, fetchDependencies: number) => Object.keys(
866
- fetchData
867
- ).map((column) => {
868
-
869
- console.log('fetchDependencies', responseData.rest, column)
858
+ request.fetchDependencies ??= eFetchDependencies.NONE;
870
859
 
871
- // check if the column is in the response
872
- // todo - this may need [0][x]
873
- if (!(column in responseData.rest)
874
- && !(0 in responseData.rest
875
- && column in responseData.rest[0])) {
860
+ if (request.fetchDependencies
861
+ && 'number' === typeof request.fetchDependencies) {
876
862
 
877
- console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData)
863
+ const fetchDependencies = request.fetchDependencies as number;
878
864
 
879
- return false;
865
+ console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')', 'color: #33ccff')
880
866
 
867
+ // noinspection JSBitwiseOperatorUsage
868
+ let dependencies: { [key: string]: iConstraint[] } = {
869
+ ...fetchDependencies & eFetchDependencies.REFERENCED ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY : {},
870
+ ...fetchDependencies & eFetchDependencies.REFERENCES ? C6.TABLES[operatingTable].TABLE_REFERENCES : {}
871
+ };
872
+
873
+ let fetchReferences: {
874
+ [externalTable: string]: {
875
+ [column: string]: string[]
881
876
  }
877
+ } = {}
882
878
 
883
- return fetchData[column].map(async (constraint) => {
879
+ let apiRequestPromises: Array<apiReturn<iGetC6RestResponse<any>>> = []
884
880
 
885
- const constraintTableName = constraint.TABLE
881
+ Object.keys(
882
+ dependencies
883
+ ).forEach(column => dependencies[column].forEach((constraint) => {
884
+ fetchReferences[constraint.TABLE] ??= {};
885
+ fetchReferences[constraint.TABLE][constraint.COLUMN] = []
886
+ fetchReferences[constraint.TABLE][constraint.COLUMN].push(responseData.rest[column] ?? responseData.rest.map((row) => row[column]))
887
+ }));
886
888
 
887
- if (fetchDependencies & eFetchDependencies.C6ENTITY
888
- && tableName === "carbon_carbons") {
889
+ for (const table in fetchReferences) {
889
890
 
890
- // todo - let's assume this handles a single row for now, handle multiple rows could improve performance
891
- // this more or less implies
892
- // todo - rethink the table ref entity system - when tables are renamed? no hooks exist in mysql
893
- const referencesTable = (responseData.rest[0]['entity_tag'] ?? '').split('/').pop()
891
+ if (fetchDependencies & eFetchDependencies.C6ENTITY
892
+ && tableName === "carbon_carbons") {
894
893
 
895
- if (!constraintTableName.endsWith(referencesTable)) {
894
+ // this more or less implies
895
+ // todo - rethink the table ref entity system - when tables are renamed? no hooks exist in mysql
896
+ const tagFilter = (tag: string) => tag.split('/').pop() ?? ''
896
897
 
897
- console.log('%c C6ENTITY: The constraintTableName (' + constraintTableName + ') did not end with referencesTable (' + referencesTable + ')', 'color: #c00')
898
+ // since were already filtering on column, we can assume the first row constraint is the same as the rest
899
+ const referencesTable: string = tagFilter(responseData.rest[0]['entity_tag'] ?? '')
898
900
 
899
- return false;
901
+ // todo - allow c6 requests with multiple table endpoints to be fetched
902
+ // const referencesTables: string[] = !(0 in responseData.rest) ? [tagFilter(responseData.rest['entity_tag'] ?? '')] : responseData.rest.map(row => tagFilter(row['entity_tag'] ?? '')).filter(n => n)
903
+ if (!table.endsWith(referencesTable)) {
900
904
 
901
- }
905
+ console.log('%c C6ENTITY: The constraintTableName (' + table + ') did not end with referencesTable (' + referencesTable + ')', 'color: #c00')
902
906
 
903
- console.log('%c C6ENTITY: The constraintTableName (' + constraintTableName + ') ended with referencesTable (' + referencesTable + ')', 'color: #0c0')
907
+ continue;
904
908
 
905
909
  }
906
910
 
907
- console.log(column, constraintTableName)
908
-
909
- const fetchTable = await C6.IMPORT(constraint.TABLE)
910
-
911
- const RestApi = fetchTable.default
912
-
913
- console.log('Fetch Dependencies will select (' + constraintTableName + ') using GET request')
914
-
915
- // todo - filter out ids that exist in state?!?
916
-
917
- // this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
918
- return await RestApi.Get({
919
- [C6.WHERE]: {
920
- // todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
921
- [constraint.COLUMN]: responseData.rest[column] ?? [C6.IN, responseData.rest.map((row) => row[column])]
922
- },
923
- fetchDependencies: fetchDependencies & eFetchDependencies.RECURSIVE
924
- || (fetchDependencies & eFetchDependencies.C6ENTITY
925
- && constraintTableName === "carbon_carbons")
926
- ? fetchDependencies
927
- : eFetchDependencies.NONE
928
- })
929
-
930
- });
911
+ console.log('%c C6ENTITY: The constraintTableName (' + table + ') ended with referencesTable (' + referencesTable + ')', 'color: #0c0')
931
912
 
932
- });
913
+ }
933
914
 
934
- let dependencies: any[] = [];
915
+ const fetchTable = await C6.IMPORT(table)
935
916
 
936
- // noinspection JSBitwiseOperatorUsage
937
- if (request.fetchDependencies & eFetchDependencies.REFERENCED) {
917
+ const RestApi = fetchTable.default
938
918
 
939
- const referencedBy = C6.TABLES[operatingTable].TABLE_REFERENCED_BY;
919
+ console.log('Fetch Dependencies will select (' + table + ') using GET request')
940
920
 
941
- console.log('REFERENCED BY (CHILDREN)', referencedBy)
921
+ // todo - filter out ids that exist in state?!?
942
922
 
943
- if (Object.keys(referencedBy).length > 0) {
944
-
945
- dependencies = await fetchDependencies(referencedBy, request.fetchDependencies)
923
+ // this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
924
+ // todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
925
+ apiRequestPromises.push(RestApi.Get({
926
+ [C6.WHERE]: {
927
+ 0: Object.keys(fetchReferences[table]).reduce((sum, column) => {
946
928
 
947
- }
929
+ fetchReferences[table][column] = fetchReferences[table][column].flat(Infinity)
948
930
 
949
- }
931
+ if (0 === fetchReferences[table][column].length) {
950
932
 
951
- if (request.fetchDependencies & eFetchDependencies.REFERENCES) {
933
+ console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData)
952
934
 
953
- const references = C6.TABLES[operatingTable].TABLE_REFERENCES;
935
+ return false;
954
936
 
955
- console.log('REFERENCES (PARENTS)', references)
937
+ }
956
938
 
957
- if (Object.keys(references).length > 0) {
939
+ sum[column] = fetchReferences[table][column].length === 1
940
+ ? fetchReferences[table][column][0]
941
+ : [
942
+ C6.IN, fetchReferences[table][column]
943
+ ]
958
944
 
959
- dependencies.push(await fetchDependencies(references, request.fetchDependencies))
945
+ return sum
946
+ }, {})
947
+ },
948
+ fetchDependencies: fetchDependencies & eFetchDependencies.RECURSIVE
949
+ || (fetchDependencies & eFetchDependencies.C6ENTITY
950
+ && table === "carbon_carbons")
951
+ ? fetchDependencies
952
+ : eFetchDependencies.NONE
953
+ }
954
+ ));
960
955
 
961
- }
962
956
  }
963
957
 
964
- dependencies = dependencies.flat(Infinity)
965
-
966
- request.fetchDependencies = dependencies;
958
+ await Promise.all(apiRequestPromises)
967
959
 
968
- await Promise.all(dependencies)
960
+ request.fetchDependencies = apiRequestPromises
969
961
 
970
962
  console.trace();
971
963