Skip to main content

Async SQL + Redis wrapper with Mongo-like interface, fire-and-forget SQL background updates.

Project description

asyncsqlpy

asyncsqlpy 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 aiopg and aioredis
  • ๐Ÿง  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.users or db["users"])
  • ๐Ÿงน Safe Shutdown โ€“ Waits for all pending operations before closing

๐Ÿง‘โ€๐Ÿ’ป Installation

pip install asyncsqlpy

โš™๏ธ Quick Start

import asyncio
from asyncsqlpy 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 logging module

๐Ÿงฐ Example Architecture

         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚   Your App     โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                โ”‚
        Async API Calls
                โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚  asyncsqlpy    โ”‚
         โ”‚  (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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncsqlpy-0.1.3.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asyncsqlpy-0.1.3-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file asyncsqlpy-0.1.3.tar.gz.

File metadata

  • Download URL: asyncsqlpy-0.1.3.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for asyncsqlpy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 683cd7c9f6dee47e0169f98db3f91852289c9d1edd8f32fed0bfc4435594ee3d
MD5 990467e90e58608b7ced92cd2fd673f1
BLAKE2b-256 2dd7b198641734cf49f2bb1b43fd010ae26bedbaffb62fe6adecd188bcc4bfee

See more details on using hashes here.

File details

Details for the file asyncsqlpy-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: asyncsqlpy-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for asyncsqlpy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9c61c3265abe0f2effb2c8b5018546c29d0df8c6b1e0ccfa8567e036273401cd
MD5 c94bf88baa0fb7a47ae5ecdb726d58a9
BLAKE2b-256 bf89a915e353ae68f4fb5cc18ac484219cabbc62f2f2ce38da304fa8cc90c95f

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