feat: add project config and database connection
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
0
app/core/__init__.py
Normal file
0
app/core/__init__.py
Normal file
32
app/core/config.py
Normal file
32
app/core/config.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from sqlalchemy.engine import URL
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
SQL_SERVER_HOST: str
|
||||
SQL_SERVER_PORT: int = 1433
|
||||
SQL_SERVER_DATABASE: str
|
||||
SQL_SERVER_USERNAME: str
|
||||
SQL_SERVER_PASSWORD: str
|
||||
SQL_SERVER_DRIVER: str = "{ODBC Driver 18 for SQL Server}"
|
||||
SQL_SERVER_TRUST_SERVER_CERTIFICATE: str = "yes"
|
||||
|
||||
@property
|
||||
def database_url(self) -> URL:
|
||||
return URL.create(
|
||||
"mssql+pyodbc",
|
||||
username=self.SQL_SERVER_USERNAME,
|
||||
password=self.SQL_SERVER_PASSWORD,
|
||||
host=self.SQL_SERVER_HOST,
|
||||
port=self.SQL_SERVER_PORT,
|
||||
database=self.SQL_SERVER_DATABASE,
|
||||
query={
|
||||
"driver": self.SQL_SERVER_DRIVER.strip("{}"),
|
||||
"TrustServerCertificate": self.SQL_SERVER_TRUST_SERVER_CERTIFICATE,
|
||||
},
|
||||
)
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
|
||||
|
||||
|
||||
settings = Settings()
|
||||
25
app/core/database.py
Normal file
25
app/core/database.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from typing import Generator
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
engine = create_engine(
|
||||
settings.database_url,
|
||||
pool_pre_ping=True,
|
||||
pool_recycle=1800,
|
||||
)
|
||||
SessionLocal = sessionmaker(bind=engine)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
def get_db() -> Generator[Session, None, None]:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user