Skip to main content

EZQL - simple wrapper around asyncpg with type safety checks and CLI validator

Project description

EzQL

EzQL - a simple wrapper around asyncpg that makes writing raw SQL in python a little easier

SQLAlchemy is overkill and you need something simpler? Have you tried writing raw SQL in Python but ended up in tears because there's no type safety? Then EzQL is your choice

Why use another bloated ORM when you can interact with your database in the most intuitive and simple way?

Good place to start

PostgreSQL is an open-source relational database beloved by most developers for its reliability, performance, and rich feature set - from advanced indexing and full-text search to JSON support and powerful extensions like PostGIS.

If you're new to PostgreSQL, here are the best places to start:

Installation

uv add ezql
pip install ezql

Example

import asyncio
from pydantic import BaseModel
from ezql import create_client

class User(BaseModel):
    __table__ = "users" # Marks this model as an EzQL model

    id: int
    name: str

async def main():
    client = await create_client(
        user,
        password,
        database,
        host,
        min_connections,
        max_connections
    )

    # Does not return anything
    await client.execute("INSERT INTO users (name) VALUES ($1)", "Nazar")

    # Returns either list of users or an empty list
    users = await client.query_as(User, "SELECT id, name FROM users WHERE name = $1", "Nazar")

    assert users[0].name == "Nazar"
    assert len(users) == 1
    assert isinstance(users[0], User)


if __name__ == "__main__":
    asyncio.run(main())

Joins

class UserWithPosts(BaseModel):
    # No __table__ — this is a DTO, not a table model
    user_name: str
    post_title: str

users_with_posts = await client.query_as(UserWithPosts, """
    SELECT users.name as user_name, posts.title as post_title
    FROM users
    JOIN posts ON posts.user_id = users.id
    WHERE users.id = $1
""", user_id)

[!WARNING] Always select columns explicitly when using joins. Using SELECT * may cause a ValidationError at runtime.

No magic - you write the SQL, EzQL maps the result.

Transactions

async with client.transaction() as tx:
    user = await tx.query_as(User, "INSERT INTO users (name) VALUES ($1) RETURNING id, name", "Nazar")
    await tx.execute("INSERT INTO posts (user_id, title) VALUES ($1, $2)", user[0].id, "My first post")

If any query fails, the entire transaction is rolled back automatically.

Validate your models before production blows up

ezql ./models-dir --dsn postgresql://test:testpassword@localhost:5432/test
Found 1 models. Validating against DB...

Validating User  table users
┏━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━┓
 Field  Model type     DB type  Status 
┡━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━┩
 id     <class 'int'> │ integer │ ✓      │
 name   <class 'str'> │ text    │ ✓      │
└───────┴───────────────┴─────────┴────────┘

All models are valid 

TODO

  • Migration tool
  • Validate queries

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

ezql-0.1.3.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

ezql-0.1.3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file ezql-0.1.3.tar.gz.

File metadata

  • Download URL: ezql-0.1.3.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ezql-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a262c9facdc9c26214252f4b38425541e50416e4f67c50f12af8dc323d0a40f6
MD5 7c2cf8a3cc126aad0d37f75c756c05c8
BLAKE2b-256 b177fffb443fd9b0ac3ef4ed29a53541501e3a7be58dfc3b609ff5cb0b950193

See more details on using hashes here.

File details

Details for the file ezql-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ezql-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ezql-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9a11a4a7825ddbe34929c7a021207cd0102f1e9f318bbe9243a0ed7884878f84
MD5 ba6fe9b8e1fc53b1ab9b273202c6e2c1
BLAKE2b-256 aee82657f4fb0c17e0b041485b41f1ba028848d3dc9c4c05b72bcb110d7b6b6d

See more details on using hashes here.

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