generateur_v3/backend/api/database/db.py
Kilton937342 936931f047 api
2022-10-10 01:34:38 +02:00

25 lines
687 B
Python

import pydantic.json
import json
from sqlmodel import SQLModel, create_engine, Session, select
sqlite_file_name = "database.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) as s:
yield s