Skip to content

Commit

Permalink
Add example of type::thing to turn array of objects into records (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhghomon authored Dec 23, 2024
1 parent 5877742 commit 1b923b9
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/content/doc-surrealql/functions/database/type.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,51 @@ The `type::thing` function converts a value into a record pointer definition.
```surql title="API DEFINITION"
type::thing(any, any) -> record
```

The following example shows this function, and its output, when used in a [`RETURN`](/docs/surrealql/statements/return) statement:

```surql
LET $tb = "person";
LET $id = "tobie";
RETURN type::thing($tb, $id);
```
<br />

An example of this function being used to turn an array of objects into records to be created or upserted:

```surql
FOR $data IN [
{
id: 9,
name: 'Billy'
},
{
id: 10,
name: 'Bobby'
}
] {
UPSERT type::thing('person', $data.id) CONTENT $data;
};
```

An example of the same except in which the `num` field is to be used as the record's ID. In this case, it can be mapped with the [`array::map()`](/docs/surrealql/functions/database/array#arraymap) function to rename `num` as `id` so that the following `CONTENT` clause does not create both a `num` and an `id` with the same value.

```surql
FOR $data IN [
{
name: 'Billy',
num: 9
},
{
name: 'Bobby',
num: 10
},
].map(|$o| {
id: $o.num,
name: $o.name
}) {
UPSERT type::thing('person', $data.id) CONTENT $data;
};
```

## `type::uuid`

Expand Down

0 comments on commit 1b923b9

Please sign in to comment.