derp.orm.engine – Database Engine¶
Async query engine (asyncpg wrapper) for Derp ORM.
- class derp.orm.engine.Transaction[source]¶
Bases:
_QueryBaseTransaction context manager with query builder support.
Queries created via this transaction’s
select,insert,update, anddeletemethods reuse the transaction’s connection instead of acquiring a new one from the pool.Example:
async with db.transaction() as txn: user = await txn.insert(User).values(name="Alice").returning(User).execute() await txn.update(Profile).set(user_id=user.id).execute()
- class derp.orm.engine.DatabaseEngine[source]¶
Bases:
_QueryBaseMain async database engine for Derp ORM.
Example
db = DatabaseEngine(“postgresql://user:pass@localhost:5432/mydb”)
- async with db:
users = await db.select(User).where(User.name == “Alice”).execute()
# Or manual lifecycle await db.connect() users = await db.select(User).execute() await db.disconnect()
- __init__(dsn, *, min_size=2, max_size=10, statement_cache_size=None)[source]¶
Initialize Derp engine.
- Parameters:
dsn (str) – PostgreSQL connection string
min_size (int) – Minimum connection pool size
max_size (int) – Maximum connection pool size
statement_cache_size (int | None) – Size of the prepared statement cache per connection. Set to 0 to disable, which is required when connecting through PgBouncer in transaction mode. None uses asyncpg’s default.
- set_cache(store)[source]¶
Set the KV store for query result caching.
- Parameters:
store (KVClient | None)
- Return type:
None
- set_router(router)[source]¶
Set the replica router for automatic read routing.
- Parameters:
router (ReplicaRouter | None)
- Return type:
None
- property pool: Pool¶
Get the connection pool.
- async execute(query, params=None)[source]¶
Execute a raw SQL query.
- Parameters:
- Returns:
List of row dicts
- Return type:
Example
result = await db.execute(“SELECT * FROM users WHERE id = $1”, [user_id])
- transaction()[source]¶
Start a transaction.
Example
- async with db.transaction():
await db.insert(User).values(…).execute() await db.update(Post).set(…).execute()
- Return type:
- acquire()[source]¶
Acquire a connection from the pool.
Example
- async with db.acquire() as conn:
await conn.execute(”…”)
- Return type:
AsyncIterator[Connection]