Skip to content

Commit

Permalink
Add field name page, reorder (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhghomon authored Mar 5, 2024
1 parent efdf8ee commit 891e7f7
Showing 1 changed file with 100 additions and 6 deletions.
106 changes: 100 additions & 6 deletions versioned_docs/version-1.2.x/surrealql/datamodel/ids.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CREATE temperature:17493 SET time = time::now(), celsius = 37.5;
```

## Object-based Record IDs
Complex Record IDs support dynamic expressions, allowing parameters, and function expressions to be used as values within the IDs. This is useful in a timeseries context, or for ensuring locality between specific records in a table.
Complex record IDs support dynamic expressions, allowing parameters, and function expressions to be used as values within the IDs. This is useful in a timeseries context, or for ensuring locality between specific records in a table.

```surql
// Set a new parameter
Expand All @@ -55,7 +55,7 @@ CREATE temperature:{ location: 'London', date: $now } SET
```

## Array-based Record IDs
Similar to Object-based Record IDs, records in SurrealDB can store arrays of values, with no limit to the depth of the arrays. Arrays can store any value stored within them, and can store different value types within the same array.
Similar to object-based record IDs, records in SurrealDB can store arrays of values, with no limit to the depth of the arrays. Arrays can store any value stored within them, and can store different value types within the same array.

```surql
// Set a new parameter
Expand All @@ -69,14 +69,14 @@ CREATE temperature:['London', $now] SET
```

## Generating Record IDs
Record IDs can be generated with a number of built-in ID generation functions. These allow for Record IDs to be generated using cryptographically-secure randomly-generated identifiers (which are suitable for dispersion across a distributed datastore), sequentially incrementing `ULID` Record identifiers, and `UUID` version 7 Record idenfitifiers.
Record IDs can be generated with a number of built-in ID generation functions. These allow for record IDs to be generated using cryptographically-secure randomly-generated identifiers (which are suitable for dispersion across a distributed datastore), sequentially incrementing `ULID` Record identifiers, and `UUID` version 7 Record idenfitifiers.

```surql
// Generate a random Record ID
// Generate a random record ID
CREATE temperature:rand() SET time = time::now(), celsius = 37.5;
// Generate a ULID-based Record ID
// Generate a ULID-based record ID
CREATE temperature:ulid() SET time = time::now(), celsius = 37.5;
// Generate a UUIDv7-based Record ID
// Generate a UUIDv7-based record ID
CREATE temperature:uuid() SET time = time::now(), celsius = 37.5;
```

Expand All @@ -95,3 +95,97 @@ SELECT * FROM temperature:['London', '2022-08-29T08:03:39']..;
-- Select all temperature records with IDs between the specified range
SELECT * FROM temperature:['London', '2022-08-29T08:03:39']..['London', '2022-08-29T08:09:31'];
```

# Field names

## Valid field names

Similar to record IDs, field names can be constructed from ASCII characters, underscores, and numbers:

```surql
--Query
CREATE user SET my_name_1 = 'name';
--Output
[
{
"id": "user:sklds4e7lqkewtddgijt",
"my_name_1": "name"
}
]
```

To create a field name with complex characters, use backticks:

```surql
--Query
CREATE user SET `mi_nómine😊` = 'name';
--Output
[
{
"id": "user:tkwse1j5o0anqjxonvzx",
"mi_nómine😊": "name"
}
]
```

Inside an object, non-ASCII field names can simply be set by using a string:

```surql
--Query
SELECT * FROM {
"mi_nómine": "name"
};
--Output
[
{
"mi_nómine": "name"
}
]
```

## Automatically generated field names

A field created from an operation will have a field name that represents the operation(s) used to construct it.

```surql
--Query
SELECT
math::mean(temps),
[ math::min(temps), math::max(temps) ]
FROM { temps: [-5, 8, 9] };
--Output
[
{
"[math::min(temps), math::max(temps)]": [
-5,
9
],
"math::mean": 4
}
]
```

Using `AS` allows these automatically calculated field names to be replaced with custom names:

```surql
--Query
SELECT
math::mean(temps) AS mean_temps,
[ math::min(temps), math::max(temps) ] AS avg_temps
FROM { temps: [-5, 8, 9] };
--Output
[
{
"avg_temps": [
-5,
9
],
"mean_temps": 4
}
]
```

0 comments on commit 891e7f7

Please sign in to comment.