11 / 26Part 2: Add a related entityBeginner
Query all reviews for a book
Your app can now attach reviews to books, but it has no way to read them back yet. You'll add a query for that.
Define the query
Add the following Rell code to src/main/queries.rell:
query get_all_reviews_for_book(isbn: text) { val reviews = book_review @* { .book.isbn == isbn } ( .reviewer_name, .review, .rating ); return reviews;}Breakdown of the query
- Input parameter: You pass an
isbnto choose the book whose reviews are returned. - Join operation: The expression
val reviews = book_review @* { .book.isbn == isbn };selects everybook_reviewentity whosebook.isbnequals theisbnyou supplied. - Attributes: The following part controls which attributes appear in the result:
rell ( .reviewer_name, .review, .rating);
Given a book, the query returns each of its reviews with the reviewer's name, the review text, and the rating.
Test it on a local Chromia node
You test against a local node with the Chromia CLI:
-
Update or start the local Chromia node:
-
If your node is running, apply the changes with:
shell chr node update -
If your node is not running, launch it with:
shell chr node start
-
-
Execute the query: Open another terminal window or tab and fetch the reviews for a book with:
shell chr query get_all_reviews_for_book "isbn=ISBN1234"You should see output like this:
json [ { "rating": 5, "review": "It was a great book", "reviewer_name": "Alice" }]
Later lessons cover more of Rell, including validation, shaping query results, and running transactions that require signatures.