Quickstart
The JavaScript SDK for SurrealDB makes it effortless to connect to your instance and start querying data. This guide will walk you through the process of connecting to a SurrealDB instance and performing basic operations.
1. Install the SDK
Follow the installation guide to install the SDK as dependency in your project. Once installed, you can import and instantiate the SDK to start using it.
The Surreal class can be instantiated multiple times to connect to multiple SurrealDB instances at once.
2. Connect to SurrealDB
You can use the .connect() method to connect to a local or remote SurrealDB instance.
This method accepts a connection string and a set of options, including namespace, database, and authentication details.
Supported connection protocols include:
WebSocket (
ws://) for long lived connections (e.g. backend or frontend applications)HTTP (
http://) for short lived stateless connections (e.g. server-side rendering applications)Embedded protocols using the WebAssembly engine or Node.js engine
In order to facilitate a wide range of use cases, the SDK supports a variety of authentication options
Connecting as system user
This approach is suitable for connecting to a SurrealDB instance as a system user, for example when connecting from a server-side application.
Alternatively you can use the .signin() method to authenticate, however passing the authentication details to the .connect() method is the preferred way and allows for automatic reconnecting.
Connecting as record user
This approach is suitable when connecting as record user from a frontend application.
Since authentication usually takes place through a login page, you can use the dedicated .signin() or .signup() methods to authenticate.
Connecting with an existing token
This approach is suitable when connecting with an existing access or refresh token.
3. Defining your tables
The JavaScript SDK is built to be fully type-safe and TypeScript compatible. For this reason we recommend defining table references in a central location
3. Inserting data into SurrealDB
Once connected, you can use the .create() method to execute a CREATE query. This method accepts either a Table or a RecordId as the first argument. Use the .content() chain to specify the record data.
4. Retrieving data from SurrealDB
Selecting records
The .select() method retrieves all records from a table, or a single record by its RecordId.
You can chain methods like .fields(), .where(), and .limit() to refine your query.
In addition to the .eq() function in the above example, we offer a comprehensive set of expression utilities for building type-safe SurrealQL conditions.
Running SurrealQL queries
For more advanced use cases, you can use the .query() method to run SurrealQL statements directly. Use parameters to safely pass dynamic values.
What's next?
You've learned how to install the SDK, connect to SurrealDB, create records, and retrieve data. There's a lot more you can do with the SDK, including updating and deleting records, handling authentication, live queries, and running embedded databases.
SurrealQL
Learn how to use SurrealQL to query your database, define schemas, and build complex expressions.
Connection management
Learn how to manage your database connections, including events, reconnecting, and more.
Authentication
Read more about authentication and how to integrate it into your application.
Executing queries
Learn how to execute queries against your database, use the query builders, and customise responses.