SurrealDB is designed to be run in many different ways, and environments. Due to the separation of the storage and API layers, SurrealDB can be run in embedded mode, from within a number of different language environments. In Rust, SurrealDB can be run as an in-memory database, it can persist data using a file-based storage engine, or on a distributed cluster.
Install the SDK
First, create a new project using cargo new and add the SurrealDB crate to your dependencies, enabling the key-value store you need:
You will need to add the following additional dependencies:
Connect to SurrealDB
Open src/main.rs and replace everything in there with the following code to try out some basic operations using the SurrealDB SDK with an embedded database. Look at integrations to connect to a database.
Run your program from the command line with:
SDK methods
The Rust SDK comes with a number of built-in functions.
| Function | Description |
|---|---|
Surreal::init() | Initialises a static database engine |
db.connect(endpoint) | Connects to a specific database endpoint, saving the connection on the static client |
{"Surreal::new::(endpoint)"} | Connects to a local or remote database endpoint |
db.use_ns(namespace).use_db(database) | Switch to a specific namespace and database |
db.signup(credentials) | Signs up a user using a specific record access method |
db.signin(credentials) | Signs this connection in using a specific access method or system user |
db.invalidate() | Invalidates the authentication for the current connection |
db.authenticate(token) | Authenticates the current connection with a JSON Web Token |
db.set(key, val) | Assigns a value as a parameter for this connection |
db.query(sql) | Runs a set of SurrealQL statements against the database |
db.select(resource) | Selects all records in a table, or a specific record |
db.create(resource).content(data) | Creates a record in the database |
db.update(resource).content(data) | Updates all records in a table, or a specific record |
db.update(resource).merge(data) | Modifies all records in a table, or a specific record |
db.update(resource).patch(data) | Applies JSON Patch changes to all records in a table, or a specific record |
db.delete(resource) | Deletes all records, or a specific record |
.init()
The DB static singleton ensures that a single database instance is available across very large or complicated applications. With the singleton, only one connection to the database is instantiated, and the database connection does not have to be shared across components or controllers.
Example usage
.connect()
Connects to a local or remote database endpoint.
Arguments
| Argument | Description |
|---|---|
endpoint
| The database endpoint to connect to. |
Example usage
.new()
Connects to a local or remote database endpoint.
Arguments
| Arguments | Description |
|---|---|
endpoint
| The database endpoint to connect to. |
Example usage
.use_ns() and .use_db()
Switch to a specific namespace and database.
Arguments
| Arguments | Description |
|---|---|
ns
| Switches to a specific namespace. |
db
| Switches to a specific database. |
Example usage
.signup()
Signs up using a specific record access method.
Arguments
| Arguments | Description |
|---|---|
credentials
| Variables used in a signup query. |
Example usage
.signin()
Signs in using a specific access method or system user.
Arguments
| Arguments | Description |
|---|---|
credentials
| Variables used in a signin query. |
Example usage
.invalidate()
Invalidates the authentication for the current connection.
Example usage
.authenticate()
Authenticates the current connection with a JWT token.
Arguments
| Arguments | Description |
|---|---|
token
| The JWT authentication token. |
Example usage
.set()
Assigns a value as a parameter for this connection.
Arguments
| Arguments | Description |
|---|---|
key
| Specifies the name of the variable. |
val
| Assigns the value to the variable name. |
Example usage
.query()
Runs a set of SurrealQL statements against the database.
Arguments
| Arguments | Description |
|---|---|
sql
| Specifies the SurrealQL statements. |
vars
| Assigns variables which can be used in the query. |
Example usage
.select()
Selects all records in a table, or a specific record, from the database.
Arguments
| Arguments | Description |
|---|---|
resource
| The table name or a record ID to select. |
Example usage
Translated query
This function will run the following query in the database:
.create()
Creates a record in the database.
Arguments
| Arguments | Description |
|---|---|
resource
| The table name or the specific record ID to create. |
data
| The document / record data to insert. |
Example usage
Translated query
This function will run the following query in the database:
.update().content()
Updates all records in a table, or a specific record, in the database.
Note
Arguments
| Arguments | Description |
|---|---|
resource
| The table name or the specific record ID to create. |
data
| The document / record data to insert. |
Example usage
Translated query
This function will run the following query in the database:
.update().merge()
Modifies all records in a table, or a specific record, in the database.
Note
Arguments
| Arguments | Description |
|---|---|
resource
| The table name or the specific record ID to create. |
data
| The document / record data to insert. |
Example usage
Translated query
This function will run the following query in the database:
.update().patch()
Applies JSON Patch changes to all records, or a specific record, in the database.
Note
Arguments
| Arguments | Description |
|---|---|
resource
| The table name or the specific record ID to modify. |
data
| The JSON Patch data with which to modify the records. |
Example usage
Translated query
This function will run the following query in the database:
.delete()
Deletes all records in a table, or a specific record, from the database.
Arguments
| Arguments | Description |
|---|---|
resource
| The table name or a record ID to select. |
Example usage
Translated query
This function will run the following query in the database: