generateur_v3/backend/api/database/db.py
Kilton937342 4941b5a154 tests
2023-02-22 12:43:39 +01:00

25 lines
712 B
Python

import pydantic.json
import json
from sqlmodel import SQLModel, create_engine, Session, select
sqlite_file_name = "database7.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
def _custom_json_serializer(*args, **kwargs) -> str:
"""
Encodes json in the same way that pydantic does.
"""
print('JSON SERIALIZATION')
return json.dumps(*args, default=pydantic.json.pydantic_encoder, **kwargs)
engine = create_engine(sqlite_url, echo=False, connect_args={"check_same_thread": False}, json_serializer=_custom_json_serializer)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def get_session():
with Session(engine, expire_on_commit=False) as s:
yield s