Skip to content

Typed Dict Example

import asyncio
from typing import TypedDict

from jsondaora import jsondaora

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


# Data Source Layer


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


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


# Domain Layer


@jsondaora
class Person(TypedDict):
    id: str
    name: str
    age: int


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


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


person_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,
    )
)


class Playlist(SortedSetDictEntity):
    person_id: str


class PlaylistRepository(SortedSetRepository[Playlist, str]):
    id_name = 'person_id'


playlist_repository = PlaylistRepository(
    memory_data_source=DictMemoryDataSource(),
    fallback_data_source=DictFallbackDataSource(),
    expire_time=60,
)


def make_playlist(person_id: str, *musics_ids: str) -> Playlist:
    return Playlist(
        person_id=person_id,
        data=[(id_, i) for i, id_ in enumerate(musics_ids)],
        max_size=None,
    )


# Application Layer


async def main() -> None:
    person = make_person('John Doe', 33)
    playlist = make_playlist(person['id'], 'm1', 'm2', 'm3')

    await person_service.add(person)
    await playlist_repository.add(playlist)

    goted_person = await person_service.get_one(person['id'])
    goted_playlist = await playlist_repository.query(goted_person['id']).entity

    print(goted_person)
    print(goted_playlist)


asyncio.run(main())
{'id': 'john_doe', 'name': 'John Doe', 'age': 33}
{'data': [b'm1', b'm2', b'm3'], 'max_size': None, 'person_id': 'john_doe'