Value types
The JavaScript SDK provides custom classes for SurrealDB-specific data types that don't have direct JavaScript equivalents. These classes ensure type safety, preserve database precision, validate input, and integrate seamlessly with the SDK's query methods.
API References
| Class | Description |
|---|---|
RecordId | Type-safe record identifiers with table and ID components |
Table | Type-safe table references for query methods |
DateTime | Datetime values with nanosecond precision |
Duration | Time duration values with multiple unit support |
Decimal | Arbitrary precision decimal numbers |
Uuid | Universally unique identifiers (v4 and v7) |
Range | Bounded or unbounded range values |
FileRef | References to files stored in SurrealDB |
Geometry* | GeoJSON geometry types (Point, Line, Polygon, etc.) |
Type mapping
SurrealQL types map to JavaScript types as follows:
| SurrealQL type | JavaScript type | Example |
|---|---|---|
bool | boolean | true, false |
int, float | number | 42, 3.14 |
string | string | "hello" |
null | null | null |
none | undefined | undefined |
array | Array | [1, 2, 3] |
object | Object | {`{ key: "value" }`} |
set | Set | new Set([1, 2, 3]) |
bytes | Uint8Array | new Uint8Array([...]) |
record | RecordId | new RecordId('users', 'john') |
| — | Table | new Table('users') |
datetime | DateTime | DateTime.now() |
duration | Duration | Duration.parse('1h30m') |
decimal | Decimal | new Decimal('19.99') |
uuid | Uuid | Uuid.v7() |
geometry | Geometry* | new GeometryPoint([1, 2]) |
range | Range | new Range(1, 10) |
file | FileRef | record.avatar |
RecordId and Table
A RecordId represents a unique record identifier consisting of a table name and an ID value. A Table represents a table reference. In the v2 SDK, query methods no longer accept plain strings as table names — you must use the Table class to avoid ambiguity between table names and record IDs.
The ID component of a RecordId can be a string, number, bigint, Uuid, array, or object. The Table class also supports a type parameter for type-safe query results.
DateTime and Duration
A DateTime represents a point in time with nanosecond precision. Unlike JavaScript's native Date which is limited to millisecond precision, DateTime preserves the full precision of SurrealDB timestamps. A Duration represents a span of time using SurrealQL duration syntax.
Decimal
A Decimal represents an arbitrary-precision decimal number. This avoids the floating-point precision issues that occur with JavaScript's number type.
Note
Uuid
A Uuid represents a universally unique identifier. The class supports generating both v4 (random) and v7 (time-ordered) UUIDs.
Range
A Range represents a bounded or unbounded range of values. Ranges are used in SurrealQL for selecting slices of records by ID or filtering numeric and temporal values. A RecordIdRange is a specialization for querying a range of records from a table.
FileRef
A FileRef represents a reference to a file stored in SurrealDB. File references are returned when working with file uploads and contain metadata about the stored file.
Geometry types
The SDK provides classes for all GeoJSON geometry types: GeometryPoint, GeometryLine, GeometryPolygon, GeometryMultiPoint, GeometryMultiLine, GeometryMultiPolygon, and GeometryCollection.
Parsing from strings
Most value classes support parsing from string representations, matching the syntax used in SurrealQL.
Parsing validates the input format and throws an error if the string is invalid.
Using native dates
By default, the SDK returns DateTime instances for datetime values. If you prefer native JavaScript Date objects (at the cost of nanosecond precision), you can enable useNativeDates in the codec options.
String prefixes
The SDK provides tagged template functions that replicate SurrealQL's string prefixes in JavaScript. These create the corresponding value class from a template literal.
Best practices
Use type parameters for type-safe queries
Prefer value classes over raw strings
Validate parsed input
Learn more
Data Types API reference for the full list of value class documentation
SurrealQL data model for the database-level type system
Utilities for comparing and converting values