Metadata-Version: 2.3
Name: sqlalchemy-service
Version: 0.2.7
Summary: 
License: LICENSE
Keywords: sqlalchemy
Author: Lawrence Naumov
Author-email: <prostolawr@gmail.com>
Maintainer: Lawrence Naumov
Maintainer-email: <prostolawr@gmail.com>
Requires-Python: >=3.9
Classifier: License :: Other/Proprietary License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Provides-Extra: fastapi
Requires-Dist: alembic (>=1.14.0,<1.15.0)
Requires-Dist: asyncpg (>=0.30.0,<0.31.0)
Requires-Dist: fastapi (>=0.115.8,<0.116.0) ; extra == "fastapi"
Requires-Dist: loguru (>=0.7.3,<0.8.0)
Requires-Dist: pydantic-settings (>=2.7.1,<2.8.0)
Requires-Dist: sqlalchemy (>=2.0.37,<2.1.0)
Project-URL: Homepage, https://github.com/Dianapp-online/sqlalchemy-service
Project-URL: Issues, https://github.com/Dianapp-online/sqlalchemy-service/issues
Description-Content-Type: text/markdown

# 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`
- `pip install sqlalchemy-service[fastapi]` for fastapi support(http exceptions and dependency injection)

## Usage
- Need environment set: POSTGRES_HOST, POSTGRES_DB, POSTGRES_PASSWORD, POSTGRES_USER
```python3
from sqlalchemy_service import BaseService, Base, init_models
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column as column
from pydantic import BaseModel
import asyncio


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


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

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

    async def list(self, page=None, count=None) -> list[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 main():
    await init_models()
    async with UserService() as service:
        print(await service.create(UserCreateSchema(name="test")))


asyncio.run(main())
```

