Skip to content

Commit b3c38e5

Browse files
david-mcafeeRachel Lee Nabors
authored andcommitted
feat(data): add TypeScript examples to DataStore docs; fix broken examples; fix syntax errors (#5155)
* feat(data): add TypeScript code examples * add additional samples; fix errors * more samples * add relational examples * add sync examples * minor fixes * fix typo * minor fixes * fix existing code snippet bug for relational models * example updates * fix(data): fix for #5156 * fix(data): fix for TS conflict handler issue: aws-amplify/amplify-js#11006 * feat(data): update PR template with E2E test check for code examples * fix(data): fix DataStore 'update with predicate' example. Fixes #5231 * fix(data): simplify random number generation in docs examples * ts docs updates * updates * add async / await to examples
1 parent 87d60cd commit b3c38e5

24 files changed

+243
-87
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ Which platform(s) are affected by this PR (if applicable)?
3030

3131
- [ ] Are any files being deleted with this PR? If so, have the needed redirects been created?
3232

33-
- [ ] Are all links in MDX files using the MDX link syntax rather than HTML link syntax? <br />
34-
_ref: MDX: `[link](https://link.com)`
33+
- [ ] Are all links in MDX files using the MDX link syntax rather than HTML link syntax? <br />
34+
_ref: MDX: `[link](https://link.com)`
3535
HTML: `<a href="https://link.com">link</a>`_
36-
36+
3737
### When this PR is ready to merge, please check the box below
3838
- [ ] Ready to merge
3939

src/fragments/lib/datastore/js/conflict.mdx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,42 @@ Finally you can configure the number of records to sync as an upper bound on ite
66

77
The code below illustrates a conflict resolution handler for the `Post` model that retries a mutation with the same title, but the most recent remote data for all other fields. The conflict resolution handler discards conflicts for all other models (by returning the `DISCARD` symbol imported from `@aws-amplify/datastore`).
88

9+
<BlockSwitcher>
10+
<Block name="TypeScript">
11+
12+
```ts
13+
import {
14+
DISCARD,
15+
PersistentModelConstructor,
16+
} from "@aws-amplify/datastore";
17+
18+
DataStore.configure({
19+
errorHandler: (error) => {
20+
console.warn("Unrecoverable error", { error });
21+
},
22+
conflictHandler: async (data) => {
23+
// Example conflict handler
24+
if (data.modelConstructor as PersistentModelConstructor<Todo> === Todo) {
25+
const remoteModel = data.remoteModel as Todo;
26+
const localModel = data.localModel as Todo;
27+
28+
const newModel = Todo.copyOf(remoteModel, (d) => {
29+
d.name = localModel.name;
30+
});
31+
32+
return newModel;
33+
}
34+
35+
return DISCARD;
36+
},
37+
maxRecordsToSync: 30000,
38+
fullSyncInterval: 60, // minutes
39+
});
40+
```
41+
42+
</Block>
43+
<Block name="JavaScript">
44+
945
```js
1046
import { DISCARD } from "@aws-amplify/datastore";
1147

@@ -15,7 +51,6 @@ DataStore.configure({
1551
},
1652
conflictHandler: async (data) => {
1753
// Example conflict handler
18-
1954
const modelConstructor = data.modelConstructor;
2055
if (modelConstructor === Post) {
2156
const remoteModel = data.remoteModel;
@@ -32,3 +67,6 @@ DataStore.configure({
3267
fullSyncInterval: 60, // minutes
3368
});
3469
```
70+
71+
</Block>
72+
</BlockSwitcher>
Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
<BlockSwitcher>
2+
<Block name="TypeScript">
3+
4+
```ts
5+
const toDelete = await DataStore.query(Post, '1234567');
6+
if (toDelete) {
7+
DataStore.delete(toDelete);
8+
}
9+
```
10+
11+
</Block>
12+
<Block name="JavaScript">
13+
114
```js
2-
const todelete = await DataStore.query(Post, '1234567');
3-
DataStore.delete(todelete);
15+
const toDelete = await DataStore.query(Post, '1234567');
16+
DataStore.delete(toDelete);
417
```
518

19+
</Block>
20+
</BlockSwitcher>
21+
22+
623
You can also pass predicate operators to delete multiple items. For example, the following will delete all draft posts:
724

825
```js
@@ -11,13 +28,30 @@ await DataStore.delete(Post, (post) => post.status.eq(PostStatus.INACTIVE));
1128

1229
Additionally, you can perform a conditional delete. For instance, only delete **if** a post is in draft status by passing in an instance of a model:
1330

31+
<BlockSwitcher>
32+
<Block name="TypeScript">
33+
34+
```ts
35+
const toDelete = await DataStore.query(Post, '123');
36+
if (toDelete) {
37+
DataStore.delete(toDelete, (post) => post.status.eq(PostStatus.INACTIVE));
38+
}
39+
```
40+
41+
</Block>
42+
<Block name="JavaScript">
43+
1444
```js
1545
const todelete = await DataStore.query(Post, '123');
1646
DataStore.delete(todelete, (post) => post.status.eq(PostStatus.INACTIVE));
1747
```
1848

49+
</Block>
50+
</BlockSwitcher>
51+
52+
1953
Also, to delete all items for a model you can use `Predicates.ALL`:
2054

2155
```js
2256
await DataStore.delete(Post, Predicates.ALL);
23-
```
57+
```

src/fragments/lib/datastore/js/data-access/observe-update-snippet.mdx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
```ts
1+
```js
22
// Example showing how to observe the model and keep state updated before
33
// performing a save. This uses the useEffect React hook, but you can
44
// substitute for a similar mechanism in your application lifecycle with
55
// other frameworks.
66

77
function App() {
8-
const [post, setPost] = useState<Post>();
8+
const [post, setPost] = useState();
99

1010
useEffect(() => {
1111
/**
@@ -22,31 +22,49 @@ function App() {
2222
};
2323
}, []);
2424

25+
/**
26+
* Create a new Post
27+
*/
28+
async function onCreate() {
29+
const post = await DataStore.save(
30+
new Post({
31+
title: `New title ${Date.now()}`,
32+
rating: Math.floor(Math.random() * (8 - 1) + 1),
33+
status: PostStatus.ACTIVE,
34+
})
35+
);
36+
setPost(post);
37+
}
38+
2539
return (
2640
<>
2741
<h1>{post?.title}</h1>
42+
<input type="button" value="NEW TODO" onClick={onCreate} />
2843
<input
44+
disabled={!post}
2945
type="text"
3046
value={post?.title ?? ""}
3147
onChange={({ target: { value } }) => {
3248
/**
3349
* Each keypress updates the post in local React state.
3450
*/
3551
setPost(
36-
Post.copyOf(post!, (draft) => {
52+
Post.copyOf(post, (draft) => {
3753
draft.title = value;
3854
})
3955
);
4056
}}
4157
/>
4258
<input
59+
disabled={!post}
4360
type="button"
4461
value="Save"
4562
onClick={async () => {
4663
/**
4764
* This post is already up-to-date because `observeQuery` updated it.
4865
*/
49-
await DataStore.save(post!);
66+
if (!post) return;
67+
await DataStore.save(post);
5068
console.log("Post saved");
5169
}}
5270
/>

src/fragments/lib/datastore/js/data-access/query-pagination-snippet.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ const posts = await DataStore.query(Post, Predicates.ALL, {
33
page: 0,
44
limit: 100
55
});
6-
```
6+
```

src/fragments/lib/datastore/js/data-access/query-predicate-multiple-snippet.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ const posts = await DataStore.query(Post, (c) => c.and(c => [
55
c.rating.gt(4),
66
c.status.eq(PostStatus.ACTIVE)
77
]));
8-
```
8+
```

src/fragments/lib/datastore/js/data-access/query-predicate-or-snippet.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ const posts = await DataStore.query(Post, (c) =>
66
c.rating.gt(4),
77
c.status.eq(PostStatus.ACTIVE)
88
]));
9-
```
9+
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
```js
2-
const posts = await DataStore.query(Post, c => c.rating.gt(4));
2+
const posts = await DataStore.query(Post, (c) => c.rating.gt(4));
33
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
```js
22
const posts = await DataStore.query(Post, Predicates.ALL, {
3-
sort: s => s.rating(SortDirection.ASCENDING).title(SortDirection.DESCENDING)
3+
sort: (s) => s.rating(SortDirection.ASCENDING).title(SortDirection.DESCENDING)
44
});
55
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
```js
22
const posts = await DataStore.query(Post, Predicates.ALL, {
3-
sort: s => s.rating(SortDirection.ASCENDING)
3+
sort: (s) => s.rating(SortDirection.ASCENDING)
44
});
55
```

0 commit comments

Comments
 (0)