File size: 1,633 Bytes
f7cecf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
from sqlalchemy import create_engine, Column, Integer, String, Boolean, JSON
from sqlalchemy.orm import sessionmaker, declarative_base

# Paste your Supabase URI here. Replace [YOUR-PASSWORD] with your actual password!
SUPABASE_URL = "postgresql://postgres.gjbfbkhygtqubvpbquws:Anayshukla11$$@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"

# SQLAlchemy requires the URL to start with 'postgresql://'
if SUPABASE_URL.startswith("postgres://"):
    SUPABASE_URL = SUPABASE_URL.replace("postgres://", "postgresql://", 1)

engine = create_engine(SUPABASE_URL, pool_pre_ping=True)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()


# --- THE USER DATABASE MODEL ---
class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String, nullable=True)
    is_admin = Column(Boolean, default=False)

    # User FPL State
    default_team_id = Column(Integer, nullable=True)
    saved_edits = Column(JSON, default={})
    drafts = Column(JSON, default=[])
    solver_settings = Column(JSON, default={"quick": {}, "advanced": {}})


# --- THE NEW JSON VAULT ---
class GlobalConfig(Base):
    __tablename__ = "global_config"
    key = Column(String, primary_key=True, index=True)
    value = Column(JSON, default={})


# Create the tables in the Supabase database
Base.metadata.create_all(bind=engine)


# Dependency to get the DB session in our API routes
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()