@@ -19,31 +19,26 @@ def __init__(self, col_name: str, file_handler: FileIO) -> None:
1919 self ._file_handler = file_handler
2020
2121 # Get the data of existing Database or empty database.
22- self ._database = Document ( self ._file_handler . read (), False )
22+ self ._database = self ._get_database ( )
2323
24- self ._cursor : int = 0
24+ # Initiating Collecting
25+ self ._collection = self ._get_collection ()
2526
26- # Initiate a default Collection.
27- # Check the Collection is already exists or no.
28- if self ._col_name in self ._database .keys ():
27+ # Cursor
28+ self ._cursor : int = 0
2929
30- # Get the existing Collection
31- self ._collection : JsonArray = self ._database [self ._col_name ]
3230
33- else :
34- # Create new Collection
35- self ._database [self ._col_name ] = JsonArray ([])
36- self ._collection : JsonArray = self ._database [self ._col_name ]
3731
3832 def insert (self , document : Mapping ) -> str :
3933 """
4034 Inserts a single Document into the Database.
4135
4236 Document should be JSON Object.
4337
44- :param document: Document to insert into database
45- :return: None
38+ :param document: Document to insert into the database.
39+ :return: Document ID.
4640 """
41+
4742 # Make sure the document implements the ``Mapping`` interface
4843 if not isinstance (document , Mapping ):
4944 raise ValueError ('Document is not a Dictionary' )
@@ -52,10 +47,14 @@ def insert(self, document: Mapping) -> str:
5247 if "_id_" in document .keys ():
5348 raise KeyError (f"You are not allowed to modify key `_id_`" )
5449
50+
51+ # getting Database
52+ _database = self ._get_database ()
53+
5554 # Create a Document
5655 _document = Document (document )
5756
58- # id of Document
57+ # ID of Document
5958 _doc_id : str = _document .id
6059
6160 # check Document is already exist or not
@@ -65,11 +64,11 @@ def insert(self, document: Mapping) -> str:
6564 self ._collection .append (_document )
6665
6766 # Add modified Collection to Database
68- self . _database [self ._col_name ] = self ._collection
67+ _database [self ._col_name ] = self ._collection
6968
7069 # print(self._database)
7170 # Write current state of Database into the Database-file
72- self ._file_handler .write (self . _database )
71+ self ._file_handler .write (_database )
7372
7473 return _doc_id
7574 else :
@@ -312,6 +311,38 @@ def drop(self) -> int:
312311
313312
314313 # ----------------------------------------------------------------#
314+ def _get_database (self ) -> Document :
315+ """
316+ Getting Database
317+
318+ :return: Database
319+ """
320+ # Get the data of existing Database or empty database.
321+ database = Document (self ._file_handler .read (), False )
322+
323+ return database
324+
325+ def _get_collection (self ) -> JsonArray :
326+ """
327+ Getting Collection
328+
329+ :return: Collection
330+ """
331+ # Initiate a default Collection.
332+ # Check the Collection is already exists or no.
333+ if self ._col_name in self ._database .keys ():
334+
335+ # Get the existing Collection
336+ _collection : JsonArray = self ._database [self ._col_name ]
337+
338+ else :
339+ # Create new Collection
340+ self ._database [self ._col_name ] = JsonArray ([])
341+ _collection : JsonArray = self ._database [self ._col_name ]
342+
343+ return _collection
344+
345+
315346 def _reset_cursor (self ) -> None :
316347 """
317348 Reset Cursor Pointer to 0th index
0 commit comments