Skip to main content

qwery

qwery is a small and lightweight query builder based on asyncpg and pydantic.

why a query builder

In my opinion query builders strike a great balance between the flexibility of raw SQL, the structure and safety of pre-crafted queries, and the comfortable data layer of an ORM.

These benefits come with some downsides:

  • You lose some flexibility when crafting queries, especially when dealing with things like partial updates.
  • While the query builder interface does provide some typing, its dynamic nature means it can never match the safety of pre-crafted queries with hand-written or generated types.
  • Complex queries returning non-standard data become unruly fast.

model, queries, helper pattern

qwery works best with a model + queries + helper pattern, namely:

  • Models describe only data and how it is stored
  • Queries describe how models interact with the database
  • Helpers describe and implement the interaction between models and the application (creation, fetching, etc)

example

from pydantic import BaseModel
from qwery import Query


class MyModel(BaseModel):
    class Meta:
        table_name = "my_table"

    id: int
    name: str
    desc: Optional[str]
    active: bool


class MyModelQueries:
    create = Query(MyModel).insert(body=True).execute()
    delete_by_id = Query(MyModel).delete().where("id = {.id}").execute()
    get_by_id = Query(MyModel).select().where("id = {.id}").fetch_one()
    get_all = Query(MyModel).select().fetch_all()


async with pool.acquire() as conn:
    model = MyModel(id=1, name="test", desc=None, active=True)
    await MyModelQueries.create(conn, model=model)

    model = await MyModelQueries.get_by_id(conn, id=1)
    models = await MyModelQueries.get_all(conn)
    assert models == [model]

    await MyModelQueries.delete(conn, id=1)

Release files for qwery 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for qwery 0.0.10
File Size Uploaded
qwery-0.0.10.tar.gz 5.6 kB Details

Release files / qwery-0.0.10.tar.gz

Download URL qwery-0.0.10.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1d1ff162b225d6dae8ef8ac8af71eee99f70e6512b54e197bbed5f313ccd4870
BLAKE2b-256 checksum
How to use checksums
2d1488f40dc5bf9813b767d528ef9ea2ddaf6e4b3e781231337ab8303b55dfd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0

Release history Release notifications | RSS feed

This release

0.0.10 This release

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page