@aws-amplify/datastore 3.7.4-custom-pk.96 → 3.7.4-custom-pk.97

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.
@@ -839,61 +839,34 @@ class IndexedDBAdapter implements Adapter {
839
839
  deleteQueue: { storeName: string; items: T[] }[]
840
840
  ): Promise<void> {
841
841
  for await (const rel of relations) {
842
- const { relationType, fieldName, modelName, targetName, targetNames } =
843
- rel;
842
+ const {
843
+ relationType,
844
+ modelName,
845
+ targetName,
846
+ targetNames,
847
+ associatedWith,
848
+ } = rel;
844
849
 
845
850
  const storeName = this.getStorename(nameSpace, modelName);
846
851
 
847
- const index: string | string[] | undefined =
848
- getIndex(
849
- this.schema.namespaces[nameSpace].relationships[modelName]
850
- .relationTypes,
851
- srcModel
852
- ) ||
853
- // if we were unable to find an index via relationTypes
854
- // i.e. for keyName connections, attempt to find one by the
855
- // associatedWith property
856
- getIndexFromAssociation(
857
- this.schema.namespaces[nameSpace].relationships[modelName].indexes,
858
- rel.associatedWith
859
- );
860
-
861
- // TODO: refactor
862
852
  switch (relationType) {
863
853
  case 'HAS_ONE':
864
854
  for await (const model of models) {
865
- const hasOneIndex = index || 'byPk';
866
-
867
- // CPK
868
- if (targetNames && targetNames.length > 0) {
869
- // iterate over targetNames array and see if each item is present in model object
870
- // targetNames here being the keys for the CHILD model
871
- const hasConnectedModelFields = targetNames.every(targetName =>
872
- model.hasOwnProperty(targetName)
873
- );
874
-
875
- // PK / Composite key for the PARENT model
876
- const keyValues = this.getIndexKeyValues(model);
855
+ const hasOneIndex = 'byPk';
877
856
 
878
- let values = [];
879
-
880
- if (hasConnectedModelFields) {
881
- // Values will be that of the child model
882
- values = targetNames.map(
883
- targetName => model[targetName]
884
- ) as any;
885
- } else {
886
- // values will be that of the parent model
887
- values = keyValues as any;
888
- }
857
+ if (targetNames?.length) {
858
+ // CPK codegen
859
+ const values = targetNames.map(targetName => model[targetName]);
889
860
 
890
861
  if (values.length === 0) break;
891
862
 
892
- const recordToDelete = <T>await this.db
893
- .transaction(storeName, 'readwrite')
894
- .objectStore(storeName)
895
- .index(hasOneIndex as string)
896
- .get(values);
863
+ const recordToDelete = <T>(
864
+ await this.db
865
+ .transaction(storeName, 'readwrite')
866
+ .objectStore(storeName)
867
+ .index(hasOneIndex)
868
+ .get(values)
869
+ );
897
870
 
898
871
  await this.deleteTraverse(
899
872
  this.schema.namespaces[nameSpace].relationships[modelName]
@@ -905,20 +878,36 @@ class IndexedDBAdapter implements Adapter {
905
878
  );
906
879
  break;
907
880
  } else {
908
- // Backwards compatibility (TODO: combine with above)
909
- const hasOneIndex = index || 'byPk';
910
-
911
- const hasOneCustomField = targetName in model;
912
- const keyValues = this.getIndexKeyValues(model);
913
- const value = hasOneCustomField ? [model[targetName]] : keyValues;
881
+ // PRE-CPK codegen
882
+ let index;
883
+ let values: string[];
884
+
885
+ if (targetName && targetName in model) {
886
+ index = hasOneIndex;
887
+ const value = model[targetName];
888
+ values = [value];
889
+ } else {
890
+ // backwards compatability for older versions of codegen that did not emit targetName for HAS_ONE relations
891
+ // TODO: can we deprecate this? it's been ~2 years since codegen started including targetName for HAS_ONE
892
+ // If we deprecate, we'll need to re-gen the MIPR in __tests__/schema.ts > newSchema
893
+ // otherwise some unit tests will fail
894
+ index = getIndex(
895
+ this.schema.namespaces[nameSpace].relationships[modelName]
896
+ .relationTypes,
897
+ srcModel
898
+ );
899
+ values = this.getIndexKeyValues(model);
900
+ }
914
901
 
915
- if (!value) break;
902
+ if (!values || !index) break;
916
903
 
917
- const recordToDelete = <T>await this.db
918
- .transaction(storeName, 'readwrite')
919
- .objectStore(storeName)
920
- .index(hasOneIndex as string)
921
- .get(value);
904
+ const recordToDelete = <T>(
905
+ await this.db
906
+ .transaction(storeName, 'readwrite')
907
+ .objectStore(storeName)
908
+ .index(index)
909
+ .get(values)
910
+ );
922
911
 
923
912
  await this.deleteTraverse(
924
913
  this.schema.namespaces[nameSpace].relationships[modelName]
@@ -933,6 +922,19 @@ class IndexedDBAdapter implements Adapter {
933
922
  break;
934
923
  case 'HAS_MANY':
935
924
  for await (const model of models) {
925
+ const index =
926
+ // explicit bi-directional @hasMany and @manyToMany
927
+ getIndex(
928
+ this.schema.namespaces[nameSpace].relationships[modelName]
929
+ .relationTypes,
930
+ srcModel
931
+ ) ||
932
+ // uni and/or implicit @hasMany
933
+ getIndexFromAssociation(
934
+ this.schema.namespaces[nameSpace].relationships[modelName]
935
+ .indexes,
936
+ associatedWith
937
+ );
936
938
  const keyValues = this.getIndexKeyValues(model);
937
939
 
938
940
  const childrenArray = await this.db