amplifyquery 1.0.13 → 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.
- package/dist/query.js +9 -2
- package/dist/service.js +84 -65
- package/dist/singleton.js +42 -9
- package/package.json +1 -1
package/dist/query.js
CHANGED
|
@@ -134,7 +134,11 @@ function configure(options = {}) {
|
|
|
134
134
|
maxAge: ((_a = config.storage) === null || _a === void 0 ? void 0 : _a.maxAge) || 1000 * 60 * 60 * 24 * 7, // Default 7 days
|
|
135
135
|
dehydrateOptions: {
|
|
136
136
|
shouldDehydrateQuery: (query) => {
|
|
137
|
-
|
|
137
|
+
var _a;
|
|
138
|
+
// Only persist successful queries to reduce hydration cancellation noise
|
|
139
|
+
if (((_a = query.state) === null || _a === void 0 ? void 0 : _a.status) !== "success")
|
|
140
|
+
return false;
|
|
141
|
+
// Avoid persisting mutation-like or transient keys if needed later
|
|
138
142
|
return true;
|
|
139
143
|
},
|
|
140
144
|
},
|
|
@@ -169,7 +173,10 @@ function invalidateModel(modelName) {
|
|
|
169
173
|
* @param id Item ID
|
|
170
174
|
*/
|
|
171
175
|
function invalidateModelItem(modelName, id) {
|
|
172
|
-
|
|
176
|
+
// New item key
|
|
177
|
+
exports.queryClient.invalidateQueries({ queryKey: [modelName, "item", id] });
|
|
178
|
+
// Backward cleanup (in case legacy keys exist)
|
|
179
|
+
exports.queryClient.removeQueries({ queryKey: [modelName, id], exact: true });
|
|
173
180
|
}
|
|
174
181
|
/**
|
|
175
182
|
* Invalidate model items with a specific field value
|
package/dist/service.js
CHANGED
|
@@ -18,6 +18,15 @@ const react_query_1 = require("@tanstack/react-query");
|
|
|
18
18
|
const auth_1 = require("aws-amplify/auth");
|
|
19
19
|
const expo_crypto_1 = require("expo-crypto");
|
|
20
20
|
const react_1 = require("react");
|
|
21
|
+
// -------------------------------
|
|
22
|
+
// Query key helpers
|
|
23
|
+
// -------------------------------
|
|
24
|
+
function itemKey(modelName, id) {
|
|
25
|
+
return [modelName, "item", id];
|
|
26
|
+
}
|
|
27
|
+
function isItemKeyForModel(modelName, key) {
|
|
28
|
+
return Array.isArray(key) && key[0] === modelName && key[1] === "item";
|
|
29
|
+
}
|
|
21
30
|
/**
|
|
22
31
|
* Utility function to get owner value based on authentication mode
|
|
23
32
|
* Sets owner value only in userPool auth mode, returns empty string for other auth modes
|
|
@@ -58,9 +67,12 @@ function findRelatedQueryKeys(modelName, queryClient) {
|
|
|
58
67
|
.getQueryCache()
|
|
59
68
|
.findAll({
|
|
60
69
|
predicate: ({ queryKey }) => {
|
|
61
|
-
// Find all query keys
|
|
62
|
-
//
|
|
63
|
-
|
|
70
|
+
// Find all query keys for the model, but EXCLUDE single-item keys
|
|
71
|
+
// Examples kept: [model], [model, 'filter', ...], [model, 'query', ...], [model, Relation, id, ...]
|
|
72
|
+
// Excluded: [model, 'item', id]
|
|
73
|
+
return (Array.isArray(queryKey) &&
|
|
74
|
+
queryKey[0] === modelName &&
|
|
75
|
+
!isItemKeyForModel(modelName, queryKey));
|
|
64
76
|
},
|
|
65
77
|
})
|
|
66
78
|
.map((query) => query.queryKey);
|
|
@@ -86,7 +98,7 @@ function performOptimisticUpdate(queryClient, modelName, relatedQueryKeys, itemI
|
|
|
86
98
|
return previousDataMap; // 빈 맵 반환으로 rollback 시 영향 없음
|
|
87
99
|
}
|
|
88
100
|
// 1. Update individual item cache
|
|
89
|
-
const singleItemQueryKey =
|
|
101
|
+
const singleItemQueryKey = itemKey(modelName, itemId);
|
|
90
102
|
const previousItemSingle = queryClient.getQueryData(singleItemQueryKey);
|
|
91
103
|
previousDataMap.set(singleItemQueryKey, previousItemSingle);
|
|
92
104
|
// Merge with existing data if available, otherwise use updateData as optimistic data
|
|
@@ -96,27 +108,27 @@ function performOptimisticUpdate(queryClient, modelName, relatedQueryKeys, itemI
|
|
|
96
108
|
queryClient.setQueryData(singleItemQueryKey, optimisticData);
|
|
97
109
|
// 2. Update list queries
|
|
98
110
|
relatedQueryKeys.forEach((queryKey) => {
|
|
99
|
-
//
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
previousDataMap.set(queryKey, previousItems); // Backup previous data
|
|
103
|
-
queryClient.setQueryData(queryKey, (oldData) => {
|
|
104
|
-
// Safely handle if oldData is null, undefined or not an array
|
|
105
|
-
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
106
|
-
const hasItem = oldItems.some((item) => item && item.id === itemId);
|
|
107
|
-
if (hasItem) {
|
|
108
|
-
// Update if existing item found
|
|
109
|
-
return oldItems.map((item) => item && item.id === itemId
|
|
110
|
-
? Object.assign(Object.assign({}, item), updateData) : item);
|
|
111
|
-
}
|
|
112
|
-
else if (optimisticData && queryKey.length < 3) {
|
|
113
|
-
// Only consider adding created item for top-level list queries
|
|
114
|
-
// Add if no existing item and optimistic update data available (for create/upsert)
|
|
115
|
-
return [...oldItems, optimisticData];
|
|
116
|
-
}
|
|
117
|
-
return oldItems; // No changes
|
|
118
|
-
});
|
|
111
|
+
// Skip single-item keys (handled above)
|
|
112
|
+
if (isItemKeyForModel(modelName, queryKey)) {
|
|
113
|
+
return;
|
|
119
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
|
+
});
|
|
120
132
|
});
|
|
121
133
|
return previousDataMap;
|
|
122
134
|
});
|
|
@@ -136,7 +148,7 @@ function handleCacheUpdateOnSuccess(queryClient, modelName, relatedQueryKeys, it
|
|
|
136
148
|
if (actualItemId &&
|
|
137
149
|
typeof actualItemId === "string" &&
|
|
138
150
|
actualItemId === itemId) {
|
|
139
|
-
queryClient.setQueryData(
|
|
151
|
+
queryClient.setQueryData(itemKey(modelName, itemId), updatedItem);
|
|
140
152
|
}
|
|
141
153
|
else {
|
|
142
154
|
console.warn(`🍬 ${modelName} handleCacheUpdateOnSuccess: ID mismatch! Expected: ${itemId}, Actual: ${actualItemId}. Skipping cache update.`);
|
|
@@ -245,7 +257,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
245
257
|
// Backup previous data for optimistic update
|
|
246
258
|
const previousDataMap = new Map();
|
|
247
259
|
// Update individual item cache
|
|
248
|
-
const singleItemQueryKey =
|
|
260
|
+
const singleItemQueryKey = itemKey(modelName, newItem.id);
|
|
249
261
|
const previousItemSingle = query_1.queryClient.getQueryData(singleItemQueryKey);
|
|
250
262
|
previousDataMap.set(singleItemQueryKey, previousItemSingle);
|
|
251
263
|
query_1.queryClient.setQueryData(singleItemQueryKey, newItem);
|
|
@@ -401,7 +413,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
401
413
|
});
|
|
402
414
|
// Update individual item caches
|
|
403
415
|
preparedItems.forEach((item) => {
|
|
404
|
-
const singleItemQueryKey =
|
|
416
|
+
const singleItemQueryKey = itemKey(modelName, item.id);
|
|
405
417
|
const previousItemSingle = query_1.queryClient.getQueryData(singleItemQueryKey);
|
|
406
418
|
previousDataMap.set(singleItemQueryKey, previousItemSingle);
|
|
407
419
|
query_1.queryClient.setQueryData(singleItemQueryKey, item);
|
|
@@ -416,7 +428,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
416
428
|
const itemId = createdItem === null || createdItem === void 0 ? void 0 : createdItem.id;
|
|
417
429
|
// 🔧 버그 수정: ID가 유효한 경우에만 개별 캐시에 저장
|
|
418
430
|
if (itemId && typeof itemId === "string") {
|
|
419
|
-
query_1.queryClient.setQueryData(
|
|
431
|
+
query_1.queryClient.setQueryData(itemKey(modelName, itemId), createdItem);
|
|
420
432
|
}
|
|
421
433
|
else {
|
|
422
434
|
console.warn(`🍬 ${modelName} createList: Invalid createdItem ID found, skipping cache update:`, itemId, createdItem);
|
|
@@ -465,7 +477,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
465
477
|
// Get item
|
|
466
478
|
get: (id_1, ...args_1) => __awaiter(this, [id_1, ...args_1], void 0, function* (id, options = { forceRefresh: false }) {
|
|
467
479
|
try {
|
|
468
|
-
const singleItemQueryKey =
|
|
480
|
+
const singleItemQueryKey = itemKey(modelName, id);
|
|
469
481
|
// Check cache first (if forceRefresh is false)
|
|
470
482
|
if (!options.forceRefresh) {
|
|
471
483
|
const cachedItem = query_1.queryClient.getQueryData(singleItemQueryKey);
|
|
@@ -606,7 +618,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
606
618
|
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
607
619
|
// 🔧 버그 수정: ID가 유효한 경우에만 개별 캐시에 저장
|
|
608
620
|
if (itemId && typeof itemId === "string") {
|
|
609
|
-
query_1.queryClient.setQueryData(
|
|
621
|
+
query_1.queryClient.setQueryData(itemKey(modelName, itemId), item);
|
|
610
622
|
}
|
|
611
623
|
else {
|
|
612
624
|
console.warn(`🍬 ${modelName} list: Invalid item ID found, skipping cache update:`, itemId, item);
|
|
@@ -661,7 +673,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
661
673
|
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
662
674
|
// 🔧 버그 수정: ID가 유효한 경우에만 개별 캐시에 저장
|
|
663
675
|
if (itemId && typeof itemId === "string") {
|
|
664
|
-
query_1.queryClient.setQueryData(
|
|
676
|
+
query_1.queryClient.setQueryData(itemKey(modelName, itemId), item);
|
|
665
677
|
}
|
|
666
678
|
else {
|
|
667
679
|
console.warn(`🍬 ${modelName} list fallback: Invalid item ID found, skipping cache update:`, itemId, item);
|
|
@@ -727,7 +739,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
727
739
|
else {
|
|
728
740
|
console.warn(`🍬 ${modelName} update API response missing. Maintaining optimistic update data.`);
|
|
729
741
|
// If no API response, return the data saved during optimistic update
|
|
730
|
-
const singleItemQueryKey =
|
|
742
|
+
const singleItemQueryKey = itemKey(modelName, itemId);
|
|
731
743
|
return (previousDataMap.get(singleItemQueryKey) || null);
|
|
732
744
|
}
|
|
733
745
|
}
|
|
@@ -759,23 +771,23 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
759
771
|
// Backup item data before deletion (for rollback)
|
|
760
772
|
const previousDataMap = new Map();
|
|
761
773
|
// Backup previous data and perform optimistic update for individual item (set to null)
|
|
762
|
-
const singleItemQueryKey =
|
|
774
|
+
const singleItemQueryKey = itemKey(modelName, id);
|
|
763
775
|
const previousItemSingle = query_1.queryClient.getQueryData(singleItemQueryKey);
|
|
764
776
|
previousDataMap.set(singleItemQueryKey, previousItemSingle);
|
|
765
777
|
query_1.queryClient.setQueryData(singleItemQueryKey, null);
|
|
766
778
|
// Backup and perform optimistic update for list queries (remove item)
|
|
767
779
|
relatedQueryKeys.forEach((queryKey) => {
|
|
768
|
-
if (
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
query_1.queryClient.setQueryData(queryKey, (oldData) => {
|
|
775
|
-
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
776
|
-
return oldItems.filter((item) => (item === null || item === void 0 ? void 0 : item.id) !== id);
|
|
777
|
-
});
|
|
780
|
+
if (isItemKeyForModel(modelName, queryKey)) {
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
const data = query_1.queryClient.getQueryData(queryKey);
|
|
784
|
+
if (data) {
|
|
785
|
+
previousDataMap.set(queryKey, data);
|
|
778
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
|
+
});
|
|
779
791
|
});
|
|
780
792
|
try {
|
|
781
793
|
// API call - apply auth mode
|
|
@@ -783,8 +795,12 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
783
795
|
yield (0, client_1.getClient)().models[modelName].delete({ id }, authModeParams);
|
|
784
796
|
console.log(`🍬 ${modelName} delete success:`, id);
|
|
785
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
|
+
}));
|
|
786
802
|
query_1.queryClient.invalidateQueries({
|
|
787
|
-
queryKey:
|
|
803
|
+
queryKey: itemKey(modelName, id),
|
|
788
804
|
refetchType: "active",
|
|
789
805
|
});
|
|
790
806
|
return true;
|
|
@@ -823,7 +839,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
823
839
|
// Optimistic update - remove items from all caches
|
|
824
840
|
ids.forEach((id) => {
|
|
825
841
|
// Backup previous data and perform optimistic update for individual item (null)
|
|
826
|
-
const singleItemQueryKey =
|
|
842
|
+
const singleItemQueryKey = itemKey(modelName, id);
|
|
827
843
|
const previousItemSingle = query_1.queryClient.getQueryData(singleItemQueryKey);
|
|
828
844
|
previousItemsCache.set(id, previousItemSingle || null);
|
|
829
845
|
previousDataMap.set(singleItemQueryKey, previousItemSingle); // Backup to map
|
|
@@ -831,17 +847,17 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
831
847
|
});
|
|
832
848
|
// Update all list query caches (remove items included in id list)
|
|
833
849
|
relatedQueryKeys.forEach((queryKey) => {
|
|
834
|
-
if (
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
query_1.queryClient.setQueryData(queryKey, (oldData) => {
|
|
841
|
-
const oldItems = Array.isArray(oldData) ? oldData : [];
|
|
842
|
-
return oldItems.filter((item) => item && !ids.includes(item.id));
|
|
843
|
-
});
|
|
850
|
+
if (isItemKeyForModel(modelName, queryKey)) {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
const data = query_1.queryClient.getQueryData(queryKey);
|
|
854
|
+
if (data) {
|
|
855
|
+
previousDataMap.set(queryKey, data);
|
|
844
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
|
+
});
|
|
845
861
|
});
|
|
846
862
|
try {
|
|
847
863
|
console.log(`🍬 ${modelName} batch delete attempt [Auth: ${authMode}]: ${ids.length} items`);
|
|
@@ -867,7 +883,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
867
883
|
const previousItem = previousItemsCache.get(failedId);
|
|
868
884
|
if (previousItem !== undefined) {
|
|
869
885
|
// Restore if not undefined (was in cache)
|
|
870
|
-
const singleItemQueryKey =
|
|
886
|
+
const singleItemQueryKey = itemKey(modelName, failedId);
|
|
871
887
|
query_1.queryClient.setQueryData(singleItemQueryKey, previousItem);
|
|
872
888
|
}
|
|
873
889
|
// Restore failed item to list queries
|
|
@@ -892,7 +908,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
892
908
|
}
|
|
893
909
|
// Invalidate all related queries to force refresh (safety mechanism)
|
|
894
910
|
relatedQueryKeys.forEach((queryKey) => query_1.queryClient.invalidateQueries({ queryKey }));
|
|
895
|
-
ids.forEach((id) => query_1.queryClient.invalidateQueries({ queryKey:
|
|
911
|
+
ids.forEach((id) => query_1.queryClient.invalidateQueries({ queryKey: itemKey(modelName, id) }));
|
|
896
912
|
console.log(`🍬 ${modelName} batch delete: ${results.success.length} items deleted, ${results.failed.length} items failed`);
|
|
897
913
|
return results;
|
|
898
914
|
}
|
|
@@ -949,7 +965,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
949
965
|
}
|
|
950
966
|
else {
|
|
951
967
|
console.warn(`🍬 ${modelName} upsert(update) no API response. Keeping optimistic update data.`);
|
|
952
|
-
const singleItemQueryKey =
|
|
968
|
+
const singleItemQueryKey = itemKey(modelName, data.id);
|
|
953
969
|
return (previousDataMap.get(singleItemQueryKey) ||
|
|
954
970
|
null);
|
|
955
971
|
}
|
|
@@ -965,7 +981,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
965
981
|
}
|
|
966
982
|
else {
|
|
967
983
|
console.warn(`🍬 ${modelName} upsert(create) no API response. Keeping optimistic update data.`);
|
|
968
|
-
const singleItemQueryKey =
|
|
984
|
+
const singleItemQueryKey = itemKey(modelName, data.id);
|
|
969
985
|
return (previousDataMap.get(singleItemQueryKey) ||
|
|
970
986
|
null);
|
|
971
987
|
}
|
|
@@ -1047,7 +1063,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1047
1063
|
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
1048
1064
|
// 🔧 버그 수정: ID가 유효한 경우에만 개별 캐시에 저장
|
|
1049
1065
|
if (itemId && typeof itemId === "string") {
|
|
1050
|
-
query_1.queryClient.setQueryData(
|
|
1066
|
+
query_1.queryClient.setQueryData(itemKey(modelName, itemId), item);
|
|
1051
1067
|
}
|
|
1052
1068
|
else {
|
|
1053
1069
|
console.warn(`🍬 ${modelName} customList: Invalid item ID found, skipping cache update:`, itemId, item);
|
|
@@ -1164,7 +1180,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1164
1180
|
// Interface functions implementation
|
|
1165
1181
|
const getItem = (0, react_1.useCallback)((id) => {
|
|
1166
1182
|
// Use useQueryData to get latest single item from current cache
|
|
1167
|
-
const cachedItem = hookQueryClient.getQueryData(
|
|
1183
|
+
const cachedItem = hookQueryClient.getQueryData(itemKey(modelName, id));
|
|
1168
1184
|
// 🔧 버그 수정: 캐시된 아이템의 ID가 요청한 ID와 일치하는지 검증
|
|
1169
1185
|
if (cachedItem) {
|
|
1170
1186
|
const itemId = cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.id;
|
|
@@ -1189,7 +1205,8 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1189
1205
|
console.error(`🍬 ${modelName} useHook create error:`, _error);
|
|
1190
1206
|
throw _error; // Re-throw error
|
|
1191
1207
|
}
|
|
1192
|
-
}), [service, refetch]
|
|
1208
|
+
}), [service, refetch] // Add refetch dependency
|
|
1209
|
+
);
|
|
1193
1210
|
const updateItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1194
1211
|
try {
|
|
1195
1212
|
const result = yield service.update(data);
|
|
@@ -1201,7 +1218,8 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1201
1218
|
console.error(`🍬 ${modelName} useHook update error:`, _error);
|
|
1202
1219
|
throw _error;
|
|
1203
1220
|
}
|
|
1204
|
-
}), [service, refetch]
|
|
1221
|
+
}), [service, refetch] // Add refetch dependency
|
|
1222
|
+
);
|
|
1205
1223
|
const deleteItem = (0, react_1.useCallback)((id) => __awaiter(this, void 0, void 0, function* () {
|
|
1206
1224
|
try {
|
|
1207
1225
|
const result = yield service.delete(id);
|
|
@@ -1213,7 +1231,8 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1213
1231
|
console.error(`🍬 ${modelName} useHook delete error:`, error);
|
|
1214
1232
|
throw error;
|
|
1215
1233
|
}
|
|
1216
|
-
}), [service, refetch]
|
|
1234
|
+
}), [service, refetch] // refetch dependency added
|
|
1235
|
+
);
|
|
1217
1236
|
const refresh = (0, react_1.useCallback)((refreshOptions) => __awaiter(this, void 0, void 0, function* () {
|
|
1218
1237
|
console.log(`🍬 ${modelName} useHook refresh called`, queryKey);
|
|
1219
1238
|
const { data } = yield refetch({ throwOnError: true }); // Throw on error
|
|
@@ -1244,7 +1263,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1244
1263
|
// Hook for managing single item - Reimplemented based on TanStack Query
|
|
1245
1264
|
useItemHook: (id) => {
|
|
1246
1265
|
const hookQueryClient = (0, react_query_1.useQueryClient)();
|
|
1247
|
-
const singleItemQueryKey =
|
|
1266
|
+
const singleItemQueryKey = itemKey(modelName, id);
|
|
1248
1267
|
// First check data from cache
|
|
1249
1268
|
const rawCachedData = hookQueryClient.getQueryData(singleItemQueryKey);
|
|
1250
1269
|
// 🔧 버그 수정: 배열이 캐시되어 있는 경우 처리
|
package/dist/singleton.js
CHANGED
|
@@ -126,8 +126,7 @@ function createSingletonService(baseService, getModelId) {
|
|
|
126
126
|
}),
|
|
127
127
|
// React hook to manage the current singleton item
|
|
128
128
|
useCurrentHook: () => {
|
|
129
|
-
|
|
130
|
-
const { data: currentId } = (0, react_query_1.useQuery)({
|
|
129
|
+
const { data: currentId, isLoading: isIdLoading, error: idError, refetch: refetchId, } = (0, react_query_1.useQuery)({
|
|
131
130
|
queryKey: [modelName, "currentId"],
|
|
132
131
|
queryFn: () => __awaiter(this, void 0, void 0, function* () {
|
|
133
132
|
var _a, _b, _c;
|
|
@@ -136,23 +135,57 @@ function createSingletonService(baseService, getModelId) {
|
|
|
136
135
|
return id || null;
|
|
137
136
|
}
|
|
138
137
|
catch (error) {
|
|
139
|
-
// Safely call getStore to surface errors if available
|
|
140
138
|
try {
|
|
141
139
|
(_c = (_b = (_a = baseService
|
|
142
140
|
.getStore) === null || _a === void 0 ? void 0 : _a.call(baseService)) === null || _b === void 0 ? void 0 : _b.setError) === null || _c === void 0 ? void 0 : _c.call(_b, error instanceof Error ? error : new Error(String(error)));
|
|
143
141
|
}
|
|
144
|
-
catch (_storeError) {
|
|
145
|
-
// Ignore store errors
|
|
146
|
-
}
|
|
142
|
+
catch (_storeError) { }
|
|
147
143
|
return null;
|
|
148
144
|
}
|
|
149
145
|
}),
|
|
150
146
|
staleTime: 1000 * 60,
|
|
151
147
|
refetchOnWindowFocus: false,
|
|
152
148
|
});
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
|
|
149
|
+
const idForItemHook = currentId !== null && currentId !== void 0 ? currentId : "";
|
|
150
|
+
const core = baseService.useItemHook(idForItemHook);
|
|
151
|
+
const item = (() => {
|
|
152
|
+
var _a;
|
|
153
|
+
if (!currentId)
|
|
154
|
+
return null;
|
|
155
|
+
const raw = core.item;
|
|
156
|
+
if (Array.isArray(raw)) {
|
|
157
|
+
const match = raw.find((i) => (i === null || i === void 0 ? void 0 : i.id) === currentId);
|
|
158
|
+
return match || null;
|
|
159
|
+
}
|
|
160
|
+
return (_a = raw) !== null && _a !== void 0 ? _a : null;
|
|
161
|
+
})();
|
|
162
|
+
const isLoading = isIdLoading || core.isLoading;
|
|
163
|
+
const error = idError || core.error || null;
|
|
164
|
+
const refresh = () => __awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
if (!currentId) {
|
|
166
|
+
const { data } = yield refetchId({ throwOnError: false });
|
|
167
|
+
if (!data)
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
return core.refresh();
|
|
171
|
+
});
|
|
172
|
+
const update = (data) => __awaiter(this, void 0, void 0, function* () {
|
|
173
|
+
if (!currentId) {
|
|
174
|
+
const { data } = yield refetchId({ throwOnError: false });
|
|
175
|
+
if (!data)
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
return core.update(data);
|
|
179
|
+
});
|
|
180
|
+
const remove = () => __awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
if (!currentId) {
|
|
182
|
+
const { data } = yield refetchId({ throwOnError: false });
|
|
183
|
+
if (!data)
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return core.delete();
|
|
187
|
+
});
|
|
188
|
+
return { item, isLoading, error, refresh, update, delete: remove };
|
|
156
189
|
} });
|
|
157
190
|
return singletonService;
|
|
158
191
|
}
|