-
Notifications
You must be signed in to change notification settings - Fork 10
/
crud.py
54 lines (39 loc) · 1.39 KB
/
crud.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
53
54
from typing import Optional
from lnbits.db import Database
from .models import Config, Relay, UserConfig
db = Database("ext_nostrclient")
async def get_relays() -> list[Relay]:
return await db.fetchall(
"SELECT * FROM nostrclient.relays",
model=Relay,
)
async def add_relay(relay: Relay) -> Relay:
await db.insert("nostrclient.relays", relay)
return relay
async def delete_relay(relay: Relay) -> None:
if not relay.url:
return
await db.execute(
"DELETE FROM nostrclient.relays WHERE url = :url", {"url": relay.url}
)
######################CONFIG#######################
async def create_config(owner_id: str) -> Config:
admin_config = UserConfig(owner_id=owner_id)
await db.insert("nostrclient.config", admin_config)
return admin_config.extra
async def update_config(owner_id: str, config: Config) -> Config:
user_config = UserConfig(owner_id=owner_id, extra=config)
await db.update("nostrclient.config", user_config, "WHERE owner_id = :owner_id")
return user_config.extra
async def get_config(owner_id: str) -> Optional[Config]:
user_config: UserConfig = await db.fetchone(
"""
SELECT * FROM nostrclient.config
WHERE owner_id = :owner_id
""",
{"owner_id": owner_id},
model=UserConfig,
)
if user_config:
return user_config.extra
return None