amplifyquery 1.0.14 β 1.0.16
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.
- package/dist/service.js +89 -43
- 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
|
-
//
|
|
112
|
-
if (
|
|
113
|
-
|
|
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
|
});
|
|
@@ -559,7 +559,10 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
559
559
|
// Check cache first (if forceRefresh is false)
|
|
560
560
|
if (!options.forceRefresh) {
|
|
561
561
|
const cachedItems = query_1.queryClient.getQueryData(queryKey);
|
|
562
|
-
|
|
562
|
+
const queryState = query_1.queryClient.getQueryState(queryKey);
|
|
563
|
+
if (cachedItems &&
|
|
564
|
+
cachedItems.length > 0 &&
|
|
565
|
+
!(queryState === null || queryState === void 0 ? void 0 : queryState.isInvalidated)) {
|
|
563
566
|
console.log(`π¬ ${modelName} list using cache`, queryKey);
|
|
564
567
|
return cachedItems.filter((item) => item !== null);
|
|
565
568
|
}
|
|
@@ -777,17 +780,17 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
777
780
|
query_1.queryClient.setQueryData(singleItemQueryKey, null);
|
|
778
781
|
// Backup and perform optimistic update for list queries (remove item)
|
|
779
782
|
relatedQueryKeys.forEach((queryKey) => {
|
|
780
|
-
if (
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
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
|
-
});
|
|
783
|
+
if (isItemKeyForModel(modelName, queryKey)) {
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
const data = query_1.queryClient.getQueryData(queryKey);
|
|
787
|
+
if (data) {
|
|
788
|
+
previousDataMap.set(queryKey, data);
|
|
790
789
|
}
|
|
790
|
+
query_1.queryClient.setQueryData(queryKey, (oldData) => {
|
|
791
|
+
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
792
|
+
return oldItems.filter((item) => (item === null || item === void 0 ? void 0 : item.id) !== id);
|
|
793
|
+
});
|
|
791
794
|
});
|
|
792
795
|
try {
|
|
793
796
|
// API call - apply auth mode
|
|
@@ -795,8 +798,12 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
795
798
|
yield (0, client_1.getClient)().models[modelName].delete({ id }, authModeParams);
|
|
796
799
|
console.log(`π¬ ${modelName} delete success:`, id);
|
|
797
800
|
// On API success, invalidate all related queries to automatically refresh
|
|
801
|
+
relatedQueryKeys.forEach((queryKey) => query_1.queryClient.invalidateQueries({
|
|
802
|
+
queryKey,
|
|
803
|
+
refetchType: "active",
|
|
804
|
+
}));
|
|
798
805
|
query_1.queryClient.invalidateQueries({
|
|
799
|
-
queryKey:
|
|
806
|
+
queryKey: itemKey(modelName, id),
|
|
800
807
|
refetchType: "active",
|
|
801
808
|
});
|
|
802
809
|
return true;
|
|
@@ -843,17 +850,17 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
843
850
|
});
|
|
844
851
|
// Update all list query caches (remove items included in id list)
|
|
845
852
|
relatedQueryKeys.forEach((queryKey) => {
|
|
846
|
-
if (
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
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
|
-
});
|
|
853
|
+
if (isItemKeyForModel(modelName, queryKey)) {
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
const data = query_1.queryClient.getQueryData(queryKey);
|
|
857
|
+
if (data) {
|
|
858
|
+
previousDataMap.set(queryKey, data);
|
|
856
859
|
}
|
|
860
|
+
query_1.queryClient.setQueryData(queryKey, (oldData) => {
|
|
861
|
+
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
862
|
+
return oldItems.filter((item) => item && !ids.includes(item.id));
|
|
863
|
+
});
|
|
857
864
|
});
|
|
858
865
|
try {
|
|
859
866
|
console.log(`π¬ ${modelName} batch delete attempt [Auth: ${authMode}]: ${ids.length} items`);
|
|
@@ -1193,6 +1200,17 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1193
1200
|
const createItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1194
1201
|
try {
|
|
1195
1202
|
const result = yield service.create(data);
|
|
1203
|
+
if (result) {
|
|
1204
|
+
// Keep hook cache in sync immediately
|
|
1205
|
+
hookQueryClient.setQueryData(queryKey, (oldData) => {
|
|
1206
|
+
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
1207
|
+
if (oldItems.some((item) => (item === null || item === void 0 ? void 0 : item.id) === result.id)) {
|
|
1208
|
+
return oldItems;
|
|
1209
|
+
}
|
|
1210
|
+
return [...oldItems, result];
|
|
1211
|
+
});
|
|
1212
|
+
hookQueryClient.setQueryData(itemKey(modelName, result.id), result);
|
|
1213
|
+
}
|
|
1196
1214
|
// Automatically refresh list after successful create
|
|
1197
1215
|
yield refetch();
|
|
1198
1216
|
return result;
|
|
@@ -1206,6 +1224,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1206
1224
|
const updateItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1207
1225
|
try {
|
|
1208
1226
|
const result = yield service.update(data);
|
|
1227
|
+
if (result) {
|
|
1228
|
+
// Keep hook cache in sync immediately
|
|
1229
|
+
hookQueryClient.setQueryData(queryKey, (oldData) => {
|
|
1230
|
+
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
1231
|
+
return oldItems.map((item) => item && item.id === result.id ? result : item);
|
|
1232
|
+
});
|
|
1233
|
+
hookQueryClient.setQueryData(itemKey(modelName, result.id), result);
|
|
1234
|
+
}
|
|
1209
1235
|
// Automatically refresh list after successful update
|
|
1210
1236
|
yield refetch();
|
|
1211
1237
|
return result;
|
|
@@ -1219,6 +1245,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1219
1245
|
const deleteItem = (0, react_1.useCallback)((id) => __awaiter(this, void 0, void 0, function* () {
|
|
1220
1246
|
try {
|
|
1221
1247
|
const result = yield service.delete(id);
|
|
1248
|
+
if (result) {
|
|
1249
|
+
// Keep hook cache in sync immediately
|
|
1250
|
+
hookQueryClient.setQueryData(queryKey, (oldData) => {
|
|
1251
|
+
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
1252
|
+
return oldItems.filter((item) => (item === null || item === void 0 ? void 0 : item.id) !== id);
|
|
1253
|
+
});
|
|
1254
|
+
hookQueryClient.setQueryData(itemKey(modelName, id), null);
|
|
1255
|
+
}
|
|
1222
1256
|
// Automatically refresh list after successful delete
|
|
1223
1257
|
yield refetch();
|
|
1224
1258
|
return result;
|
|
@@ -1326,13 +1360,25 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1326
1360
|
const deleteItem = (0, react_1.useCallback)(() => __awaiter(this, void 0, void 0, function* () {
|
|
1327
1361
|
try {
|
|
1328
1362
|
const result = yield deleteMutation.mutateAsync();
|
|
1363
|
+
if (result) {
|
|
1364
|
+
// Ensure the hook's query cache reflects deletion immediately
|
|
1365
|
+
hookQueryClient.setQueryData(singleItemQueryKey, null);
|
|
1366
|
+
hookQueryClient.invalidateQueries({
|
|
1367
|
+
queryKey: [modelName],
|
|
1368
|
+
refetchType: "active",
|
|
1369
|
+
});
|
|
1370
|
+
hookQueryClient.invalidateQueries({
|
|
1371
|
+
queryKey: singleItemQueryKey,
|
|
1372
|
+
refetchType: "active",
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
1329
1375
|
return result;
|
|
1330
1376
|
}
|
|
1331
1377
|
catch (error) {
|
|
1332
1378
|
console.error(`π¬ ${modelName} useItemHook delete error:`, error);
|
|
1333
1379
|
throw error;
|
|
1334
1380
|
}
|
|
1335
|
-
}), [deleteMutation]);
|
|
1381
|
+
}), [deleteMutation, hookQueryClient, modelName, singleItemQueryKey]);
|
|
1336
1382
|
// Change loading state to false when isLoading is true and cached data exists
|
|
1337
1383
|
const effectiveLoading = isLoading && !cachedData;
|
|
1338
1384
|
// μΊμ μ 리λ₯Ό μν ν¨κ³Ό μ΅μ ν (ν λ²λ§ μ€ν)
|