Surreal
The Surreal class is the main entry point for the Java SDK. It provides methods for connecting to a SurrealDB instance, authenticating, querying, and managing data. The class implements AutoCloseable, so it can be used in a try-with-resources block to ensure the connection is closed automatically.
Source: surrealdb.java
Connection Methods
Surreal()
Creates a new Surreal instance. The instance is not connected to any server until .connect() is called.
Returns: Surreal
.connect(url)
Connects the instance to a SurrealDB server using the specified URL. The URL scheme determines the connection protocol. See the start command documentation for server configuration options.
| Parameter | Type | Description |
|---|---|---|
url | String | The connection URL. Supported schemes: ws://, wss://, http://, https://, memory://, surrealkv://. |
Returns: Surreal (for method chaining)
.close()
Closes the active connection and releases all associated resources. This is called automatically when using try-with-resources.
Returns: void
.useNs(namespace)
Switches the connection to a specific namespace.
| Parameter | Type | Description |
|---|---|---|
ns | String | The namespace to switch to. |
Returns: Surreal (for method chaining)
.useDb(database)
Switches the connection to a specific database.
| Parameter | Type | Description |
|---|---|---|
db | String | The database to switch to. |
Returns: Surreal (for method chaining)
.useDefaults()
Resets the namespace and database to the server defaults.
Returns: Surreal (for method chaining)
.getNamespace()
Returns the namespace currently in use on this connection.
Returns: String
.getDatabase()
Returns the database currently in use on this connection.
Returns: String
.newSession()
Creates a new isolated session that shares the underlying connection but maintains its own namespace, database, authentication state, and variables.
Returns: Surreal
Authentication Methods
.signin(credential)
Signs in to the database with the provided credentials. The credential type determines the authentication level: root, namespace, database, or record access.
| Parameter | Type | Description |
|---|---|---|
credential | Credential | The credentials to sign in with. Use RootCredential, NamespaceCredential, DatabaseCredential, or RecordCredential. |
Returns: Token
.signup(credential)
Signs up a new record user using a record access method defined with DEFINE ACCESS.
| Parameter | Type | Description |
|---|---|---|
credential | RecordCredential | The record access credentials including namespace, database, access method, and any additional fields required by the access definition. |
Returns: Token
.authenticate(token)
Authenticates the current connection using an existing JWT token.
| Parameter | Type | Description |
|---|---|---|
token | String | A valid JWT token string. |
Returns: Surreal (for method chaining)
.invalidate()
Invalidates the current authentication, removing the associated session token.
Returns: Surreal (for method chaining)
Query Methods
.query(sql)
Executes one or more SurrealQL statements and returns a Response containing the results of each statement.
| Parameter | Type | Description |
|---|---|---|
sql | String | The SurrealQL query string to execute. |
Returns: Response
.queryBind(sql, params)
Executes a parameterized SurrealQL query. Parameters are safely injected into the query, preventing SurrealQL injection.
| Parameter | Type | Description |
|---|---|---|
sql | String | The SurrealQL query string with parameter placeholders. |
params | Map<String, ?> | A map of parameter names to values. |
Returns: Response
.run(name, args)
Runs a server-side SurrealDB function defined with DEFINE FUNCTION.
| Parameter | Type | Description |
|---|---|---|
name | String | The function name (e.g. "fn::calculate_total"). |
args | Object... | Arguments to pass to the function. |
Returns: Value
Data Methods
Note
.create(type, target, content)
Creates one or more records. When called with a table name, SurrealDB generates random IDs. When called with a RecordId, the record is created with that specific ID.
| Parameter | Type | Description |
|---|---|---|
type | Class<T> | The class to deserialize results into. |
target | String or RecordId | The table name or specific record ID. |
contents | T or T... | The record content(s) to create. |
Returns: List<T> (table target) or T (record ID target)
.select(type, target)
Selects records from a table or retrieves specific records by ID.
| Parameter | Type | Description |
|---|---|---|
type | Class<T> | The class to deserialize results into. |
target | String, RecordId, RecordId..., or RecordIdRange | The table name, a single record ID, multiple record IDs, or a record ID range. |
Returns: Iterator<T> (table), Optional<T> (single ID), List<T> (multiple IDs or range)
.selectSync(type, target)
Thread-safe variant of .select() for table-level queries. Returns a synchronized iterator safe for use across multiple threads.
| Parameter | Type | Description |
|---|---|---|
type | Class<T> | The class to deserialize results into. |
target | String | The table name to select from. |
Returns: Iterator<T> (synchronized)
.insert(type, target, content)
Inserts one or more records into a table.
| Parameter | Type | Description |
|---|---|---|
type | Class<T> | The class to deserialize results into. |
target | String | The table to insert into. |
contents | T... | The record content(s) to insert. |
Returns: List<T>
.update(type, target, upType, content)
Updates existing records. Use UpType.CONTENT to replace the entire record, UpType.MERGE to merge fields into the existing record, or UpType.PATCH to apply a JSON Patch.
| Parameter | Type | Description |
|---|---|---|
type | Class<T> | The class to deserialize results into. |
target | RecordId, RecordIdRange, or String | The record ID, record ID range, or table name to update. |
upType | UpType | The update strategy: UpType.CONTENT (replace), UpType.MERGE (merge), or UpType.PATCH (JSON Patch). |
content | T | The update content. |
Returns: T (single record) or Iterator<T> (table)
.updateSync(type, target, upType, content)
Thread-safe variant of .update() for table-level updates. Returns a synchronized iterator safe for use across multiple threads.
Returns: Iterator<T> (synchronized)
.upsert(type, target, upType, content)
Updates an existing record or creates a new one if it does not exist. Accepts the same parameters as .update().
| Parameter | Type | Description |
|---|---|---|
type | Class<T> | The class to deserialize results into. |
target | RecordId, RecordIdRange, or String | The record ID, record ID range, or table name to upsert. |
upType | UpType | The update strategy: UpType.CONTENT (replace), UpType.MERGE (merge), or UpType.PATCH (JSON Patch). |
content | T | The record content. |
Returns: T (single record) or Iterator<T> (table)
.upsertSync(type, target, upType, content)
Thread-safe variant of .upsert() for table-level upserts. Returns a synchronized iterator safe for use across multiple threads.
Returns: Iterator<T> (synchronized)
.delete(target)
Deletes records from the database. Supports deleting a single record, multiple records by ID, a range of records, or all records in a table.
| Parameter | Type | Description |
|---|---|---|
target | RecordId, RecordId..., RecordIdRange, or String | A single record ID, multiple record IDs, a record ID range, or a table name. |
Returns: void
.relate(from, table, to)
Creates a graph relation between two records.
| Parameter | Type | Description |
|---|---|---|
type | Class<T extends Relation> | The class to deserialize the relation into. Omit for untyped Value return. |
from | RecordId | The source record. |
table | String | The relation table name. |
to | RecordId | The target record. |
content | T | Additional data to attach to the edge record. |
Returns: Value or T
.insertRelation(target, content)
Inserts a relation record into a relation table with additional data.
| Parameter | Type | Description |
|---|---|---|
type | Class<T extends InsertRelation> | The relation class to deserialize into. |
target | String | The relation table name. |
content | T | The relation content, including in and out fields. |
Returns: T
.insertRelations(target, contents)
Inserts multiple relation records into a relation table using varargs.
| Parameter | Type | Description |
|---|---|---|
type | Class<T extends InsertRelation> | The relation class to deserialize into. |
target | String | The relation table name. |
contents | T... | The relation records to insert, each including in and out fields. |
Returns: List<T>
Live Query Methods
Note
.selectLive(table)
Starts a live query that receives real-time notifications when records in the specified table are created, updated, or deleted.
| Parameter | Type | Description |
|---|---|---|
table | String | The table to watch for changes. |
Returns: LiveStream
Transaction Methods
.beginTransaction()
Starts a new atomic transaction. All operations performed on the returned Transaction are grouped and only applied when committed.
Returns: Transaction
Utility Methods
.version()
Returns the version string of the connected SurrealDB server.
Returns: String
.health()
Checks the health of the connected SurrealDB server.
Returns: boolean
.exportSql(path)
Exports the current database to a file at the specified path.
| Parameter | Type | Description |
|---|---|---|
path | String | The file path to export the database to. |
Returns: boolean
.importSql(path)
Imports a database from a file at the specified path.
| Parameter | Type | Description |
|---|---|---|
path | String | The file path to import the database from. |
Returns: boolean
See Also
Transaction — Transaction reference
Response — Query response reference
LiveStream — Live query reference
Connecting to SurrealDB — Connection protocols and patterns
Authentication — Authentication concepts
SurrealQL — Query language reference
DEFINE USER — System user configuration
DEFINE ACCESS — Record access method configuration