Async SQL + Redis wrapper with Mongo-like interface, fire-and-forget SQL background updates.
Project description
PyAsyncSQL
PyAsyncSQL is an ultra-fast, asynchronous SQL layer that mimics MongoDB-style operations with full Redis caching and background write queue support.
Itโs designed for high-performance systems that need MongoDB-like flexibility over PostgreSQL.
๐ Features
- โก Async I/O โ Fully asynchronous using
aiopgandaioredis - ๐ง MongoDB-like syntax โ Use familiar methods like
find_one,insert_one,update_many, etc. - ๐ Redis caching โ Automatic query caching with TTL
- ๐งฉ Background SQL Worker Queue โ Batched inserts/updates/deletes to reduce I/O overhead
- ๐ Pub/Sub Watcher โ Real-time change streaming via Redis channels
- ๐ฅ Automatic Retry + Reconnect โ Fault-tolerant retry for transient SQL/Redis errors
- ๐งฐ Dynamic Collections โ Access collections as attributes or subscripts (e.g.
db.usersordb["users"]) - ๐งน Safe Shutdown โ Waits for all pending operations before closing
๐งโ๐ป Installation
pip install pyasyncsql
โ๏ธ Quick Start
import asyncio
from pyasyncsql import AsyncSqlDB
DSN = "postgres://user:password@hostname:port/dbname?sslmode=require"
REDIS_URL = "redis://localhost:6379"
async def main():
db = AsyncSqlDB(DSN, REDIS_URL)
await db.connect()
users = db["users"]
# Insert
await users.insert_one({"name": "Alice", "age": 25})
# Find
user = await users.find_one({"name": "Alice"})
print(user)
# Update
await users.update_many({"name": "Alice"}, {"$set": {"age": 26}})
# Delete
await users.delete_one({"name": "Alice"})
# Close safely (waits for background queue to finish)
await db.close()
asyncio.run(main())
๐ Watcher (Real-Time Stream)
import asyncio
async def listener(data):
print("Database change detected:", data)
async def watch_changes():
db = AsyncSqlDB(DSN, REDIS_URL)
await db.connect()
users = db["users"]
await users.watch(listener)
asyncio.run(watch_changes())
๐งฎ API Reference
AsyncSqlDB
| Method | Description |
|---|---|
connect() |
Initialize PostgreSQL + Redis connection |
close() |
Gracefully close and flush all background tasks |
__getitem__ / __getattr__ |
Access collection dynamically |
AsyncMongoLikeCollection
| Method | Description |
|---|---|
find_one(filter) |
Fetch one document |
find(filter) |
Return list of matching documents |
insert_one(doc) |
Insert a new document |
insert_many(docs) |
Insert multiple documents |
update_one(filter, update) |
Update a single document |
update_many(filter, update) |
Update multiple documents |
delete_one(filter) |
Delete a single document |
delete_many(filter) |
Delete multiple documents |
count_documents(filter) |
Count matching documents |
watch(callback) |
Listen for real-time change events via Redis |
๐งฑ Background Worker Queue
All write operations (insert/update/delete) are queued and flushed in batches to PostgreSQL, improving throughput dramatically under high load.
await db.close() # ensures all batched operations are written before shutdown
โ ๏ธ Error Handling
- Automatic retries with exponential backoff for SQL and Redis operations
- Transparent reconnects for transient connection failures
- Warnings are logged via Pythonโs
loggingmodule
๐งฐ Example Architecture
โโโโโโโโโโโโโโโโโโ
โ Your App โ
โโโโโโโโฌโโโโโโโโโโ
โ
Async API Calls
โ
โโโโโโโโผโโโโโโโโโโ
โ PyAsyncSQL โ
โ (Mongo-like) โ
โโโโโโโโฌโโโโโโโโโโ
โ
โโโโโโโโโโโโดโโโโโโโโโโโโ
โ โ
โโโโโโผโโโโโโ โโโโโโผโโโโโ
โPostgreSQLโ โ Redis โ
โ(storage) โ โ(cache + โ
โ โ โ pub/sub)โ
โโโโโโโโโโโโ โโโโโโโโโโโ
๐งพ License
MIT License ยฉ 2025 Sathishzus
๐ฌ Author
Sathishzus โ Open Source Systems & Cloud Performance Tools
๐ GitHub | ๐ Website
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asyncsqlpy-0.1.8.tar.gz.
File metadata
- Download URL: asyncsqlpy-0.1.8.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
381791c872d0e31e42476e50c50f223eb0b5fd64cf723731ef311ac17e49e228
|
|
| MD5 |
09e7a9ecc58554ce25d536234f3b67a3
|
|
| BLAKE2b-256 |
6a9b22c67cdaa84450012d6e71d212399677280bccbd9a5c74abed05fca43303
|
File details
Details for the file asyncsqlpy-0.1.8-py3-none-any.whl.
File metadata
- Download URL: asyncsqlpy-0.1.8-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
727a45f98dffcc5fe1f4b0bd91bb6e4b0ee4562cc96c38a5b35094752996df79
|
|
| MD5 |
bec98d3e31fd0cd7b5c6f4768de1c4cb
|
|
| BLAKE2b-256 |
67ac2bb4fda0771d2e538c8442237eef62d83fd92214eb37ca6f6b414b876226
|