@carbonorm/carbonnode 1.6.6 → 1.6.8

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.
@@ -107,21 +107,24 @@ type DeepPartialAny<T> = {
107
107
 
108
108
  export enum eFetchDependencies {
109
109
  NONE = 0,
110
- REFERENCED = 1,
111
- CHILDREN = 1,
112
- REFERENCES = 2,
113
- PARENTS = 2,
114
- ALL = 3
110
+ REFERENCED = 0b1,
111
+ CHILDREN = 0b1,
112
+ REFERENCES = 0b10,
113
+ PARENTS = 0b10,
114
+ ALL = 0b11,
115
+ C6ENTITY = 0b100,
116
+ RECURSIVE = 0b1000,
115
117
  }
116
118
 
119
+ // todo - I don't like that these essentially become reserved words.
117
120
  export type iAPI<RestTableInterfaces extends { [key: string]: any }> = RestTableInterfaces & {
118
121
  dataInsertMultipleRows?: RestTableInterfaces[],
119
122
  cacheResults?: boolean, // aka ignoreCache
120
- fetchDependencies?: eFetchDependencies | Promise<apiReturn<iGetC6RestResponse<any>>>[],
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>>>[],
121
125
  debug?: boolean,
122
126
  success?: string | ((r: AxiosResponse) => (string | void)),
123
127
  error?: string | ((r: AxiosResponse) => (string | void)),
124
- blocking?: boolean
125
128
  }
126
129
 
127
130
  interface iCacheAPI<ResponseDataType = any> {
@@ -401,7 +404,7 @@ export default function restApi<
401
404
 
402
405
  const operatingTableFullName = fullTableList[0];
403
406
 
404
- const operatingTable = removePrefixIfExists(operatingTableFullName,C6.PREFIX);
407
+ const operatingTable = removePrefixIfExists(operatingTableFullName, C6.PREFIX);
405
408
 
406
409
  const tables = fullTableList.join(',')
407
410
 
@@ -852,13 +855,14 @@ export default function restApi<
852
855
 
853
856
  }
854
857
 
855
- if (request.fetchDependencies ??= eFetchDependencies.NONE) {
858
+ if ((request.fetchDependencies ??= eFetchDependencies.NONE)
859
+ && 'number' === typeof request.fetchDependencies) {
856
860
 
857
861
  console.groupCollapsed('%c API: fetchDependencies segment (' + requestMethod + ' ' + tableName + ')', 'color: #0c0')
858
862
 
859
863
  const fetchDependencies = async (fetchData: {
860
864
  [key: string]: iConstraint[]
861
- }) => Object.keys(
865
+ }, fetchDependencies: number) => Object.keys(
862
866
  fetchData
863
867
  ).map((column) => {
864
868
 
@@ -876,22 +880,52 @@ export default function restApi<
876
880
 
877
881
  }
878
882
 
883
+
879
884
  return fetchData[column].map(async (constraint) => {
880
885
 
881
- console.log(column, constraint.TABLE)
886
+ const constraintTableName = constraint.TABLE
887
+
888
+ if (fetchDependencies & eFetchDependencies.C6ENTITY
889
+ && tableName === "carbon_carbons") {
890
+
891
+ // todo - let's assume this handles a single row for now, handle multiple rows could improve performance
892
+ // this more or less implies
893
+ // todo - rethink the table ref entity system - when tables are renamed? no hooks exist in mysql
894
+ const referencesTable = (responseData.rest[0]['entity_tag'] ?? '').split('/').pop()
895
+
896
+ if (!constraintTableName.endsWith(referencesTable)) {
897
+
898
+ console.log('%c C6ENTITY: The constraintTableName ('+constraintTableName+') did not end with referencesTable (' + referencesTable + ')', 'color: #c00')
899
+
900
+ return false;
901
+
902
+ }
903
+
904
+ console.log('%c C6ENTITY: The constraintTableName ('+constraintTableName+') ended with referencesTable (' + referencesTable + ')', 'color: #0c0')
905
+
906
+ }
907
+
908
+ console.log(column, constraintTableName)
882
909
 
883
910
  const fetchTable = await C6.IMPORT(constraint.TABLE)
884
911
 
885
912
  const RestApi = fetchTable.default
886
913
 
887
- console.log(constraint, fetchTable)
914
+ console.log('Fetch Dependencies will select ('+constraintTableName+') using GET request')
888
915
 
916
+ // this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
889
917
  return RestApi.Get({
890
918
  [C6.WHERE]: {
891
- // todo - using value to avoid joins.... but. maybe this should be a parameterizable option
892
- [constraint.COLUMN]: responseData.rest[column]
893
- }
894
- });
919
+ // todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
920
+ // todo - handle multiple rows returned
921
+ [constraint.COLUMN]: responseData.rest[column] ?? responseData.rest[0][column]
922
+ },
923
+ fetchDependencies: fetchDependencies & eFetchDependencies.RECURSIVE
924
+ || (fetchDependencies & eFetchDependencies.C6ENTITY
925
+ && constraintTableName === "carbon_carbons")
926
+ ? fetchDependencies
927
+ : eFetchDependencies.NONE
928
+ })
895
929
 
896
930
  });
897
931
 
@@ -899,43 +933,35 @@ export default function restApi<
899
933
 
900
934
  let dependencies: any[] = [];
901
935
 
902
- // noinspection FallThroughInSwitchStatementJS
903
- switch (request.fetchDependencies) {
904
- // @ts-ignore
905
- case eFetchDependencies.ALL:
906
-
907
- console.log('Fetching all dependencies.')
908
-
909
- case eFetchDependencies.CHILDREN: // todo - make this a binary flag with more expressive options
910
- // @ts-ignore
911
- case eFetchDependencies.REFERENCED:
936
+ // noinspection JSBitwiseOperatorUsage
937
+ if (request.fetchDependencies & eFetchDependencies.REFERENCED) {
912
938
 
913
- const referencedBy = C6.TABLES[operatingTable].TABLE_REFERENCED_BY;
939
+ const referencedBy = C6.TABLES[operatingTable].TABLE_REFERENCED_BY;
914
940
 
915
- console.log('REFERENCED BY (CHILDREN)', referencedBy)
941
+ console.log('REFERENCED BY (CHILDREN)', referencedBy)
916
942
 
917
- dependencies = (await fetchDependencies(referencedBy)).flat(Infinity)
943
+ if (Object.keys(referencedBy).length > 0) {
918
944
 
919
- if (request.fetchDependencies !== eFetchDependencies.ALL) {
945
+ dependencies = await fetchDependencies(referencedBy, request.fetchDependencies)
920
946
 
921
- break;
947
+ }
922
948
 
923
- }
949
+ }
924
950
 
925
- case eFetchDependencies.PARENTS:
926
- case eFetchDependencies.REFERENCES:
951
+ if (request.fetchDependencies & eFetchDependencies.REFERENCES) {
952
+ const references = C6.TABLES[operatingTable].TABLE_REFERENCES;
927
953
 
928
- const references = C6.TABLES[operatingTable].TABLE_REFERENCES;
954
+ console.log('REFERENCES (PARENTS)', references)
929
955
 
930
- console.log('REFERENCES (PARENTS)', references)
956
+ if (Object.keys(references).length > 0) {
931
957
 
932
- dependencies = [...dependencies, (await fetchDependencies(references)).flat(Infinity)]
958
+ dependencies.push(await fetchDependencies(references, request.fetchDependencies))
933
959
 
934
- break;
935
- default:
936
- throw new Error('The value of fetchDependencies (' + request.fetchDependencies?.toString() + ') was not recognized.')
960
+ }
937
961
  }
938
962
 
963
+ dependencies = dependencies.flat(Infinity)
964
+
939
965
  request.fetchDependencies = dependencies;
940
966
 
941
967
  await Promise.all(dependencies)