amplifyquery 1.0.18 → 1.0.20
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/README.md +192 -2
- package/dist/config.d.ts +12 -0
- package/dist/config.js +24 -0
- package/dist/index.js +4 -0
- package/dist/service.js +33 -7
- package/dist/singleton.js +28 -2
- package/dist/types.d.ts +10 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,9 +8,11 @@ A library that combines AWS Amplify and React Query, making it easier to manage
|
|
|
8
8
|
- 🔄 **React Query Integration**: Leverage all React Query features like caching, retries, background updates, etc.
|
|
9
9
|
- 📱 **Offline Support**: Persistent query caching via MMKV for fast data loading even offline.
|
|
10
10
|
- 🪝 **Convenient Hooks API**: Abstract complex data synchronization into simple Hooks.
|
|
11
|
+
- 🔴 **Realtime Subscriptions**: Built-in support for AWS Amplify realtime updates with automatic cache synchronization.
|
|
11
12
|
- 🛡 **Auth Mode Support**: Supports various AWS Amplify authentication modes (API Key, IAM, Cognito, etc.).
|
|
12
13
|
- ⚙️ **Global Configuration**: Set model mappings and auth modes once - no more repetitive configuration.
|
|
13
14
|
- ⚡ **Performance Optimized**: Maximize performance with request batching and intelligent caching.
|
|
15
|
+
- 🔄 **Automatic Cache Sync**: Mutations automatically update the cache for consistent UI state.
|
|
14
16
|
|
|
15
17
|
## Installation
|
|
16
18
|
|
|
@@ -192,6 +194,8 @@ await TodoService.delete("some-id");
|
|
|
192
194
|
|
|
193
195
|
### 5. Using React Hooks
|
|
194
196
|
|
|
197
|
+
#### Basic Usage
|
|
198
|
+
|
|
195
199
|
```tsx
|
|
196
200
|
import React from "react";
|
|
197
201
|
import { View, Text, Button } from "react-native";
|
|
@@ -206,11 +210,16 @@ function TodoScreen() {
|
|
|
206
210
|
create,
|
|
207
211
|
update,
|
|
208
212
|
delete: deleteTodo,
|
|
213
|
+
getItem,
|
|
209
214
|
} = TodoService.useHook();
|
|
210
215
|
|
|
211
216
|
// Hook for managing a single item
|
|
212
|
-
const {
|
|
213
|
-
|
|
217
|
+
const {
|
|
218
|
+
item: settings,
|
|
219
|
+
isLoading: isSettingsLoading,
|
|
220
|
+
update: updateSettings,
|
|
221
|
+
refresh: refreshSettings,
|
|
222
|
+
} = UserSettingsService.useItemHook("settings-id");
|
|
214
223
|
|
|
215
224
|
if (isLoading) return <Text>Loading...</Text>;
|
|
216
225
|
if (error) return <Text>Error: {error.message}</Text>;
|
|
@@ -237,8 +246,118 @@ function TodoScreen() {
|
|
|
237
246
|
}
|
|
238
247
|
```
|
|
239
248
|
|
|
249
|
+
#### Realtime Subscriptions
|
|
250
|
+
|
|
251
|
+
Enable real-time updates using AWS Amplify's `observeQuery` feature:
|
|
252
|
+
|
|
253
|
+
```tsx
|
|
254
|
+
function TodoScreen() {
|
|
255
|
+
// Enable realtime for list updates
|
|
256
|
+
const {
|
|
257
|
+
items: todos,
|
|
258
|
+
isLoading,
|
|
259
|
+
isSynced, // true when realtime subscription is active
|
|
260
|
+
create,
|
|
261
|
+
update,
|
|
262
|
+
delete: deleteTodo,
|
|
263
|
+
} = TodoService.useHook({
|
|
264
|
+
realtime: {
|
|
265
|
+
enabled: true,
|
|
266
|
+
// Optional: filter events to subscribe to
|
|
267
|
+
events: ["create", "update", "delete"],
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// Enable realtime for single item updates
|
|
272
|
+
const {
|
|
273
|
+
item: todo,
|
|
274
|
+
isSynced: isItemSynced,
|
|
275
|
+
update: updateTodo,
|
|
276
|
+
} = TodoService.useItemHook(todoId, {
|
|
277
|
+
realtime: {
|
|
278
|
+
enabled: true,
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
return (
|
|
283
|
+
<View>
|
|
284
|
+
{isSynced && <Text>🟢 Live updates active</Text>}
|
|
285
|
+
{todos.map((todo) => (
|
|
286
|
+
<Text key={todo.id}>{todo.name}</Text>
|
|
287
|
+
))}
|
|
288
|
+
</View>
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
**Realtime Features:**
|
|
294
|
+
- ✅ Automatic cache synchronization when data changes
|
|
295
|
+
- ✅ Works across multiple devices/sessions
|
|
296
|
+
- ✅ `isSynced` flag indicates subscription status
|
|
297
|
+
- ✅ Optimistic updates are immediately reflected
|
|
298
|
+
- ✅ No manual refresh needed for real-time changes
|
|
299
|
+
|
|
300
|
+
**Note:** When `realtime.enabled` is `true`, the initial fetch is skipped to avoid duplicate data. The subscription provides the initial data set.
|
|
301
|
+
|
|
240
302
|
## Advanced Features
|
|
241
303
|
|
|
304
|
+
### Realtime Subscriptions
|
|
305
|
+
|
|
306
|
+
AmplifyQuery supports real-time data synchronization using AWS Amplify's `observeQuery` API. When enabled, your UI automatically updates when data changes on the server or other devices.
|
|
307
|
+
|
|
308
|
+
#### useHook Realtime Options
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
const {
|
|
312
|
+
items,
|
|
313
|
+
isSynced, // true when subscription is active
|
|
314
|
+
create,
|
|
315
|
+
update,
|
|
316
|
+
delete: deleteItem,
|
|
317
|
+
} = TodoService.useHook({
|
|
318
|
+
realtime: {
|
|
319
|
+
enabled: true, // Enable realtime subscription
|
|
320
|
+
events: ["create", "update", "delete"], // Optional: filter events
|
|
321
|
+
observeOptions: {
|
|
322
|
+
// Optional: additional observeQuery options
|
|
323
|
+
filter: { completed: { eq: false } },
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
#### useItemHook Realtime Options
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
const {
|
|
333
|
+
item,
|
|
334
|
+
isSynced, // true when subscription is active
|
|
335
|
+
update,
|
|
336
|
+
delete: deleteItem,
|
|
337
|
+
} = TodoService.useItemHook(todoId, {
|
|
338
|
+
realtime: {
|
|
339
|
+
enabled: true,
|
|
340
|
+
observeOptions: {
|
|
341
|
+
// Optional: additional observeQuery options
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
});
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
#### How It Works
|
|
348
|
+
|
|
349
|
+
1. **Initial Load**: When `realtime.enabled` is `true`, the hook subscribes to `observeQuery` instead of making a one-time fetch
|
|
350
|
+
2. **Cache Updates**: All changes (create/update/delete) are automatically synchronized to the React Query cache
|
|
351
|
+
3. **Cross-Device**: Changes made on other devices or sessions are immediately reflected
|
|
352
|
+
4. **Optimistic Updates**: Local mutations are immediately reflected, then confirmed via realtime events
|
|
353
|
+
|
|
354
|
+
#### Best Practices
|
|
355
|
+
|
|
356
|
+
- Use realtime for collaborative features or when data changes frequently
|
|
357
|
+
- Monitor `isSynced` to show connection status to users
|
|
358
|
+
- Combine with optimistic updates for the best user experience
|
|
359
|
+
- Consider using `events` filter to reduce unnecessary updates
|
|
360
|
+
|
|
242
361
|
### Global Configuration
|
|
243
362
|
|
|
244
363
|
AmplifyQuery supports global configuration to reduce code duplication and simplify service creation.
|
|
@@ -338,6 +457,25 @@ TodoService.resetCache();
|
|
|
338
457
|
const todos = await TodoService.list({ forceRefresh: true });
|
|
339
458
|
```
|
|
340
459
|
|
|
460
|
+
#### Automatic Cache Synchronization
|
|
461
|
+
|
|
462
|
+
AmplifyQuery automatically synchronizes the cache when you use hook methods (`create`, `update`, `delete`):
|
|
463
|
+
|
|
464
|
+
- **Immediate Updates**: Changes are reflected in the UI immediately after successful operations
|
|
465
|
+
- **Consistent State**: The cache stays in sync across all hook instances
|
|
466
|
+
- **Optimistic Updates**: UI updates before server confirmation for better UX
|
|
467
|
+
- **Realtime Integration**: Works seamlessly with realtime subscriptions
|
|
468
|
+
|
|
469
|
+
```tsx
|
|
470
|
+
// Create, update, delete automatically update the cache
|
|
471
|
+
const { create, update, delete: deleteItem } = TodoService.useHook();
|
|
472
|
+
|
|
473
|
+
// These operations immediately update the hook's items array
|
|
474
|
+
await create({ name: "New Todo" });
|
|
475
|
+
await update({ id: todoId, completed: true });
|
|
476
|
+
await deleteItem(todoId);
|
|
477
|
+
```
|
|
478
|
+
|
|
341
479
|
### Authentication Modes
|
|
342
480
|
|
|
343
481
|
Access data with various authentication methods.
|
|
@@ -363,10 +501,62 @@ await TodoService.list({ authMode: "iam" });
|
|
|
363
501
|
const adminTodoService = TodoService.withAuthMode("iam");
|
|
364
502
|
```
|
|
365
503
|
|
|
504
|
+
### Hook API Reference
|
|
505
|
+
|
|
506
|
+
#### useHook(options?)
|
|
507
|
+
|
|
508
|
+
Returns a hook for managing a list of items.
|
|
509
|
+
|
|
510
|
+
**Options:**
|
|
511
|
+
- `initialFetchOptions?: { fetch?: boolean, filter?: Record<string, any> }` - Control initial data fetch
|
|
512
|
+
- `customList?: { queryName: string, args: Record<string, any>, forceRefresh?: boolean }` - Use custom query for list
|
|
513
|
+
- `realtime?: { enabled?: boolean, observeOptions?: Record<string, any>, events?: Array<"create" | "update" | "delete"> }` - Enable realtime subscriptions
|
|
514
|
+
|
|
515
|
+
**Returns:**
|
|
516
|
+
```typescript
|
|
517
|
+
{
|
|
518
|
+
items: T[]; // Array of items
|
|
519
|
+
isLoading: boolean; // Loading state
|
|
520
|
+
error: Error | null; // Error state
|
|
521
|
+
isSynced?: boolean; // Realtime sync status (if realtime enabled)
|
|
522
|
+
getItem: (id: string) => T | undefined; // Get item by ID from cache
|
|
523
|
+
refresh: (options?: { filter?: Record<string, any> }) => Promise<T[]>; // Refresh list
|
|
524
|
+
create: (data: Partial<T>) => Promise<T | null>; // Create new item
|
|
525
|
+
update: (data: Partial<T> & { id: string }) => Promise<T | null>; // Update item
|
|
526
|
+
delete: (id: string) => Promise<boolean>; // Delete item
|
|
527
|
+
customList?: (queryName: string, args: Record<string, any>, options?: { forceRefresh?: boolean }) => Promise<T[]>; // Custom list query
|
|
528
|
+
}
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
#### useItemHook(id, options?)
|
|
532
|
+
|
|
533
|
+
Returns a hook for managing a single item.
|
|
534
|
+
|
|
535
|
+
**Parameters:**
|
|
536
|
+
- `id: string` - The ID of the item to manage
|
|
537
|
+
|
|
538
|
+
**Options:**
|
|
539
|
+
- `realtime?: { enabled?: boolean, observeOptions?: Record<string, any> }` - Enable realtime subscription
|
|
540
|
+
|
|
541
|
+
**Returns:**
|
|
542
|
+
```typescript
|
|
543
|
+
{
|
|
544
|
+
item: T | null; // The item (null if not found or not loaded)
|
|
545
|
+
isLoading: boolean; // Loading state
|
|
546
|
+
error: Error | null; // Error state
|
|
547
|
+
isSynced?: boolean; // Realtime sync status (if realtime enabled)
|
|
548
|
+
refresh: () => Promise<T | null>; // Refresh item
|
|
549
|
+
update: (data: Partial<T>) => Promise<T | null>; // Update item (id not needed)
|
|
550
|
+
delete: () => Promise<boolean>; // Delete item (id not needed)
|
|
551
|
+
}
|
|
552
|
+
```
|
|
553
|
+
|
|
366
554
|
## Important Notes
|
|
367
555
|
|
|
368
556
|
- This library is designed to be used with AWS Amplify v6 or higher.
|
|
369
557
|
- Requires React Query v5 or higher.
|
|
558
|
+
- When `realtime.enabled` is `true`, the initial fetch is skipped to avoid duplicate data.
|
|
559
|
+
- All mutations (`create`, `update`, `delete`) automatically synchronize the cache.
|
|
370
560
|
- Test thoroughly before using in a production project.
|
|
371
561
|
|
|
372
562
|
## License
|
package/dist/config.d.ts
CHANGED
|
@@ -35,3 +35,15 @@ export declare function getDefaultAuthMode(): "apiKey" | "iam" | "identityPool"
|
|
|
35
35
|
* Reset global configuration (mainly for testing)
|
|
36
36
|
*/
|
|
37
37
|
export declare function resetConfig(): void;
|
|
38
|
+
/**
|
|
39
|
+
* Enable/disable singleton auto-create behavior for useCurrentHook().
|
|
40
|
+
*/
|
|
41
|
+
export declare function setSingletonAutoCreate(config: {
|
|
42
|
+
enabled?: boolean;
|
|
43
|
+
models?: string[];
|
|
44
|
+
}): void;
|
|
45
|
+
export declare function getSingletonAutoCreate(): {
|
|
46
|
+
enabled?: boolean;
|
|
47
|
+
models?: string[];
|
|
48
|
+
} | undefined;
|
|
49
|
+
export declare function isSingletonAutoCreateEnabledForModel(modelName: string): boolean;
|
package/dist/config.js
CHANGED
|
@@ -9,6 +9,9 @@ exports.getOwnerQueryName = getOwnerQueryName;
|
|
|
9
9
|
exports.setDefaultAuthMode = setDefaultAuthMode;
|
|
10
10
|
exports.getDefaultAuthMode = getDefaultAuthMode;
|
|
11
11
|
exports.resetConfig = resetConfig;
|
|
12
|
+
exports.setSingletonAutoCreate = setSingletonAutoCreate;
|
|
13
|
+
exports.getSingletonAutoCreate = getSingletonAutoCreate;
|
|
14
|
+
exports.isSingletonAutoCreateEnabledForModel = isSingletonAutoCreateEnabledForModel;
|
|
12
15
|
// Global configuration state
|
|
13
16
|
let globalConfig = {};
|
|
14
17
|
/**
|
|
@@ -61,3 +64,24 @@ function resetConfig() {
|
|
|
61
64
|
globalConfig = {};
|
|
62
65
|
console.log("🔧 AmplifyQuery: Global configuration reset");
|
|
63
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Enable/disable singleton auto-create behavior for useCurrentHook().
|
|
69
|
+
*/
|
|
70
|
+
function setSingletonAutoCreate(config) {
|
|
71
|
+
globalConfig.singletonAutoCreate = {
|
|
72
|
+
enabled: config.enabled === true,
|
|
73
|
+
models: Array.isArray(config.models) ? config.models : undefined,
|
|
74
|
+
};
|
|
75
|
+
console.log("🔧 AmplifyQuery: Singleton auto-create configured", globalConfig.singletonAutoCreate);
|
|
76
|
+
}
|
|
77
|
+
function getSingletonAutoCreate() {
|
|
78
|
+
return globalConfig.singletonAutoCreate;
|
|
79
|
+
}
|
|
80
|
+
function isSingletonAutoCreateEnabledForModel(modelName) {
|
|
81
|
+
const cfg = getSingletonAutoCreate();
|
|
82
|
+
if (!(cfg === null || cfg === void 0 ? void 0 : cfg.enabled))
|
|
83
|
+
return false;
|
|
84
|
+
if (!cfg.models || cfg.models.length === 0)
|
|
85
|
+
return true;
|
|
86
|
+
return cfg.models.includes(modelName);
|
|
87
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -69,6 +69,10 @@ function configure(config) {
|
|
|
69
69
|
if (config.defaultAuthMode) {
|
|
70
70
|
(0, config_1.setDefaultAuthMode)(config.defaultAuthMode);
|
|
71
71
|
}
|
|
72
|
+
// Set singleton auto-create
|
|
73
|
+
if (config.singletonAutoCreate) {
|
|
74
|
+
(0, config_1.setSingletonAutoCreate)(config.singletonAutoCreate);
|
|
75
|
+
}
|
|
72
76
|
// Apply React Query settings
|
|
73
77
|
(0, query_1.configure)({
|
|
74
78
|
isCachingEnabled: config.isCachingEnabled,
|
package/dist/service.js
CHANGED
|
@@ -1122,7 +1122,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1122
1122
|
},
|
|
1123
1123
|
// React Hook returning method - Reimplemented based on TanStack Query
|
|
1124
1124
|
useHook: (options) => {
|
|
1125
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
1125
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
|
|
1126
1126
|
const hookQueryClient = (0, react_query_1.useQueryClient)();
|
|
1127
1127
|
// Determine query key
|
|
1128
1128
|
const queryKey = (0, react_1.useMemo)(() => {
|
|
@@ -1372,10 +1372,32 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1372
1372
|
}), [service, refetch] // refetch dependency added
|
|
1373
1373
|
);
|
|
1374
1374
|
const refresh = (0, react_1.useCallback)((refreshOptions) => __awaiter(this, void 0, void 0, function* () {
|
|
1375
|
+
var _a, _b;
|
|
1375
1376
|
console.log(`🍬 ${modelName} useHook refresh called`, queryKey);
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1377
|
+
// IMPORTANT:
|
|
1378
|
+
// TanStack Query's refetch() re-runs queryFn, but our service.list/customList can short-circuit
|
|
1379
|
+
// and return cached data unless forceRefresh is true.
|
|
1380
|
+
// refresh() must guarantee a network fetch to reflect server-side updates.
|
|
1381
|
+
try {
|
|
1382
|
+
if (options === null || options === void 0 ? void 0 : options.customList) {
|
|
1383
|
+
return yield service.customList(options.customList.queryName, options.customList.args, { forceRefresh: true });
|
|
1384
|
+
}
|
|
1385
|
+
const mergedFilter = (_a = refreshOptions === null || refreshOptions === void 0 ? void 0 : refreshOptions.filter) !== null && _a !== void 0 ? _a : (_b = options === null || options === void 0 ? void 0 : options.initialFetchOptions) === null || _b === void 0 ? void 0 : _b.filter;
|
|
1386
|
+
if (mergedFilter) {
|
|
1387
|
+
return yield service.list({
|
|
1388
|
+
filter: mergedFilter,
|
|
1389
|
+
forceRefresh: true,
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
return yield service.list({ forceRefresh: true });
|
|
1393
|
+
}
|
|
1394
|
+
catch (e) {
|
|
1395
|
+
// Keep previous behavior of surfacing errors via refetch when desired
|
|
1396
|
+
// while still logging consistently.
|
|
1397
|
+
console.error(`🍬 ${modelName} useHook refresh error:`, e);
|
|
1398
|
+
throw e;
|
|
1399
|
+
}
|
|
1400
|
+
}), [modelName, options === null || options === void 0 ? void 0 : options.customList, (_p = options === null || options === void 0 ? void 0 : options.initialFetchOptions) === null || _p === void 0 ? void 0 : _p.filter, queryKey, service]);
|
|
1379
1401
|
const customListFn = (0, react_1.useCallback)((queryName, args, options) => __awaiter(this, void 0, void 0, function* () {
|
|
1380
1402
|
try {
|
|
1381
1403
|
const result = yield service.customList(queryName, args, options);
|
|
@@ -1527,9 +1549,13 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1527
1549
|
// Interface function implementations
|
|
1528
1550
|
const refreshItem = (0, react_1.useCallback)(() => __awaiter(this, void 0, void 0, function* () {
|
|
1529
1551
|
console.log(`🍬 ${modelName} useItemHook refresh called`, singleItemQueryKey);
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1552
|
+
// IMPORTANT:
|
|
1553
|
+
// refetch() re-runs queryFn which calls service.get(id) (default forceRefresh=false).
|
|
1554
|
+
// If cache exists, service.get will return cached data and never hit the network.
|
|
1555
|
+
// refresh() must guarantee a network fetch to reflect server-side updates.
|
|
1556
|
+
const latest = yield service.get(id, { forceRefresh: true });
|
|
1557
|
+
return latest || null;
|
|
1558
|
+
}), [id, modelName, service, singleItemQueryKey]); // Added id/modelName for correctness
|
|
1533
1559
|
const updateItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1534
1560
|
// No additional work needed here as cache is already updated in service.update
|
|
1535
1561
|
try {
|
package/dist/singleton.js
CHANGED
|
@@ -14,6 +14,8 @@ exports.createSingletonService = createSingletonService;
|
|
|
14
14
|
const client_1 = require("./client");
|
|
15
15
|
const auth_1 = require("aws-amplify/auth");
|
|
16
16
|
const react_query_1 = require("@tanstack/react-query");
|
|
17
|
+
const config_1 = require("./config");
|
|
18
|
+
const react_1 = require("react");
|
|
17
19
|
/**
|
|
18
20
|
* Function to create an extension service for singleton models
|
|
19
21
|
* @param baseService Base service
|
|
@@ -125,7 +127,7 @@ function createSingletonService(baseService, getModelId) {
|
|
|
125
127
|
}
|
|
126
128
|
}),
|
|
127
129
|
// React hook to manage the current singleton item
|
|
128
|
-
useCurrentHook: () => {
|
|
130
|
+
useCurrentHook: (options) => {
|
|
129
131
|
const { data: currentId, isLoading: isIdLoading, error: idError, refetch: refetchId, } = (0, react_query_1.useQuery)({
|
|
130
132
|
queryKey: [modelName, "currentId"],
|
|
131
133
|
queryFn: () => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -147,7 +149,8 @@ function createSingletonService(baseService, getModelId) {
|
|
|
147
149
|
refetchOnWindowFocus: false,
|
|
148
150
|
});
|
|
149
151
|
const idForItemHook = currentId !== null && currentId !== void 0 ? currentId : "";
|
|
150
|
-
const core = baseService.useItemHook(idForItemHook);
|
|
152
|
+
const core = baseService.useItemHook(idForItemHook, options);
|
|
153
|
+
const didAutoCreateRef = (0, react_1.useRef)(false);
|
|
151
154
|
const item = (() => {
|
|
152
155
|
var _a;
|
|
153
156
|
if (!currentId)
|
|
@@ -161,6 +164,29 @@ function createSingletonService(baseService, getModelId) {
|
|
|
161
164
|
})();
|
|
162
165
|
const isLoading = isIdLoading || core.isLoading;
|
|
163
166
|
const error = idError || core.error || null;
|
|
167
|
+
(0, react_1.useEffect)(() => {
|
|
168
|
+
if (!currentId)
|
|
169
|
+
return;
|
|
170
|
+
if (isLoading)
|
|
171
|
+
return;
|
|
172
|
+
if (!(0, config_1.isSingletonAutoCreateEnabledForModel)(modelName))
|
|
173
|
+
return;
|
|
174
|
+
if (didAutoCreateRef.current)
|
|
175
|
+
return;
|
|
176
|
+
if (item)
|
|
177
|
+
return;
|
|
178
|
+
didAutoCreateRef.current = true;
|
|
179
|
+
// Best-effort: create { id } if missing.
|
|
180
|
+
void (() => __awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
try {
|
|
182
|
+
yield singletonService.upsertCurrent({});
|
|
183
|
+
yield core.refresh();
|
|
184
|
+
}
|
|
185
|
+
catch (e) {
|
|
186
|
+
console.warn(`🍬 ${modelName} useCurrentHook auto-create failed:`, e);
|
|
187
|
+
}
|
|
188
|
+
}))();
|
|
189
|
+
}, [currentId, isLoading, item, modelName]);
|
|
164
190
|
const refresh = () => __awaiter(this, void 0, void 0, function* () {
|
|
165
191
|
if (!currentId) {
|
|
166
192
|
const { data } = yield refetchId({ throwOnError: false });
|
package/dist/types.d.ts
CHANGED
|
@@ -118,7 +118,12 @@ export interface SingletonAmplifyService<T> extends AmplifyDataService<T> {
|
|
|
118
118
|
}) => Promise<T | null>;
|
|
119
119
|
updateCurrent: (data: Partial<T>) => Promise<T | null>;
|
|
120
120
|
upsertCurrent: (data: Partial<T>) => Promise<T | null>;
|
|
121
|
-
useCurrentHook: (
|
|
121
|
+
useCurrentHook: (options?: {
|
|
122
|
+
realtime?: {
|
|
123
|
+
enabled?: boolean;
|
|
124
|
+
observeOptions?: Record<string, any>;
|
|
125
|
+
};
|
|
126
|
+
}) => ItemHook<T>;
|
|
122
127
|
}
|
|
123
128
|
export interface BaseModel {
|
|
124
129
|
id: string;
|
|
@@ -142,6 +147,10 @@ export interface AmplifyQueryConfig {
|
|
|
142
147
|
client: GraphQLClient;
|
|
143
148
|
defaultAuthMode?: AuthMode;
|
|
144
149
|
modelOwnerQueryMap?: Record<string, string>;
|
|
150
|
+
singletonAutoCreate?: {
|
|
151
|
+
enabled?: boolean;
|
|
152
|
+
models?: string[];
|
|
153
|
+
};
|
|
145
154
|
isCachingEnabled?: boolean;
|
|
146
155
|
queryClientConfig?: QueryClientConfig;
|
|
147
156
|
storage?: {
|