amplifyquery 1.0.7 β 1.0.9
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 +118 -31
- 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)
|
|
@@ -453,31 +487,44 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
453
487
|
// Get parameters based on auth mode
|
|
454
488
|
const { authModeParams } = yield getOwnerByAuthMode(authMode);
|
|
455
489
|
// API call - apply auth mode
|
|
456
|
-
const { data:
|
|
490
|
+
const { data: apiResponse } = yield (0, client_1.getClient)().models[modelName].get({ id }, authModeParams);
|
|
491
|
+
// Handle case where API returns array instead of single item
|
|
492
|
+
let item = apiResponse;
|
|
493
|
+
if (Array.isArray(apiResponse)) {
|
|
494
|
+
console.warn(`π¬ ${modelName} get: API returned array instead of single item. Taking first item.`);
|
|
495
|
+
item = apiResponse.find((i) => (i === null || i === void 0 ? void 0 : i.id) === id) || apiResponse[0] || null;
|
|
496
|
+
}
|
|
457
497
|
// Update cache
|
|
458
498
|
if (item) {
|
|
459
|
-
|
|
460
|
-
//
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
//
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
499
|
+
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
500
|
+
// π§ λ²κ·Έ μμ : API μλ΅ μμ΄ν
μ IDκ° μμ²ν IDμ μΌμΉνλμ§ κ²μ¦
|
|
501
|
+
if (itemId && typeof itemId === "string" && itemId === id) {
|
|
502
|
+
query_1.queryClient.setQueryData(singleItemQueryKey, item);
|
|
503
|
+
// Update related list queries (lists that might contain this item)
|
|
504
|
+
const relatedQueryKeys = findRelatedQueryKeys(modelName, query_1.queryClient);
|
|
505
|
+
relatedQueryKeys.forEach((queryKey) => {
|
|
506
|
+
// Exclude single item keys, only process list queries
|
|
507
|
+
if (queryKey.length > 1 && queryKey[1] !== id) {
|
|
508
|
+
// Query keys with id as second element are not single item get query key format, so treat as list
|
|
509
|
+
query_1.queryClient.setQueryData(queryKey, (oldData) => {
|
|
510
|
+
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
511
|
+
const exists = oldItems.some((oldItem) => oldItem && oldItem.id === id);
|
|
512
|
+
if (exists) {
|
|
513
|
+
return oldItems.map((oldItem) => oldItem && oldItem.id === id ? item : oldItem);
|
|
514
|
+
}
|
|
515
|
+
else {
|
|
516
|
+
// Need to check if item matches list query filter conditions (not checking here)
|
|
517
|
+
// If checking is difficult, invalidateQueries might be safer
|
|
518
|
+
// Currently implemented to add item to all related lists that might contain it on API get success
|
|
519
|
+
return [...oldItems, item];
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
console.warn(`π¬ ${modelName} get: API response ID mismatch! Requested: ${id}, API Response ID: ${itemId}. Skipping cache update.`);
|
|
527
|
+
}
|
|
481
528
|
}
|
|
482
529
|
return item || null;
|
|
483
530
|
}
|
|
@@ -553,7 +600,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
553
600
|
query_1.queryClient.setQueryData(queryKey, filteredItems);
|
|
554
601
|
// Update individual item cache
|
|
555
602
|
filteredItems.forEach((item) => {
|
|
556
|
-
|
|
603
|
+
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
604
|
+
// π§ λ²κ·Έ μμ : IDκ° μ ν¨ν κ²½μ°μλ§ κ°λ³ μΊμμ μ μ₯
|
|
605
|
+
if (itemId && typeof itemId === "string") {
|
|
606
|
+
query_1.queryClient.setQueryData([modelName, itemId], item);
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
console.warn(`π¬ ${modelName} list: Invalid item ID found, skipping cache update:`, itemId, item);
|
|
610
|
+
}
|
|
557
611
|
});
|
|
558
612
|
return filteredItems;
|
|
559
613
|
}
|
|
@@ -601,7 +655,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
601
655
|
}
|
|
602
656
|
query_1.queryClient.setQueryData(queryKey, filteredItems);
|
|
603
657
|
filteredItems.forEach((item) => {
|
|
604
|
-
|
|
658
|
+
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
659
|
+
// π§ λ²κ·Έ μμ : IDκ° μ ν¨ν κ²½μ°μλ§ κ°λ³ μΊμμ μ μ₯
|
|
660
|
+
if (itemId && typeof itemId === "string") {
|
|
661
|
+
query_1.queryClient.setQueryData([modelName, itemId], item);
|
|
662
|
+
}
|
|
663
|
+
else {
|
|
664
|
+
console.warn(`π¬ ${modelName} list fallback: Invalid item ID found, skipping cache update:`, itemId, item);
|
|
665
|
+
}
|
|
605
666
|
});
|
|
606
667
|
return filteredItems;
|
|
607
668
|
}
|
|
@@ -980,7 +1041,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
980
1041
|
query_1.queryClient.setQueryData(queryKey, filteredItems);
|
|
981
1042
|
// Update individual item cache
|
|
982
1043
|
filteredItems.forEach((item) => {
|
|
983
|
-
|
|
1044
|
+
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
1045
|
+
// π§ λ²κ·Έ μμ : IDκ° μ ν¨ν κ²½μ°μλ§ κ°λ³ μΊμμ μ μ₯
|
|
1046
|
+
if (itemId && typeof itemId === "string") {
|
|
1047
|
+
query_1.queryClient.setQueryData([modelName, itemId], item);
|
|
1048
|
+
}
|
|
1049
|
+
else {
|
|
1050
|
+
console.warn(`π¬ ${modelName} customList: Invalid item ID found, skipping cache update:`, itemId, item);
|
|
1051
|
+
}
|
|
984
1052
|
});
|
|
985
1053
|
return filteredItems;
|
|
986
1054
|
}
|
|
@@ -1093,7 +1161,19 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1093
1161
|
// Interface functions implementation
|
|
1094
1162
|
const getItem = (0, react_1.useCallback)((id) => {
|
|
1095
1163
|
// Use useQueryData to get latest single item from current cache
|
|
1096
|
-
|
|
1164
|
+
const cachedItem = hookQueryClient.getQueryData([modelName, id]);
|
|
1165
|
+
// π§ λ²κ·Έ μμ : μΊμλ μμ΄ν
μ IDκ° μμ²ν IDμ μΌμΉνλμ§ κ²μ¦
|
|
1166
|
+
if (cachedItem) {
|
|
1167
|
+
const itemId = cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.id;
|
|
1168
|
+
if (itemId === id) {
|
|
1169
|
+
return cachedItem;
|
|
1170
|
+
}
|
|
1171
|
+
else {
|
|
1172
|
+
console.warn(`π¬ ${modelName} useHook.getItem: Cache ID mismatch! Requested: ${id}, Cached: ${itemId}. Returning undefined.`);
|
|
1173
|
+
return undefined;
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
return undefined;
|
|
1097
1177
|
}, [hookQueryClient, modelName]);
|
|
1098
1178
|
const createItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1099
1179
|
try {
|
|
@@ -1166,7 +1246,14 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1166
1246
|
const hookQueryClient = (0, react_query_1.useQueryClient)();
|
|
1167
1247
|
const singleItemQueryKey = [modelName, id];
|
|
1168
1248
|
// First check data from cache
|
|
1169
|
-
const
|
|
1249
|
+
const rawCachedData = hookQueryClient.getQueryData(singleItemQueryKey);
|
|
1250
|
+
// π§ λ²κ·Έ μμ : μΊμλ λ°μ΄ν°μ IDκ° μμ²ν IDμ μΌμΉνλμ§ κ²μ¦
|
|
1251
|
+
const cachedData = rawCachedData && (rawCachedData === null || rawCachedData === void 0 ? void 0 : rawCachedData.id) === id
|
|
1252
|
+
? rawCachedData
|
|
1253
|
+
: undefined;
|
|
1254
|
+
if (rawCachedData && !cachedData) {
|
|
1255
|
+
console.warn(`π¬ ${modelName} useItemHook: Cache ID mismatch! Requested: ${id}, Cached: ${rawCachedData === null || rawCachedData === void 0 ? void 0 : rawCachedData.id}. Ignoring cached data.`);
|
|
1256
|
+
}
|
|
1170
1257
|
// Single item query
|
|
1171
1258
|
const { data: item, isLoading, error, refetch, } = (0, react_query_1.useQuery)({
|
|
1172
1259
|
queryKey: singleItemQueryKey,
|