Files
FileCodeBox/database.py
T

42 lines
1.3 KiB
Python
Raw Permalink Normal View History

2022-12-09 19:06:47 +08:00
import datetime
2022-12-19 18:01:50 +08:00
from sqlalchemy import Boolean, Column, Integer, String, DateTime, JSON
2022-12-09 19:06:47 +08:00
from sqlalchemy.ext.declarative import declarative_base
2022-12-11 14:04:25 +08:00
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.ext.asyncio.session import AsyncSession
2022-12-13 21:03:15 +08:00
import settings
engine = create_async_engine(settings.DATABASE_URL)
2022-12-09 19:06:47 +08:00
Base = declarative_base()
2022-12-11 14:04:25 +08:00
2022-12-11 18:14:41 +08:00
async def init_models():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
2022-12-11 18:03:46 +08:00
2022-12-11 14:04:25 +08:00
async def get_session():
async with AsyncSession(engine, expire_on_commit=False) as s:
yield s
2022-12-09 19:06:47 +08:00
2022-12-19 18:01:50 +08:00
class Values(Base):
__tablename__ = 'values'
id = Column(Integer, primary_key=True, index=True)
key = Column(String, unique=True)
value = Column(JSON)
2022-12-09 19:06:47 +08:00
class Codes(Base):
2022-12-11 14:04:25 +08:00
__tablename__ = "codes"
2022-12-09 19:06:47 +08:00
id = Column(Integer, primary_key=True, index=True)
code = Column(String(10), unique=True, index=True)
key = Column(String(30), unique=True, index=True)
name = Column(String(500))
size = Column(Integer)
type = Column(String(20))
text = Column(String(500))
used = Column(Boolean, default=False)
count = Column(Integer, default=-1)
2022-12-09 19:06:47 +08:00
use_time = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
exp_time = Column(DateTime, nullable=True)