The Python SDK translates all datatypes native to SurrealQL into either datatypes native to Python, or a custom implementation. This document describes all datatypes, and links to their respective documentation.
Datatype | Kind | Documentation | |
---|---|---|---|
String | Native | str | |
Number | Translated to int , float or complex | int, float or complex | |
Float | Native | float | |
Decimal | via decimal.Decimal | Decimal | |
Bool | Native | bool | |
None | Native | None | |
Array | Translated to list | list | |
Object | Translated to dict | dict | |
Datetime | Via datetime.datetime | datetime | |
Binary | Native | bytes | |
UUID | via uuid.UUID | UUID | |
Duration | Custom | Duration | |
Geometry | Custom | Geometry | |
Table | Custom | Table | |
RecordID | Custom | RecordID |
Duration
When you receive a duration from SurrealDB, it will always be represented as a Duration
class.
Signaturefrom surrealdb import Duration Duration(duration: str | int | list[int, int])
Duration
from surrealdb import Duration # Parsed from a duration string Duration("1w2d") # Input milliseconds Duration(1000) # From a compact format ([ms, ns]) Duration([10000, 300]) # By a unit amount Duration.nanoseconds(1000) Duration.microseconds(1000) Duration.milliseconds(1000) Duration.seconds(1000) Duration.minutes(1000) Duration.hours(1000) Duration.days(1000) Duration.weeks(1000)
Duration
from surrealdb import Duration dur = Duration("7d") # Format as string, always as small as possible dur.to_string() # 1w # Get inner milliseconds dur._milliseconds # 604800000 # Get compact format dur.to_compact() # [604800] # How many of a full unit fit into the duration dur.nanoseconds # 604800000000000 dur.microseconds # 604800000000 dur.milliseconds # 604800000 dur.seconds # 604800 dur.minutes # 10080 dur.hours # 168 dur.days # 7 dur.weeks # 1
Geometry
When a Geometry is sent back from SurrealDB, be it a Point
, LineString
, Polygon
, MultiPoint
, MultiLineString
, MultiPolygon
or Collection
, it will be represented as a derivative of the Geometry
class.
Below, are all the methods implemented across all geometry derivatives.
.to_json()
Used to convert a geometry to a GeoJSON representation
Signaturefrom surrealdb import Geometry Geometry.to_json()
Examplefrom surrealdb import GeometryLine, GeometryPoint import json line = GeometryLine([ GeometryPoint([1, 2]), GeometryPoint([3, 4]), ]) line.to_json() # { type: "LineString", coordinates: [ [1, 2], [3, 4] ] } json.dumps(line) # '{ type: "LineString", coordinates: [ [1, 2], [3, 4] ] }'
.is()
Used to convert a check if geometry X is exactly equal to geometry Y
Signaturefrom surrealdb import Geometry Geometry.is(geometry: Geometry)
Examplefrom surrealdb import GeometryLine, GeometryPoint point1 = GeometryPoint([1, 2]) point2 = GeometryPoint([3, 4]) line = GeometryLine([point1, point2]) point1.is(point1) # true point1.is(point2) # false point1.is(line) # false # Checks the inner values, does not need to be the same instance duplicate = GeometryPoint([1, 2]) point1.is(duplicate) # true
.clone()
Used to deeply clone a geometry. Creates a replica of the original instance, but changing the instance won’t affect the other.
Signaturefrom surrealdb import Geometry Geometry.clone()
.coordinates
A getter property, representing the coordinates as shown in GeoJSON format for X Geometry
Signaturefrom surrealdb import Geometry Geometry.coordinates
GeometryPoint
A point in space, made up of a long and lat coordinate, automatically converted to a float.
Signaturefrom surrealdb import GeometryPoint GeometryPoint([long: int | Decimal, lat: int | Decimal])
GeometryLine
A line, made up of two or more points
Signaturefrom surrealdb import GeometryLine, GeometryPoint GeometryLine([GeometryPoint, GeometryPoint, ...])
GeometryPolygon
A polygon, made up of self-closing lines
Note: The lines inside the polygon will automatically be closed if not already, meaning that the last point will be the same as the first.
Signaturefrom surrealdb import GeometryPolygon, GeometryLine GeometryPolygon([GeometryLine, ...])
GeometryMultiPoint
A collection of one or more points
Signaturefrom surrealdb import GeometryMultiPoint, GeometryPoint GeometryMultiPoint([GeometryPoint, ...])
GeometryMultiLine
A collection of one or more lines
Signaturefrom surrealdb import GeometryMultiLine, GeometryLine GeometryMultiLine([GeometryLine, ...])
GeometryMultiPolygon
A collection of one or more polygons
Signaturefrom surrealdb import GeometryMultiPolygon, GeometryPolygon GeometryMultiPolygon([GeometryPolygon, ...])
GeometryCollection
A collection of one or more Geometry
derivatives
Signaturefrom surrealdb import GeometryCollection, Geometry GeometryCollection([Geometry, ...])
Table
When you get a table name sent back from SurrealDB, it will be represented as a Table
class.
Signaturefrom surrealdb import Table Table(table: str)
from surrealdb import Table table = Table("table") str(table) # "table"
A Table
will be represented as a string in JSON
from surrealdb import Table import json table = Table("table") json.dumps(table) # "table"
RecordID
When you receive a RecordID back from SurrealDB, it will always be represented as a RecordID
. The class holds tb
and id
fields, representing the table name, and a unique identifier for the record on that table. A RecordID
can be converted into a string, and will be represented as such when it’s converted to JSON.
Signaturefrom surrealdb import RecordID RecordID(tb: str, id: Any)
RecordID
Constructingfrom surrealdb import RecordID # table is "person" # unique identifier on the table is "john" rid = RecordID("person", "john")
Extracting datafrom surrealdb import RecordID # Simple rid = RecordID("person", "john") rid.table_name # "person" rid.id # "john" # Complex rid = RecordID("recording", {"city": "London", "data": 123}) rid.id # {"city": "London", "data": 123} rid.id["city"] # "London" rid.id["data"] # 123
Simplefrom surrealdb import RecordID str(RecordID('table', 123)) # 'table:123' str(RecordID('table', 'abc')) # 'table:abc'
Complex charactersfrom surrealdb import RecordID str(RecordID('table', '123')) # 'table:⟨123⟩' str(RecordID('table', '123withletters')) # 'table:123withletters' str(RecordID('table', 'complex-string')) # 'table:⟨complex-string⟩' str(RecordID('table-name', 123)) # '⟨table-name⟩:123'
Objects and Arraysfrom surrealdb import RecordID str(RecordID('table', {"city": "London"})) # 'table:{ city: "London" }' str(RecordID('table', ["London"])) # 'table:["London"]'
The RecordID.parse
method allows you to convert a string representation of a RecordID back into a RecordID
object.
Basic parsingfrom surrealdb import RecordID # Parse a simple RecordID string record_id = RecordID.parse("user:john") print(record_id.table_name) # "user" print(record_id.id) # "john"
Parsing special formatsfrom surrealdb import RecordID # Parse RecordID with special characters in the ID record_id = RecordID.parse("item:⟨complex-id⟩") print(record_id.table_name) # "item" print(record_id.id) # "complex-id"
Advanced parsing examplesfrom surrealdb import RecordID # Parse a RecordID from a query result result_id = "weather:['London', '2025-05-07']" record_id = RecordID.parse(result_id) print(record_id.table_name) # "weather" # The ID will be parsed as a string representation print(record_id.id) # "['London', '2025-05-07']" # Using parse with error handling try: record_id = RecordID.parse("invalid-format") # Handle the parsed record ID except ValueError: print("Invalid RecordID format")