Skip to main content

A simple Pydantic ORM implementation for SurrealDB

Project description

Surrantic

A simple and intuitive Pydantic-based ORM for SurrealDB, providing both synchronous and asynchronous operations.

Features

  • 🔄 Both synchronous and asynchronous operations
  • 🏗️ Built on top of Pydantic for robust data validation
  • 🚀 Simple and intuitive API
  • 📝 Type hints and comprehensive documentation
  • 🔍 Automatic timestamp handling for created/updated fields
  • 🎯 Support for complex queries and relationships

Installation

pip install surrantic

Quick Start

from datetime import datetime
from typing import Optional
from surrantic import ObjectModel
from surrealdb import RecordID

class User(ObjectModel):
    table_name = "user"  # Define the table name in SurrealDB
    name: str
    email: str
    age: Optional[int] = None

# Async Usage
async def main():
    # Create a new user
    user = User(name="John Doe", email="john@example.com", age=30)
    await user.asave()  # Saves to SurrealDB
    print(f"User created with ID: {user.id}")

    # Fetch all users
    all_users = await User.aget_all()
    for user in all_users:
        print(f"Found user: {user.name}")

    # Get a specific user
    user_id = "user:123"  # or RecordID object
    user = await User.aget(user_id)
    if user:
        print(f"Found user: {user.name}")

    # Delete a user
    await user.adelete()

# Synchronous Usage
def sync_example():
    user = User(name="Jane Doe", email="jane@example.com")
    user.save()  # Synchronous save
    
    # Get all users
    users = User.get_all()
    
    # Delete user
    user.delete()

Advanced Usage

Custom Queries and Ordering

# Get all users ordered by name
users = await User.aget_all(order_by="name", order_direction="ASC")

Timestamps

Created and updated timestamps are automatically handled:

user = User(name="John", email="john@example.com")
await user.asave()
print(f"Created at: {user.created}")  # Automatically set
print(f"Updated at: {user.updated}")  # Automatically set

Configuration

Database Connection

By default, Surrantic uses environment variables for database configuration:

SURREAL_ADDRESS=ws://localhost:8000
SURREAL_USER=root
SURREAL_PASS=root
SURREAL_NAMESPACE=test
SURREAL_DATABASE=test

You can also override these settings directly in your code using SurranticConfig:

from surrantic import SurranticConfig

# Override all or some of the connection settings
SurranticConfig.configure(
    address="ws://mydb:8000",
    user="myuser",
    password="mypass",
    namespace="myns",
    database="mydb"
)

# Your models will now use the new configuration
user = User(name="John", email="john@example.com")
await user.asave()  # Uses the custom configuration

Logging

Surrantic includes configurable logging:

from surrantic.logging_config import setup_logging
import logging

# Console only logging
setup_logging(level=logging.DEBUG)

# Console and file logging
setup_logging(level=logging.INFO, log_file="surrantic.log")

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Requirements

  • Python 3.11+
  • pydantic >= 2.0.0
  • surrealdb >= 0.3.0

Acknowledgments

Project details


Download files

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

Source Distribution

surrantic-0.1.1.tar.gz (37.3 kB view details)

Uploaded Source

Built Distribution

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

surrantic-0.1.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file surrantic-0.1.1.tar.gz.

File metadata

  • Download URL: surrantic-0.1.1.tar.gz
  • Upload date:
  • Size: 37.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for surrantic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d01dfcc9208ab4d226aa96e7519ad807f7c7acf19a30c199bc8377aa238727fc
MD5 058e3701c7f9d6cb80723de2077a3e90
BLAKE2b-256 4176fd382e358a3c896280190465a647e266f1a8f069375c13d640253141ea1b

See more details on using hashes here.

File details

Details for the file surrantic-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: surrantic-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for surrantic-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 84e4439f44a3b7469bad7a7af4c1d2fb254c3569a15941f758821d7669d470ce
MD5 9dcf20cbb0d9825fe9292a6dbc03d0d2
BLAKE2b-256 bda8e5cfd3571b46f0102d4e1ceefb214a41b5dc374e0803a12b1c876caad232

See more details on using hashes here.

Supported by

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