hub/persistence/db_config.py

38 lines
1.1 KiB
Python

"""
Persistence (Postgresql) configuration
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
import os
from dotenv import load_dotenv
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# load environmental variables
load_dotenv()
class BaseConfiguration(object):
"""
Base configuration class to hold common persistence configuration
"""
def __init__(self, db_name: str):
self._db_name = db_name
self._db_host = os.getenv('DB_HOST')
self._db_user = os.getenv('DB_USER')
self._db_pass = os.getenv('DB_PASSWORD')
self._db_port = os.getenv('DB_PORT')
self.hub_token = os.getenv('HUB_TOKEN')
def conn_string(self):
"""
Returns a connection string postgresql
:return: connection string
"""
if self._db_pass:
return f'postgresql://{self._db_user}:{self._db_pass}@{self._db_host}:{self._db_port}/{self._db_name}'
return f'postgresql://{self._db_user}@{self._db_host}:{self._db_port}/{self._db_name}'