Skip to content

Commit

Permalink
array::fold() is useful for bulk insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhghomon committed Dec 11, 2024
1 parent f11239a commit b708669
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/content/doc-surrealql/functions/database/array.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,17 @@ As the output above shows, it is often nice to know which item of the array one
"I_don't_like_whitespace"
```

The `array::fold()` function can be used to generate an array of values that can then be passed on to statements like [`INSERT`](/docs/surrealql/statements/insert) for bulk insertion.

```surql
INSERT INTO person (
-- Create 1000 objects with a random ULID
(<array>0..1000).fold([], |$v, $_| {
$v.append( { id: rand::ulid() });
})
);
```

This function is also useful for aggregating the results of graph queries. The following shows a graph table called `to` that holds the distance from one city to another. The `array::fold()` function can then be used to pass an object along that tracks the first and last city, while accumulating the distance and number of trips along the way.

```surql
Expand Down

0 comments on commit b708669

Please sign in to comment.