Skip to main content

A Rails-like ORM for FastAPI using SQLModel

Project description

FastRecord

A Rails-like ORM for FastAPI using SQLModel, bringing Active Record pattern to Python.

Version Compatibility

FastRecord Python FastAPI SQLModel Redis
0.1.x ≥3.13 ≥0.115.5 ≥0.0.22 ≥5.2.0

Requirements

  • Python: 3.13 or higher required for modern typing features and performance improvements
  • FastAPI: 0.115.5 or higher for middleware and dependency injection features
  • SQLModel: 0.0.22 or higher for ORM functionality
  • Redis: 5.2.0 or higher (with hiredis) for caching support
  • Pydantic Settings: 2.6.1 or higher for configuration management
  • Inflect: 7.4.0 or higher for naming conventions

[Previous README content remains the same...]

Features

  • Active Record pattern implementation
  • Chainable query interface
  • Built-in caching with Redis/memory support
  • Callbacks (before/after save, create, update, destroy)
  • Validations
  • Relationship management (has_many, belongs_to, has_one)
  • Soft deletes
  • Pagination
  • Eager loading

Installation

pip install fastrecord

Quick Start

from fastrecord import FastRecord, Field
from typing import Optional, List
from datetime import datetime


class User(FastRecord):
    name: str = Field(...)
    email: str = Field(...)
    posts: List["Post"] = []


class Post(FastRecord):
    title: str = Field(...)
    content: str = Field(...)
    author_id: Optional[int] = Field(default=None, foreign_key="user.id")
    author: Optional[User] = None


# Create
user = User.create(name="John", email="john@example.com")

# Query
user = User.where(name="John").first()
posts = Post.where(author_id=user.id).order("created_at").limit(5).all()

# Update
user.update(name="John Doe")

# Delete
user.delete()  # Soft delete
user.destroy()  # Hard delete

Configuration

from fastrecord import configure

configure(
    DATABASE_URL="postgresql://user:pass@localhost/dbname",
    CACHE_ENABLED=True,
    CACHE_TYPE="redis",
    CACHE_REDIS_URL="redis://localhost:6379/0"
)

FastAPI Integration

from fastapi import FastAPI
from fastrecord import DatabaseMiddleware

app = FastAPI()
app.add_middleware(DatabaseMiddleware)

Validations

from fastrecord.validation import PresenceValidator, FormatValidator


class User(FastRecord):
    name: str = Field(...)
    email: str = Field(...)

    @validates("name", PresenceValidator)
    def validate_name(self):
        pass

    @validates("email", FormatValidator, with_=r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
    def validate_email(self):
        pass

Relationships

class User(FastRecord):
    posts = has_many("Post")
    profile = has_one("Profile")


class Post(FastRecord):
    author = belongs_to("User")

Caching

class User(FastRecord):
    @cached(ttl=3600)
    def expensive_calculation(self):
        # Complex computation
        pass


# Query caching
users = User.where(active=True).cache(ttl=300).all()

License

MIT License. See LICENSE file for details.

Contributing

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

Author

Tushar Mangukiya tushar.m@innovalabs.tech

FastRecord Examples

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

fastrecord-0.1.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

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

fastrecord-0.1.0-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file fastrecord-0.1.0.tar.gz.

File metadata

  • Download URL: fastrecord-0.1.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/24.1.0

File hashes

Hashes for fastrecord-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5e06a9e65c164a5b3227de18b6ede548c8a88a384dc950b8533154c1edb0a88c
MD5 d8ea39c3dd217fec0e196772897d8126
BLAKE2b-256 322eff6f3bdf3907b931d278e201189def1f2407d9e8965f319367f16e6a0c48

See more details on using hashes here.

File details

Details for the file fastrecord-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fastrecord-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/24.1.0

File hashes

Hashes for fastrecord-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06423844768ba88043085aac0188d6a9cb65f3c0bf0c0a9fe721d32e4a891935
MD5 3ff76667c909eccfbfbaf0f326925dd9
BLAKE2b-256 0703fbb4804ae342cc434272203acfa67b9278cc4fb6487e98a0b5dc79c18fed

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