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.
Files changed (2) hide show
  1. package/dist/service.js +118 -31
  2. 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
- queryClient.setQueryData([modelName, itemId], updatedItem);
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
- query_1.queryClient.setQueryData([modelName, createdItem.id], createdItem);
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
- return cachedItem;
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: item } = yield (0, client_1.getClient)().models[modelName].get({ id }, authModeParams);
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
- query_1.queryClient.setQueryData(singleItemQueryKey, item);
460
- // Update related list queries (lists that might contain this item)
461
- const relatedQueryKeys = findRelatedQueryKeys(modelName, query_1.queryClient);
462
- relatedQueryKeys.forEach((queryKey) => {
463
- // Exclude single item keys, only process list queries
464
- if (queryKey.length > 1 && queryKey[1] !== id) {
465
- // Query keys with id as second element are not single item get query key format, so treat as list
466
- query_1.queryClient.setQueryData(queryKey, (oldData) => {
467
- const oldItems = Array.isArray(oldData) ? oldData : [];
468
- const exists = oldItems.some((oldItem) => oldItem && oldItem.id === id);
469
- if (exists) {
470
- return oldItems.map((oldItem) => oldItem && oldItem.id === id ? item : oldItem);
471
- }
472
- else {
473
- // Need to check if item matches list query filter conditions (not checking here)
474
- // If checking is difficult, invalidateQueries might be safer
475
- // Currently implemented to add item to all related lists that might contain it on API get success
476
- return [...oldItems, item];
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
- query_1.queryClient.setQueryData([modelName, item.id], item);
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
- query_1.queryClient.setQueryData([modelName, item.id], item);
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
- query_1.queryClient.setQueryData([modelName, item.id], item);
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
- return hookQueryClient.getQueryData([modelName, id]);
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 cachedData = hookQueryClient.getQueryData(singleItemQueryKey);
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amplifyquery",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Amplify+Query",
5
5
  "keywords": [
6
6
  "Amplify",