Backbone ORM is a lightweight, asynchronous Object-Relational Mapper (ORM) for Python, built on top of the PyPika SQL query builder. It provides a clean and efficient interface for interacting with PostgreSQL databases, leveraging type hints and asynchronous programming to enable scalable and maintainable database operations.
- Asynchronous Support: Built with asyncio to support non-blocking database operations.
- Type-Hinted Models: Utilizes Python's type hints for defining models, enhancing code clarity and editor support.
- PostgreSQL Integration: Specifically designed for PostgreSQL databases, with support for connection pooling and schema management.
- Redis Integration: Includes support for Redis, allowing for caching and other in-memory data storage solutions.
- Flexible Querying: Provides a flexible query builder and supports soft deletes and model relationships.
- python 3.10+
- pypika 0.48+
- pydantic 2.0+
- basalam.backbone-redis-cache 0.0.11+
pip install basalam.backbone-orm --upgrade
from basalam.backbone_orm import ModelAbstract
class UserModel(ModelAbstract):
id: int
name: str
import typing
import aioredis
from basalam.backbone_orm import (
T,
DriverEnum,
PostgresManager,
ConnectionConfig,
RepositoryAbstract,
)
postgres = PostgresManager(
default=DriverEnum.POOL,
config=ConnectionConfig(...)
)
redis = aioredis.Redis(...)
class UserRepo(RepositoryAbstract[UserModel]):
@classmethod
async def connection(cls) -> PostgresConnection:
return await postgres.acquire()
@classmethod
def redis(cls) -> aioredis.Redis:
return redis
@classmethod
def table_name(cls) -> str:
return "users"
@classmethod
def model(cls) -> typing.Type[T]:
return UserModel
@classmethod
def soft_deletes(cls) -> bool:
return True
@classmethod
def default_relations(cls) -> typing.List[str]:
return []
user = await UserRepo.find_by_id(1)
# install pytest
pip install pytest
# run tests
python -m pytest
- 0.0.11: Build and push process now handled by GitLab CI
- 0.0.13: fix - Correct return type of
update_return
method - 0.0.14: Add support for custom order enums
- 0.0.15: Introduce
has_relations
inModelAbstract
- 1.0.0: Introduce
QueryBuilder
andConnection Manager
- 1.0.9: Extend
QueryBuilderAbstract
from PyPika'sPostgreSQLQueryBuilder
- 2.0.0: Drop support for Pydantic v1 and ensure compatibility with Pydantic v2
- 2.0.6: Add
with_thrashed
option tofind_by_id
method - 2.0.14: Fix
dict
method inModelAbstract
for Pydantic v2 compatibility