forked from VinokurovVE/tests
crud api
This commit is contained in:
29
backend_fastapi/database.py
Normal file
29
backend_fastapi/database.py
Normal file
@ -0,0 +1,29 @@
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
class Model(DeclarativeBase):
|
||||
pass
|
||||
|
||||
async_engine = create_async_engine(
|
||||
os.getenv("SQL_URL"),
|
||||
connect_args={"check_same_thread": False}
|
||||
)
|
||||
|
||||
|
||||
async_session = async_sessionmaker(
|
||||
async_engine,
|
||||
autoflush=True,
|
||||
autocommit=False,
|
||||
expire_on_commit =False
|
||||
)
|
||||
|
||||
|
||||
async def connect() -> None:
|
||||
async with async_engine.begin() as conn:
|
||||
await conn.run_sync(Model.metadata.create_all, checkfirst=True)
|
||||
|
||||
async def disconnect() -> None:
|
||||
if async_engine:
|
||||
await async_engine.dispose()
|
Reference in New Issue
Block a user