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 up your 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.5.tar.gz (39.3 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.5-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: slenderql-0.1.5.tar.gz
  • Upload date:
  • Size: 39.3 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.5.tar.gz
Algorithm Hash digest
SHA256 b54ef1cdb2cc90fb580609e73546bd998187353333d8f0ff62d958e6761b1134
MD5 61bfd3414f4e40b52ac9eb6183990165
BLAKE2b-256 f318bcced0c8500603ce18fa632e048d97dfef728c8e3119bb2795ead5a6ba0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for slenderql-0.1.5.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.5-py3-none-any.whl.

File metadata

  • Download URL: slenderql-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.7 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 72787e9cf3320f86f6dc049ed469861ed38d14d476c9e0767a7f12c676bcb7bc
MD5 a507467f1b20116cb95700957382ab00
BLAKE2b-256 89cbb7591561cbc46a49a913d015c68c715af4a183eebef53e121cb15afd5dd3

See more details on using hashes here.

Provenance

The following attestation bundles were made for slenderql-0.1.5-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