Skip to content

Commit a06e79c

Browse files
Development
- Fixed issues with Data not knowing what the key name is
1 parent 8de4e0a commit a06e79c

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

record/data.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,19 @@ def add(self,
135135
"""
136136

137137
# Add the record and store the ID
138-
self._value['_id'] = self._storage.add(self._value, conflict, revision)
138+
self._value[self._storage._key] = self._storage.add(
139+
self._value,
140+
conflict,
141+
revision
142+
)
139143

140144
# Clear changes and other flags
141145
self._changes = None
142146
self._errors = None
143147
self._overwrite = False
144148

145149
# Return the ID
146-
return self._value['_id']
150+
return self._value[self._storage._key]
147151

148152
def changed(self) -> bool:
149153
"""Changed
@@ -221,15 +225,21 @@ def save(self, revision_info: dict = None) -> bool:
221225

222226
# Pass the current value to the storage's save method
223227
result = self._storage.save(
224-
self._value['_id'], self._value, True, revision_info
228+
self._value[self._storage._key],
229+
self._value,
230+
True,
231+
revision_info
225232
)
226233

227234
# Else, we are just updating
228235
else:
229236

230237
# Pass the changes to the storage's save method
231238
result = self._storage.save(
232-
self._value['_id'], self._changes, False, revision_info
239+
self._value[self._storage._key],
240+
self._changes,
241+
False,
242+
revision_info
233243
)
234244

235245
# If we were successful, clear all flags and changes

record/storage.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@ class Storage(Tree, abc.ABC):
3838
define.Tree
3939
"""
4040

41+
def __init__(self,
42+
details: dict | str,
43+
extend: dict | Literal[False] = undefined,
44+
key_name: str = '_id'
45+
):
46+
"""Constructor
47+
48+
Initialises the instance
49+
50+
Arguments:
51+
details (dict | str): Definition or filepath to load
52+
extend (dict | False): Optional, a dictionary to extend the \
53+
definition
54+
key_name (str): Optional, the name of the primary key field, \
55+
defaults to '_id'
56+
57+
Raises:
58+
KeyError, ValueError
59+
60+
Returns
61+
Storage
62+
"""
63+
64+
# Call the parent constructor
65+
super(Storage, self).__init__(details, extend)
66+
67+
# Store the key name
68+
self._key: str = key_name
69+
4170
@abc.abstractmethod
4271
def add(self,
4372
value: dict,

0 commit comments

Comments
 (0)