data
This commit is contained in:
30
src/Database/Database.py
Normal file
30
src/Database/Database.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
class Database:
|
||||||
|
def __init__(self, connection_string: dict):
|
||||||
|
"""
|
||||||
|
connection_string ist ein Dictionary, z. B.:
|
||||||
|
{
|
||||||
|
"driver": "postgresql+psycopg2",
|
||||||
|
"username": "postgres",
|
||||||
|
"password": "mein_passwort",
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 5432,
|
||||||
|
"database": "myapp"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
self.connection_string = connection_string
|
||||||
|
self.engine = self._create_engine()
|
||||||
|
self.SessionLocal = sessionmaker(bind=self.engine)
|
||||||
|
|
||||||
|
def _create_engine(self):
|
||||||
|
conn = self.connection_string
|
||||||
|
url = f"{conn['driver']}://{conn['username']}:{conn['password']}@{conn['host']}:{conn['port']}/{conn['database']}"
|
||||||
|
return create_engine(url, echo=True)
|
||||||
|
|
||||||
|
def get_engine(self):
|
||||||
|
return self.engine
|
||||||
|
|
||||||
|
def get_session(self):
|
||||||
|
return self.SessionLocal()
|
||||||
15
src/Database/User.py
Normal file
15
src/Database/User.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String, DateTime, func
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
username = Column(String(50), unique=True, nullable=False)
|
||||||
|
password = Column(String(255), nullable=False)
|
||||||
|
created_at = Column(DateTime, server_default=func.now())
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<User(id={self.id}, username='{self.username}')>"
|
||||||
Reference in New Issue
Block a user