2022-10-06 07:40:03 +00:00
|
|
|
from sqlmodel import SQLModel
|
|
|
|
|
|
|
|
from models.models.lofter import Lofter
|
2022-11-04 15:54:19 +00:00
|
|
|
from models.models.fragment import Fragment
|
2022-10-06 07:40:03 +00:00
|
|
|
|
2022-11-24 07:08:30 +00:00
|
|
|
__all__ = ["Lofter", "Fragment", "Sqlite"]
|
2022-10-06 07:40:03 +00:00
|
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
class Sqlite:
|
|
|
|
def __init__(self):
|
|
|
|
self.engine = create_async_engine("sqlite+aiosqlite:///data/data.db")
|
|
|
|
self.Session = sessionmaker(bind=self.engine, class_=AsyncSession)
|
|
|
|
|
|
|
|
async def create_db_and_tables(self):
|
|
|
|
async with self.engine.begin() as session:
|
|
|
|
await session.run_sync(SQLModel.metadata.create_all)
|
|
|
|
|
|
|
|
async def get_session(self):
|
|
|
|
async with self.Session() as session:
|
|
|
|
yield session
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.Session.close_all()
|