Taimwe commited on
Commit
e4f2ba6
·
verified ·
1 Parent(s): 3050429

Add database

Browse files
Files changed (1) hide show
  1. app/database.py +19 -0
app/database.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """SQLAlchemy engine, session factory and declarative base."""
2
+ from sqlalchemy import create_engine
3
+ from sqlalchemy.orm import sessionmaker, declarative_base
4
+
5
+ from .config import DATABASE_URL
6
+
7
+ _connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {}
8
+
9
+ engine = create_engine(DATABASE_URL, connect_args=_connect_args)
10
+ SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
11
+ Base = declarative_base()
12
+
13
+
14
+ def get_db():
15
+ db = SessionLocal()
16
+ try:
17
+ yield db
18
+ finally:
19
+ db.close()