Skip to main content

Base model for SqlAlchemy async orm queries

Project description

SQLAlchemy Async ORM Queries

This is a simple implementation of an asynchronous ORM (Object-Relational Mapping) with SQLAlchemy, designed to work with asynchronous operations in Python. The code provided here demonstrates basic CRUD (Create, Read, Update, Delete) operations using SQLAlchemy's async features.

Installation

pip install sqla-async-orm-queries

Alternatively, if you prefer to use poetry for package dependencies:

poetry shell
poetry add sqla-async-orm-queries

Usage Examples Before using this code, ensure you have the following dependency installed:

Python 3.7 or above

Usage

Create your async engine

import asyncio
from sqlalchemy import Column, String, Integer, and_
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqla_async_orm_queries import Model, init_session


engine = create_async_engine(
    DATABASE_URL,
    echo=True,
)

Create your session. Use async_sessionmaker

SessionLocal = async_sessionmaker(
    expire_on_commit=True,
    class_=AsyncSession,
    bind=engine,
)

Declare your model

class Test(Model):
    __tablename__ = "test"

    id = Column(Integer, primary_key=True, nullable=False)
    country = Column(String())
    name = Column(String())
    surname = Column(String())

Initialized your session

init_session(SessionLocal)

Example of creating an entry

await Test.create({"id": 1, "country": "AZ", "name": "Amrah", "surname": "Baghirov"})

Example of selecting all entries

all_entries = await Test.select_all()

specific_entries = await Test.select_all(Test.name=="Amrah")

Example of selecting one entry

entry = await Test.select_one(Test.country == "AZ")

# You can use and_ & or_ operation
entry = await Test.select_one(and_(Test.country == "AZ", Test.name == "Amrah"))

Example of updating an entry

updated_entry = await Test.update({"name": "Ulvi"}, Test.country == "AZ")

Example of deleting an entry

await Test.delete(Test.country == "AZ")

Example of selecting all entries with pagination

all_entries_pagination = await Test.select_with_pagination(page=1, size=1)

Example of selecting all entries with pagination and args

all_entries_pagination_and_criteria = await Test.select_with_pagination(
    Test.name == "Amrah", page=1, size=1
)

Example of self-updating

entry = await Test.select_one(Test.country == "AZ")
entry.country = "EN"
await entry.apply()

You can check full example in examples folder

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

sqla_async_orm_queries-1.2.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

sqla_async_orm_queries-1.2.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file sqla_async_orm_queries-1.2.0.tar.gz.

File metadata

  • Download URL: sqla_async_orm_queries-1.2.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.8.18 Linux/6.5.0-1024-azure

File hashes

Hashes for sqla_async_orm_queries-1.2.0.tar.gz
Algorithm Hash digest
SHA256 ff174ab28f00e957ca3c6de3a482f1aa5f52070c4a172617029dfc8b20ec56f5
MD5 7a1a5eaed58fd435748bb2e3059b7477
BLAKE2b-256 6dc08938982bc3fcf26441637bf9ff72f8dcd91941fa237568e7e3c1fa2a9467

See more details on using hashes here.

File details

Details for the file sqla_async_orm_queries-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqla_async_orm_queries-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a643e2b2c6b779416ff04ddc7a7cb2f99061efc0b3dcf7b315e3f422905d5ec1
MD5 66febd3437f0ede04bfdfb837e9485d1
BLAKE2b-256 334de68604de0e644d43e2fe515ba386e7ebdbf937c02c0d8f2a6e5391ee6fcf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page