Skip to main content

TinyDB bridge implementation for asyncio-based applications

Project description

Alt Text

tinybridge

TinyDB bridge implementation for asyncio-based applications.

AIOBridge is an async-safe adapter for TinyDB, inspired by its design and intended for use within asyncio-based concurrent tasks. It enables safe and structured interaction with TinyDB from asynchronous Python code.

Key capabilities

  • Implements an async context manager for automatic resource handling
  • Ensures concurrency safety via a shared asyncio.Lock per DB file path
  • Executes all TinyDB operations using asyncio.to_thread() for non-blocking behavior
  • Provides functional-style error handling via Result objects
  • Supports configurable per-operation timeouts for robustness under load

Installation

Install via pip:

pip install tinybridge

Or using uv:

uv add tinybridge

tinybridge depends on tinydb and result, both installed automatically as dependencies.

Configuration

AIOBridge accepts the following options during initialization:

Parameter Type Default Description
path str Path to the TinyDB JSON file
timeout int 10 Timeout (in seconds) applied to each operation
tinydb_class type TinyDB Optional class to override the default TinyDB implementation
**kwargs dict Additional keyword arguments passed to the TinyDB constructor

Customizing tinydb_class

You can override the default TinyDB class either by passing a custom class directly or by subclassing AIOBridge.

Via tinydb_class argument

from tinybridge import AIOBridge
from tinydb import TinyDB
from tinydb.storages import MemoryStorage

class InMemoryTinyDB(TinyDB):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, storage=MemoryStorage, **kwargs)

async with AIOBridge("db.json", tinydb_class=InMemoryTinyDB) as db:
    ...

By subclassing AIOBridge

from tinybridge import AIOBridge
from tinydb import TinyDB
from tinydb.storages import JSONStorage
from tinydb.middlewares import CachingMiddleware

class CustomTinyDB(TinyDB):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, storage=CachingMiddleware(JSONStorage), **kwargs)

class CustomAIOBridge(AIOBridge):
    tinydb_class = CustomTinyDB

Usage Example

Minimal example demonstrating asynchronous insert:

import asyncio
from tinybridge import AIOBridge

async def main():
    async with AIOBridge("db.json") as db:
        result = await db.insert({"name": "Alice"})
        if result.is_ok():
            print("Inserted:", result.unwrap())
        else:
            print("Insert failed:", result.unwrap_err())

asyncio.run(main())

All standard TinyDB operations—such as get, search, update, and remove—are supported. Each method is wrapped for async compatibility and returns a Result object for safe error handling.

Similar Projects

Alternative community-driven efforts to add async capabilities to TinyDB:

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

tinybridge-0.1.2.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

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

tinybridge-0.1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file tinybridge-0.1.2.tar.gz.

File metadata

  • Download URL: tinybridge-0.1.2.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.18

File hashes

Hashes for tinybridge-0.1.2.tar.gz
Algorithm Hash digest
SHA256 887c5f0a2a9b76b4a54da5d51a043367de384f6ca7468fada10dd6cd22784489
MD5 0e295e392dac1d20d27853fd845ab1f3
BLAKE2b-256 0e0adcfe107096d7e0fdbd8113042dd10e778aecce569f94eb49705f9aa0751b

See more details on using hashes here.

File details

Details for the file tinybridge-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tinybridge-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.18

File hashes

Hashes for tinybridge-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c9320f5ddb5e1aca0500aec8a09420a41e7748d05e65054e720a603c3a21e944
MD5 da80031beee1471b6088a8ac397cae15
BLAKE2b-256 32ae547729da61378a8f4326d12013fc5f875d8428133c3eee98de158095671f

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