-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtelegram_db.py
51 lines (37 loc) · 1.15 KB
/
telegram_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
''' TelegramDB '''
from typing import Any
from pymongo.collection import ReturnDocument
from models.base import DBBase
class TelegramDB(DBBase):
''' TelegramDB Collection '''
def __init__(self) -> None:
super().__init__('telegram')
def index(self) -> None:
''' To make collection's index
Indexs:
- `uid`, `uid`
'''
self.create_index([('uid', 1), ])
def add(self, data: dict[str, Any]) -> None:
''' save data
Args:
data (dict): The data to insert / update.
The data's struct
Struct:
- `_id`: telegram id.
- `added`: Added time in datatime.
- `first_name`: First name.
- `id`: telegram id.
- `is_bot`: `bool`.
- `language_code`: Language in ISO 639.
- `uid`: Mapping to our user id.
- `username`: User name.
TODO:
Need refactor in pydantic.
'''
self.find_one_and_update(
{'_id': data['id']},
{'$set': data},
upsert=True,
return_document=ReturnDocument.AFTER,
)