Skip to content

Commit

Permalink
Add page on value (#694)
Browse files Browse the repository at this point in the history
Co-authored-by: ekwuno <[email protected]>
  • Loading branch information
Dhghomon and Ekwuno authored Jul 29, 2024
1 parent 8de5f1d commit a5b88f2
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
sidebar_position: 16
sidebar_label: Values
title: Values | SurrealQL
description: Every type in SurrealDB is a value

---

# Values

Each of the types mentioned in the data model is a subset of an all-encompassing type called a value.

## Comparing and ordering values

While it is unsurprising that a data type can be compared with itself, it may be surprising that different types can also be compared with each other.

```surql
RETURN 9 > 1; // Returns true
RETURN [] > time::now(); // Also returns true
```

This comparison is possible because every type in SurrealDB is a subset of value, and a comparison of any type with another is also simply a comparison of a value with another value. The order of values from least to greatest is:

* `none`
* `null`
* `bool`
* `number`
* `string`
* `duration`
* `datetime`
* `UUID`
* `array`
* `object`
* `geometry`
* `bytes`
* `record`

As a result, all of the following return `true`.

```surql
RETURN [
null > none,
true > null,
1 > true,
'a' > 999999999,
1s > 'a',
time::now() > 1s,
rand::uuid() > time::now(),
[] > rand::uuid(),
{} > [],
(89.0, 89.0) > {},
<bytes>'Aeon' > (89.0, 89.0),
person:aeon > <bytes>'Aeon'
];
```

Being able to compare a value with any other value is what makes SurrealDB's record range syntax possible. As the least possible value is NONE, a query for the records below that begins with NONE and ends with the `..` open range syntax will return everything.

```surql
CREATE time_data:['2024-07-23T06:36:56.004Z'];
CREATE time_data:['2024-07-24T06:36:56.014Z'];
CREATE time_data:['2024-07-25T06:36:56.014Z'];
SELECT * FROM time_data:[<datetime>'2024-07-24']..;
SELECT * FROM time_data:[NONE]..;
```

Inside a schema, the keyword `any` is used to denote any possible value.

```surql
DEFINE FIELD anything ON TABLE person TYPE any;
```

## Values and truthiness

Any value is considered to be truthy if it is not NONE, NULL, or a default value for the data type. A data type at its default value is one that is empty, such as an empty string or array or object, or a number set to 0.

The following example shows the result of the `array::all()` method, which checks to see if all of the items inside an array are truthy or not.

```surql
RETURN array::all(["", 1, 2, 3]); // false because of ""
RETURN array::all([{}, 1, 2, 3]); // false because of {}
RETURN array::all(["SurrealDB", { is_nice_database: true }, 1, 2, 3]); // true
```

As [the ! operator](../operators/#not) reverses the truthiness of a value, a doubling of this operator can also be used to check for truthiness.

```surql
RETURN [
!!"Has a value", !!"", // true, false
!!true, !!false, // true, false
!!{ is_nice_database: true }, !!{} // true, false
];
```
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ A variety of operators in SurrealQL allow for complex manipulation of data, and
</tr>
<tr>
<td scope="row" data-label="Operator"><a href="#not"><code>!</code></a></td>
<td scope="row" data-label="Description">Reverses the truthiness of a boolean value</td>
<td scope="row" data-label="Description">Reverses the truthiness of a value</td>
</tr>
<tr>
<td scope="row" data-label="Operator"><a href="#not_not"><code>!!</code></a></td>
<td scope="row" data-label="Description">Determines the truthiness of a value</td>
</tr>
<tr>
<td scope="row" data-label="Operator"><a href="#nco"><code>??</code></a></td>
Expand Down Expand Up @@ -203,16 +207,30 @@ SELECT * FROM 0 OR false OR 10;

## `!` {#not}

Reverses the truthiness of a boolean value.
Reverses the truthiness of a value.

```surql
SELECT * FROM !(TRUE OR FALSE)
FALSE
false
SELECT * FROM !"Has a value";
false
```

<br />

## `!!` {#not_not}

Determines the truthiness of a value (simply an application of the `!` operator twice).

```surql
SELECT * FROM !!"Has a value";
true
```

## `??` {#nco}

Check whether either of two values are truthy and not `NULL`.
Expand Down

0 comments on commit a5b88f2

Please sign in to comment.