@@ -35,7 +35,7 @@ def __init__(self, col_name: str, binary_file: FileIO) -> None:
3535 self ._database [self ._col_name ] = []
3636 self ._collection : list = self ._database [self ._col_name ]
3737
38- def insert (self , document : Mapping ) -> int :
38+ def insert (self , document : Mapping ) -> str :
3939 """
4040 Inserts a single Document into the Database.
4141
@@ -48,11 +48,15 @@ def insert(self, document: Mapping) -> int:
4848 if not isinstance (document , Mapping ):
4949 raise ValueError ('Document is not a Dictionary' )
5050
51+ # Check if user trying to modify "_id_"
52+ if "_id_" in document .keys ():
53+ raise KeyError (f"You are not allowed to modify key `_id_`" )
54+
5155 # Create a Document
5256 _document = Document (document )
5357
54- # Numbers of Document
55- doc_count : int = 0
58+ # id of Document
59+ _doc_id : str = _document . id
5660
5761 # check Document is already exist or not
5862 if not self ._doc_is_exists (_document .id ):
@@ -67,11 +71,11 @@ def insert(self, document: Mapping) -> int:
6771 # Write current state of Database into the Database-file
6872 self ._binary_file .write (self ._database )
6973
70- return doc_count + 1
74+ return _doc_id
7175 else :
7276 raise ValueError (f"Document id `{ _document .id } ` is already exists" )
7377
74- def insert_all (self , document_list : List [Mapping ]) -> int :
78+ def insert_all (self , document_list : List [Mapping ]) -> List [ str ] :
7579 """
7680 Inserts a single ``Document`` into the ``Database``.
7781
@@ -85,16 +89,15 @@ def insert_all(self, document_list: List[Mapping]) -> int:
8589 if not isinstance (document_list , List ):
8690 raise ValueError ('Document is not a List of Dictionary' )
8791
88- # Numbers of Document
89- doc_count : int = 0
92+ # id of Document
93+ _doc_id : List [ str ] = []
9094
9195 # Iterate over all Documents of document_list & Insert one by one.
9296 for document in document_list :
9397 # insert every single document in Database & increment ``doc_count``.
94- doc_count += self .insert (document )
95-
96- return doc_count
98+ _doc_id .append (self .insert (document ))
9799
100+ return _doc_id
98101
99102 """ FIND_ONE
100103 def __find_one(self, query: Mapping = None) -> Document | None:
@@ -199,29 +202,80 @@ def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | No
199202
200203 return _result
201204
202- def delete (self , query : Mapping = None ) -> int :
205+ def delete (self , query : Mapping = None ) -> List [ str ] :
203206 """
204207 Delete single or multiple Document when meet the Conditions or ``query``.
205208
209+ [Recommended]
210+ Use unique identifier as ``query``.
211+
206212 :param query: Condition to search Document
207213 :return: int - amount of effected Document
208214 """
209- # Effected Documents count
210- _doc_count = 0
215+ # IDs of Effected Documents
216+ _doc_id : List [ str ] = []
211217
212218 # Fetching a Documents meet the query
213219 _documents = self .find (query , None )
214220
215221 # Fetch every Document & remove from Collection
216222 for _doc in _documents :
217223 self ._collection .remove (_doc )
218- _doc_count += 1
224+ _doc_id . append ( _doc [ "_id_" ])
219225
220226 self ._binary_file .write (self ._database )
221227
222- return _doc_count
228+ return _doc_id
229+
230+ def update (self , document : Mapping , query : Mapping = None ) -> List [str ]:
231+ """
232+ Fetch all the Documents mathc the conditions and update them.
233+
234+ [Recommended]
235+ Use a unique identifier as ``query``.
236+
237+ :param document: New key values to update
238+ :param query: Condition to search Document.
239+ :return: List of document ID.
240+ """
241+ _documents = self .find (query )
242+ _new_doc = document
243+
244+ # IDs of Effected Documents
245+ _doc_id : List [str ] = []
246+
247+ # Fetch all Documents to update if got multiple.
248+ for _doc in _documents :
249+ for key , value in _new_doc .items ():
250+
251+ # Check if user trying to modify "_id_"
252+ if key == "_id_" :
253+ raise KeyError (f"You are not allowed to modify key `{ key } `" )
254+
255+ # Update Document
256+ # Create new field if needed.
257+ _doc [key ] = value
258+
259+ _doc_id .append (_doc ["_id_" ])
260+
261+ # Write current state of Database
262+ self ._binary_file .write (self ._database )
263+
264+ return _doc_id
223265
224266
267+ def count (self , query : Mapping = None , limit : tuple = None ) -> int :
268+ """
269+ Return amount of Document found.
270+
271+ :param query: Condition to search Document.
272+ :param limit: If there is any limit.
273+ :return: (int) amount of Document found.
274+ """
275+ count = len (self .find (query = query , limit = limit ))
276+
277+ return count
278+
225279 def _reset_cursor (self ) -> None :
226280 """
227281 Reset Cursor Pointer to 0th index
@@ -282,8 +336,6 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
282336 # If it has any value then this is the desired Document.
283337 # Else such Document in Collection.
284338
285-
286-
287339 if 0 not in _bag_of_query :
288340
289341 # return the Document
@@ -296,17 +348,8 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
296348 else :
297349 return None
298350
299-
300351 return result
301352
302-
303-
304-
305- def count (self , query : Mapping = None , limit : tuple = None ) -> int :
306- return len (self .find (query = query , limit = limit ))
307-
308-
309-
310353 # ======================== #
311354 def _doc_is_exists (self , doc_id : str | int ) -> bool :
312355 # Iterate over all Documents of Collection
0 commit comments