1
1
import json
2
+ import copy
2
3
import shortuuid
3
4
import aiofiles
4
5
@@ -36,8 +37,9 @@ def _override_only_unset(__dict: dict, __target: dict):
36
37
new_target = dict ()
37
38
for field in unset_fields :
38
39
new_target [field ] = __target [field ]
39
- __dict .update (new_target )
40
- return __dict
40
+ new_dict = copy .deepcopy (__dict )
41
+ new_dict .update (new_target )
42
+ return new_dict
41
43
42
44
43
45
class Database (dict ):
@@ -51,6 +53,7 @@ class Database(dict):
51
53
'created_at' ,
52
54
'updated_at' ,
53
55
]
56
+ _memory = dict ()
54
57
55
58
def __init__ (self , * arg , ** kwargs ):
56
59
self .__dict__ = dict (* arg , ** kwargs )
@@ -63,6 +66,7 @@ def __init__(self, *arg, **kwargs):
63
66
self .__records__ : dict (),
64
67
}
65
68
self .__dict__ = _override_only_unset (self .__dict__ , defaults )
69
+ self .commit ()
66
70
67
71
def __getitem__ (self , key : str ) -> Any :
68
72
try :
@@ -75,6 +79,7 @@ def __setitem__(self, key, value) -> None:
75
79
76
80
def __delitem__ (self , key ) -> None :
77
81
try :
82
+ self ._update_timestamp ()
78
83
return self .records .__delitem__ (key )
79
84
except KeyError :
80
85
return None
@@ -109,12 +114,18 @@ def metadata(self) -> dict:
109
114
meta [column ] = self .__dict__ .get (column )
110
115
return meta
111
116
117
+ def _update_timestamp (self ) -> None :
118
+ self .__dict__ .update ({
119
+ 'updated_at' : datetime .now ().isoformat ()
120
+ })
121
+
112
122
def get (self , key : Union [str , List [str ]], default = None ) -> Union [Any , List [Any ]]:
113
123
_type , _keys = _from_maybe_list (key )
114
124
values = [self .records .get (k , default ) for k in _keys ]
115
125
return _return_maybe (_type , values )
116
126
117
127
def update (self , mapping : Union [dict , tuple ] = (), ** kwargs ) -> None :
128
+ self ._update_timestamp ()
118
129
return self .records .update (mapping , ** kwargs )
119
130
120
131
def modify (
@@ -132,6 +143,7 @@ def modify(
132
143
target = dict ()
133
144
target [_id ] = _value
134
145
self .records .update (target )
146
+ self ._update_timestamp ()
135
147
136
148
def add (self , item : Union [Any , List [Any ]]) -> Union [str , List [str ]]:
137
149
_type , _items = _from_maybe_list (item )
@@ -142,18 +154,21 @@ def add(self, item: Union[Any, List[Any]]) -> Union[str, List[str]]:
142
154
self .records [uid ] = i
143
155
ids .append (uid )
144
156
157
+ self ._update_timestamp ()
145
158
return _return_maybe (_type , ids )
146
159
147
160
def remove (self , key : Union [str , List [str ]]) -> Union [str , List [str ]]:
148
161
_type , _keys = _from_maybe_list (key )
149
162
popped = [self .records .pop (key ) for key in _keys ]
163
+ self ._update_timestamp ()
150
164
return _return_maybe (_type , popped )
151
165
152
166
def all (self ) -> List [Any ]:
153
167
return self .records .values ()
154
168
155
169
def clear (self ) -> None :
156
170
self .records .clear ()
171
+ self ._update_timestamp ()
157
172
158
173
def find (self , func : Callable [..., bool ]) -> List [str ]:
159
174
ids = []
@@ -187,10 +202,10 @@ def drop(self) -> int:
187
202
return del_count
188
203
189
204
def commit (self ) -> None :
190
- pass
205
+ self . _memory = copy . deepcopy ( self . __dict__ )
191
206
192
207
def rollback (self ) -> None :
193
- pass
208
+ self . __dict__ = copy . deepcopy ( self . _memory )
194
209
195
210
async def save (
196
211
self ,
0 commit comments