Skip to main content

SQLAlchemy utils for Postgres and Sqlite

Project description

SQLArgon

Test Build License Python Format PyPi Mypy Ruff security: bandit

Wrapper around SQLAlchemy async session, core and Postgres native features


Version: 0.6.13

Documentation: https://asynq-io.github.io/sqlargon/

Repository: https://github.com/asynq-io/sqlargon


About

This library provides glue code to use sqlalchemy async sessions, core queries and orm models from one object which provides somewhat of repository pattern. This solution has few advantages:

  • no need to pass session object to every function/method. It is stored (and optionally injected) in repository object
  • write data access queries in one place
  • no need to import insert,update, delete, select from sqlalchemy over and over again
  • Implicit cast of results to .scalars().all() or .one()
  • Your view model (e.g. FastAPI routes) does not need to know about the underlying storage. Repository class can be replaced at any moment any object providing similar interface.

Usage

import sqlalchemy as sa
from sqlalchemy.orm import Mapped
from sqlargon import GUID, GenerateUUID, Database, Base, SQLAlchemyRepository

db = Database(url=...)

class User(Base):
        id = sa.Column(
            GUID(), primary_key=True, server_default=GenerateUUID(), nullable=False
        )
        name: Mapped[str] = sa.Column(sa.Unicode(255))


class UserRepository(SQLAlchemyRepository[User]):

    async def get_user_by_name(self, name: str):
        # custom user function
        return await self.select().filter_by(name=name).one()

user_repository = UserRepository(db)

# select
await user_repository.all()
await user_repository.list(User.name == "test", User.id >= 18)


# insert
user = await user_repository.insert({"name": "test"}).one()

await user_repository.commit()



# delete
await user_repository.delete().filter(name="John").one()

# custom sqlalchemy core functions

users = await user_repository.select().join(...).filter(
    User.name == "test"
).filter_by(...).order_by(User.created_at).limit(2).all()

Sessions

Manager object needs sqlalchemy.ext.asyncio.AsyncSession, but it's possible to provide the session object by yourself, by subclassing Manager class e.g.

from sqlargon import Database, SQLAlchemyRepository
from fastapi import Depends

db = Database(url="sqlite+aiosqlite:///:memory:")


class UserRepository(SQLAlchemyRepository[User]):
    ...




from fastapi import FastAPI

app = FastAPI()


@app.get("/users")
async def get_users(user_repository: UserRepository = db.Depends(UserRepository)):
    return await user_repository.all()

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

sqlargon-0.6.13.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

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

sqlargon-0.6.13-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file sqlargon-0.6.13.tar.gz.

File metadata

  • Download URL: sqlargon-0.6.13.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for sqlargon-0.6.13.tar.gz
Algorithm Hash digest
SHA256 869e6aa55cc71bc2b88596f639c2497239658d9bb7a2e260450eae7382dd1e4a
MD5 0dfe6ff78d1566fe5839ea8287fb5c5f
BLAKE2b-256 df26634a0fdbb7f8a268ca51b8f9f359b8e16a360d2bdd9d23c1178f3899001f

See more details on using hashes here.

File details

Details for the file sqlargon-0.6.13-py3-none-any.whl.

File metadata

  • Download URL: sqlargon-0.6.13-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for sqlargon-0.6.13-py3-none-any.whl
Algorithm Hash digest
SHA256 c7d4cd8ac691df97d9999dc7596a51be8422c3ac2bd29251f8bb7a4151d90721
MD5 b10e0ceee5d7c7eae5a360142e966d3b
BLAKE2b-256 ae1fefd5c7d6d8096c876bdbdb3a568393db2e3bdd4c751cd7b3434929c77251

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