amplifyquery 1.0.15 β 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 +44 -2
- package/package.json +1 -1
package/dist/service.js
CHANGED
|
@@ -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
|
}
|
|
@@ -1197,6 +1200,17 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1197
1200
|
const createItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1198
1201
|
try {
|
|
1199
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
|
+
}
|
|
1200
1214
|
// Automatically refresh list after successful create
|
|
1201
1215
|
yield refetch();
|
|
1202
1216
|
return result;
|
|
@@ -1210,6 +1224,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1210
1224
|
const updateItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1211
1225
|
try {
|
|
1212
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
|
+
}
|
|
1213
1235
|
// Automatically refresh list after successful update
|
|
1214
1236
|
yield refetch();
|
|
1215
1237
|
return result;
|
|
@@ -1223,6 +1245,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1223
1245
|
const deleteItem = (0, react_1.useCallback)((id) => __awaiter(this, void 0, void 0, function* () {
|
|
1224
1246
|
try {
|
|
1225
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
|
+
}
|
|
1226
1256
|
// Automatically refresh list after successful delete
|
|
1227
1257
|
yield refetch();
|
|
1228
1258
|
return result;
|
|
@@ -1330,13 +1360,25 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1330
1360
|
const deleteItem = (0, react_1.useCallback)(() => __awaiter(this, void 0, void 0, function* () {
|
|
1331
1361
|
try {
|
|
1332
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
|
+
}
|
|
1333
1375
|
return result;
|
|
1334
1376
|
}
|
|
1335
1377
|
catch (error) {
|
|
1336
1378
|
console.error(`π¬ ${modelName} useItemHook delete error:`, error);
|
|
1337
1379
|
throw error;
|
|
1338
1380
|
}
|
|
1339
|
-
}), [deleteMutation]);
|
|
1381
|
+
}), [deleteMutation, hookQueryClient, modelName, singleItemQueryKey]);
|
|
1340
1382
|
// Change loading state to false when isLoading is true and cached data exists
|
|
1341
1383
|
const effectiveLoading = isLoading && !cachedData;
|
|
1342
1384
|
// μΊμ μ 리λ₯Ό μν ν¨κ³Ό μ΅μ ν (ν λ²λ§ μ€ν)
|