amplifyquery 1.0.14 → 1.0.15

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.
Files changed (2) hide show
  1. package/dist/service.js +45 -41
  2. package/package.json +1 -1
package/dist/service.js CHANGED
@@ -108,27 +108,27 @@ function performOptimisticUpdate(queryClient, modelName, relatedQueryKeys, itemI
108
108
  queryClient.setQueryData(singleItemQueryKey, optimisticData);
109
109
  // 2. Update list queries
110
110
  relatedQueryKeys.forEach((queryKey) => {
111
- // Process only list query keys since single item key is handled above
112
- if (queryKey.length > 1 && queryKey[1] !== itemId) {
113
- const previousItems = queryClient.getQueryData(queryKey);
114
- previousDataMap.set(queryKey, previousItems); // Backup previous data
115
- queryClient.setQueryData(queryKey, (oldData) => {
116
- // Safely handle if oldData is null, undefined or not an array
117
- const oldItems = Array.isArray(oldData) ? oldData : [];
118
- const hasItem = oldItems.some((item) => item && item.id === itemId);
119
- if (hasItem) {
120
- // Update if existing item found
121
- return oldItems.map((item) => item && item.id === itemId
122
- ? Object.assign(Object.assign({}, item), updateData) : item);
123
- }
124
- else if (optimisticData && queryKey.length < 3) {
125
- // Only consider adding created item for top-level list queries
126
- // Add if no existing item and optimistic update data available (for create/upsert)
127
- return [...oldItems, optimisticData];
128
- }
129
- return oldItems; // No changes
130
- });
111
+ // Skip single-item keys (handled above)
112
+ if (isItemKeyForModel(modelName, queryKey)) {
113
+ return;
131
114
  }
115
+ const previousItems = queryClient.getQueryData(queryKey);
116
+ previousDataMap.set(queryKey, previousItems); // Backup previous data
117
+ queryClient.setQueryData(queryKey, (oldData) => {
118
+ // Safely handle if oldData is null, undefined or not an array
119
+ const oldItems = Array.isArray(oldData) ? oldData : [];
120
+ const hasItem = oldItems.some((item) => item && item.id === itemId);
121
+ if (hasItem) {
122
+ // Update if existing item found
123
+ return oldItems.map((item) => item && item.id === itemId
124
+ ? Object.assign(Object.assign({}, item), updateData) : item);
125
+ }
126
+ // Only add created item to top-level list queries (e.g. [modelName])
127
+ if (optimisticData && queryKey.length === 1) {
128
+ return [...oldItems, optimisticData];
129
+ }
130
+ return oldItems; // No changes
131
+ });
132
132
  });
133
133
  return previousDataMap;
134
134
  });
@@ -777,17 +777,17 @@ function createAmplifyService(modelName, defaultAuthMode) {
777
777
  query_1.queryClient.setQueryData(singleItemQueryKey, null);
778
778
  // Backup and perform optimistic update for list queries (remove item)
779
779
  relatedQueryKeys.forEach((queryKey) => {
780
- if (queryKey.length > 1 && queryKey[1] !== id) {
781
- // Exclude single item keys
782
- const data = query_1.queryClient.getQueryData(queryKey);
783
- if (data) {
784
- previousDataMap.set(queryKey, data);
785
- }
786
- query_1.queryClient.setQueryData(queryKey, (oldData) => {
787
- const oldItems = Array.isArray(oldData) ? oldData : [];
788
- return oldItems.filter((item) => (item === null || item === void 0 ? void 0 : item.id) !== id);
789
- });
780
+ if (isItemKeyForModel(modelName, queryKey)) {
781
+ return;
790
782
  }
783
+ const data = query_1.queryClient.getQueryData(queryKey);
784
+ if (data) {
785
+ previousDataMap.set(queryKey, data);
786
+ }
787
+ query_1.queryClient.setQueryData(queryKey, (oldData) => {
788
+ const oldItems = Array.isArray(oldData) ? oldData : [];
789
+ return oldItems.filter((item) => (item === null || item === void 0 ? void 0 : item.id) !== id);
790
+ });
791
791
  });
792
792
  try {
793
793
  // API call - apply auth mode
@@ -795,8 +795,12 @@ function createAmplifyService(modelName, defaultAuthMode) {
795
795
  yield (0, client_1.getClient)().models[modelName].delete({ id }, authModeParams);
796
796
  console.log(`🍬 ${modelName} delete success:`, id);
797
797
  // On API success, invalidate all related queries to automatically refresh
798
+ relatedQueryKeys.forEach((queryKey) => query_1.queryClient.invalidateQueries({
799
+ queryKey,
800
+ refetchType: "active",
801
+ }));
798
802
  query_1.queryClient.invalidateQueries({
799
- queryKey: [modelName],
803
+ queryKey: itemKey(modelName, id),
800
804
  refetchType: "active",
801
805
  });
802
806
  return true;
@@ -843,17 +847,17 @@ function createAmplifyService(modelName, defaultAuthMode) {
843
847
  });
844
848
  // Update all list query caches (remove items included in id list)
845
849
  relatedQueryKeys.forEach((queryKey) => {
846
- if (queryKey.length > 1 && !ids.includes(queryKey[1])) {
847
- // Exclude single item keys and list keys where id is second element (handled individually)
848
- const data = query_1.queryClient.getQueryData(queryKey);
849
- if (data) {
850
- previousDataMap.set(queryKey, data);
851
- }
852
- query_1.queryClient.setQueryData(queryKey, (oldData) => {
853
- const oldItems = Array.isArray(oldData) ? oldData : [];
854
- return oldItems.filter((item) => item && !ids.includes(item.id));
855
- });
850
+ if (isItemKeyForModel(modelName, queryKey)) {
851
+ return;
852
+ }
853
+ const data = query_1.queryClient.getQueryData(queryKey);
854
+ if (data) {
855
+ previousDataMap.set(queryKey, data);
856
856
  }
857
+ query_1.queryClient.setQueryData(queryKey, (oldData) => {
858
+ const oldItems = Array.isArray(oldData) ? oldData : [];
859
+ return oldItems.filter((item) => item && !ids.includes(item.id));
860
+ });
857
861
  });
858
862
  try {
859
863
  console.log(`🍬 ${modelName} batch delete attempt [Auth: ${authMode}]: ${ids.length} items`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amplifyquery",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "Amplify+Query",
5
5
  "keywords": [
6
6
  "Amplify",