Skip to content

Commit 5815ec6

Browse files
update text bassed on feedback
1 parent 8535fb8 commit 5815ec6

File tree

3 files changed

+169
-127
lines changed

3 files changed

+169
-127
lines changed

tutorials/modules/ROOT/pages/pipelines-advanced.adoc

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
= Advanced pipelines
1+
= Advanced query pipelines
22
:keywords: typedb, typeql, tutorial, console
33
:pageTitle:
44
:summary:
@@ -16,6 +16,8 @@ This tutorial assumes you have access to a running TypeDB instance, as well as a
1616

1717
We recommend xref:{page-version}@home::install/ce.adoc[installing TypeDB Community Edition] as it is distributed bundled with Console for easy setup.
1818

19+
This is a continuation of xref:{page-version}@tutorials::pipelines-read.adoc[] and xref:{page-version}@tutorials::pipelines-crud.adoc[], though you don't need to have completed them to follow along.
20+
1921
== Example schema and data
2022

2123
IMPORTANT: TODO PERMALINKS TO `typedb/typedb-examples`
@@ -43,12 +45,12 @@ include::{page-version}@academy::attachment$bookstore-data.tql[lines=18..]
4345
----
4446
====
4547

46-
== Select order status conditionally using disjunctions
48+
== Set order status based on stock using `or`
4749

4850
Let's say we want to create an order for a user, but we want to check the book is in stock first.
4951
If it isn't, we need to mark the order as invalid.
5052

51-
We can do this by using a xref:{page-version}@typeql-reference::patterns/disjunctions.adoc[disjunction] to check if the book is in stock or not and setting `$status` accordingly.
53+
We can do this by using an `or` to check if the book is in stock or not and setting `$status` accordingly.
5254

5355
[,typeql]
5456
----
@@ -71,9 +73,9 @@ insert
7173
has quantity 1;
7274
----
7375

74-
== Collect sales statistics using ``reduce``-``groupby``
76+
== Group sales statistics by genre using `reduce` with `groupby`
7577

76-
We can use a xref:{page-version}@typeql-reference::pipelines/reduce.adoc[reduce] stage to collect sales statistics for each genre within a given year.
78+
We can use a reduce stage to collect sales statistics for each genre within a given year.
7779

7880
The `groupby` keyword allows us to sum the sale quantities grouped by the value of a specific variable, in this case `$genre`.
7981

@@ -82,11 +84,8 @@ The `groupby` keyword allows us to sum the sale quantities grouped by the value
8284
#!test[write, rollback, count = 8]
8385
match
8486
$item isa book, has genre $genre;
85-
$order-line isa order-line,
86-
links ($order, $item),
87-
has quantity $quantity;
88-
($order) isa action-execution,
89-
has timestamp $timestamp;
87+
order-line ($order, $item), has quantity $quantity;
88+
action-execution ($order), has timestamp $timestamp;
9089
$timestamp >= 2022-01-01T00:00;
9190
$timestamp < 2023-01-01T00:00;
9291
reduce
@@ -117,30 +116,26 @@ Note that when a book has multiple genres, the number of times it has been sold
117116
Finished. Total answers: 8
118117
----
119118

120-
== Update user data conditionally using optionals
119+
== Update user loyalty tier based on spending using `try`
121120

122121
Imagine we want to update a user's loyalty tier based on their total spending.
123-
When a user pays for an order, we want to update their total spending and, if their spending has crossed some threshold, we want to increase their loyalty tier as well.
122+
After a user has paid for an order, we want to update their total spending and, if their spending has crossed some threshold, we want to increase their loyalty tier as well.
123+
124+
We've covered in xref:{page-version}@tutorials::pipelines-read.adoc#_fetch_discounted_price_with_optionals[Reading data] how to use a `try` pattern to assign a variable based on whether the data
125+
exists in the database.
126+
This extends to all kinds of patterns, including comparisons.
127+
All patterns in a `try` block must succeed, otherwise the new variables defined in it will not be assigned.
124128

125-
We can use an xref:{page-version}@typeql-reference::patterns/optionals.adoc[optional] pattern to only assign the `$new-loyalty-tier` when necessary.
129+
We can use a `try` pattern to only assign the `$new-loyalty-tier` when the user's total spending has exceeded the threshold.
126130
Then, we can use the `try` keyword to conditionally set their loyalty tier in the `update` stage.
127131
The `try` block in the `update` stage will only be executed if the `$new-loyalty-tier` is assigned.
128132

129133
[,typeql]
130134
----
131135
#!test[write, rollback, count = 1]
132136
match
133-
$user isa user, has id "u0002";
134-
$order isa order, has id "o0004";
135-
($user, $order) isa action-execution;
136-
($order, $item) isa order-line, has quantity $quantity;
137-
$item has price $price;
138-
let $line-total = $price * $quantity;
139-
reduce
140-
$total = sum($line-total) groupby $order, $user;
141-
match
142-
$user has total-spending $spending;
143-
let $new-spending = $spending + $total;
137+
$user isa user, has id "u0002", has total-spending $spending;
138+
let $new-spending = $spending + 9.99;
144139
try {
145140
$user has loyalty-tier $tier;
146141
$new-spending > 100 * $tier * $tier;
@@ -168,9 +163,21 @@ TypeQL core concepts
168163
Learn more about chaining TypeQL clauses into pipelines
169164
****
170165

171-
.xref:{page-version}@typeql-reference::index.adoc[]
166+
.xref:{page-version}@typeql-reference::patterns/disjunctions.adoc[]
167+
[.clickable]
168+
****
169+
`or` documentation in TypeQL reference
170+
****
171+
172+
.xref:{page-version}@typeql-reference::pipelines/reduce.adoc
173+
[.clickable]
174+
****
175+
`reduce` documentation in TypeQL reference
176+
****
177+
178+
.xref:{page-version}@typeql-reference::patterns/optionals.adoc
172179
[.clickable]
173180
****
174-
TypeQL reference manual
181+
`try` documentation in TypeQL reference
175182
****
176183
--

0 commit comments

Comments
 (0)