Skip to content

Commit

Permalink
Add examples of RETURN VALUE (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhghomon authored Dec 12, 2024
1 parent 67bc6bd commit b676259
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/content/doc-surrealql/statements/create.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ CREATE person SET age = 46, username = "john-smith" RETURN BEFORE;
CREATE person SET age = 46, username = "john-smith" RETURN AFTER;
```

You can also return specific fields from a created record, as well as ad-hoc fields to modify the output as needed.
You can also return specific fields from a created record, the value of a single field using `VALUE`, as well as ad-hoc fields to modify the output as needed.

```surql
CREATE person
Expand All @@ -328,9 +328,15 @@ RETURN
age,
interests,
age + 1 AS age_next_year;
CREATE |person:5|
SET age = 20
RETURN VALUE age;
```

```surql title="Response"
-------- Query --------
[
{
age: 46,
Expand All @@ -341,6 +347,16 @@ RETURN
]
}
]
-------- Query --------
[
20,
20,
20,
20,
20
]
```

### Timeout
Expand Down
24 changes: 23 additions & 1 deletion src/content/doc-surrealql/statements/insert.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ INSERT INTO company {
} RETURN AFTER;
```

You can also return specific fields from a created record, as well as ad-hoc fields to modify the output as needed.
You can also return specific fields from a created record, the value of a single field using `VALUE`, as well as ad-hoc fields to modify the output as needed.

```surql
INSERT INTO person {
Expand All @@ -168,9 +168,24 @@ RETURN
age,
interests,
age + 1 AS age_next_year;
INSERT INTO planet [
{
name: 'Venus',
surface_temp: 462,
temp_55_km_up: 27
},
{
name: 'Earth',
surface_temp: 15,
temp_55_km_up: -55
}
] RETURN VALUE temp_55_km_up;
```

```surql title="Response"
-------- Query --------
[
{
age: 46,
Expand All @@ -181,6 +196,13 @@ RETURN
]
}
]
-------- Query --------
[
27,
-55
]
```

## Bulk insert
Expand Down
5 changes: 5 additions & 0 deletions src/content/doc-surrealql/statements/relate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ RELATE person:l19zjikkw1p1h9o6ixrg->wrote->article:8nkk6uj4yprt49z7y3zm
RELATE person:l19zjikkw1p1h9o6ixrg->wrote->article:8nkk6uj4yprt49z7y3zm
SET time.written = time::now()
RETURN time;
-- Return only the value of a specific field without the field name
RELATE person:l19zjikkw1p1h9o6ixrg->wrote->article:8nkk6uj4yprt49z7y3zm
SET time.written = time::now()
RETURN VALUE time;
```

### Using the `TIMEOUT` clause
Expand Down
5 changes: 4 additions & 1 deletion src/content/doc-surrealql/statements/update.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ UPDATE person:tobie PATCH [

## Alter the `RETURN` value

By default, the update statement returns the record value once the changes have been made. To change the return value of each record, specify a `RETURN` clause, specifying either `NONE`, `BEFORE`, `AFTER`, `DIFF`, or a comma-separated list of specific fields to return.
By default, the update statement returns the record value once the changes have been made. To change the return value of each record, use the `RETURN` clause, specifying `NONE`, `BEFORE`, `AFTER`, `DIFF`, a comma-separated list of specific fields or ad-hoc fields, or `VALUE` for a single field without its key name.

```surql
-- Don't return any result
Expand All @@ -454,6 +454,9 @@ UPDATE person SET skills += 'reading' RETURN BEFORE;
-- Return the record after changes were applied (the default)
UPDATE person SET skills += 'reading' RETURN AFTER;
-- Return the value of the 'skills' field without the field name
UPDATE person SET skills += 'reading' RETURN VALUE skills;
-- Return a specific field only from the updated records
UPDATE person:tobie SET skills = ['skiing', 'music'] RETURN name, interests;
```
Expand Down

0 comments on commit b676259

Please sign in to comment.