Skip to content

Commit

Permalink
DOC-243 & DOC-217 : Add range and literals (#782)
Browse files Browse the repository at this point in the history
Co-authored-by: Obinna Ekwuno <[email protected]>
  • Loading branch information
Dhghomon and Ekwuno authored Aug 28, 2024
1 parent 573e703 commit 4875117
Show file tree
Hide file tree
Showing 20 changed files with 412 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,22 @@ CREATE person SET results = [
{ score: 69, date: "2018-09-17T08:00:00Z", name: "Advanced Computer Science 101" },
{ score: 73, date: "2019-04-20T08:00:00Z", name: "Distributed Databases" },
];
```

A maximum number of items can be specified for an array.

```surql
DEFINE FIELD employees ON TABLE team TYPE array<record<employee>, 5>;
CREATE team SET employees = [
employee:one,
employee:two,
employee:three,
employee:four,
employee:five,
employee:doesnt_belong
];
```

```bash title="Response"
'Expected a array<record<employee>, 5> but the array had 6 items'
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 4
sidebar_position: 5
sidebar_label: Closures
title: Closures | SurrealQL
description: Anonymous functions in SurrealDB allow you to define small, reusable pieces of logic that can be used throughout your queries.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 14
sidebar_position: 6
sidebar_label: Datetimes
title: Datetimes | SurrealQL
description: SurrealDB has native support for datetimes with nanosecond precision. SurrealDB is able to parse datetimes from strings.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 5
sidebar_position: 7
sidebar_label: Formatters
title: Formatters | SurrealQL
description: Formatting functions in SurrealQL accept certain text formats for date/time formatting.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 6
sidebar_position: 8
sidebar_label: Futures
title: Futures | SurrealQL
description: Futures are values which are only computed when the data is selected and returned to the client.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 7
sidebar_position: 9
sidebar_label: Geometries
title: Geometries | SurrealQL
description: SurrealDB makes working with GeoJSON easy, with support for Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and Collection values.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 8
sidebar_position: 15
sidebar_label: Record IDs
title: Record IDs | SurrealQL
description: In SurrealDB, document record IDs store both the table name, and the record ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,6 @@ SurrealQL allows you to describe data with specific data types. These data types
</ul>
</td>
</tr>
<tr>
<td scope="row" data-label="Type">
<code>set</code>
</td>
<td scope="row" data-label="Description">
A set of items.
The set type also allows you to define which types can be stored in the set and the max length.
Items are automatically deduplicated.
<ul>
<li><code>set</code></li>
<li><code>set&lt;string&gt;</code></li>
<li><code>set&lt;string, 10&gt;</code></li>
</ul>
</td>
</tr>
<tr>
<td scope="row" data-label="Type">
<code>bool</code>
Expand Down Expand Up @@ -107,6 +92,24 @@ SurrealQL allows you to describe data with specific data types. These data types
Store a value in a 64 bit float.
</td>
</tr>
<tr>
<td scope="row" data-label="Type">
<a href="#geometry"><code>geometry</code></a>
</td>
<td scope="row" data-label="Description">
<a href="https://www.rfc-editor.org/rfc/rfc7946" target="_blank" title="Link to RFC 7946">RFC 7946</a> compliant data type for storing geometry in the <a href="https://geojson.org/" target="_blank" title="Link to the GeoJson website">GeoJson format</a>.
<ul>
<li><code>geometry&lt;feature&gt;</code></li>
<li><code>geometry&lt;point&gt;</code></li>
<li><code>geometry&lt;LineString&gt;</code></li>
<li><code>geometry&lt;polygon&gt;</code></li>
<li><code>geometry&lt;multipoint&gt;</code></li>
<li><code>geometry&lt;multiline&gt;</code></li>
<li><code>geometry&lt;multipolygon&gt;</code></li>
<li><code>geometry&lt;collection&gt;</code></li>
</ul>
</td>
</tr>
<tr>
<td scope="row" data-label="Type">
<code>int</code>
Expand All @@ -133,6 +136,19 @@ SurrealQL allows you to describe data with specific data types. These data types
Store formatted objects containing values of any supported type with no limit to object depth or nesting.
</td>
</tr>
<tr>
<td scope="row" data-label="Type">
<a href="/docs/surrealdb/surrealql/datamodel/literals"><code>literal</code></a>
</td>
<td scope="row" data-label="Description">
A value that may have multiple representations or formats, similar to an enum or a union type. Can be composed of strings, numbers, objects, arrays, or durations.
<ul>
<li><code>"a" | "b"</code></li>
<li><code>[number, "abc"]</code></li>
<li><code>123 | 456 | string | 1y1m1d</code></li>
</ul>
</td>
</tr>
<tr>
<td scope="row" data-label="Type">
<code>option</code>
Expand All @@ -146,10 +162,16 @@ SurrealQL allows you to describe data with specific data types. These data types
</tr>
<tr>
<td scope="row" data-label="Type">
<code>string</code>
<code>range</code>
</td>
<td scope="row" data-label="Description">
Describes a text-like value.
A range of possible values. Lower and upper bounds can be set, in the absence of which the range becomes open-ended. A range of integers can be used in a FOR loop.
<ul>
<li><code>0..10</code> </li>
<li><code>0..=10</code> </li>
<li><code>..10</code> </li>
<li><code>'a'..'z'</code> </li>
</ul>
</td>
</tr>
<tr>
Expand All @@ -167,22 +189,27 @@ SurrealQL allows you to describe data with specific data types. These data types
</tr>
<tr>
<td scope="row" data-label="Type">
<a href="#geometry"><code>geometry</code></a>
<code>set</code>
</td>
<td scope="row" data-label="Description">
<a href="https://www.rfc-editor.org/rfc/rfc7946" target="_blank" title="Link to RFC 7946">RFC 7946</a> compliant data type for storing geometry in the <a href="https://geojson.org/" target="_blank" title="Link to the GeoJson website">GeoJson format</a>.
A set of items.
The set type also allows you to define which types can be stored in the set and the max length.
Items are automatically deduplicated.
<ul>
<li><code>geometry&lt;feature&gt;</code></li>
<li><code>geometry&lt;point&gt;</code></li>
<li><code>geometry&lt;LineString&gt;</code></li>
<li><code>geometry&lt;polygon&gt;</code></li>
<li><code>geometry&lt;multipoint&gt;</code></li>
<li><code>geometry&lt;multiline&gt;</code></li>
<li><code>geometry&lt;multipolygon&gt;</code></li>
<li><code>geometry&lt;collection&gt;</code></li>
<li><code>set</code></li>
<li><code>set&lt;string&gt;</code></li>
<li><code>set&lt;string, 10&gt;</code></li>
</ul>
</td>
</tr>
<tr>
<td scope="row" data-label="Type">
<code>string</code>
</td>
<td scope="row" data-label="Description">
Describes a text-like value.
</td>
</tr>
</tbody>
</table>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
---
sidebar_position: 10
sidebar_label: Literals
title: Literals | SurrealQL
description: A value that may have multiple representations or formats.

---

import Since from '@site/src/components/Since'

# Literals

<Since v="v2.0.0" />

A literal is a value that may have multiple representations or formats, similar to an enum or a union type. A literal can be composed of strings, numbers, objects, arrays, or durations.

## Examples

A literal can be as simple as a declaration that a parameter must be a certain value.

```surql
LET $nine: 9 = 9;
LET $nine: 9 = 10;
```

```bash title="Response"
-------- Query --------

NONE

-------- Query --------

'Found 10 for param $nine, but expected a 9'
```

Using `|` allows a literal to be a number of possible options.

```surql
LET $nine: 9 | "9" | "nine" = "Nein";
```

```bash title="Response"
"Found 'Nein' for param $nine, but expected a 9 | '9' | 'nine'"
```

A literal can contain possible types in addition to possible values.

```surql
LET $flexible_param: datetime | uuid | "N/A" = "N/A";
LET $flexible_param: datetime | uuid | "N/A" = <datetime>"2024-09-01";
```

Literals that include the option to be an array or an object can contain rich data.

```surql
LET $status: "Ok" | { err: string } = { err: "Forgot to plug it in" };
```

## Literals in database schema

Literals can be defined inside a database schema by using a [DEFINE FIELD](/docs/surrealdb/surrealql/statements/define/field) statement.

```surql
DEFINE FIELD error_info ON TABLE info TYPE
{ error: "Continue" } |
{ error: "RetryWithId", id: string } |
{ error: "Deprecated", message: string };
CREATE info SET
error_info = { error: "Deprecated", message: "You shouldn't use this anymore" };
-- Doesn't conform to definition, will not work
CREATE info SET
error_info = "You shouldn't use this anymore";
```

```bash title="Response"
-------- Query --------

[
{
error_info: {
error: 'Deprecated',
message: "You shouldn't use this anymore"
},
id: info:pkckjrri8q1pg12unyuo
}
]

-------- Query --------

"Found \"You shouldn't use this anymore\" for field `error_info`, with record `info:dq375w4lv02aj2dj7122`, but expected a { error: 'Continue' } | { error: 'RetryWithId', id: string } | { error: 'Deprecated', message: string }"
```

## Matching on literals

While SurrealQL does not have a `match` or `switch` operator, `IF LET` statements can be used to match on a literal, particularly if each possible type is an object. The following shows a similar example to the above except that each object begins with a field containing the name of the type of error.

```surql
DEFINE FIELD error_info ON TABLE info TYPE
{ Continue: { message: "" }} |
{ Retry: { error: "Retrying", after: duration }} |
{ Deprecated: { message: string }};
```

Next, we will [define a function](/docs/surrealdb/surrealql/statements/define/function) to handle this field and return a certain type of message depending on the error. Note the following:

* The `LET` statement in the first line is simply to shorten the path to the information contained inside `error_info`
* `IF LET` statement works here because [IF](/docs/surrealdb/surrealql/statements/throw) involves a check for [truthiness](/docs/surrealdb/surrealql/datamodel/values#values-and-truthiness), returning `true` as long as it finds a value that is not none, empty, or zero.

```surql
DEFINE FUNCTION fn::handle_error($data: record<info>) -> string {
LET $err = $data.error_info;
RETURN IF $err.Continue {
"Continue"
}
ELSE IF $err.Retry {
sleep($err.Retry.after);
"Now retrying again"
}
ELSE IF $err.Deprecated {
$err.Deprecated.message
}
};
```

With the function set up, the `info` records can be inserted and run one at a time through the function.

```surql
INSERT INTO info [
{ error_info: { Continue: { message: "" } }},
{ error_info: { Retry: { error: "Retrying", after: 1s } }},
{ error_info: { Deprecated: { message: "Thought I said you shouldn't use this anymore" } }}
];
LET $info = SELECT * FROM info;
fn::handle_error($info[0].id);
fn::handle_error($info[1].id);
fn::handle_error($info[2].id);
```

```bash title="Output"
-------- Query --------

'Continue'

-------- Query --------

-- After waiting 1 second
'Now retrying again'

-------- Query --------

"Thought I said you shouldn't use this anymore"
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 9
sidebar_position: 11
sidebar_label: None and Null
title: None and Null | SurrealQL
description: SurrealDB uses two types called None and Null to represent two different ways in which data may not exist.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 10
sidebar_position: 12
sidebar_label: Numbers
title: Numbers | SurrealQL
description: In SurrealDB, numbers can be one of three types - 64-bit integers, 64-bit floating point numbers, or 128-bit decimal numbers.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 11
sidebar_position: 13
sidebar_label: Objects
title: Objects | SurrealQL
description: SurrealDB records can store objects, with no limit to the depth of any nested objects or values within.
Expand Down
Loading

0 comments on commit 4875117

Please sign in to comment.