Menu

1 / 2

I built a 57-line asyncpg wrapper because SQLAlchemy was driving me insane

DEV Community·kernz·6 months ago
#dbRki7zn
#sql#python#sqlalchemy#postgres#users
Reading 0:00
15s threshold

I came from Rust where I used sqlx — you write raw SQL, you get typed structs back. Simple, honest, fast.

Then I had to write Python and reached for SQLAlchemy. Big mistake.

Suddenly I was learning a DSL on top of SQL. Debugging what ORM decided to generate behind my back. Fighting N+1 queries I didn't even know were happening. Writing text() to escape into raw SQL anyway.

So I built EzQL.

What is it?

A minimal wrapper around asyncpg. You write SQL. You get typed Pydantic models back. That's literally it.

The entire core is 57 lines of code.

How it works

Define your model:

from pydantic import BaseModel

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

Enter fullscreen mode

Exit fullscreen mode

Connect and query:

from ezql import create_client

client = await create_client ( user , password , database , host , min_connections = 2 , max_connections = 10 )

# Fire and forget await client .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.