Skip to content

Commit

Permalink
Add bulk update/upsert description (#725)
Browse files Browse the repository at this point in the history
Co-authored-by: Obinna Ekwuno <[email protected]>
  • Loading branch information
Starnick4444 and Ekwuno authored Aug 28, 2024
1 parent 03e7b01 commit 8c1b503
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ UPDATE person:tobie PATCH [
]
```

## Bulk update

<Since v="v2.0.0" />

The `UPDATE` statement supports bulk update, which allows multiple records to be updated in a single query. The `@value` part of `CONTENT` and `MERGE` clause can be either a single object or an array of objects.

```surql
UPDATE person CONTENT [
{id: r"person:jaime", name: "Jaime", surname: "Morgan Hitchcock"},
{id: "tobie", name: "Tobie", surname: "Morgan Hitchcock"},
... 1000 more records
]
```

## 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.
Expand All @@ -179,4 +193,4 @@ When processing a large result set with many interconnected records, it is possi

```surql
UPDATE person:tobie SET important = true WHERE ->knows->person->(knows WHERE influencer = true) TIMEOUT 5s;
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ UPSERT person:100 SET name = 'Tobie', company = 'SurrealDB', skills = ['Rust', '
```
The `UPSERT` statement can be used to modify records in the database if they already exist. If the record does not exist, it will be created.

In the case where the record ID isn't specified, any exisiting records in the table will be UPSERTd. For example, the following query will UPSERT all records in the `person` table:
In the case where the record ID isn't specified, any exisiting records in the table will be upserted. For example, the following query will UPSERT all records in the `person` table:

```surql
-- UPSERT all records in a table
Expand Down Expand Up @@ -74,7 +74,7 @@ UPSERT person:tobie SET interests -= 'Java';
```
## Using WHERE clause

The `UPSERT` statement supports conditional matching of records using a `WHERE` clause. If the expression in the `WHERE` clause evaluates to true, then the respective record will be UPSERTd.
The `UPSERT` statement supports conditional matching of records using a `WHERE` clause. If the expression in the `WHERE` clause evaluates to true, then the respective record will be upserted.

```surql
-- UPSERT all records which match the condition
Expand All @@ -100,7 +100,7 @@ UPSERT person:tobie CONTENT {

## MERGE clause

Instead of specifying the full record data using the `SET` clause or the `CONTENT` keyword, it is also possible to merge-UPSERT only specific fields by using the `MERGE` keyword and specifying only the fields which are to be UPSERTd.
Instead of specifying the full record data using the `SET` clause or the `CONTENT` keyword, it is also possible to merge-UPSERT only specific fields by using the `MERGE` keyword and specifying only the fields which are to be upserted.

```surql
-- UPSERT certain fields on all records
Expand Down Expand Up @@ -131,6 +131,20 @@ UPSERT person:tobie PATCH [
]
```

## Bulk upsert

<Since v="v2.0.0" />

The `UPSERT` statement supports bulk upsert, which allows multiple records to be upserted in a single query. The `@value` part of `CONTENT` and `MERGE` clause can be either a single object or an array of objects.

```surql
UPSERT person CONTENT [
{id: r"person:jaime", name: "Jaime", surname: "Morgan Hitchcock"},
{id: "tobie", name: "Tobie", surname: "Morgan Hitchcock"},
... 1000 more records
]
```

## Alter the `RETURN` value

By default, the UPSERT 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.
Expand All @@ -148,12 +162,12 @@ UPSERT person SET interests += 'reading' RETURN BEFORE;
-- Return the record after changes were applied (the default)
UPSERT person SET interests += 'reading' RETURN AFTER;
-- Return a specific field only from the UPSERTd records
-- Return a specific field only from the upserted records
UPSERT person:tobie SET interests = ['skiing', 'music'] RETURN name, interests;
```

When processing a large result set with many interconnected records, it is possible to use the `TIMEOUT` keywords to specify a timeout duration for the statement. If the statement continues beyond this duration, then the transaction will fail, no records will be UPSERTd in the database, and the statement will return an error.
When processing a large result set with many interconnected records, it is possible to use the `TIMEOUT` keywords to specify a timeout duration for the statement. If the statement continues beyond this duration, then the transaction will fail, no records will be upserted in the database, and the statement will return an error.

```surql
UPSERT person:3 SET important = true WHERE ->knows->person->(knows WHERE influencer = true) TIMEOUT 5s;
```
```

0 comments on commit 8c1b503

Please sign in to comment.