Skip to main content

An asynchronous ORM inspired by Django's ORM, built on top of SQLAlchemy.

Project description

AsyncDjangoORM

AsyncDjangoORM is an asynchronous ORM inspired by Django's ORM, built on top of SQLAlchemy. It provides Django-like Querysets and AsyncManagers, allowing you to interact with databases using Python async/await.

Ideal for telegram bots while building application with aiogram

Features

Full async support using async/await.

Django-style Queryset and AsyncManager.

CRUD operations: get, create, get_or_create, update_or_create.

Query methods: filter, exclude, order_by, annotate, aggregate, bulk_create, bulk_update, bulk_delete.

Relation handling: select_related and prefetch_related.

Supports PostgreSQL, MySQL, and SQLite.

Lightweight, flexible, and easy to integrate.

Installation

Install via pip:

pip install asyncdjangoorm


# PostgreSQL
pip install asyncdjangoorm[postgres]

# MySQL
pip install asyncdjangoorm[mysql]

# SQLite (default)
pip install asyncdjangoorm[sqlite]

Database Configuration

SQLite (default)

export DATABASE_URL="sqlite+aiosqlite:///./mydb.db"

PostgreSQL (asyncpg)

export DATABASE_URL="postgresql+asyncpg://user:password@localhost:5432/mydb"

MySQL (aiomysql)

export DATABASE_URL="mysql+aiomysql://user:password@localhost:3306/mydb"

Getting Started with asyncdjangoorm

asyncdjangoorm is an asynchronous ORM inspired by Django, built on top of SQLAlchemy. It integrates easily with aiogram for Telegram bots.


1️⃣ Install the package

pip install asyncdjangoorm

2️⃣ Define Models

You can define your models using SQLAlchemy and attach AsyncManager for async operations.

# models.py
from sqlalchemy import Column, Integer, String
from asyncdjangoorm import TimeStampedModel, AsyncManager

class MyModel(TimeStampedModel):
    __tablename__ = "my_model"
    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True)
    value = Column(Integer)

# Attach AsyncManager
MyModel.objects = AsyncManager(MyModel)

TimeStampedModel automatically adds created_at and updated_at fields.


3️⃣ Initialize the Database

Before using your models, initialize all tables:

# init_db_example.py
import asyncio
from asyncdjangoorm import init_db
from models import MyModel

async def main():
    await init_db()  # Create all tables

if __name__ == "__main__":
    asyncio.run(main())

4️⃣ Using the ORM

Basic usage examples:

# usage_example.py
import asyncio
from models import MyModel

async def main():
    # Create a new object
    await MyModel.objects.create(name="Test", value=42)

    # Fetch all objects
    items = await MyModel.objects.all()
    print(items)

    # Filter objects
    filtered = await MyModel.objects.filter(value__gt=10)
    print(filtered)

    # Get or create an object
    obj, created = await MyModel.objects.get_or_create(name="Example")
    print(obj, "Created:", created)

if __name__ == "__main__":
    asyncio.run(main())

5️⃣ Integrating with aiogram

Example Telegram bot using asyncdjangoorm:

# bot_example.py
import asyncio
from aiogram import Bot, Dispatcher, types
from asyncdjangoorm import init_db
from models import MyModel

BOT_TOKEN = "YOUR_BOT_TOKEN"

bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()

async def on_startup():
    await init_db()  # Initialize tables

@dp.message_handler(commands=["create"])
async def create_item(message: types.Message):
    await MyModel.objects.create(name="FromBot", value=99)
    await message.answer("Item created!")

@dp.message_handler(commands=["list"])
async def list_items(message: types.Message):
    items = await MyModel.objects.all()
    text = "\n".join(f"{item.id}: {item.name} = {item.value}" for item in items) or "No items found."
    await message.answer(text)

if __name__ == "__main__":
    asyncio.run(on_startup())
    dp.run_polling(bot)

6️⃣ Notes

  • AsyncManager allows async CRUD operations, filtering, ordering, and annotations.

  • Compatible with all Python 3 versions and aiogram 2.x and 3.x.

  • You can attach AsyncManager to any SQLAlchemy model.

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

asyncdjangoorm-0.1.5.tar.gz (252.8 kB view details)

Uploaded Source

File details

Details for the file asyncdjangoorm-0.1.5.tar.gz.

File metadata

  • Download URL: asyncdjangoorm-0.1.5.tar.gz
  • Upload date:
  • Size: 252.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for asyncdjangoorm-0.1.5.tar.gz
Algorithm Hash digest
SHA256 8fc59990510f76fae4cf1f1ca6f0c1b586f05297f6460d4936eafc1d29ae8ac5
MD5 c0cd69d724d0c7936abaf6a5f68f3478
BLAKE2b-256 cf217df0b4bd2102f89dfdfe278a33d2831bcaba9a8d81daea32fb053883469d

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