amplifyquery 1.0.18 → 1.0.19
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/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
|