The JavaScript SDK provides custom classes for SurrealDB-specific data types, ensuring type safety and data integrity when working with the database.
These SurrealQL types map directly to JavaScript primitives:
| SurrealQL Type | JavaScript Type | Example |
|---|---|---|
bool | boolean | true, false |
int, float | number | 42, 3.14 |
string | string | ”hello” |
null | null | null |
none | undefined | undefined |
These SurrealQL types map to JavaScript complex types:
| SurrealQL Type | JavaScript Type | Example |
|---|---|---|
array | Array | [1, 2, 3] |
object | Object | { key: "value" } |
set | Set | new Set([1, 2, 3]) |
These SurrealQL types require custom classes for proper representation:
| SurrealQL Type | JavaScript Class | Description |
|---|---|---|
record | RecordId | Type-safe record identifiers |
datetime | DateTime | Datetime with nanosecond precision |
duration | Duration | Time duration values |
decimal | Decimal | Arbitrary precision decimals |
uuid | Uuid | Universally unique identifiers |
geometry | Geometry* | Geometric/spatial data types |
future | Future | Deferred computation values |
bytes | Uint8Array | Binary data |
Additional types for working with queries and ranges:
| Class | Purpose | Documentation |
|---|---|---|
Table | Type-safe table references | View docs → |
RecordIdRange | Range of record IDs | View docs → |
Range | Generic range values | View docs → |
RecordId - Type-safe record identifiers with table and ID components
new RecordId(table, id) - Create record IDRecordId.parse(string) - Parse from stringRecordIdRange for querying rangesDateTime - Datetime values with nanosecond precision
DateTime.now() - Current datetimeDateTime.parse(string) - Parse from ISO string.toDate() - Convert to JavaScript DateDuration - Time duration with support for multiple units
Duration.parse('5h30m') - Parse from string.toMilliseconds() - Convert to millisecondsDecimal - Arbitrary precision decimal numbers
new Decimal('19.99') - Create precise decimalUuid - Universally unique identifiers
Uuid.v4() - Generate random UUIDUuid.v7() - Generate time-ordered UUIDTable - Type-safe table references
new Table<T>(name) - Create typed table referenceGeometryPoint - Single pointGeometryLine - Line between pointsGeometryPolygon - Polygon shapeGeometryMultiPoint, GeometryMultiLine, GeometryMultiPolygonGeometryCollection - Mixed geometry collectionFuture - Deferred computation values for schema definitions
Range - Generic range values for numeric and date ranges
import { RecordId, DateTime, Duration, Decimal, Uuid, Table } from 'surrealdb'; // Record identifiers const userId = new RecordId('users', 'john'); const user = await db.select(userId); // Datetime with precision const event = await db.create(new Table('events')).content({ name: 'Meeting', timestamp: DateTime.now(), duration: Duration.parse('1h30m') }); // Precise decimals const product = await db.create(new Table('products')).content({ name: 'Widget', price: new Decimal('19.99') }); // UUID generation const sessionId = Uuid.v7(); // Time-ordered const randomId = Uuid.v4(); // Random
// DateTime conversions const datetime = DateTime.now(); const jsDate = datetime.toDate(); const isoString = datetime.toString(); // Duration conversions const duration = Duration.parse('5h30m'); const milliseconds = duration.toMilliseconds(); const seconds = duration.toSeconds(); // Decimal conversions const decimal = new Decimal('123.456'); const number = decimal.toNumber(); // May lose precision const string = decimal.toString(); // Preserves precision
// Parse various types const recordId = RecordId.parse('users:john'); const datetime = DateTime.parse('2024-01-15T12:00:00Z'); const duration = Duration.parse('1h30m45s'); const uuid = Uuid.parse('550e8400-e29b-41d4-a716-446655440000');
Using custom type classes provides several advantages:
// Good: Type-safe const users = new Table<User>('users'); const results: User[] = await db.select(users); // Good: Explicit ID type const userId = new RecordId<'users', string>('users', 'john');
// Good: Type-safe await db.select(new RecordId('users', 'john')); // Avoid: String-based await db.query('SELECT * FROM users:john');
// Good: Preserve precision const price = new Decimal('19.99'); const display = price.toString(); // Caution: May lose precision const number = price.toNumber();
try { const uuid = Uuid.parse(userInput); } catch (error) { console.error('Invalid UUID format'); }