-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
46 lines (35 loc) · 1.37 KB
/
Copy pathdatabase.py
File metadata and controls
46 lines (35 loc) · 1.37 KB
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
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime
# Database URL, change as needed (e.g., to PostgreSQL or MySQL)
DATABASE_URL = "sqlite:///database.db"
# Create the database engine
engine = create_engine(DATABASE_URL, echo=True)
# Create a base class for models
Base = declarative_base()
# Define your models
class Prompt(Base):
__tablename__ = 'prompts'
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer)
prompt = Column(String(1000))
link = Column(String, nullable=True)
language = Column(String , nullable=True)
thumbnail= Column(String, nullable=True)
cost = Column(Float, nullable=True)
created_at = Column(DateTime, default=datetime.utcnow)
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer)
prompt = Column(String(1000))
link = Column(String, nullable=True)
cost = Column(Float, nullable=True)
created_at = Column(DateTime, default=datetime.utcnow)
def create_db_and_tables():
Base.metadata.create_all(engine)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_session():
with SessionLocal() as session:
yield session