Skip to main content

Communicates with databases using repository pattern and service patterns

Project description

dbdaora

dbdaora

Communicates with NoSQL (and SQL for future) databases using repository and service patterns and python dataclasses


Documentation: https://dutradda.github.io/dbdaora/

Source Code: https://github.com/dutradda/dbdaora


Key Features

  • Creates an optional service layer with cache and circuit breaker

  • Supports for redis data types:

    • Hash
    • Sorted Set
    • (Others data types are planned)
  • Backup redis data into other databases:

    • Google Datastore
    • Mongodb (soon)
    • SQL databases with SQLAlchemy (soon)
    • (Others data bases are planned)
  • Support for other databases are in development.

Requirements

  • Python 3.8+

  • jsondaora for data validation/parsing

  • circuitbreaker

  • cachetools

  • Optionals:

    • aioredis
    • google-cloud-datastore

Instalation

$ pip install dbdaora

Simple redis hash example

import asyncio
from dataclasses import dataclass

from dbdaora import (
    DictFallbackDataSource,
    DictMemoryDataSource,
    HashRepository,
)


@dataclass
class Person:
    id: str
    name: str
    age: int


def make_person(name: str, age: int) -> Person:
    return Person(name.replace(' ', '_').lower(), name, age)


class PersonRepository(HashRepository[Person, str]):
    key_attrs = ('id',)


repository = PersonRepository(
    memory_data_source=DictMemoryDataSource(),
    fallback_data_source=DictFallbackDataSource(),
    expire_time=60,
)
person = make_person('John Doe', 33)
asyncio.run(repository.add(person))

geted_person = asyncio.run(repository.query(person.id).entity)
print(geted_person)
Person(id='john_doe', name='John Doe', age=33)

Simple redis sorted set example

import asyncio

from dbdaora import (
    DictFallbackDataSource,
    DictMemoryDataSource,
    SortedSetEntity,
    SortedSetRepository,
)


class Playlist(SortedSetEntity):
    id: str


class PlaylistRepository(SortedSetRepository[Playlist, str]):
    ...


repository = PlaylistRepository(
    memory_data_source=DictMemoryDataSource(),
    fallback_data_source=DictFallbackDataSource(),
    expire_time=60,
)
data = [('m1', 1), ('m2', 2), ('m3', 3)]
playlist = Playlist(id='my_plalist', data=data)
asyncio.run(repository.add(playlist))

geted_playlist = asyncio.run(repository.query(playlist.id).entity)
print(geted_playlist)
Playlist(id='my_plalist', data=[b'm1', b'm2', b'm3'], max_size=None)

Using the service layer

The service layer uses the backup dataset when redis is offline, opening a circuit breaker.

It has an optional cache system too.

import asyncio
from dataclasses import dataclass

from dbdaora import (
    DictFallbackDataSource,
    DictMemoryDataSource,
    HashRepository,
    make_hash_service,
)


@dataclass
class Person:
    id: str
    name: str
    age: int


def make_person(name: str, age: int) -> Person:
    return Person(name.replace(' ', '_').lower(), name, age)


class PersonRepository(HashRepository[Person, str]):
    ...


async def make_memory_data_source() -> DictMemoryDataSource:
    return DictMemoryDataSource()


async def make_fallback_data_source() -> DictFallbackDataSource:
    return DictFallbackDataSource()


service = asyncio.run(
    make_hash_service(
        PersonRepository,
        memory_data_source_factory=make_memory_data_source,
        fallback_data_source_factory=make_fallback_data_source,
        repository_expire_time=60,
    )
)
person = make_person('John Doe', 33)
asyncio.run(service.add(person))

geted_person = asyncio.run(service.get_one(person.id))
print(geted_person)
Person(id='john_doe', name='John Doe', age=33)

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

dbdaora-0.25.0.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

dbdaora-0.25.0-py3-none-any.whl (48.2 kB view details)

Uploaded Python 3

File details

Details for the file dbdaora-0.25.0.tar.gz.

File metadata

  • Download URL: dbdaora-0.25.0.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.23.0

File hashes

Hashes for dbdaora-0.25.0.tar.gz
Algorithm Hash digest
SHA256 b7531c638661e032c1704971306e838f2276029ff474b65f453c63da1868bdbd
MD5 1e0ba03e063221af8ea4b142a401eaaf
BLAKE2b-256 b6f2aee744a9fee1fd1a386bfe76832b7e2f62686682b38a62f059a6e2b506c7

See more details on using hashes here.

File details

Details for the file dbdaora-0.25.0-py3-none-any.whl.

File metadata

  • Download URL: dbdaora-0.25.0-py3-none-any.whl
  • Upload date:
  • Size: 48.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.23.0

File hashes

Hashes for dbdaora-0.25.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b669ab19c1623f6de72e3e22d9c3a266e8b66b7aa91848069d1728145f0f94f
MD5 cf19d9077687ceb0b27449430e56f742
BLAKE2b-256 29c6df7e07c2f4925530bcc41b014a056f97c100b63eae6c3c36f09b6093b816

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page