classUSessionDB(DBBase):''' USessionDB Collection Args: token (str): If is `None`, it will generate an random sha256 code. Attributes: token (str): An session token. '''def__init__(self,token:Optional[str]=None)->None:super().__init__('usession')iftokenisNone:message=hashlib.sha256()message.update((f'{uuid4().hex}{time()}').encode('utf8'))token=message.hexdigest()self.token=tokendefindex(self)->None:''' To make collection's index Indexs: - `created_at` - `ipinfo` - `uid` - `alive` '''self.create_index([('created_at',1),])self.create_index([('ipinfo',1),])self.create_index([('uid',1),])self.create_index([('alive',1),])defadd(self,data:dict[str,Any])->InsertOneResult:''' save Args: data (dict): The data to insert. Returns: Return the inserted data. '''doc={}doc.update(data)doc['_id']=self.tokenreturnself.insert_one(doc)defget(self)->Optional[dict[str,Any]]:''' Get data Returns: Return the data. '''returnself.find_one({'_id':self.token,'alive':True})
defadd(self,data:dict[str,Any])->InsertOneResult:''' save Args: data (dict): The data to insert. Returns: Return the inserted data. '''doc={}doc.update(data)doc['_id']=self.tokenreturnself.insert_one(doc)
defindex(self)->None:''' To make collection's index Indexs: - `created_at` - `ipinfo` - `uid` - `alive` '''self.create_index([('created_at',1),])self.create_index([('ipinfo',1),])self.create_index([('uid',1),])self.create_index([('alive',1),])