-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
23 lines (17 loc) · 761 Bytes
/
database.py
File metadata and controls
23 lines (17 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
engine = create_async_engine('sqlite+aiosqlite:///tasks.db')
new_session = async_sessionmaker(engine, expire_on_commit=False)
class Model(DeclarativeBase):
pass
class TaskOrm(Model):
__tablename__ = "tasks"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column()
description: Mapped[str | None] = mapped_column(nullable=True)
async def create_tables():
async with engine.begin()as conn:
await conn.run_sync(Model.metadata.create_all)
async def delete_tables():
async with engine.begin()as conn:
await conn.run_sync(Model.metadata.drop_all)