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.1.tar.gz (4.9 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.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ezql-0.1.1.tar.gz
  • Upload date:
  • Size: 4.9 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.1.tar.gz
Algorithm Hash digest
SHA256 5a8dd1458de5de5de757a399dc5e70cd182d04e24c29f9c97136fa911b73bb80
MD5 2cb0f007ab88fa741cae613c3377e70e
BLAKE2b-256 b3cf5d838bc5a993d4c99504bb5f6e0b6f744bc815b7f9ead4ab2c8385e7b89d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ezql-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7e1736d765dff4dc07b4f558eacd196c43340be1f7ec99530bbefa7479a652f5
MD5 573576ede1c20de792a538f42a19ce10
BLAKE2b-256 536ac39708dd6d469be0134035be2446f9ae83f689403a512e15dac31ea96e35

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