Skip to main content

surql

License Python Version SurrealDB

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-scored FULLTEXT indexes and a fulltext_search_query helper -- the lexical leg of hybrid retrieval
  • Graph Traversal - Native SurrealDB graph features with edge relationships
  • Query Caching - Memory and Redis-backed caching with @cache_query decorator
  • 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:

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oneiriq_surql-1.10.0.tar.gz (701.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oneiriq_surql-1.10.0-py3-none-any.whl (282.5 kB view details)

Uploaded Python 3

File details

Details for the file oneiriq_surql-1.10.0.tar.gz.

File metadata

  • Download URL: oneiriq_surql-1.10.0.tar.gz
  • Upload date:
  • Size: 701.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for oneiriq_surql-1.10.0.tar.gz
Algorithm Hash digest
SHA256 b81bb445ade952c12262e6f6ac9ffc8af6e14f96e90118c0d25ea785de6a951e
MD5 bc2b34417d7f84e4c242526115615a40
BLAKE2b-256 4006c5b748292671768b78371cb19151561718f34279a8cf04ca81f4ee0a8162

See more details on using hashes here.

Provenance

The following attestation bundles were made for oneiriq_surql-1.10.0.tar.gz:

Publisher: publish.yml on Oneiriq/surql-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file oneiriq_surql-1.10.0-py3-none-any.whl.

File metadata

  • Download URL: oneiriq_surql-1.10.0-py3-none-any.whl
  • Upload date:
  • Size: 282.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for oneiriq_surql-1.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 af8f1949d173e6959782d20fffde2b5bc749ab580c6dae2976eef9df4359fd60
MD5 be2df93e96a11115a4a99f87b8e4f916
BLAKE2b-256 e633b2c7902fbc0dc81dc9e315d522d2cbca38fdeff08c52151fdbec91698f34

See more details on using hashes here.

Provenance

The following attestation bundles were made for oneiriq_surql-1.10.0-py3-none-any.whl:

Publisher: publish.yml on Oneiriq/surql-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.10.0 This release

2 files

1.9.0

2 files

1.8.0

2 files

1.7.1

2 files

1.7.0

2 files

1.6.4

2 files

1.6.3

2 files

1.6.2

2 files

1.6.1

2 files

1.6.0

2 files

1.5.15

2 files

1.5.14

2 files

1.5.13

2 files

1.5.12

2 files

1.5.11

2 files

1.5.10

2 files

1.5.9

2 files

1.5.8

2 files

1.5.6

2 files

1.5.5

2 files

1.5.2

2 files

1.5.0

2 files

1.3.1

2 files

1.3.0

2 files

1.2.1

2 files

1.1.0

2 files

1.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page