11import json
2+ from typing import Mapping , List
23
34from .document import Document
45from .fileio import FileIO , BinaryFileIO
@@ -25,7 +26,7 @@ def __init__(self, col_name: str, binary_file: FileIO) -> None:
2526 self ._database [self ._col_name ] = []
2627 self ._collection : list = self ._database [self ._col_name ]
2728
28- def insert (self , document : dict ) -> None :
29+ def insert (self , document : Mapping ) -> int :
2930 """
3031 Inserts a single Document into the Database.
3132
@@ -34,14 +35,71 @@ def insert(self, document: dict) -> None:
3435 :param document: Document to insert into database
3536 :return: None
3637 """
38+ # Make sure the document implements the ``Mapping`` interface
39+ if not isinstance (document , Mapping ):
40+ raise ValueError ('Document is not a Dictionary' )
3741
38- # Append the document into the Collection
39- self . _collection . append (document )
42+ # Create a Document
43+ _document = Document (document )
4044
41- # Add modified Collection to Database
42- self ._database [self ._col_name ] = self ._collection
43- # print(self._database)
44- # Write current state of Database into the Database-file
45- self ._binary_file .write (self ._database )
45+ # Numbers of Document
46+ doc_count : int = 0
47+
48+ # check Document is already exist or not
49+ if not self ._doc_is_exists (_document .id ):
50+
51+ # Append the document into the Collection
52+ self ._collection .append (_document )
53+
54+ # Add modified Collection to Database
55+ self ._database [self ._col_name ] = self ._collection
56+
57+ # print(self._database)
58+ # Write current state of Database into the Database-file
59+ self ._binary_file .write (self ._database )
60+
61+ return doc_count + 1
62+ else :
63+ raise ValueError (f"Document id `{ _document .id } ` is already exists" )
64+
65+ def insert_all (self , document_list : List [Mapping ]) -> int :
66+ """
67+ Inserts a single ``Document`` into the ``Database``.
68+
69+ Document should be a ``List`` of ``JSON Object``.
70+
71+ :param document_list: List of Documents to insert into Database
72+ :return: Inserted row(s) count
73+ """
74+
75+ # Make sure the document_list implements the ``List`` interface.
76+ if not isinstance (document_list , List ):
77+ raise ValueError ('Document is not a List of Dictionary' )
78+
79+ # Numbers of Document
80+ doc_count : int = 0
81+
82+ # Iterate over all Documents of document_list & Insert one by one.
83+ for document in document_list :
84+
85+ # insert every single document in Database & increment ``doc_count``.
86+ doc_count += self .insert (document )
87+
88+ return doc_count
89+
90+
91+ # ======================== #
92+ def _doc_is_exists (self , doc_id : str | int ) -> bool :
93+ # Iterate over all Documents of Collection
94+ for doc in self ._collection :
95+ if doc ["_id_" ] == doc_id :
96+ return True
4697
98+ return False
4799
100+ def _get_document_by_id (self , doc_id ) -> Document | str :
101+ for doc in self ._collection :
102+ if doc ["_id_" ] == doc_id :
103+ return doc
104+ else :
105+ return "none"
0 commit comments