File size: 1,413 Bytes
1915039 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base() # Define base class for ORM models
class Product(Base):
__tablename__ = "products"
id = Column(Integer, primary_key=True) # Primary key for products
name = Column(String, nullable=False)
current_stock = Column(Float, nullable=False)
safety_stock = Column(Float, nullable=False)
lead_time = Column(Float, nullable=False)
monthly_demand = Column(Float, nullable=False)
product_class = Column(String, nullable=False)
class Transaction(Base):
__tablename__ = "transactions"
id = Column(Integer, primary_key=True) # Primary key for transactions
product_id = Column(Integer, nullable=False)
quantity = Column(Float, nullable=False)
transaction_type = Column(String, nullable=False)
timestamp = Column(DateTime, server_default="CURRENT_TIMESTAMP")
engine = create_engine("sqlite:///:memory:") # In-memory SQLite database
SessionLocal = sessionmaker(bind=engine) # Session factory for database access
def initialize_database():
Base.metadata.create_all(engine) # Create all tables
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close() # Ensure database session is closed |