Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/database/orm.py

39 lines
1,016 B
Python
Raw Normal View History

from sqlalchemy import create_engine
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.orm import sessionmaker
2021-08-07 07:56:48 +00:00
from config import Config
2022-06-13 04:49:22 +00:00
from database.orm_base import Base
2021-07-28 18:51:24 +00:00
DB_LINK = Config('db_path')
2021-10-20 14:04:30 +00:00
2021-08-24 16:49:19 +00:00
class DBSession:
def __init__(self):
self.engine = create_engine(DB_LINK, isolation_level="READ UNCOMMITTED")
2021-08-24 16:49:19 +00:00
self.Session = sessionmaker()
self.Session.configure(bind=self.engine)
2021-08-24 16:49:19 +00:00
@property
def session(self):
return self.Session()
2022-06-13 04:49:22 +00:00
def create(self):
Base.metadata.create_all(bind=self.engine, checkfirst=True)
class AsyncDBSession:
def __init__(self):
self.engine = create_async_engine(DB_LINK, isolation_level="READ UNCOMMITTED")
self.Session = async_sessionmaker()
self.Session.configure(bind=self.engine)
async def session(self):
return self.Session()
def create(self):
Base.metadata.create_all(bind=self.engine, checkfirst=True)
2022-06-13 04:49:22 +00:00
Session = DBSession()