surreal-basics
Simple and transparent SurrealDB connection abstraction for Python.
Why surreal-basics?
When working with SurrealDB, you need to manage connections, authentication, and namespaces for each operation. surreal-basics abstracts this complexity, offering:
- Automatic connection: Configure once, use anywhere
- Optimized performance: Persistent (singleton) connections for WebSocket and HTTP
- Smart retry: Automatic handling of transient errors (lock conflicts)
- Consistent API:
repo_*functions for async andrepo_*_syncfor sync
Installation
pip install surreal-basics
Or with uv:
uv add surreal-basics
Compatibility
Built on the official surrealdb 2.x SDK, which supports SurrealDB servers
v2.0.0 through v3.x. Requires Python 3.11+.
SurrealDB 3.x note: v3 is stricter than v2 — reading from a table that was never defined (e.g.
SELECT * FROM thingbefore any record exists) raises an error instead of returning an empty list. Define the table or insert a record first. surreal-basics surfaces this difference rather than masking it.
Quick Start
Configuration via environment variables
export SURREAL_HOST=localhost
export SURREAL_PORT=8000
export SURREAL_USER=root
export SURREAL_PASS=root
export SURREAL_NS=test
export SURREAL_DB=test
export SURREAL_MODE=ws # or "http"
Basic usage
from surreal_basics import repo_query, repo_create, repo_select
# Simple query
results = await repo_query("SELECT * FROM user WHERE active = true")
# Create record
user = await repo_create("user", {"name": "John", "email": "john@test.com"})
# Select by ID
user = await repo_select("user:123")
Synchronous mode
from surreal_basics import repo_query_sync, repo_create_sync
# Same operations, without async/await
results = repo_query_sync("SELECT * FROM user")
user = repo_create_sync("user", {"name": "Mary"})
Programmatic configuration
import surreal_basics
# Configure connection
surreal_basics.init(
host="localhost",
port=8000,
namespace="my_ns",
database="my_db",
mode="ws", # "ws" or "http"
persistent=True, # persistent connection (recommended)
)
# Or just change the mode
surreal_basics.mode = "http"
Performance
Benchmarks with 1000 operations each (localhost):
| Operation | HTTP Sync | HTTP Async | WS Sync | WS Async |
|---|---|---|---|---|
| CREATE | ~130 ops/s | ~180 ops/s | ~650 ops/s | ~750 ops/s |
| SELECT | ~150 ops/s | ~200 ops/s | ~800 ops/s | ~3500 ops/s |
| UPDATE | ~130 ops/s | ~170 ops/s | ~600 ops/s | ~2800 ops/s |
| DELETE | ~140 ops/s | ~190 ops/s | ~700 ops/s | ~3000 ops/s |
Recommendation: Use WebSocket (mode="ws") for best performance. HTTP is useful for serverless environments or when WebSocket is not available.
API Reference
Async Functions
| Function | Description |
|---|---|
repo_query(query, vars) |
Execute SurrealQL query |
repo_create(table, data) |
Create record |
repo_select(table_or_id) |
Select records or by ID |
repo_update(table, id, data) |
Update existing record |
repo_upsert(table, id, data) |
Create or update (merge) |
repo_delete(record_id) |
Delete record |
repo_insert(table, data_list) |
Bulk insert |
repo_relate(source, rel, target) |
Create relationship |
Sync Functions
All async functions have sync equivalents with _sync suffix:
repo_query_sync,repo_create_sync, etc.
Configuration
| Variable | Default | Description |
|---|---|---|
SURREAL_HOST |
localhost | SurrealDB host |
SURREAL_PORT |
8000 | Port |
SURREAL_USER |
root | Username |
SURREAL_PASS |
root | Password |
SURREAL_NS |
test | Namespace |
SURREAL_DB |
test | Database |
SURREAL_AUTH_SCOPE |
root | Signin scope: "root", "namespace", or "database" |
SURREAL_MODE |
ws | Mode: "ws" or "http" |
SURREAL_PERSISTENT |
true | Persistent connection |
Migrations
surreal-basics includes a built-in migration system for managing SurrealDB schema changes.
Quick start
# Create a migration
sbl-migrate create create_users --dir ./migrations
# Check status
sbl-migrate status --dir ./migrations
# Apply pending migrations
sbl-migrate up --dir ./migrations
# Rollback last migration
sbl-migrate down --dir ./migrations
# Abort if the target isn't the namespace/database you expect
sbl-migrate up --expect-ns acme-prod --expect-db app
# Adopt tracking on a database whose schema already matches the files
sbl-migrate baseline --dir ./migrations
# Abort rather than replay the history on a database that was never baselined
sbl-migrate up --require-baseline
Programmatic usage
from surreal_basics.migrate import MigrationRunner, AsyncMigrationRunner
# Sync
runner = MigrationRunner("./migrations")
runner.run_up()
# Async
runner = AsyncMigrationRunner("./migrations")
await runner.run_up()
Migration files use the naming convention NNN_name.surrealql with optional NNN_name_down.surrealql for rollbacks. See docs/migrations.md for full documentation.
Error Handling
from surreal_basics import (
SurrealDBConnectionError, # Connection failed
SurrealDBMigrationError, # Migration failed
SurrealDBQueryError, # Query error (no retry)
SurrealDBTransientError, # Transient error (automatic retry)
)
try:
result = await repo_query("SELECT * FROM user")
except SurrealDBConnectionError as e:
print(f"Connection failed: {e}")
except SurrealDBQueryError as e:
print(f"Query error: {e}")
Documentation
See docs/ for complete documentation including:
- Configuration - Environment variables, init() and connection modes
- API Reference - Complete documentation of all functions
- Migrations - Schema migration system
Development
# Clone and install
git clone https://github.com/lfnovo/surreal-basics.git
cd surreal-basics
uv sync
# Run tests
uv run pytest
# Run benchmark
uv run python benchmark_library.py
License
MIT
Metadata
Release files for surreal-basics 0.8.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| surreal_basics-0.8.1.tar.gz | 173.4 kB | Without provenance | Details for surreal_basics-0.8.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| surreal_basics-0.8.1-py3-none-any.whl | Python 3 | none | any | Details for surreal_basics-0.8.1-py3-none-any.whl |
Total release size: 207.8 kB
Release files / surreal_basics-0.8.1.tar.gz
| Download URL | surreal_basics-0.8.1.tar.gz |
|---|---|
| Size | 173.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c9183b5d9aed31521e858641cad8e7620c5ce468872899bd4113e05a039fec47
|
|
BLAKE2b-256 checksum How to use checksums |
0c3a5e79a7aa552192d1af582bfd229e97a7f517ea3562b04726735078c67beb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / surreal_basics-0.8.1-py3-none-any.whl
| Download URL | surreal_basics-0.8.1-py3-none-any.whl |
|---|---|
| Size | 34.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f166e7ad887398089c630b45f8726c103734b0eabda1fd17ebc7038441c3f548
|
|
BLAKE2b-256 checksum How to use checksums |
d0761aaf8bac335538e7594c82cb7cbd599ce9fbbe02c4a272db6dc8f9ea2d6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for surreal-basics 0.8.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.