28 lines
787 B
Python
28 lines
787 B
Python
from pathlib import Path
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
def generate_database_uri(data_dir: Path, use_memory_db: bool = False) -> str:
|
|
if use_memory_db:
|
|
return "sqlite:///:memory:"
|
|
else:
|
|
sqlite_path = (data_dir / 'sqlite.db').absolute()
|
|
return f"sqlite:///{sqlite_path}"
|
|
|
|
# db_uri = 'postgresql+psycopg2://{user}:{pw}@{host}:{port}/{name}'.format(
|
|
# user="tdt4130_project",
|
|
# pw="",
|
|
# host="localhost",
|
|
# port=5432,
|
|
# name="tdt4130_project",
|
|
# )
|
|
|
|
def connect_to_database(
|
|
data_dir: Path,
|
|
echo_sql: bool = False,
|
|
use_memory_db: bool = False,
|
|
) -> sessionmaker:
|
|
engine = create_engine(generate_database_uri(data_dir, use_memory_db), echo=echo_sql)
|
|
|
|
return sessionmaker(bind=engine) |