Skip to content

Commit 9d64646

Browse files
committed
Added document counter
1 parent d6e104a commit 9d64646

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

filexdb/collection.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from typing import Mapping, List
33

4-
from .document import Document
4+
from .document import Document, JsonArray
55
from .fileio import FileIO
66

77

@@ -75,7 +75,7 @@ def insert(self, document: Mapping) -> str:
7575
else:
7676
raise ValueError(f"Document id `{_document.id}` is already exists")
7777

78-
def insert_all(self, document_list: List[Mapping]) -> List[str]:
78+
def insert_all(self, document_list: List[Mapping]) -> JsonArray[str]:
7979
"""
8080
Inserts a single ``Document`` into the ``Database``.
8181
@@ -97,7 +97,7 @@ def insert_all(self, document_list: List[Mapping]) -> List[str]:
9797
# insert every single document in Database & increment ``doc_count``.
9898
_doc_id.append(self.insert(document))
9999

100-
return _doc_id
100+
return JsonArray(_doc_id)
101101

102102
def __find_one(self, query: Mapping = None) -> Document | None: # Not works, right nom
103103
"""
@@ -132,7 +132,7 @@ def __find_one(self, query: Mapping = None) -> Document | None: # Not wo
132132

133133
return _result
134134

135-
def find(self, query=None, limit=None) -> List[Document]:
135+
def find(self, query=None, limit=None) -> JsonArray[Document]:
136136
"""
137137
Finds all ``Document`` of ``Collection``.
138138
@@ -188,7 +188,7 @@ def find(self, query=None, limit=None) -> List[Document]:
188188
else:
189189
_result = self._collection
190190

191-
return _result
191+
return JsonArray(_result)
192192

193193
elif query is not None and type(query) == type({}):
194194
if limit:
@@ -224,9 +224,9 @@ def find(self, query=None, limit=None) -> List[Document]:
224224

225225
self._reset_cursor()
226226

227-
return _result
227+
return JsonArray(_result)
228228

229-
def delete(self, query=None) -> List[str]:
229+
def delete(self, query=None) -> JsonArray[str]:
230230
"""
231231
Delete single or multiple Document when meet the Conditions or ``query``.
232232
@@ -249,9 +249,9 @@ def delete(self, query=None) -> List[str]:
249249

250250
self._file_handler.write(self._database)
251251

252-
return _doc_id
252+
return JsonArray(_doc_id)
253253

254-
def update(self, document: Mapping, query=None) -> List[str]:
254+
def update(self, document: Mapping, query=None) -> JsonArray[str]:
255255
"""
256256
Fetch all the Documents mathc the conditions and update them.
257257
@@ -285,7 +285,7 @@ def update(self, document: Mapping, query=None) -> List[str]:
285285
# Write current state of Database
286286
self._file_handler.write(self._database)
287287

288-
return _doc_id
288+
return JsonArray(_doc_id)
289289

290290
def count(self, query=None, limit: tuple = None) -> int:
291291
"""
@@ -315,7 +315,7 @@ def _reset_cursor(self) -> None:
315315
"""
316316
self._cursor = 0
317317

318-
def _find_document_by_query(self, query: Mapping) -> List:
318+
def _find_document_by_query(self, query: Mapping) -> JsonArray[Document]:
319319
"""
320320
Finds a single ``Document`` of ``Collection``.
321321
@@ -379,20 +379,6 @@ def _find_document_by_query(self, query: Mapping) -> List:
379379
else:
380380
return None
381381

382-
return result
383-
384-
# ======================== #
385-
def _doc_is_exists(self, doc_id: str) -> bool:
386-
# Iterate over all Documents of Collection
387-
for doc in self._collection:
388-
if doc["_id_"] == doc_id:
389-
return True
382+
return JsonArray(result)
390383

391-
return False
392384

393-
def _find_document_by_id(self, doc_id) -> Document:
394-
for doc in self._collection:
395-
if doc["_id_"] == doc_id:
396-
return doc
397-
else:
398-
return None

filexdb/document.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ def __init__(self, value: Mapping) -> None:
2727

2828
super().__init__(self._doc)
2929

30-
def beautify(self) -> str:
30+
def prettify(self) -> str:
3131
"""
3232
Beautify the ``JSON Object`` with new lines & proper indentation.
3333
3434
Convert `JSON Object`` into `JSON String`` using ``json.dumps()``.
3535
36-
:return: JSON String
36+
:return: JSON Object
3737
"""
3838

3939
# Dumping JSON Object & adding indentation
@@ -42,3 +42,45 @@ def beautify(self) -> str:
4242
return self._doc
4343

4444

45+
46+
class JsonArray(list):
47+
def __init__(self, _value: list) -> None:
48+
self.value = _value
49+
super().__init__(self.value)
50+
51+
52+
def prettify(self) -> str:
53+
"""
54+
Beautify the ``JSON Array`` with new lines & proper indentation.
55+
56+
Convert `JSON Array`` into `JSON Array`` using ``json.dumps()``.
57+
58+
:return: JSON Array
59+
"""
60+
61+
# Dumping JSON Object & adding indentation
62+
self.value = json.dumps(self.value, indent=4)
63+
64+
return self.value
65+
66+
def docs_count(self) -> int:
67+
"""
68+
Return amount of Document found.
69+
70+
:return: (int) amount of Document found.
71+
"""
72+
count = len(self.value)
73+
74+
return count
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+

0 commit comments

Comments
 (0)