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
  • 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
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())

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.0a2.tar.gz (8.6 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.0a2-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_service-1.0.0a2.tar.gz.

File metadata

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

File hashes

Hashes for sqlalchemy_service-1.0.0a2.tar.gz
Algorithm Hash digest
SHA256 e1ed4d1a9808b3f3c481e8c60a18c49fedbf5706ca7dae6d3e8079f6c908b38e
MD5 e925295c60d7ad573ff98e0dc8812121
BLAKE2b-256 75a41074b4764e0b2f62670e2bdadfc562270b16ac812e8243f1c439b27ee4f5

See more details on using hashes here.

File details

Details for the file sqlalchemy_service-1.0.0a2-py3-none-any.whl.

File metadata

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

File hashes

Hashes for sqlalchemy_service-1.0.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 ef9810277f7e52dc31dd25dec0735e18a21e303c0d22b3dadac9c6d3cefab5c7
MD5 757d1ec1170614d47ec898a31f8df8f4
BLAKE2b-256 77d8b332f7e40e267d77553d749b0b7a3e533f892561b99ddce19062a0d4887e

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