From b8d7b9d5763da69c9bc110c5bc92debb92cf55da Mon Sep 17 00:00:00 2001 From: Cologler Date: Sat, 8 Aug 2020 14:38:27 +0800 Subject: [PATCH 1/3] find doc_id from fields --- tinydb/table.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tinydb/table.py b/tinydb/table.py index 96b3f4a8..2df4a782 100644 --- a/tinydb/table.py +++ b/tinydb/table.py @@ -325,6 +325,9 @@ def perform_update(table, doc_id): # Update documents by setting all fields from the provided data table[doc_id].update(fields) + if cond is None and doc_ids is None and isinstance(fields, Document): + doc_ids = [fields.doc_id] + if doc_ids is not None: # Perform the update operation for documents specified by a list # of document IDs From f390104b4961fb80922391e61ae009b2d323c10a Mon Sep 17 00:00:00 2001 From: Cologler Date: Sat, 8 Aug 2020 14:45:28 +0800 Subject: [PATCH 2/3] remove with document --- tinydb/table.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tinydb/table.py b/tinydb/table.py index 2df4a782..bd24cf33 100644 --- a/tinydb/table.py +++ b/tinydb/table.py @@ -416,19 +416,23 @@ def upsert(self, document: Mapping, cond: Query) -> List[int]: def remove( self, - cond: Optional[Query] = None, + cond: Optional[Document, Query] = None, doc_ids: Optional[Iterable[int]] = None, ) -> List[int]: """ Remove all matching documents. - :param cond: the condition to check against + :param cond: the condition to check against, or the document to remove :param doc_ids: a list of document IDs :returns: a list containing the removed documents' ID """ if cond is None and doc_ids is None: raise RuntimeError('Use truncate() to remove all documents') + if doc_ids is None and isinstance(cond, Document): + doc_ids = [cond.doc_id] + cond = None + if cond is not None: removed_ids = [] From 6f8c9da9a71f3d504479e7f44c5baded083762fd Mon Sep 17 00:00:00 2001 From: Cologler Date: Sat, 8 Aug 2020 14:50:01 +0800 Subject: [PATCH 3/3] update typing --- tinydb/table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tinydb/table.py b/tinydb/table.py index bd24cf33..f7a15ceb 100644 --- a/tinydb/table.py +++ b/tinydb/table.py @@ -416,7 +416,7 @@ def upsert(self, document: Mapping, cond: Query) -> List[int]: def remove( self, - cond: Optional[Document, Query] = None, + cond: Optional[Union[Document, Query]] = None, doc_ids: Optional[Iterable[int]] = None, ) -> List[int]: """