Expressions and statements
A query is a series of zero or more statements and expressions separated by newlines or semicolons. Multiple statements and expressions in a block must end with an expression.
Statements and expressions can span multiple lines, and there is no line continuation character.
at
Get the result of an expression at a given time.
Description
The at
expression gets the result of expression2 at the expression1
time snapshot of the data. The expression1 operand is a
Time or
TransactionTime.
The at
expression returns null
if the requested document doesn’t exist.
This changes the time when
byId()
is
evaluated. Fauna stores document history up to the limit defined by
history_days
.
The default number of days stored is zero so history_days
must be increased
for at
to be meaningful. See
Temporality for more information on
document retention.
Examples
The following example gets the current document and a snapshot of the document from yesterday.
-
After running this query yesterday to create a document,
{ id: "9780547928227", coll: Product, ts: Time("2099-04-10T16:22:32.420Z"), name: "lemon", state: "yesterday state" }
-
And running this query today to update the document,
{ id: "9780547928227", coll: Product, ts: Time("2099-04-10T16:23:03.520Z"), name: "lemon", state: "today state" }
-
The following query returns the current data,
"today state"
-
And the following query returns the data from yesterday,
"yesterday state"
The following examples show that when comparing documents using the ==
operator, only the document id
field is compared, ignoring all other fields.
Compare the state
fields of different versions of the same document,
which differ because of the state change between yesterday and today:
false
Compare versions of the full document. The documents are identical
because the document id
fields are the same:
true
if … else
Use conditional branching for execution control flow.
Description
The if
and if … else
expressions conditionally execute a block
depending on the Boolean value of expression1. If
expression1 evaluates to true
, expression2 executes.
Otherwise, expression3 executes.
The last expression in the if
or else
block that satisfies the
condition is the value returned for the block.
The result of the if
expression is a value that can be assigned to a
variable. If the expression evaluates to false
and else
isn’t included, the expression returns null
.
let
Assign a value to a variable.
Description
A let
statement declares a variable with a name identifier and assigns a
value to the variable. Optionally, you can type the variable as any of the
supported types.
The name can be any valid identifier but can’t be a single underscore (_) character.
The variable is scoped to the block in which it is declared. A variable can be referenced in a nested block, but a variable declared in a nested block can’t be referenced outside of the block.
Assigning a value to a variable can be done only using the let
statement.
You can’t assign a value to name directly:
let x = 100 // valid assignment
x = 1000 // invalid assignment
A variable of the same name can be declared again in the same block and assigned a new value. It is a new variable declaration, and the previously declared variable can no longer be referenced:
let x = 100 // (A) valid assignment
let x = 1000 // (B) valid assignment and (A) can no longer be referenced
x // returns 1000
Examples
[
5,
"hello",
[
5,
"hello"
]
]
Variable scoping example where the declared x
variable has global scope,
while the scope of variables a
and b
is limited to the block:
3
Assign a new value to the variable:
[
"bar",
"foo"
]
Use a previous definition of the variable when redefining the variable:
2
Type a variable:
2
Incorrectly typing a variable results in an error:
invalid_query
error: Type `"2"` isn't a subtype of `Int`
at *query*:1:5
|
1 | let x: Int = "2"
| ^
|
cause: Type `String` isn't a subtype of `Int`
|
1 | let x: Int = "2"
| ^^^
|
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!