The future type is only available up to SurrealDB 2.x. Since version 3.0.0-beta, it has been replaced by defined fields using the COMPUTED clause. Most examples in this page include an equivalent COMPUTED clause to aid in migrating to the new implementation.
Futures are values which are only computed when the data is selected and returned to the client. Futures can be stored inside records, to enable dynamic values which are always calculated when queried.
Simple futures
Any value or expression can be used inside a future. This value will be dynamically computed on every access to the record.
CREATEuser:one; SELECT * FROMONLYuser:one; -- Sleep for one second SLEEP1s; -- `accessed_at` is a different value now SELECT * FROMONLYuser:one;
This differs from a VALUE clause which is only calculated when it is modified (created or updated), but is not recalculated during a SELECT query which does not modify a record.
DEFINEFIELDupdatedONTABLEuserVALUEtime::now();
CREATEuser:one; SELECT * FROMONLYuser:one; -- Sleep for one second SLEEP1s; -- `updated` is still the same SELECT * FROMONLYuser:one;
Futures depending on statements
If the value of a future is the result of a statement, it must be wrapped in parentheses.
When defining a future on a field, be sure to avoid any statements that would cause infinite recursion. In the following example, the random_friend field is defined by a statement that uses a SELECT statement on all the fields of the same person table, one of which will also use the same future to compute its value.
You've now seen how to create dynamically computed properties on records, using either simple values, and values which depend on local and remote record fields. Take a look at the next chapter to get into SurrealDB's geospatial types.