bbs-backend/models/sqlite.py

31 lines
850 B
Python
Raw Normal View History

2023-04-14 14:09:47 +00:00
from sqlmodel import SQLModel
2023-04-23 06:08:36 +00:00
from models.models.tip import Tip
2023-04-14 14:09:47 +00:00
from pathlib import Path
2023-04-23 06:08:36 +00:00
__all__ = ["Tip", "Sqlite"]
2023-04-14 14:09:47 +00:00
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel.ext.asyncio.session import AsyncSession
DataPath = Path("data")
DataPath.mkdir(exist_ok=True, parents=True)
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()