Skip to main content

Backbone ORM

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.

Features

  • 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.

Requirements

  • python 3.10+
  • pypika 0.48+
  • pydantic 2.0+
  • basalam.backbone-redis-cache 0.0.11+

Installation & Upgrade

pip install basalam.backbone-orm --upgrade

Usage

Define Models

from basalam.backbone_orm import ModelAbstract

class UserModel(ModelAbstract):
    id: int
    name: str

Create Repositories

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 []

Perform Queries

user = await UserRepo.find_by_id(1)

Testing

# install pytest
pip install pytest

# run tests
python -m pytest

Changelog

  • 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 in ModelAbstract
  • 1.0.0: Introduce QueryBuilder and Connection Manager
  • 1.0.9: Extend QueryBuilderAbstract from PyPika's PostgreSQLQueryBuilder
  • 2.0.0: Drop support for Pydantic v1 and ensure compatibility with Pydantic v2
  • 2.0.6: Add with_thrashed option to find_by_id method
  • 2.0.14: Fix dict method in ModelAbstract for Pydantic v2 compatibility

Metadata

Release files for basalam.backbone-orm 2.2.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for basalam.backbone-orm 2.2.13
File Size Uploaded
basalam_backbone_orm-2.2.13.tar.gz 21.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for basalam.backbone-orm 2.2.13
File Interpreter ABI Platform
basalam_backbone_orm-2.2.13-py3-none-any.whl Python 3 none any Details

Total release size: 46.9 kB

Release files / basalam_backbone_orm-2.2.13.tar.gz

Download URL basalam_backbone_orm-2.2.13.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1eec14b1bad9e9a66e3c6c0f919e0d38f3d05e345b475d3319376331d519996b
BLAKE2b-256 checksum
How to use checksums
f45e4e3737b964057f0779a20aa7401c768e1a0a122c6891d2fa69981d645e3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / basalam_backbone_orm-2.2.13-py3-none-any.whl

Download URL basalam_backbone_orm-2.2.13-py3-none-any.whl
Size 25.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab30ac354b9fadb0cc792e9703177be247bb5f7b87bddbcc59c8b113a4c4ccda
BLAKE2b-256 checksum
How to use checksums
2a7a717e8b6e229c88fd205a03b39d5bbb2fe6f7948003ac698f8bb4ef226989
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

2.2.13 This release

2 release files

2.2.11

2 release files

2.2.10

2 release files

2.2.9

2 release files

2.2.8

2 release files

2.2.7

2 release files

2.2.6

2 release files

2.2.5

2 release files

2.2.4

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.13

2 release files

2.0.12

2 release files

2.0.11

2 release files

2.0.10

2 release files

1.0.13

2 release files

1.0.12

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page