iShotaBot/models/sqlite.py

28 lines
821 B
Python
Raw Normal View History

2022-10-06 07:40:03 +00:00
from sqlmodel import SQLModel
from models.models.lofter import Lofter
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()