Skip to main content

Psycopg plugin for Litestar

Project description

Litestar Psycopg

PyPI - Version PyPI - Support Python Versions litestar-psycopg PyPI - Downloads linting - 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

pip install litestar-psycopg

Example

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

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])

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

litestar_psycopg-0.1.1.tar.gz (70.7 kB view details)

Uploaded Source

Built Distribution

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

litestar_psycopg-0.1.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file litestar_psycopg-0.1.1.tar.gz.

File metadata

  • Download URL: litestar_psycopg-0.1.1.tar.gz
  • Upload date:
  • Size: 70.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for litestar_psycopg-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f7a087a1606792db8f17dc71f144ad0849edbff1d6287dee06bb94f6037bae80
MD5 70730283a1253343227a37fc5f48204e
BLAKE2b-256 879fd38b26ab4750e262a9552ebf10dcc68371cc03c3f0eaf9292af2644d2649

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_psycopg-0.1.1.tar.gz:

Publisher: publish.yml on Kumokage/litestar-psycopg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file litestar_psycopg-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for litestar_psycopg-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 709f5f96815511cdfbfc3c23c9e521891bc3bb573049d273675de359c554ebf8
MD5 0c5b5d6feffab085a754652ecb586b11
BLAKE2b-256 7a0873562cc824c6170a31db1dbd61f6a69b06f09c4e83301b609fe340a5ede9

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_psycopg-0.1.1-py3-none-any.whl:

Publisher: publish.yml on Kumokage/litestar-psycopg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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