-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdb.py
52 lines (40 loc) · 1.43 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
52
from typing import Optional
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column
from trustregistry.database import Base, SessionLocal
from trustregistry.list_type import StringList
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
def schema_id_gen(context):
parameters = context.get_current_parameters()
did = parameters["did"]
name = parameters["name"]
version = parameters["version"]
return f"{did}:2:{name}:{version}"
class Actor(Base):
__tablename__ = "actors"
id: Mapped[str] = mapped_column(String, primary_key=True, index=True, unique=True)
name: Mapped[str] = mapped_column(String, unique=True, index=True)
roles: Mapped[str] = mapped_column(StringList, index=True)
didcomm_invitation: Mapped[Optional[str]] = mapped_column(
String, unique=True, index=True
)
did: Mapped[str] = mapped_column(String, unique=True, index=True)
image_url: Mapped[Optional[str]] = mapped_column(String, index=True)
class Schema(Base):
__tablename__ = "schemas"
id: Mapped[str] = mapped_column(
String,
primary_key=True,
index=True,
unique=True,
default=schema_id_gen,
onupdate=schema_id_gen,
)
did: Mapped[str] = mapped_column(String, index=True)
name: Mapped[str] = mapped_column(String, index=True)
version: Mapped[str] = mapped_column(String, index=True)