From 1afad90dac563d1635944c09c3432a7fd38394ac Mon Sep 17 00:00:00 2001 From: Richard McClellan Date: Thu, 4 Feb 2021 14:55:53 -0600 Subject: [PATCH 1/6] Update java_relationship to account for cascading delete --- markdown/tests/android/java_relationship.md | 44 +++++++++---------- markdown/tests/android/kotlin_relationship.md | 42 +++++++++--------- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/markdown/tests/android/java_relationship.md b/markdown/tests/android/java_relationship.md index 04091bd..f21f072 100644 --- a/markdown/tests/android/java_relationship.md +++ b/markdown/tests/android/java_relationship.md @@ -50,12 +50,22 @@ In one-to-one relationships, if the target model instance is deleted, it will al ```java Amplify.DataStore.delete(author, - deleted -> Log.i("Amplify DataStore", "Author + Post deleted"), + deleted -> Log.i("Amplify DataStore", "Author deleted and relationship cleared on Post"), failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); ``` In this example, the `Post`'s "author" field will be cleared and the `Author` model instance will be deleted. +If the source model is deleted, the target model will be automatically deleted as well. + +```java +Amplify.DataStore.delete(post, + deleted -> Log.i("Amplify DataStore", "Author + Post deleted"), + failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); +``` + +In this example, the `Post` model instance will be deleted and the `Author` model instance will be deleted, locally, and on the backend. + :::NEW_COMMAND::: :::ONE_TO_MANY::: @@ -104,30 +114,20 @@ Amplify.DataStore.query(Article.class, Where.matches(Article.PUBLICATION_ID.eq(" **Delete** -In one-to-many relationships, delete the target model instance first and then delete the source model. +In one-to-many relationships, deleting the source model will automatically delete all target models that belong to it, both locally, and on the backend. ```java -Amplify.DataStore.query(Article.class, Where.matches(Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID")), - matches -> { - while (matches.hasNext()) { - Article article = matches.next(); - Amplify.DataStore.delete(article, - deletedArticle -> Log.i("Amplify DataStore", "Article deleted"), - failure -> {}); +Amplify.DataStore.query(Publication.class, Where.id("YOUR_PUBLICATION_ID"), + matchedPublications -> { + while(matchedPublications.hasNext()) { + Publication match = matchedPublications.next(); + Amplify.DataStore.delete(match, + deleted -> Log.i("Amplify DataStore", "Publication and all related Article instances deleted"), + failure -> Log.e("Amplify DataStore", "Deletion failed.", failure)); } - Amplify.DataStore.query(Publication.class, Where.id("YOUR_PUBLICATION_ID"), - matchedPublications -> { - while(matchedPublications.hasNext()) { - Publication match = matchedPublications.next(); - Amplify.DataStore.delete(match, - deleted -> Log.i("Amplify DataStore", "Publication deleted"), - failure -> Log.e("Amplify DataStore", "Deletion failed.", failure)); - } - }, - failure -> {}); - - }, failure -> {} -); + }, + failure -> {}); + ``` In this example, the `Publication` with "YOUR_PUBLICATION_ID" and all of its related `Article` model instances will be deleted. diff --git a/markdown/tests/android/kotlin_relationship.md b/markdown/tests/android/kotlin_relationship.md index 4783a95..f4a329e 100644 --- a/markdown/tests/android/kotlin_relationship.md +++ b/markdown/tests/android/kotlin_relationship.md @@ -48,12 +48,22 @@ In one-to-one relationships, if the target model instance is deleted, it will al ```kt Amplify.DataStore.delete(newAuthor, - { Log.i("Amplify DataStore", "Author + Post deleted") }, + { Log.i("Amplify DataStore", "Author deleted and relationship cleared on Post") }, { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) ``` In this example, the `Post`'s "author" field will be cleared and the `Author` model instance will be deleted. +If the source model is deleted, the target model will be automatically deleted as well. + +```kt +Amplify.DataStore.delete(post, + { Log.i("Amplify DataStore", "Author + Post deleted") }, + { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) +``` + +In this example, the `Post` model instance will be deleted and the `Author` model instance will be deleted, locally, and on the backend. + :::NEW_COMMAND::: :::ONE_TO_MANY::: @@ -102,29 +112,19 @@ Amplify.DataStore.query(Article::class.java, Where.matches(Article.PUBLICATION_I **Delete** -In one-to-many relationships, delete the target model instance first and then delete the source model. +In one-to-many relationships, deleting the source model will automatically delete all target models that belong to it, both locally, and on the backend. ```kt -Amplify.DataStore.query(Article::class.java, Where.matches(Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID")), - { matches -> - while (matches.hasNext()) { - val article = matches.next() - Amplify.DataStore.delete(article!!, - { deletedArticle -> Log.i("Amplify DataStore", "Article deleted") }, - { failure -> Log.e("Amplify DataStore", "Deletion failed.", failure)}) - } - Amplify.DataStore.query(Publication::class.java, Where.id("YOUR_PUBLICATION_ID"), - { matchedPublications -> - while (matchedPublications.hasNext()) { - val match: Publication = matchedPublications.next() - Amplify.DataStore.delete(match, - { deleted -> Log.i("Amplify DataStore", "Publication deleted") }, - { failure -> Log.e("Amplify DataStore", "Deletion failed.", failure) }) - } - } - ) { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } +Amplify.DataStore.query(Publication::class.java, Where.id("YOUR_PUBLICATION_ID"), + { matchedPublications -> + while (matchedPublications.hasNext()) { + val match: Publication = matchedPublications.next() + Amplify.DataStore.delete(match, + { deleted -> Log.i("Amplify DataStore", "Publication and all related Article instances deleted") }, + { failure -> Log.e("Amplify DataStore", "Deletion failed.", failure) }) + } } -) { failure -> Log.e("Amplify DataStore", "Query failed.", failure)} +) { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } ``` In this example, the `Publication` with "YOUR_PUBLICATION_ID" and all of its related `Article` model instances will be deleted. From b027568861691d69b01731e0623dc5427eb98e11 Mon Sep 17 00:00:00 2001 From: Richard McClellan Date: Thu, 4 Feb 2021 16:03:20 -0600 Subject: [PATCH 2/6] Update markdown/tests/android/java_relationship.md Co-authored-by: Raphael Kim <52714340+raphkim@users.noreply.github.com> --- markdown/tests/android/java_relationship.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/markdown/tests/android/java_relationship.md b/markdown/tests/android/java_relationship.md index f21f072..871fa4d 100644 --- a/markdown/tests/android/java_relationship.md +++ b/markdown/tests/android/java_relationship.md @@ -119,7 +119,7 @@ In one-to-many relationships, deleting the source model will automatically delet ```java Amplify.DataStore.query(Publication.class, Where.id("YOUR_PUBLICATION_ID"), matchedPublications -> { - while(matchedPublications.hasNext()) { + while (matchedPublications.hasNext()) { Publication match = matchedPublications.next(); Amplify.DataStore.delete(match, deleted -> Log.i("Amplify DataStore", "Publication and all related Article instances deleted"), @@ -213,4 +213,4 @@ Amplify.DataStore.delete( failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); ``` -The `toBeDeletedTag` `Tag` instance and all `PostTag` instances where _tag_ is linked to `toBeDeletedTag` will be deleted. \ No newline at end of file +The `toBeDeletedTag` `Tag` instance and all `PostTag` instances where _tag_ is linked to `toBeDeletedTag` will be deleted. From 0c5b2110f513cf05ed7e3e8bb8d15e9e7c96742b Mon Sep 17 00:00:00 2001 From: Richard McClellan Date: Thu, 4 Feb 2021 16:03:30 -0600 Subject: [PATCH 3/6] Update markdown/tests/android/kotlin_relationship.md Co-authored-by: Raphael Kim <52714340+raphkim@users.noreply.github.com> --- markdown/tests/android/kotlin_relationship.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/markdown/tests/android/kotlin_relationship.md b/markdown/tests/android/kotlin_relationship.md index f4a329e..29d914c 100644 --- a/markdown/tests/android/kotlin_relationship.md +++ b/markdown/tests/android/kotlin_relationship.md @@ -112,7 +112,7 @@ Amplify.DataStore.query(Article::class.java, Where.matches(Article.PUBLICATION_I **Delete** -In one-to-many relationships, deleting the source model will automatically delete all target models that belong to it, both locally, and on the backend. +In one-to-many relationships, deleting the source model will automatically delete all target models that belong to it, both locally and on the backend. ```kt Amplify.DataStore.query(Publication::class.java, Where.id("YOUR_PUBLICATION_ID"), @@ -209,4 +209,4 @@ Amplify.DataStore.delete( { deleted -> Log.i("Amplify DataStore", "Deleted $deleted") }, { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) ``` -The `toBeDeletedTag` `Tag` instance and all `PostTag` instances where _tag_ is linked to `toBeDeletedTag` will be deleted. \ No newline at end of file +The `toBeDeletedTag` `Tag` instance and all `PostTag` instances where _tag_ is linked to `toBeDeletedTag` will be deleted. From 769838c3ffc26872c8213a6f55104fd8c8061707 Mon Sep 17 00:00:00 2001 From: Richard McClellan Date: Thu, 4 Feb 2021 16:04:19 -0600 Subject: [PATCH 4/6] Update markdown/tests/android/kotlin_relationship.md Co-authored-by: Raphael Kim <52714340+raphkim@users.noreply.github.com> --- markdown/tests/android/kotlin_relationship.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/markdown/tests/android/kotlin_relationship.md b/markdown/tests/android/kotlin_relationship.md index 29d914c..bfdde10 100644 --- a/markdown/tests/android/kotlin_relationship.md +++ b/markdown/tests/android/kotlin_relationship.md @@ -59,7 +59,7 @@ If the source model is deleted, the target model will be automatically deleted a ```kt Amplify.DataStore.delete(post, { Log.i("Amplify DataStore", "Author + Post deleted") }, - { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) + { Log.e("Amplify DataStore", "Deletion failed", it) }) ``` In this example, the `Post` model instance will be deleted and the `Author` model instance will be deleted, locally, and on the backend. From ab4d21fa27fb0003784edee21e3cc4b599af8d48 Mon Sep 17 00:00:00 2001 From: Richard McClellan Date: Thu, 4 Feb 2021 17:37:00 -0600 Subject: [PATCH 5/6] Fix indentation --- markdown/tests/android/java_relationship.md | 158 ++++++++--------- markdown/tests/android/kotlin_relationship.md | 167 +++++++++--------- 2 files changed, 167 insertions(+), 158 deletions(-) diff --git a/markdown/tests/android/java_relationship.md b/markdown/tests/android/java_relationship.md index 871fa4d..61db1d0 100644 --- a/markdown/tests/android/java_relationship.md +++ b/markdown/tests/android/java_relationship.md @@ -9,21 +9,21 @@ In order to save a 1:1 relationship, create the target model instance first and ```java Author newAuthor = Author.builder() - .name("Rene Brandel") - .build(); + .name("Rene Brandel") + .build(); Post post = Post.builder() - .content("My first post!") - .author(newAuthor) - .build(); + .content("My first post!") + .author(newAuthor) + .build(); Amplify.DataStore.save(newAuthor, - savedAuthor -> { - Amplify.DataStore.save(post, - savedPost -> Log.i("Amplify DataStore", "Post saved."), - failure -> Log.e("Amplify DataStore", "Error while saving:", failure) - ); - }, - failure -> Log.e("Amplify DataStore", "Error while saving:", failure) + savedAuthor -> { + Amplify.DataStore.save(post, + savedPost -> Log.i("Amplify DataStore", "Post saved."), + failure -> Log.e("Amplify DataStore", "Error while saving:", failure) + ); + }, + failure -> Log.e("Amplify DataStore", "Error while saving:", failure) ); ``` Here we've first created a new `Author` instance and then saved it to the `Post`'s "author" relationship field. @@ -34,13 +34,13 @@ To query one-to-one relationships, access the target model instance through its ```java Amplify.DataStore.query(Post.class, - matches -> { - while (matches.hasNext()) { - Post post = matches.next(); - Author author = post.getAuthor(); - } - }, - failure -> Log.e("Amplify DataStore", "Query failed.", failure) + matches -> { + while (matches.hasNext()) { + Post post = matches.next(); + Author author = post.getAuthor(); + } + }, + failure -> Log.e("Amplify DataStore", "Query failed.", failure) ); ``` @@ -50,8 +50,8 @@ In one-to-one relationships, if the target model instance is deleted, it will al ```java Amplify.DataStore.delete(author, - deleted -> Log.i("Amplify DataStore", "Author deleted and relationship cleared on Post"), - failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); + deleted -> Log.i("Amplify DataStore", "Author deleted and relationship cleared on Post"), + failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); ``` In this example, the `Post`'s "author" field will be cleared and the `Author` model instance will be deleted. @@ -60,8 +60,8 @@ If the source model is deleted, the target model will be automatically deleted a ```java Amplify.DataStore.delete(post, - deleted -> Log.i("Amplify DataStore", "Author + Post deleted"), - failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); + deleted -> Log.i("Amplify DataStore", "Author + Post deleted"), + failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); ``` In this example, the `Post` model instance will be deleted and the `Author` model instance will be deleted, locally, and on the backend. @@ -77,22 +77,22 @@ In order to save a one-to-many relationship, create the source model instance fi ```java Publication publication = Publication.builder() - .title("Amplify Weekly") - .build(); + .title("Amplify Weekly") + .build(); Article article = Article.builder() - .publicationId(publication.getId()) - .title("Add auth to your app in 3 steps") - .build(); + .publicationId(publication.getId()) + .title("Add auth to your app in 3 steps") + .build(); Amplify.DataStore.save(publication, - savedPublication -> { - Amplify.DataStore.save(article, - savedArticle -> Log.i("Amplify DataStore", "Article saved." + savedArticle), - failure -> Log.e("Amplify DataStore", "Error while saving:", failure) - ); - }, - failure -> Log.e("Amplify DataStore", "Error while saving:", failure) + savedPublication -> { + Amplify.DataStore.save(article, + savedArticle -> Log.i("Amplify DataStore", "Article saved." + savedArticle), + failure -> Log.e("Amplify DataStore", "Error while saving:", failure) + ); + }, + failure -> Log.e("Amplify DataStore", "Error while saving:", failure) ); ``` Here we've first created a new `Publication` instance and then saved its _id_ to the `Article`'s "publicationID" relationship field. @@ -103,13 +103,13 @@ To query one-to-many relationships, filter based on the source model instance's ```java Amplify.DataStore.query(Article.class, Where.matches(Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID")), - matches -> { - while(matches.hasNext()) { - Article article = matches.next(); - Log.i("Amplify DataStore", "Matched article: " + article); - } - }, - failure -> Log.e("Amplify DataStore", "Query failed.", failure)); + matches -> { + while(matches.hasNext()) { + Article article = matches.next(); + Log.i("Amplify DataStore", "Matched article: " + article); + } + }, + failure -> Log.e("Amplify DataStore", "Query failed.", failure)); ``` **Delete** @@ -118,16 +118,16 @@ In one-to-many relationships, deleting the source model will automatically delet ```java Amplify.DataStore.query(Publication.class, Where.id("YOUR_PUBLICATION_ID"), - matchedPublications -> { - while (matchedPublications.hasNext()) { - Publication match = matchedPublications.next(); - Amplify.DataStore.delete(match, - deleted -> Log.i("Amplify DataStore", "Publication and all related Article instances deleted"), - failure -> Log.e("Amplify DataStore", "Deletion failed.", failure)); - } - }, - failure -> {}); - + matchedPublications -> { + while (matchedPublications.hasNext()) { + Publication match = matchedPublications.next(); + Amplify.DataStore.delete(match, + deleted -> Log.i("Amplify DataStore", "Publication and all related Article instances deleted"), + failure -> Log.e("Amplify DataStore", "Deletion failed.", failure)); + } + }, + failure -> {} +); ``` In this example, the `Publication` with "YOUR_PUBLICATION_ID" and all of its related `Article` model instances will be deleted. @@ -145,30 +145,30 @@ In order to save a many-to-many relationship, create both model instance first a ```java Post post = Post.builder() - .body("How to build deploy a web app on AWS Amplify") - .build(); + .body("How to build deploy a web app on AWS Amplify") + .build(); Tag tag = Tag.builder() - .label("static-web-hosting") - .build(); + .label("static-web-hosting") + .build(); PostTag postTag = PostTag.builder() - .post(post) - .tag(tag) - .build(); + .post(post) + .tag(tag) + .build(); Amplify.DataStore.save(post, savedPost -> { Log.i("Amplify DataStore", "post saved."); Amplify.DataStore.save(tag, - savedEditor -> { - Log.i("Amplify DataStore", "Tag saved."); - Amplify.DataStore.save(postTag, - saved -> Log.i("Amplify DataStore", "PostTag saved."), - failure -> Log.e("Amplify DataStore", "PostTag not saved.", failure) - ); - }, - failure -> Log.e("Amplify DataStore", "Tag not saved.", failure) + savedEditor -> { + Log.i("Amplify DataStore", "Tag saved."); + Amplify.DataStore.save(postTag, + saved -> Log.i("Amplify DataStore", "PostTag saved."), + failure -> Log.e("Amplify DataStore", "PostTag not saved.", failure) + ); + }, + failure -> Log.e("Amplify DataStore", "Tag not saved.", failure) ); }, failure -> Log.e("Amplify DataStore", "Post not saved.", failure) @@ -183,12 +183,12 @@ To query many-to-many relationships, filter the join model based on one of the m ```java Amplify.DataStore.query(ContentTag.class, Where.matches(ContentTag.CONTENT.eq("YOUR_CONTENT_ID")), - matches -> { - while (matches.hasNext()) { - ContentTag contentTag = matches.next(); - Log.i("Amplify DataStore", "Tag: " + contentTag.getTag()); - } - }, failure -> {}); + matches -> { + while (matches.hasNext()) { + ContentTag contentTag = matches.next(); + Log.i("Amplify DataStore", "Tag: " + contentTag.getTag()); + } + }, failure -> {}); ``` In this example, first filter the _join model_ `PostTag` with your `Post`'s _id, then map the `PostTag`s to `Tag`s. @@ -199,18 +199,18 @@ Deleting the _join model instance_ will not delete any source model instances. ```java Amplify.DataStore.delete( - toBeDeletedPostTag, - deleted -> Log.i("Amplify DataStore", "Deleted " + deleted), - failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); + toBeDeletedPostTag, + deleted -> Log.i("Amplify DataStore", "Deleted " + deleted), + failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); ``` Both the `Post` and the `Tag` instances will not be deleted. Only the join model instances containing the link between a `Post` and a `Tag`. Deleting a _source model instance_ will also delete the join model instances containing the source model instance. ```java Amplify.DataStore.delete( - toBeDeletedTag, - deleted -> Log.i("Amplify DataStore", "Deleted " + deleted), - failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); + toBeDeletedTag, + deleted -> Log.i("Amplify DataStore", "Deleted " + deleted), + failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); ``` The `toBeDeletedTag` `Tag` instance and all `PostTag` instances where _tag_ is linked to `toBeDeletedTag` will be deleted. diff --git a/markdown/tests/android/kotlin_relationship.md b/markdown/tests/android/kotlin_relationship.md index bfdde10..089400d 100644 --- a/markdown/tests/android/kotlin_relationship.md +++ b/markdown/tests/android/kotlin_relationship.md @@ -9,20 +9,21 @@ In order to save a 1:1 relationship, create the target model instance first and ```kt val newAuthor = Author.builder() - .name("Rene Brandel") - .build() + .name("Rene Brandel") + .build() val post = Post.builder() - .content("My first post!") - .author(newAuthor) - .build() + .content("My first post!") + .author(newAuthor) + .build() Amplify.DataStore.save(newAuthor, - { - Amplify.DataStore.save(post, - { Log.i("Amplify DataStore", "Post saved.") }, - { failure -> Log.e("Amplify DataStore", "Error while saving:", failure) }) - }, - { failure -> Log.e("Amplify DataStore", "Error while saving:", failure) } + { + Amplify.DataStore.save(post, + { Log.i("Amplify DataStore", "Post saved.") }, + { failure -> Log.e("Amplify DataStore", "Error while saving:", failure) } + ) + }, + { failure -> Log.e("Amplify DataStore", "Error while saving:", failure) } ) ``` Here we've first created a new `Author` instance and then saved it to the `Post`'s "author" relationship field. @@ -33,13 +34,14 @@ To query one-to-one relationships, access the target model instance through its ```kt Amplify.DataStore.query(Post::class.java, - { matches -> - while (matches.hasNext()) { - val post = matches.next() - val author = post.author - } + { matches -> + while (matches.hasNext()) { + val post = matches.next() + val author = post.author } -) { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } + }, + { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } +) ``` **Delete** @@ -48,8 +50,9 @@ In one-to-one relationships, if the target model instance is deleted, it will al ```kt Amplify.DataStore.delete(newAuthor, - { Log.i("Amplify DataStore", "Author deleted and relationship cleared on Post") }, - { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) + { Log.i("Amplify DataStore", "Author deleted and relationship cleared on Post") }, + { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) } +) ``` In this example, the `Post`'s "author" field will be cleared and the `Author` model instance will be deleted. @@ -58,8 +61,9 @@ If the source model is deleted, the target model will be automatically deleted a ```kt Amplify.DataStore.delete(post, - { Log.i("Amplify DataStore", "Author + Post deleted") }, - { Log.e("Amplify DataStore", "Deletion failed", it) }) + { Log.i("Amplify DataStore", "Author + Post deleted") }, + { Log.e("Amplify DataStore", "Deletion failed", it) } +) ``` In this example, the `Post` model instance will be deleted and the `Author` model instance will be deleted, locally, and on the backend. @@ -75,22 +79,22 @@ In order to save a one-to-many relationship, create the source model instance fi ```kt val publication: Publication = Publication.builder() - .title("Amplify Weekly") - .build() + .title("Amplify Weekly") + .build() val article: Article = Article.builder() - .publicationId(publication.getId()) - .title("Add auth to your app in 3 steps") - .build() + .publicationId(publication.getId()) + .title("Add auth to your app in 3 steps") + .build() Amplify.DataStore.save(publication, - { savedPublication -> - Amplify.DataStore.save(article, - { savedArticle -> Log.i("Amplify DataStore", "Article saved. $savedArticle") }, - { failure -> Log.e("Amplify DataStore", "Error while saving:", failure) } - ) - }, - { failure -> Log.e("Amplify DataStore", "Error while saving:", failure) } + { savedPublication -> + Amplify.DataStore.save(article, + { savedArticle -> Log.i("Amplify DataStore", "Article saved. $savedArticle") }, + { failure -> Log.e("Amplify DataStore", "Error while saving:", failure) } + ) + }, + { failure -> Log.e("Amplify DataStore", "Error while saving:", failure) } ) ``` Here we've first created a new `Publication` instance and then saved its _id_ to the `Article`'s "publicationID" relationship field. @@ -101,13 +105,14 @@ To query one-to-many relationships, filter based on the source model instance's ```kt Amplify.DataStore.query(Article::class.java, Where.matches(Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID")), - { matches -> - while (matches.hasNext()) { - val article: Article = matches.next() - Log.i("Amplify DataStore", "Matched article: $article") - } + { matches -> + while (matches.hasNext()) { + val article: Article = matches.next() + Log.i("Amplify DataStore", "Matched article: $article") } -) { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } + }, + { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } +) ``` **Delete** @@ -116,15 +121,17 @@ In one-to-many relationships, deleting the source model will automatically delet ```kt Amplify.DataStore.query(Publication::class.java, Where.id("YOUR_PUBLICATION_ID"), - { matchedPublications -> + { matchedPublications -> while (matchedPublications.hasNext()) { - val match: Publication = matchedPublications.next() - Amplify.DataStore.delete(match, - { deleted -> Log.i("Amplify DataStore", "Publication and all related Article instances deleted") }, - { failure -> Log.e("Amplify DataStore", "Deletion failed.", failure) }) - } + val match: Publication = matchedPublications.next() + Amplify.DataStore.delete(match, + { deleted -> Log.i("Amplify DataStore", "Publication and all related Article instances deleted") }, + { failure -> Log.e("Amplify DataStore", "Deletion failed.", failure) } + ) } -) { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } + } + { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } + ) ``` In this example, the `Publication` with "YOUR_PUBLICATION_ID" and all of its related `Article` model instances will be deleted. @@ -142,33 +149,33 @@ In order to save a many-to-many relationship, create both model instance first a ```kt val post: Post = Post.builder() - .body("How to build deploy a web app on AWS Amplify") - .build() + .body("How to build deploy a web app on AWS Amplify") + .build() val tag: Tag = Tag.builder() - .label("static-web-hosting") - .build() + .label("static-web-hosting") + .build() val postTag: PostTag = PostTag.builder() - .post(post) - .tag(tag) - .build() + .post(post) + .tag(tag) + .build() Amplify.DataStore.save(post, - { savedPost -> - Log.i("Amplify DataStore", "post saved.") - Amplify.DataStore.save(tag, - { savedEditor -> - Log.i("Amplify DataStore", "Tag saved.") - Amplify.DataStore.save(postTag, - { saved -> Log.i("Amplify DataStore", "PostTag saved.") }, - { failure -> Log.e("Amplify DataStore", "PostTag not saved.", failure) } - ) - }, - { failure -> Log.e("Amplify DataStore", "Tag not saved.", failure) } - ) - }, - { failure -> Log.e("Amplify DataStore", "Post not saved.", failure) } + { savedPost -> + Log.i("Amplify DataStore", "post saved.") + Amplify.DataStore.save(tag, + { savedEditor -> + Log.i("Amplify DataStore", "Tag saved.") + Amplify.DataStore.save(postTag, + { saved -> Log.i("Amplify DataStore", "PostTag saved.") }, + { failure -> Log.e("Amplify DataStore", "PostTag not saved.", failure) } + ) + }, + { failure -> Log.e("Amplify DataStore", "Tag not saved.", failure) } + ) + }, + { failure -> Log.e("Amplify DataStore", "Post not saved.", failure) } ) ``` @@ -180,12 +187,14 @@ To query many-to-many relationships, filter the join model based on one of the m ```kt Amplify.DataStore.query(ContentTag::class.java, Where.matches(ContentTag.CONTENT.eq("YOUR_CONTENT_ID")), - { matches -> - while (matches.hasNext()) { - val contentTag = matches.next() - Log.i("Amplify DataStore", "Tag: " + contentTag.tag) - } - }) { failure -> Log.e("Amplify DataStore", "Query failed.", failure)} + { matches -> + while (matches.hasNext()) { + val contentTag = matches.next() + Log.i("Amplify DataStore", "Tag: " + contentTag.tag) + } + }, + { failure -> Log.e("Amplify DataStore", "Query failed.", failure)} +) ``` In this example, first filter the _join model_ `PostTag` with your `Post`'s _id, then map the `PostTag`s to `Tag`s. @@ -196,17 +205,17 @@ Deleting the _join model instance_ will not delete any source model instances. ```kt Amplify.DataStore.delete( - toBeDeletedPostTag, - { deleted -> Log.i("Amplify DataStore", "Deleted $deleted") }, - { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) + toBeDeletedPostTag, + { deleted -> Log.i("Amplify DataStore", "Deleted $deleted") }, + { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) ``` Both the `Post` and the `Tag` instances will not be deleted. Only the join model instances containing the link between a `Post` and a `Tag`. Deleting a _source model instance_ will also delete the join model instances containing the source model instance. ```kt Amplify.DataStore.delete( - toBeDeletedTag, - { deleted -> Log.i("Amplify DataStore", "Deleted $deleted") }, - { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) + toBeDeletedTag, + { deleted -> Log.i("Amplify DataStore", "Deleted $deleted") }, + { failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) ``` The `toBeDeletedTag` `Tag` instance and all `PostTag` instances where _tag_ is linked to `toBeDeletedTag` will be deleted. From 98ed0f49c28bb421edc62e21bafbb774b13f53c2 Mon Sep 17 00:00:00 2001 From: Richard McClellan Date: Thu, 4 Feb 2021 18:00:51 -0600 Subject: [PATCH 6/6] Simply success log message for deleting target model --- markdown/tests/android/java_relationship.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/markdown/tests/android/java_relationship.md b/markdown/tests/android/java_relationship.md index 61db1d0..ac69d0b 100644 --- a/markdown/tests/android/java_relationship.md +++ b/markdown/tests/android/java_relationship.md @@ -50,7 +50,7 @@ In one-to-one relationships, if the target model instance is deleted, it will al ```java Amplify.DataStore.delete(author, - deleted -> Log.i("Amplify DataStore", "Author deleted and relationship cleared on Post"), + deleted -> Log.i("Amplify DataStore", "Author deleted."), failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); ```