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:

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 conn:
    await conn.execute("INSERT INTO users (name) VALUES ($1)", "Nazar")
    await conn.execute("INSERT INTO posts (user_id, title) VALUES ($1, $2)", 1, "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 

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.0.tar.gz (4.8 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.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ezql-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 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.0.tar.gz
Algorithm Hash digest
SHA256 782ad9b354297574d7e195639cbb48dbf2a17cb822efed443ae7f397646ee0e8
MD5 5481c89bb48b5e5b6232ea898fe7f839
BLAKE2b-256 9861da4448b5cc777e73ad2a27753b61b92b0dd08fb59cace41e2394f13355a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ezql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43cdc6bf6c4267163568b6b2119b873852a63dc29fe3fc2fcdeef673492809da
MD5 6e7edb464f3230830876996a393b4e15
BLAKE2b-256 0825ae47c99ccbf51803d9fc75eea164516c4d04c242fa368645dc6d225c19c8

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