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.9 | ≥0.115.5 | ≥0.0.22 | ≥5.2.0 |
Requirements
- Python: 3.9 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
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Author
Tushar Mangukiya tushar.m@innovalabs.tech
FastRecord Examples
Project details
Release history Release notifications | RSS feed
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.1.tar.gz
(20.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastrecord-0.1.1.tar.gz.
File metadata
- Download URL: fastrecord-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
152e50204eaa86cfe2af6f6697a9efa6aa4ea45b8b4dc3f151cd0ed5ec38b6f7
|
|
| MD5 |
5affbeb2c0f1cbf62b1c2f8d55bd221a
|
|
| BLAKE2b-256 |
62075d2cf4fa3defefe9c7ee5f07bcb0645512fbe6789f2049f0cc83a364c816
|
File details
Details for the file fastrecord-0.1.1-py3-none-any.whl.
File metadata
- Download URL: fastrecord-0.1.1-py3-none-any.whl
- Upload date:
- Size: 29.9 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d4dbfad8268db6ec64825d177fcfc702cbb84b02cbfd1d6ac0c12bb98a5ecf5
|
|
| MD5 |
09835ac4411e0a2f1b05b3551ac0c39a
|
|
| BLAKE2b-256 |
a41710d50396b96405129bdeb5147a5576b4178c0f0ad198aeb4c1e163a4cbf1
|