summaryrefslogtreecommitdiff
path: root/app/database.py
diff options
context:
space:
mode:
authorRasmus Luha <rasmus.luha@gmail.com>2022-02-06 13:41:11 +0200
committerRasmus Luha <rasmus.luha@gmail.com>2022-02-06 13:41:11 +0200
commit6a6afdbe72c626b01245c9372c9d10be79789bb0 (patch)
treecfdb5aea7538296bad0105c813fce4cd33c19ef5 /app/database.py
parent5e19a0569288de21365c61b0db78639880732dd0 (diff)
restrucurring the stucture of the folderstruture --> structure
Diffstat (limited to 'app/database.py')
-rw-r--r--app/database.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/app/database.py b/app/database.py
new file mode 100644
index 0000000..1abafa0
--- /dev/null
+++ b/app/database.py
@@ -0,0 +1,38 @@
+from sqlalchemy import create_engine # Copied imports from fastapi sqlalchemy docs.
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import sessionmaker
+from psycopg2.extras import RealDictCursor
+from time import sleep
+import psycopg2 #Library to connect to postgres
+from .config import settings
+
+SQLALCHEMY_DATABASE_URL = f'postgresql://{settings.database_username}:{settings.database_password}@{settings.database_hostname}:{settings.database_port}/{settings.database_name}'
+
+
+engine = create_engine(SQLALCHEMY_DATABASE_URL)
+
+SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
+Base = declarative_base()
+
+# Dependency
+def get_db():
+ db = SessionLocal()
+ try:
+ yield db
+ finally:
+ db.close()
+
+
+# Setting connection with DataBase
+# PS: thats only for reference --> kautad nüüd ju hoopiki sqlalchemyt db sessioniks. Ülal
+
+#while True:
+# try:
+# conn = psycopg2.connect(host='localhost', database='fastapi', user='postgres', password='password123', cursor_factory = RealDictCursor)#CursorFactory minig library teema, no matter::prg pole password
+# cursor = conn.cursor()
+# print("DataBase Connection Was a Huge and Massive Success!")
+# break
+# except Exception as error:
+# print("Connecting to database failed")
+# print("error was: ", error)
+# sleep(3)