Skip to main content

Missing library for working with raw SQL in Python/psycopg

Project description

SlenderQL

SlenderQL - Missing library for working with raw SQL in Python/psycopg

PyPI version

Missing raw SQL tooling for python
  • 💯 No ORM, no magic, just SQL
  • 🎨 Highlighted by default in IDEs
  • 🏜️ DRY you queries, without losing control
  • 🧐 Pydantic and Psycopg under the hood


Installation

pip install SlenderQL

or

uv add SlenderQL

Usage

from slenderql.repository import Filter, ILike, GTE, LT, BaseRepository, DB
from pydantic import BaseModel
from datetime import datetime


class User(BaseModel):
    name: str
    email: str
    joined_at: datetime


class UserRepository(BaseRepository):
    model = User

    async def all(
        self,
        query: str | None = None,
        after: datetime | None = None,
        before: datetime | None = None,
        limit: int = 100,
        offset: int = 0,
    ) -> list[User]:
        return await self.fetchall(
            """
            SELECT * FROM users
            WHERE ({name} OR {email})
                AND {after}
                AND {before}
            ORDER BY joined_at DESC
            LIMIT %s OFFSET %s
            """,
            (
                ILike("name", query),
                ILike("email", query),
                GTE("after", after, "joined_at"),
                LT("before", before, "joined_at"),
                limit,
                offset,
            )
        )


class RepostoryManager(DB):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.users = UserRepository(self)

db = RepostoryManager(
    f"user=postgres "
    f"password=postgres "
    f"host=postgres "
    f"dbname=postgres"
)

now you can use all() method with multiple filters

>>> await db.users.all(query="alex")
>>> await db.users.all(query="alex", after=datetime(2023, 1, 1))
>>> await db.users.all(query="alex", after=datetime(2023, 1, 1), before=datetime(2023, 2, 1))
>>> await db.users.all(before=datetime(2023, 2, 1), limit=10)

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

slenderql-0.1.4.tar.gz (36.0 kB view details)

Uploaded Source

Built Distribution

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

slenderql-0.1.4-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file slenderql-0.1.4.tar.gz.

File metadata

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

File hashes

Hashes for slenderql-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d3d073b54ec0d812895058c7bc89e559f3ad9b3990116361df23f077b855722f
MD5 12b0aac64bafe10cf7715b5e85166c74
BLAKE2b-256 05bb5d64ed9ad529e3048e975b9a7199cad4cef5911ec346cc221e90502d17d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for slenderql-0.1.4.tar.gz:

Publisher: publish.yml on hati-health/slender

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

File details

Details for the file slenderql-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: slenderql-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for slenderql-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 23d796d2748178beb521ebd4993b77b02fbb4c331656e5806b8951f528c71000
MD5 8db105bae4cbcf67caf3e2c1bab6b6a4
BLAKE2b-256 a409d3080e7efbc98a12fe987a03bad39a29762b8dc9e1dbf71b8f66e4009757

See more details on using hashes here.

Provenance

The following attestation bundles were made for slenderql-0.1.4-py3-none-any.whl:

Publisher: publish.yml on hati-health/slender

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