Как правильно подключиться к БД при написании телеграм-бота на python?

Я пишу свой первый проект - телеграм бота, используя python+peewee+postgresql (без django).

Я просто хочу понять, как правильно подключиться к базе данных не единоразово (в начале выполнения кода), а каждый раз когда это необходимо. Пример: юзер тапает на кнопку -> открывается подключение с бд -> строка добавляется в таблицу.


Сейчас файловая структура проекта выглядит так:

tgbot.py

# some lines with imports

updater = Updater(TELEGRAM_TOKEN)
dp = updater.dispatcher
dp = setup_dispatcher(dp)

# some lines with logging config

updater.start_polling()
updater.idle()

dispatcher.py

# some lines with imports

# some def for event handlers
# !!!!! here will be database connections (in functions for events)

def setup_dispatcher(dp):
    db_conn = DBConnection()  # Enter database connection
    from dbmodels import db
    db = db_conn.get_connection()

    ToDo.create(...)  # creating line in postgres-table

    dp.add_handler(CommandHandler('start', start)) 
    dp.add_handler(CommandHandler('location', ask_for_location))
    dp.add_handler(MessageHandler(Filters.location, change_location)) 
    dp.add_handler(CommandHandler('today', ask_for_today))

    return dp

dbhelper.py

# some lines with imports

class DBConnection(Singleton):

    def __init__(self): ...  # initializing some variables like self.database, etc

    def get_connection(self):
        """ Creating PostgreSQL's database connection """
        if self.connection is None:
            try:
                self.connection = PostgresqlDatabase(self.database, user=self.user, password=self.password, host=self.host, port=self.port)
                self.curs = self.connection.cursor()
            except (Exception, Error) as error:
                print("PostgreSQL's connection error: \n", error)
                sys.exit(1)
        return self.connection

    def __del__(self):
        """ Closing database connection """
        if self.connection is not None:
            self.curs.close()
            self.connection.close()
        print("PostgreSQL's connection closed.")

dbmodels.py

# some lines with imports

db = PostgresqlDatabase(None)

class Singleton:
    """ Singleton realisation for database connection class in dbhelper.py """
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

class BaseModel(Model):
    """ Basic class, used for tables-classes in models.py """
    class Meta:
        database = db

models.py

# some lines with imports

class User(BaseModel):
    """ A model for client's table """
    # initializing some fields
    class Meta:
        db_table = "clients"
    ...

class ToDo(BaseModel):
    """ A model of to-do's table """
    # initializing some fields
    class Meta:
        db_table = "to_do_s"
    ...

Ответы (0 шт):