Metadata-Version: 2.4
Name: litestar-psycopg
Version: 0.1.1
Summary: Psycopg plugin for Litestar
Project-URL: Issue, https://github.com/kumokage/litestar-psycopg/issues/
Project-URL: Source, https://github.com/kumokage/litestar-psycopg
Author-email: Alex Glushko <alextvrus@gmail.com>
License: Apache-2.0
License-File: LICENSE
Keywords: litestar,psycopg
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Topic :: Database :: Database Engines/Servers
Classifier: Topic :: Software Development
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: litestar>=2.16.0
Requires-Dist: psycopg[binary,pool]>=3.2.7
Description-Content-Type: text/markdown

# Litestar Psycopg

[![PyPI - Version](https://img.shields.io/pypi/v/litestar-psycopg?labelColor=202235&color=edb641&logo=python&logoColor=edb641)](https://badge.fury.io/py/litestar-psycopg) ![PyPI - Support Python Versions](https://img.shields.io/pypi/pyversions/litestar-psycopg?labelColor=202235&color=edb641&logo=python&logoColor=edb641) ![litestar-psycopg PyPI - Downloads](https://img.shields.io/pypi/dm/litestar-psycopg?logo=python&label=package%20downloads&labelColor=202235&color=edb641&logoColor=edb641) [![linting - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json&labelColor=202235)](https://github.com/astral-sh/ruff)
A barebones Psycopg plugin for Litestar. This plugin is useful for when you plan to use no ORM or need to manage the postgres connection separately.

## Usage

### Installation

```shell
pip install litestar-psycopg
```

### Example

Here is a basic application that demonstrates how to use the plugin.

```python
from __future__ import annotations

from typing import TYPE_CHECKING

import msgspec
from litestar import Controller, Litestar, get
from litestar.exceptions import InternalServerException

from litestar_psycopg import PsycopgConfig, PsycopgPlugin, AsyncConnectionPoolConfig

if TYPE_CHECKING:
    from psycopg import AsyncConnection


class PostgresHealthCheck(msgspec.Struct):
    """A new type describing a User"""

    version: str
    uptime: float


class SampleController(Controller):
    @get(path="/sample")
    async def sample_route(self, db_connection: AsyncConnection) -> PostgresHealthCheck:
        """Check database available and returns app config info."""
        cursor = await db_connection.execute(
            "select version() as version, extract(epoch from current_timestamp - pg_postmaster_start_time()) as uptime",
        )
        result = await cursor.fetchone()
        if result:
            return PostgresHealthCheck(version=result[0], uptime=result[1])
        raise InternalServerException


psycopg = PsycopgPlugin(
    config=PsycopgConfig(
        pool_config=AsyncConnectionPoolConfig(
            conninfo="postgresql://app:app@localhost:5432/app"
        )
    )
)
app = Litestar(plugins=[psycopg], route_handlers=[SampleController])
```
