-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
51 lines (42 loc) · 1.52 KB
/
db.py
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 asyncio
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy import Column, Integer, String, Boolean, Text, DateTime, func
from lib import asynccontextmanager
Base = declarative_base()
class DB:
def __init__(self, url):
self._url = url
self.engine = None
def connect(self):
self.engine = create_async_engine(self._url)
@asynccontextmanager
async def session(self, *args, **kwargs):
ASession = sessionmaker(
self.engine,
expire_on_commit=False,
class_=AsyncSession
)
new_session = ASession(*args, **kwargs)
try:
yield new_session
await new_session.commit()
except Exception as exc:
await new_session.rollback()
raise
finally:
await new_session.close()
class Email(Base):
__tablename__ = 'mailkeeper_email'
id = Column(Integer, primary_key=True)
sender = Column(String(255), nullable=False)
recipient = Column(String(255), nullable=False)
body = Column(Text, nullable=False)
raw_content = Column(Text, nullable=False)
subject = Column(String(255), nullable=False)
created = Column(DateTime(timezone=True),)
inbound = Column(Boolean, nullable=True)
bounced = Column(Boolean, nullable=True)
message_id = Column(String(255), nullable=True)
status = Column(String(12))
extended_delivery_status = Column(String(255))