25 lines
712 B
Python
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
|
|
|