Skip to content

Commit 4499d0b

Browse files
committed
Fixed minor issues
1 parent 41cbbd1 commit 4499d0b

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

filexdb/collection.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
1-
from .document import Document
21
import json
32

3+
from .document import Document
4+
from .fileio import FileIO, BinaryFileIO
5+
46

57
class Collection:
6-
def __init__(self, database, db_name, col_name: str) -> None:
7-
self._database = database
8-
self._db_name = db_name
8+
def __init__(self, col_name: str, binary_file: FileIO) -> None:
99
self._col_name = col_name
10+
self._binary_file = binary_file
11+
12+
# Get the data of existing Database or empty database.
13+
self._database = self._binary_file.read()
14+
# print(self._database)
15+
# Initiate a default Collection
16+
17+
# Check the Collection is already exists or no
18+
if self._col_name in self._database.keys():
19+
20+
# Get the existing Collection
21+
self._collection: list = self._database[self._col_name]
22+
23+
else:
24+
# Create new Collection
25+
self._database[self._col_name] = []
26+
self._collection: list = self._database[self._col_name]
27+
28+
def insert(self, document: dict) -> None:
29+
"""
30+
Inserts a single Document into the Database.
31+
32+
Document should be JSON Object.
33+
34+
:param document: Document to insert into database
35+
:return: None
36+
"""
37+
38+
# Append the document into the Collection
39+
self._collection.append(document)
40+
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)
1046

11-
self._database[self._col_name] = []
12-
# self._collection = list(self._database[self._col_name])
13-
14-
def insert(self, doc: dict) -> None:
15-
_doc = doc
16-
print(_doc)
17-
print(self._database)
18-
# data = list(self._collection).append(doc)
19-
data = self._database[self._col_name].append(_doc)
20-
print(self._database)
21-
with open(f"{self._db_name}.json", "w") as f:
22-
json.dump(self._database, f, indent=4)
2347

filexdb/database.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from typing import Dict, Type, List
22

33
from .collection import Collection
4+
from .fileio import BinaryFileIO
45

56

67
class FileXdb:
78

8-
def __init__(self, db_name: str, data_dir: str | None):
9+
def __init__(self, db_name: str, data_dir: str = None):
910
"""
1011
Creates a Databased in ``data_dir`` Directory named ``db_name``.
1112
@@ -18,13 +19,23 @@ def __init__(self, db_name: str, data_dir: str | None):
1819
:param data_dir: Where the Database will be stored.
1920
"""
2021

21-
self._database = {}
2222
self._db_name = db_name
2323
self._data_dir = data_dir
2424

25+
# Creating an instance of FileIO to Read Write Database-File.
26+
self._binary_file = BinaryFileIO(self._db_name, self._data_dir)
27+
2528
def collection(self, col_name: str) -> Collection:
26-
collection = Collection(self._database, self._db_name, col_name)
27-
# self.database[col_name] = collection
29+
"""
30+
Creates a brand-new Collection if the Collection is not exists.
31+
32+
If Collection is already exists then it interact with it.
33+
34+
:param col_name: Collection name to interact with.
35+
:return: An instance of Collection Baseclass.
36+
"""
37+
# Initiating collection
38+
collection = Collection(col_name, self._binary_file)
2839

2940
return collection
3041

0 commit comments

Comments
 (0)