Helpers are split across two packages. @surrealdb/sqon covers value comparison, conversion, and escaping. The surrealdb driver adds query-building tools for parameterised SurrealQL. Import from surrealdb when you use the driver and want both layers in one place.
SQON utilities
These live in @surrealdb/sqon alongside the value types. You do not need the database client to use them.
| Utility | Description |
|---|---|
equals(a, b) | Deep equality comparison for all value types |
jsonify(value) | Converts value types to JSON-safe string representations |
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 |
toSurqlString(value) | Converts a value tree to a SurrealQL string representation |
Comparing values with deep equality
=== compares object references, so two RecordId instances with the same table and ID still fail an identity check. equals() walks the structure instead, including nested objects, arrays, SurrealDB value classes, Date, RegExp, and mixed bigint/number values.
Handy when you want to know whether a fetched record changed between two reads:
Note
Jsonifying query results
jsonify() turns SurrealDB value classes in a result into the JSON string forms SurrealDB would use. Plain numbers, strings, and objects are left as they are. The return type reflects the conversion.
You can also call .json() on query methods to jsonify the result in one step.
For JSON with explicit type wrappers (for example { "$recordId": ... }), use JsonCodec instead.
Escaping identifiers and values
The escape helpers quote identifiers and values for hand-built SurrealQL. Prefer bound queries or value classes when you can.
Warning
Converting to SurrealQL strings
toSurqlString() walks a value tree (objects, arrays, SQON classes) and returns a SurrealQL literal string. Useful for logs, debugging, or SurrealQL snippets outside bound queries.
SDK utilities
These ship with the surrealdb driver and tie into its query engine. They are not exported from @surrealdb/sqon.
| Utility | Description |
|---|---|
expr() | Composes type-safe expressions for WHERE clauses and conditions |
surql | Tagged template for composing parameterized queries |
BoundQuery | Parameterized query class with manual control |
s, d, r, u | Tagged templates for SurrealQL string, datetime, record, and UUID prefixes |
Building expressions
expr() and its operators build conditions for .where() and for surql templates. See Bound queries for the full list.
Operators cover comparisons (eq, ne, gt, gte, lt, lte), logic (and, or, not), strings and arrays (contains, containsAny, containsAll), geometry (inside, outside, intersects), and search (matches, knn).
Composing parameterized queries
surql and BoundQuery bind parameters so you do not splice user input into query strings. See Bound queries.
String prefix templates
The s, d, r, and u templates build typed literals with SurrealQL's string prefixes. See Value types.
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
Codecs for serialising value types over CBOR and JSON
Bound queries for safe query composition
Value types for SurrealDB-specific data classes