surql
A code-first database toolkit for SurrealDB. Define schemas, generate migrations, build queries, and perform typed CRUD -- all from Python.
Features
- Code-First Migrations - Schema changes defined in code with automatic migration generation
- Type-Safe Query Builder - Composable queries with Pydantic model integration
- SurrealDB v3 Ready - Emits v3-correct SurrealQL (datetime casts,
count() GROUP ALL,type::thing(table, id)record-id construction, buffered transactions, idempotent DDL) - Query UX Helpers - First-class wrappers for
time::now,math::*,string::*,count_if,type_record, and typed aggregations -- no raw SurrealQL required - Vector Search - HNSW, DISKANN, and MTREE index support with 8 distance metrics, EFC/M tuning, and F16 half-precision vectors
- Full-Text Search (BM25) -
DEFINE ANALYZER+ BM25-scoredFULLTEXTindexes and afulltext_search_queryhelper -- the lexical leg of hybrid retrieval - Graph Traversal - Native SurrealDB graph features with edge relationships
- Query Caching - Memory and Redis-backed caching with
@cache_querydecorator - Live Queries - Real-time change notifications and streaming
- Schema Visualization - Mermaid, GraphViz, and ASCII diagrams
- CLI Tools - Migrations, schema inspection, multi-environment orchestration, validation, and database management
- Async-First - Built with async/await, connection pooling, and retry logic
Install
pip install oneiriq-surql
# or with uv
uv add oneiriq-surql
Quick Start
Define a schema
from surql.schema.fields import string_field, int_field, datetime_field
from surql.schema.table import table_schema, unique_index, TableMode
user_schema = table_schema(
'user',
mode=TableMode.SCHEMAFULL,
fields=[
string_field('name'),
string_field('email', assertion='string::is::email($value)'),
int_field('age', assertion='$value >= 0 AND $value <= 150'),
datetime_field('created_at', default='time::now()', readonly=True),
],
indexes=[unique_index('email_idx', ['email'])],
)
Run migrations
surql migrate create "Add user table"
surql migrate up
surql migrate status
Build queries with first-class helpers
from surql import (
Query,
aggregate_records,
count_if,
math_mean_fn,
math_sum_fn,
time_now_fn,
type_record,
)
# Fluent UPDATE with server-side function values
sql = (
Query()
.update('user:alice')
.set(status='active', last_seen=time_now_fn())
.to_surql()
)
# Typed aggregate -- GROUP ALL + count() rendered correctly for v3
rows = await aggregate_records(
table='order',
select={
'total': count_if(),
'revenue': math_sum_fn('amount'),
'avg_ticket': math_mean_fn('amount'),
},
where="status = 'paid'",
group_all=True,
)
# Record-ID construction without string concatenation
ref = type_record('user', 'alice').to_surql()
# -> type::thing('user', 'alice')
Deploy across environments
# Sequential deploy to staging then production
surql orchestrate deploy -e staging,production
# Rolling deploy in batches of 2
surql orchestrate deploy -e prod1,prod2,prod3,prod4 \
--strategy rolling --batch-size 2
Documentation
Full documentation at oneiriq.github.io/surql-py:
- SurrealDB v3 patterns -- the forms surql emits for v3 compatibility
- Query UX helpers -- typed wrappers for common SurrealQL calls
- Upgrade notes -- 1.3.1 -> 1.4.0 -> 1.5.0 -> 1.5.1
Requirements
- Python 3.12+
- SurrealDB 1.0+ (integration CI runs against SurrealDB v3.0.5)
License
Apache License 2.0 - see LICENSE.
TypeScript / Deno / Node.js
Looking for SurrealDB tooling in TypeScript? Check out surql -- a type-safe query builder and client for SurrealDB available on JSR and NPM.
Support
- Documentation: oneiriq.github.io/surql-py
- Issues: GitHub Issues
- Changelog: CHANGES
Metadata
Release files for oneiriq-surql 1.10.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 | |
|---|---|---|---|
| oneiriq_surql-1.10.1.tar.gz | 703.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| oneiriq_surql-1.10.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 986.2 kB
Release files / oneiriq_surql-1.10.1.tar.gz
| Download URL | oneiriq_surql-1.10.1.tar.gz |
|---|---|
| Size | 703.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a42c5c4d5872cd4db5dd4de3ddec9fccb461f38e986e82863c9100b963a24afc
|
|
BLAKE2b-256 checksum How to use checksums |
f0d2ae68ff5f89b224f3263a8270741c1c425755b4e9bab0e9a4f60f9af2f2de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.
Transparency logRelease files / oneiriq_surql-1.10.1-py3-none-any.whl
| Download URL | oneiriq_surql-1.10.1-py3-none-any.whl |
|---|---|
| Size | 282.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9621165efdbde0ba8588806d0c696b77d67bcdefbe7fe9e01b23acd59bf4ebdd
|
|
BLAKE2b-256 checksum How to use checksums |
1fd212642e3e72625f4fa7e1d4f31d46f8f4fe33fbfb1c8235773abe79b7fb81
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.
Transparency log