Bound queries
When composing dynamic queries, it is important to avoid string interpolation to prevent injection vulnerabilities. The JavaScript SDK provides bound queries and the surql template tag to safely parameterize values, along with an expressions API for composing dynamic conditions.
API References
| Utility | Description |
|---|---|
surql | Tagged template literal for composing parameterized queries |
BoundQuery | Class for manually building parameterized queries |
expr() | Composes type-safe expressions for use in queries |
Using the surql template tag
The surql tagged template literal is the recommended way to compose parameterized queries. Interpolated values are automatically bound as parameters, preventing injection and preserving type safety.
The surrealql export is an alias for surql if you prefer the longer name.
Note
Building queries with BoundQuery
The BoundQuery class provides manual control over query composition. You can construct a query with initial bindings, and incrementally append fragments with additional parameters.
Appending query fragments
Use the .append() method to conditionally add SurrealQL fragments. The method uses the same tagged template literal syntax as surql, so interpolated values are automatically bound.
Composing expressions
The expressions API provides functions for building dynamic conditions in a type-safe way. Expressions integrate with both surql and query builder methods like .where().
Learn more
surql API reference for template tag details
BoundQuery API reference for manual query building
expr API reference for the full expressions API
Executing queries for running queries against the database