Skip to main content

No project description provided

Project description

Sqlalchemy-service

This is a library that simplifies working with database CRUD queries and connection management.

Features

  • A class to reduce the amount of code needed for database CRUD queries and connection management.

Installation

  • pip install sqlalchemy-service[postresql]
  • pip install sqlalchemy-service[mysql]
  • pip install sqlalchemy-service[postgresql,fastapi] for fastapi support(http exceptions and dependency injection)

Usage

  • Need environment set: POSTGRES_HOST, POSTGRES_DATABASE, POSTGRES_PASSWORD, POSTGRES_USER
import asyncio
from random import randint

from fastapi import Response
from pydantic import BaseModel
from sqlalchemy import ScalarResult
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column as column

from sqlalchemy_service import Base
from sqlalchemy_service import BaseService
from sqlalchemy_service.base_db.base import ServiceEngine
from sqlalchemy_service.base_db.db_configure import MySQLDBConfiguration


class User(Base):
    __tablename__ = "users"
    id: Mapped[int] = column(primary_key=True)
    name: Mapped[str]


class UserCreateSchema(BaseModel):
    name: str


class UserUpdateSchema(BaseModel):
    name: str | None = None


# You can manually set urls for engines, or use autodetect
engine_1 = ServiceEngine()
engine_2 = ServiceEngine(MySQLDBConfiguration().get_url())


class UserService[Table: User, int](BaseService):
    base_table = User
    engine = engine_1

    # Redefine BaseService.get_session to connect with 2 engines
    # Just don't rewrite it to connect with one engine
    async def get_session(self):
        if randint(0, 1):
            return engine_1
        return engine_2

    # Redefine BaseService.depend to use dependencies in your service.
    # Just don't rewrite it to use service without dependencies
    @classmethod
    async def depend(cls, response: Response = Response):
        async with cls(response) as service:
            yield service


    async def create(self, schema: UserCreateSchema) -> User:
        return await self._create(schema)

    async def list(self, page=None, count=None) -> ScalarResult[User]:
        return await self._get_list(page=page, count=count)

    async def get(self, user_id: int) -> User:
        """Return user. If user not found, throws 404 HTTPException"""
        return await self._get_one(id=user_id)

    async def update(self, user_id: int, schema: UserUpdateSchema) -> User:
        return await self._update(user_id, schema)

    async def delete(self, user_id: int):
        await self._delete(user_id)

    async def count(self) -> int:
        return await self._count()

    async def count_with_name_like(self, name: str) -> int:
        query = self._count_query()
        query = self._query_like_filter(query, name=name)
        return await self.session.scalar(query)


async def main():
    # Use alembic to create database schema
    async with UserService() as service:
        print(await service.create(UserCreateSchema(name="test")))


asyncio.run(main())

# or in fastapi apps

from fastapi import FastAPI
from fastapi import Depends


app = FastAPI()


@app.get('/users/{user_id}')
async def get_users(
        user_id: int,
        user_service: UserService = Depends(UserService.depend)
):
    return await user_service.get(user_id)


@app.post('/users')
async def create_user(
        schema: UserCreateSchema,
        user_service: UserService = Depends(UserService.depend)
):
    user = await user_service.create(schema)
    await user_service.refresh()  # Need to calculate user ID
    return user

Updates

1.0.9

  • Bug fix get list query pagination

1.0.7

  • We decide that POSTGRES_<config> is not deprecated and is used by postgres docker image.

1.0.5:

  • FastAPI depends support

1.0.0:

  • custom engine style
  • more use-ready commands

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

sqlalchemy_service-1.0.9.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_service-1.0.9-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_service-1.0.9.tar.gz.

File metadata

  • Download URL: sqlalchemy_service-1.0.9.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.13.2 Linux/6.13.5-200.fc41.x86_64

File hashes

Hashes for sqlalchemy_service-1.0.9.tar.gz
Algorithm Hash digest
SHA256 8d7799df08e161d2fb0fa31f2aa53a614253d133dd484c5c7ae9fc2fe2051046
MD5 938997a12c4cf98b75a9859c8f8ae2ab
BLAKE2b-256 fed1568ee44ac45b724b11e74ab0e56d6121a539c99a097f2d3124c18ad3a7ec

See more details on using hashes here.

File details

Details for the file sqlalchemy_service-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: sqlalchemy_service-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.13.2 Linux/6.13.5-200.fc41.x86_64

File hashes

Hashes for sqlalchemy_service-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 3ccfc3b8fcc7440b8ebadcf787943af7e0f9f6249354a837169747ebb9378164
MD5 dd381df051f0882d2854a4e843fc85c3
BLAKE2b-256 0d64e12b577f59ee4123dddc82c7efdeee9a2d5d50d8a2feb3e4440767f37b7b

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