All possibles types in a SurrealDB Schemafull table
SurrealDB is a powerfull database, which 1.0 has been released in 2023. It's a new gen database, which can be schemafull or schemaless. In this article, we will focus on the schemafull part of SurrealDB.
Even if the official SurrealDB documentation is quite complete about SurrealQL language querrying, it lacks a bit of information about the types available in a schemafull table. This article aims to fill this gap.
Reminder about creating a table and field
To create a table in SurrealDB, you need to have previously defined a database and namespace. Then you can create a table with the following command:
DEFINE TABLE product SCHEMAFULL;
Then to define a field in a table, you can use the following command:
DEFINE FIELD my_field ON TABLE product TYPE data_type;
Types available in SurrealDB schemafull table
basic types
Any
DEFINE FIELD my_field ON TABLE product TYPE any;
Booleans
DEFINE FIELD active ON TABLE product TYPE bool;
Numbers
Any number
DEFINE FIELD price ON TABLE product TYPE number;
Integer
DEFINE FIELD price ON TABLE product TYPE int;
Float
DEFINE FIELD price ON TABLE product TYPE float;
Decimal
DEFINE FIELD price ON TABLE product TYPE decimal;
Null
DEFINE FIELD price ON TABLE product TYPE null;
String
DEFINE FIELD name ON TABLE product TYPE string;
Bytes
DEFINE FIELD icon ON TABLE product TYPE bytes;
Uuid
DEFINE FIELD ext_id ON TABLE product TYPE uuid;
Times related types
Datetime
DEFINE FIELD created_at ON TABLE product TYPE datetime;
Duration
DEFINE FIELD duration ON TABLE product TYPE duration;
Point
DEFINE FIELD location ON TABLE product TYPE point;
Record
DEFINE FIELD created_by ON TABLE product TYPE record;
Composed types
Composed types are formed by combining basic types with composed_type<type>
.
Optional type
By default, all fields are required. If you want to make a field optional, you can use the option
type.
DEFINE FIELD description ON TABLE product TYPE option<string>;
Multiple types
If you want to allow multiple types for a field, you can use the either
type.
DEFINE FIELD additional_propertie ON TABLE product TYPE string|number;
Array
DEFINE FIELD tags ON TABLE product TYPE array<string>;
Set
DEFINE FIELD tags ON TABLE product TYPE set<string>;
Geometry
DEFINE FIELD location ON TABLE product TYPE geometry<line>;
In this case the only types allowed are the following defined in the SurrealDB documentation Theses types respects the GeoJSON format (defined in rfc7946).
SurrealDB full course
If you want to learn more about SurrealDB, you can subscribe to our newsletter to be informed about the course we are preparing, and get a discount when it will be released.