amplifyquery 1.0.7 โ 1.0.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.
- package/dist/service.js +111 -30
- package/package.json +1 -1
package/dist/service.js
CHANGED
|
@@ -77,6 +77,14 @@ function findRelatedQueryKeys(modelName, queryClient) {
|
|
|
77
77
|
function performOptimisticUpdate(queryClient, modelName, relatedQueryKeys, itemId, updateData) {
|
|
78
78
|
return __awaiter(this, void 0, void 0, function* () {
|
|
79
79
|
const previousDataMap = new Map();
|
|
80
|
+
// ๐ง ๋ฒ๊ทธ ์์ : updateData์ ID์ ์์ฒญํ ID๊ฐ ์ผ์นํ๋์ง ๊ฒ์ฆ
|
|
81
|
+
const updateDataId = updateData === null || updateData === void 0 ? void 0 : updateData.id;
|
|
82
|
+
if (!updateDataId ||
|
|
83
|
+
typeof updateDataId !== "string" ||
|
|
84
|
+
updateDataId !== itemId) {
|
|
85
|
+
console.warn(`๐ฌ ${modelName} performOptimisticUpdate: ID mismatch! Expected: ${itemId}, UpdateData ID: ${updateDataId}. Skipping optimistic update.`);
|
|
86
|
+
return previousDataMap; // ๋น ๋งต ๋ฐํ์ผ๋ก rollback ์ ์ํฅ ์์
|
|
87
|
+
}
|
|
80
88
|
// 1. Update individual item cache
|
|
81
89
|
const singleItemQueryKey = [modelName, itemId];
|
|
82
90
|
const previousItemSingle = queryClient.getQueryData(singleItemQueryKey);
|
|
@@ -123,7 +131,17 @@ function performOptimisticUpdate(queryClient, modelName, relatedQueryKeys, itemI
|
|
|
123
131
|
*/
|
|
124
132
|
function handleCacheUpdateOnSuccess(queryClient, modelName, relatedQueryKeys, itemId, updatedItem) {
|
|
125
133
|
// 1. Update individual item cache
|
|
126
|
-
|
|
134
|
+
const actualItemId = updatedItem === null || updatedItem === void 0 ? void 0 : updatedItem.id;
|
|
135
|
+
// ๐ง ๋ฒ๊ทธ ์์ : ์ค์ ์์ดํ
ID์ ์์ฒญํ ID๊ฐ ์ผ์นํ๋์ง ๊ฒ์ฆ
|
|
136
|
+
if (actualItemId &&
|
|
137
|
+
typeof actualItemId === "string" &&
|
|
138
|
+
actualItemId === itemId) {
|
|
139
|
+
queryClient.setQueryData([modelName, itemId], updatedItem);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
console.warn(`๐ฌ ${modelName} handleCacheUpdateOnSuccess: ID mismatch! Expected: ${itemId}, Actual: ${actualItemId}. Skipping cache update.`);
|
|
143
|
+
return; // ID๊ฐ ์ผ์นํ์ง ์์ผ๋ฉด ์บ์ ์
๋ฐ์ดํธ ์ค๋จ
|
|
144
|
+
}
|
|
127
145
|
// 2. Update list query cache (with relational filtering applied)
|
|
128
146
|
relatedQueryKeys.forEach((queryKey) => {
|
|
129
147
|
// Check if relational query - e.g. ["Mission", "Daily", "daily-id", ...]
|
|
@@ -395,7 +413,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
395
413
|
const { data: createdItem } = yield (0, client_1.getClient)().models[modelName].create(newItem, authModeParams);
|
|
396
414
|
// Update individual item cache on API success
|
|
397
415
|
if (createdItem) {
|
|
398
|
-
|
|
416
|
+
const itemId = createdItem === null || createdItem === void 0 ? void 0 : createdItem.id;
|
|
417
|
+
// ๐ง ๋ฒ๊ทธ ์์ : ID๊ฐ ์ ํจํ ๊ฒฝ์ฐ์๋ง ๊ฐ๋ณ ์บ์์ ์ ์ฅ
|
|
418
|
+
if (itemId && typeof itemId === "string") {
|
|
419
|
+
query_1.queryClient.setQueryData([modelName, itemId], createdItem);
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
console.warn(`๐ฌ ${modelName} createList: Invalid createdItem ID found, skipping cache update:`, itemId, createdItem);
|
|
423
|
+
}
|
|
399
424
|
}
|
|
400
425
|
return createdItem || newItem;
|
|
401
426
|
}
|
|
@@ -445,7 +470,16 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
445
470
|
if (!options.forceRefresh) {
|
|
446
471
|
const cachedItem = query_1.queryClient.getQueryData(singleItemQueryKey);
|
|
447
472
|
if (cachedItem) {
|
|
448
|
-
|
|
473
|
+
// ๐ง ๋ฒ๊ทธ ์์ : ์บ์๋ ์์ดํ
์ ์ค์ ID๊ฐ ์์ฒญํ ID์ ์ผ์นํ๋์ง ๊ฒ์ฆ
|
|
474
|
+
const itemId = cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.id;
|
|
475
|
+
if (itemId === id) {
|
|
476
|
+
return cachedItem;
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
// ID๊ฐ ์ผ์นํ์ง ์์ผ๋ฉด ์บ์์์ ์ ๊ฑฐํ๊ณ API ํธ์ถ
|
|
480
|
+
console.warn(`๐ฌ ${modelName} get: Cache ID mismatch! Requested: ${id}, Cached: ${itemId}. Removing invalid cache and fetching from API.`);
|
|
481
|
+
query_1.queryClient.removeQueries({ queryKey: singleItemQueryKey });
|
|
482
|
+
}
|
|
449
483
|
}
|
|
450
484
|
}
|
|
451
485
|
// Determine auth mode (use provided option if available)
|
|
@@ -456,28 +490,35 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
456
490
|
const { data: item } = yield (0, client_1.getClient)().models[modelName].get({ id }, authModeParams);
|
|
457
491
|
// Update cache
|
|
458
492
|
if (item) {
|
|
459
|
-
|
|
460
|
-
//
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
//
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
493
|
+
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
494
|
+
// ๐ง ๋ฒ๊ทธ ์์ : API ์๋ต ์์ดํ
์ ID๊ฐ ์์ฒญํ ID์ ์ผ์นํ๋์ง ๊ฒ์ฆ
|
|
495
|
+
if (itemId && typeof itemId === "string" && itemId === id) {
|
|
496
|
+
query_1.queryClient.setQueryData(singleItemQueryKey, item);
|
|
497
|
+
// Update related list queries (lists that might contain this item)
|
|
498
|
+
const relatedQueryKeys = findRelatedQueryKeys(modelName, query_1.queryClient);
|
|
499
|
+
relatedQueryKeys.forEach((queryKey) => {
|
|
500
|
+
// Exclude single item keys, only process list queries
|
|
501
|
+
if (queryKey.length > 1 && queryKey[1] !== id) {
|
|
502
|
+
// Query keys with id as second element are not single item get query key format, so treat as list
|
|
503
|
+
query_1.queryClient.setQueryData(queryKey, (oldData) => {
|
|
504
|
+
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
505
|
+
const exists = oldItems.some((oldItem) => oldItem && oldItem.id === id);
|
|
506
|
+
if (exists) {
|
|
507
|
+
return oldItems.map((oldItem) => oldItem && oldItem.id === id ? item : oldItem);
|
|
508
|
+
}
|
|
509
|
+
else {
|
|
510
|
+
// Need to check if item matches list query filter conditions (not checking here)
|
|
511
|
+
// If checking is difficult, invalidateQueries might be safer
|
|
512
|
+
// Currently implemented to add item to all related lists that might contain it on API get success
|
|
513
|
+
return [...oldItems, item];
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
console.warn(`๐ฌ ${modelName} get: API response ID mismatch! Requested: ${id}, API Response ID: ${itemId}. Skipping cache update.`);
|
|
521
|
+
}
|
|
481
522
|
}
|
|
482
523
|
return item || null;
|
|
483
524
|
}
|
|
@@ -553,7 +594,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
553
594
|
query_1.queryClient.setQueryData(queryKey, filteredItems);
|
|
554
595
|
// Update individual item cache
|
|
555
596
|
filteredItems.forEach((item) => {
|
|
556
|
-
|
|
597
|
+
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
598
|
+
// ๐ง ๋ฒ๊ทธ ์์ : ID๊ฐ ์ ํจํ ๊ฒฝ์ฐ์๋ง ๊ฐ๋ณ ์บ์์ ์ ์ฅ
|
|
599
|
+
if (itemId && typeof itemId === "string") {
|
|
600
|
+
query_1.queryClient.setQueryData([modelName, itemId], item);
|
|
601
|
+
}
|
|
602
|
+
else {
|
|
603
|
+
console.warn(`๐ฌ ${modelName} list: Invalid item ID found, skipping cache update:`, itemId, item);
|
|
604
|
+
}
|
|
557
605
|
});
|
|
558
606
|
return filteredItems;
|
|
559
607
|
}
|
|
@@ -601,7 +649,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
601
649
|
}
|
|
602
650
|
query_1.queryClient.setQueryData(queryKey, filteredItems);
|
|
603
651
|
filteredItems.forEach((item) => {
|
|
604
|
-
|
|
652
|
+
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
653
|
+
// ๐ง ๋ฒ๊ทธ ์์ : ID๊ฐ ์ ํจํ ๊ฒฝ์ฐ์๋ง ๊ฐ๋ณ ์บ์์ ์ ์ฅ
|
|
654
|
+
if (itemId && typeof itemId === "string") {
|
|
655
|
+
query_1.queryClient.setQueryData([modelName, itemId], item);
|
|
656
|
+
}
|
|
657
|
+
else {
|
|
658
|
+
console.warn(`๐ฌ ${modelName} list fallback: Invalid item ID found, skipping cache update:`, itemId, item);
|
|
659
|
+
}
|
|
605
660
|
});
|
|
606
661
|
return filteredItems;
|
|
607
662
|
}
|
|
@@ -980,7 +1035,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
980
1035
|
query_1.queryClient.setQueryData(queryKey, filteredItems);
|
|
981
1036
|
// Update individual item cache
|
|
982
1037
|
filteredItems.forEach((item) => {
|
|
983
|
-
|
|
1038
|
+
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
1039
|
+
// ๐ง ๋ฒ๊ทธ ์์ : ID๊ฐ ์ ํจํ ๊ฒฝ์ฐ์๋ง ๊ฐ๋ณ ์บ์์ ์ ์ฅ
|
|
1040
|
+
if (itemId && typeof itemId === "string") {
|
|
1041
|
+
query_1.queryClient.setQueryData([modelName, itemId], item);
|
|
1042
|
+
}
|
|
1043
|
+
else {
|
|
1044
|
+
console.warn(`๐ฌ ${modelName} customList: Invalid item ID found, skipping cache update:`, itemId, item);
|
|
1045
|
+
}
|
|
984
1046
|
});
|
|
985
1047
|
return filteredItems;
|
|
986
1048
|
}
|
|
@@ -1093,7 +1155,19 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1093
1155
|
// Interface functions implementation
|
|
1094
1156
|
const getItem = (0, react_1.useCallback)((id) => {
|
|
1095
1157
|
// Use useQueryData to get latest single item from current cache
|
|
1096
|
-
|
|
1158
|
+
const cachedItem = hookQueryClient.getQueryData([modelName, id]);
|
|
1159
|
+
// ๐ง ๋ฒ๊ทธ ์์ : ์บ์๋ ์์ดํ
์ ID๊ฐ ์์ฒญํ ID์ ์ผ์นํ๋์ง ๊ฒ์ฆ
|
|
1160
|
+
if (cachedItem) {
|
|
1161
|
+
const itemId = cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.id;
|
|
1162
|
+
if (itemId === id) {
|
|
1163
|
+
return cachedItem;
|
|
1164
|
+
}
|
|
1165
|
+
else {
|
|
1166
|
+
console.warn(`๐ฌ ${modelName} useHook.getItem: Cache ID mismatch! Requested: ${id}, Cached: ${itemId}. Returning undefined.`);
|
|
1167
|
+
return undefined;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
return undefined;
|
|
1097
1171
|
}, [hookQueryClient, modelName]);
|
|
1098
1172
|
const createItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1099
1173
|
try {
|
|
@@ -1166,7 +1240,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1166
1240
|
const hookQueryClient = (0, react_query_1.useQueryClient)();
|
|
1167
1241
|
const singleItemQueryKey = [modelName, id];
|
|
1168
1242
|
// First check data from cache
|
|
1169
|
-
const
|
|
1243
|
+
const rawCachedData = hookQueryClient.getQueryData(singleItemQueryKey);
|
|
1244
|
+
// ๐ง ๋ฒ๊ทธ ์์ : ์บ์๋ ๋ฐ์ดํฐ์ ID๊ฐ ์์ฒญํ ID์ ์ผ์นํ๋์ง ๊ฒ์ฆ
|
|
1245
|
+
const cachedData = rawCachedData && (rawCachedData === null || rawCachedData === void 0 ? void 0 : rawCachedData.id) === id
|
|
1246
|
+
? rawCachedData
|
|
1247
|
+
: undefined;
|
|
1248
|
+
if (rawCachedData && !cachedData) {
|
|
1249
|
+
console.warn(`๐ฌ ${modelName} useItemHook: Cache ID mismatch! Requested: ${id}, Cached: ${rawCachedData === null || rawCachedData === void 0 ? void 0 : rawCachedData.id}. Ignoring cached data.`);
|
|
1250
|
+
}
|
|
1170
1251
|
// Single item query
|
|
1171
1252
|
const { data: item, isLoading, error, refetch, } = (0, react_query_1.useQuery)({
|
|
1172
1253
|
queryKey: singleItemQueryKey,
|