Utilities
The JavaScript SDK provides a set of utility functions for common tasks like deep value comparison, JSON conversion, SurrealQL string serialization, identifier escaping, and expression building. These complement the SDK's core query methods and value types.
API References
| Utility | Description |
|---|---|
equals(a, b) | Deep equality comparison for all value types |
expr() | Composes type-safe expressions for WHERE clauses and conditions |
surql | Tagged template for composing parameterized queries |
BoundQuery | Parameterized query class with manual control |
escapeIdent(name) | Escapes table and field names for use in SurrealQL |
escapeKey(key) | Escapes object keys for use in queries |
escapeRid(value) | Escapes record ID components |
escapeValue(value) | Escapes arbitrary values for embedding in queries |
Comparing values with deep equality
JavaScript's === operator compares objects by reference, which means two RecordId instances with the same table and ID are not considered equal. The equals() function performs deep structural comparison across all value types, including SurrealDB's custom classes, nested objects, arrays, Date, RegExp, and bigint/number normalization.
This is particularly useful for detecting changes in query results.
Note
Jsonifying query results
The jsonify() function converts SurrealDB value types in a result to their JSON representations, in the same manner that SurrealDB would serialize them. It only transforms SurrealDB-specific classes, leaving all other values untouched, and is fully type-safe.
You can also jsonify query results directly using the .json() chain on query methods.
Building expressions
The expr() function and its companion operators allow you to compose dynamic, type-safe conditions for use in query builder methods like .where() and in surql templates. See Bound queries for the full guide.
Available operators include comparison (eq, ne, gt, gte, lt, lte), logical (and, or, not), string/array (contains, containsAny, containsAll), geometry (inside, outside, intersects), and search (matches, knn).
Composing parameterized queries
The surql tagged template and BoundQuery class provide safe parameterization for dynamic queries. See Bound queries for the full guide.
Escaping identifiers and values
When you need to construct SurrealQL strings manually, the escape functions ensure that identifiers and values are properly quoted. In most cases, you should prefer bound queries or the value type classes instead of manual escaping.
Warning
Best practices
Use surql for parameterization
Use expr for complex conditions
Use equals for deep comparison
Learn more
equals API reference for deep comparison details
expr API reference for expression builder operators
surql API reference for template tag details
BoundQuery API reference for manual query building
Escape functions API reference for all escape utilities
Bound queries for safe query composition
Value types for SurrealDB-specific data classes