githubEdit

Client

The generated client provides the main entry point for database operations.

Creating a Client

client, err := som.NewClient(ctx, som.Config{
    Address:   "ws://localhost:8000",
    Username:  "root",
    Password:  "root",
    Namespace: "myapp",
    Database:  "production",
})
if err != nil {
    log.Fatal(err)
}

Configuration Options

type Config struct {
    // Address is the SurrealDB server URL
    // Supports: ws://, wss://, http://, https://
    Address string

    // Username for authentication
    Username string

    // Password for authentication
    Password string

    // Namespace to use
    Namespace string

    // Database within the namespace
    Database string
}

Version Verification

When creating a client, SOM automatically verifies that the connected SurrealDB server meets the minimum required version (currently 3.0.5). If the version check fails, NewClient returns a som.ErrUnsupportedVersion error:

Accessing Repositories

The client provides typed repository access for each model:

Connection Management

Closing the Client

Context Usage

All operations accept a context for cancellation and timeouts:

Raw Queries

Execute arbitrary SurrealQL with parameter binding:

See Raw Queries for full documentation.

Error Handling

Check for errors on all operations:

Structured Server Errors

SurrealDB v3 returns structured error responses. SOM exposes these as som.ServerError, which you can extract using errors.As:

SOM automatically recognizes common domain errors:

Error
Description

som.ErrOptimisticLock

Update failed due to version mismatch

som.ErrAlreadyDeleted

Soft delete on already-deleted record

som.ErrNotFound

Record not found

som.ErrUnsupportedVersion

Server version below minimum required

Thread Safety

The client is safe for concurrent use from multiple goroutines.

Last updated