Skip to main content

Python Utilities & Basalam Micro-Services SDK

Project description

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

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

basalam_backbone_orm-2.2.4.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

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

basalam_backbone_orm-2.2.4-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file basalam_backbone_orm-2.2.4.tar.gz.

File metadata

  • Download URL: basalam_backbone_orm-2.2.4.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for basalam_backbone_orm-2.2.4.tar.gz
Algorithm Hash digest
SHA256 265d0d3ad24579cd8c139352ec4103d377359918a23550d474456257e0269372
MD5 3ef52a817774f8fe3a708552bdb57d2d
BLAKE2b-256 e8436098e5ce1567ea8fdac5a37ac0f2ef8c1f38e2e3b22f58a0f620770fc7d0

See more details on using hashes here.

File details

Details for the file basalam_backbone_orm-2.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for basalam_backbone_orm-2.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 408941e4a5318db4b75a286e79a3333b7237c0f53f3896285e8a68cfbb75c8ab
MD5 7e80efc3750e10724ca8c58f07c6f9df
BLAKE2b-256 e2f8cf94ae97ebcc89c54974a15acbba2116a780054d3954749323e5688236d4

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