@nkzw/fate-indexeddb 1.16.1 → 2.0.0
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 +64 -7
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1629,6 +1629,31 @@ Mutations are delivered in the order they were saved, one at a time per persiste
|
|
|
1629
1629
|
|
|
1630
1630
|
The mutation promise stays pending until confirmation, which can take until the user is back online. After a reload, use [Persistence State](#persistence-state) to show pending or failed changes.
|
|
1631
1631
|
|
|
1632
|
+
We can add client fields or change computations without invalidating saved server mutations. Their recovery data contains server fields; saved client fields use [migrations](/docs/guide/client-state.md#migrations). If the server schema scope changes or a mutation is removed, fate keeps the mutation's input and marks it as failed.
|
|
1633
|
+
|
|
1634
|
+
#### Waiting for Local Persistence
|
|
1635
|
+
|
|
1636
|
+
Let's say we want to close our post editor once the change is saved on the device, even while offline. Each mutation promise has a `locallyPersisted` promise that we can wait for:
|
|
1637
|
+
|
|
1638
|
+
```tsx
|
|
1639
|
+
const pending = fate.mutations.post.edit({
|
|
1640
|
+
input: { id: post.id, title },
|
|
1641
|
+
optimistic: { title },
|
|
1642
|
+
});
|
|
1643
|
+
|
|
1644
|
+
void reportResult(pending);
|
|
1645
|
+
await pending.locallyPersisted;
|
|
1646
|
+
closeEditor();
|
|
1647
|
+
```
|
|
1648
|
+
|
|
1649
|
+
Our editor closes after fate saves the mutation and the data needed to restore it after a reload. If the mutation is still waiting to be sent, it is also available in the [persistence state](#persistence-state) at this point. Awaiting `pending` waits for the server response instead.
|
|
1650
|
+
|
|
1651
|
+
The server can still reject the change after it was saved locally. Replace `reportResult` with your app's handler for the eventual result, including returned errors and rejected promises. A later rejection doesn't change the resolved `locallyPersisted` promise.
|
|
1652
|
+
|
|
1653
|
+
For [client mutations](/docs/guide/client-state.md#reading-and-updating-state), `locallyPersisted` waits for the client data to be saved. The same property is available when calling `fate.actions` directly, including actions bound with `.with()`.
|
|
1654
|
+
|
|
1655
|
+
If validation or storage fails, `locallyPersisted` rejects, so our editor can stay open and show the error. It also rejects if the mutation isn't saved: when using `persist: false`, without persistence configured, or for a server mutation without durable delivery. Each mutation is saved separately; waiting for one save doesn't combine it with a later mutation.
|
|
1656
|
+
|
|
1632
1657
|
#### Skipping Persistence
|
|
1633
1658
|
|
|
1634
1659
|
For a call that should not be saved or retried, pass `persist: false`:
|
|
@@ -1683,7 +1708,35 @@ const server = createFateServer({
|
|
|
1683
1708
|
|
|
1684
1709
|
Your app supplies the database and receipt helpers. Lock each `(scope, id)` across server processes, and use `ctx.db` for mutation writes so they share the receipt's transaction. See the [complete SQLite example](https://github.com/nkzw-tech/fate/blob/main/example/persistence/server.ts).
|
|
1685
1710
|
|
|
1686
|
-
Keep receipts for as long as an offline client might retry. For effects outside the database, such as payments or emails, use the destination's idempotency support and a transactional outbox.
|
|
1711
|
+
Keep receipts for as long as an offline client might retry. For mutations with an expiry, we can clean up their receipts as described in [Receipt Retention](#receipt-retention). For effects outside the database, such as payments or emails, use the destination's idempotency support and a transactional outbox.
|
|
1712
|
+
|
|
1713
|
+
### Receipt Retention
|
|
1714
|
+
|
|
1715
|
+
Let's say a user likes a post, but closes the app before receiving the server response. When they return, fate retries the mutation with the same ID. The server reads its saved receipt and returns the result without adding another like.
|
|
1716
|
+
|
|
1717
|
+
By default, a queued mutation expires after **30 days**, including time spent offline. We can change this with `mutationMaxAge` on `createPersistence`. The server's `createMutationIdempotency` also defaults to 30 days through its `maxAge` option. To allow a longer lifetime, configure both sides.
|
|
1718
|
+
|
|
1719
|
+
The mutation ID includes its creation time and expiry. Retries and page reloads keep the same ID, so changing the configuration only affects new mutations. The server rejects mutations from a client clock more than five minutes ahead. A clock that is behind gives the mutation less time to reach the server.
|
|
1720
|
+
|
|
1721
|
+
#### Cleaning Up Receipts
|
|
1722
|
+
|
|
1723
|
+
Save the receipt's optional `expiresAt` timestamp with its result. We can delete expired receipts once every server handling these mutations enforces expiry. Allow some extra time for differences between server clocks. For example, a daily job could delete receipts that expired more than a day ago.
|
|
1724
|
+
|
|
1725
|
+
This is why mutations have a fixed deadline. If we deleted a receipt while its mutation could still be retried, the server would have no way to know that it had already applied the change. Rejecting expired mutations lets us clean up receipts without applying the same mutation again.
|
|
1726
|
+
|
|
1727
|
+
Native HTTP delivery uses protocol version 3 for expiring mutations. Older native servers reject these requests before executing them. Custom durable transports must also enforce expiry and preserve the `MUTATION_EXPIRED` error (HTTP 410).
|
|
1728
|
+
|
|
1729
|
+
#### Handling Expired Mutations
|
|
1730
|
+
|
|
1731
|
+
When a mutation expires, the server returns `MUTATION_EXPIRED`, even if its receipt has already been deleted. fate rolls back the optimistic update and keeps the input in persistence with `status: 'failed'` and `failureReason: 'expired'`.
|
|
1732
|
+
|
|
1733
|
+
For our like button, the server may have applied the like before the user closed the app. We should refresh the post before deciding whether to offer another attempt. fate doesn't assign a new ID or retry the expired mutation automatically. After handling the failure, call `session.discard(id)` to remove it from the queue.
|
|
1734
|
+
|
|
1735
|
+
When the receipt and database changes share a transaction, the server applies each mutation ID at most once within its lifetime. A mutation can still expire before reaching the server. Effects outside that transaction need their own idempotency support.
|
|
1736
|
+
|
|
1737
|
+
#### Existing Receipts
|
|
1738
|
+
|
|
1739
|
+
Older mutation IDs have no expiry. `createMutationIdempotency` accepts them by default, so their receipts need to stay saved. After retiring older clients, set `allowLegacyIdentities: false` to reject these mutations with the same expiry error. Once all server instances use that setting, their receipts can be deleted too.
|
|
1687
1740
|
|
|
1688
1741
|
### tRPC, GraphQL, and Custom Transports
|
|
1689
1742
|
|
|
@@ -1718,7 +1771,11 @@ return idempotency.execute({
|
|
|
1718
1771
|
|
|
1719
1772
|
Forward the entire identity on every attempt. When `identity.replayOnly` is true, the endpoint must return the saved result or a 404 without running the mutation. `createMutationIdempotency` handles this for you.
|
|
1720
1773
|
|
|
1721
|
-
|
|
1774
|
+
We can also send saved mutations through an existing GraphQL route. Set `durableMutations` to the supported mutation names without passing a custom `mutateDurably` adapter. fate sends each listed mutation separately, adding `@fateDurable` to the operation and its identity to `extensions.fateMutation`.
|
|
1775
|
+
|
|
1776
|
+
Set up the server before enabling this option. Declare `directive @fateDurable on MUTATION`, then wrap the single mutation with `createMutationIdempotency` in your execution middleware. For requests with `identity.replayOnly`, return the saved GraphQL result or a 404 without running the mutation. A server without the directive rejects the operation during GraphQL validation.
|
|
1777
|
+
|
|
1778
|
+
For a custom GraphQL adapter that supports only some mutations, pass `durableMutations: ['setLocale', 'updateSettings']` alongside `mutateDurably`. Other mutations send immediately. Explicit `persist: true` fails for an unsupported mutation, and previously queued calls remain saved if the adapter stops supporting them. Omitting the list means the adapter supports every mutation.
|
|
1722
1779
|
|
|
1723
1780
|
You can use persistence for cached reads and client state before adding a durable mutation adapter. Server mutations send immediately by default in that setup. Pass `persist: true` when a call must be saved before delivery. It fails before sending if persistence or the adapter is missing.
|
|
1724
1781
|
|
|
@@ -3119,13 +3176,13 @@ For Prisma, pass Prisma query options such as `{ where: { ... } }` in `extra` in
|
|
|
3119
3176
|
|
|
3120
3177
|
### Data View Resolvers
|
|
3121
3178
|
|
|
3122
|
-
Data views can define computed fields with their own data dependencies. Let's say we want to show a user's email only to that user, and include a comment count on each post. We can define both fields with `
|
|
3179
|
+
Data views can define computed fields with their own data dependencies. Let's say we want to show a user's email only to that user, and include a comment count on each post. We can define both fields with `memo`:
|
|
3123
3180
|
|
|
3124
3181
|
```tsx
|
|
3125
|
-
import {
|
|
3182
|
+
import { memo, count } from '@nkzw/fate/server';
|
|
3126
3183
|
|
|
3127
3184
|
export const userDataView = dataView<UserItem>('User')({
|
|
3128
|
-
email:
|
|
3185
|
+
email: memo({
|
|
3129
3186
|
authorize: ({ id }, context?: AppContext) => context?.sessionUser?.id === id,
|
|
3130
3187
|
select: {
|
|
3131
3188
|
email: true,
|
|
@@ -3136,7 +3193,7 @@ export const userDataView = dataView<UserItem>('User')({
|
|
|
3136
3193
|
});
|
|
3137
3194
|
|
|
3138
3195
|
export const postDataView = dataView<PostItem>('Post')({
|
|
3139
|
-
commentCount:
|
|
3196
|
+
commentCount: memo({
|
|
3140
3197
|
select: {
|
|
3141
3198
|
count: count('comments'),
|
|
3142
3199
|
},
|
|
@@ -3148,7 +3205,7 @@ export const postDataView = dataView<PostItem>('Post')({
|
|
|
3148
3205
|
|
|
3149
3206
|
The `select` object describes what each resolver needs. `email: true` selects the user's stored email, and `count('comments')` asks the adapter to count the post's comments. The resolver receives those values in its second argument. Annotating the first argument with `UserItem` or `PostItem` lets TypeScript infer the dependency types.
|
|
3150
3207
|
|
|
3151
|
-
Use `field('path')` to give a dependency a different name or read through a relationship. The adapters fetch the dependencies for you, while the client only receives the computed field's result. The same `dataView` and `
|
|
3208
|
+
Use `field('path')` to give a dependency a different name or read through a relationship. The adapters fetch the dependencies for you, while the client only receives the computed field's result. The same `dataView` and `memo` helpers are available for [client computed fields](/docs/guide/client-state.md#computed-fields).
|
|
3152
3209
|
|
|
3153
3210
|
### Connecting the Client
|
|
3154
3211
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nkzw/fate-indexeddb",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Optional IndexedDB persistence adapter for fate.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"idb": "^8.0.4"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@nkzw/fate": "
|
|
23
|
-
"fake-indexeddb": "^6.2.
|
|
24
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@1.
|
|
25
|
-
"vite-plus": "1.
|
|
22
|
+
"@nkzw/fate": "2.0.0",
|
|
23
|
+
"fake-indexeddb": "^6.2.8",
|
|
24
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.6.0",
|
|
25
|
+
"vite-plus": "1.6.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"@nkzw/fate": "^1.5.1"
|