__init__.py 806 Bytes
from functools import lru_cache

from elasticsearch import AsyncElasticsearch

from config import settings
from .data_dao import DataDao

es_client = None
pg_client = None


async def connect():
    if settings.db_es_enable:
        global es_client
        es_client = AsyncElasticsearch([settings.db_es_url])
        await es_client.ping()
    if settings.db_postgres_enable:
        from .postgres_client import get_pg_client
        global pg_client
        pg_client = get_pg_client()
        await pg_client.connect()


async def disconnect():
    global es_client, pg_client
    if es_client is not None:
        await es_client.disconnect()
        es_client = None
    if pg_client is not None:
        await pg_client.disconnect()


@lru_cache()
def get_data_dao():
    return DataDao(es_client)