You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,19 +24,19 @@ I will try to keep up with the latest version of `TinyDB`.
24
24
25
25
# New Features
26
26
27
-
***Event hooks**: You can now use event hooks to do something before or after an operation. See [Event Hooks](#event-hooks) for more details.
27
+
***Event Hooks**: You can now use event hooks to do something before or after an operation. See [Event Hooks](#event-hooks) for more details.
28
28
29
-
***Redesigned ID & Doc class**: You can [replace](#replacing-id-&-document-class) and [customise them](#customise-id-class) more pleasingly.
29
+
***Redesigned ID & Doc Class**: You can [replace](#replacing-id-&-document-class) and [customise them](#customise-id-class) more pleasingly.
30
30
31
-
***DB-level caching**: This significantly improves the performance of all operations. However, the responsibility of converting the data to the correct type is transferred to the Storage[^2][^disable-db-level].
31
+
***DB-level Caching**: This significantly improves the performance of all operations. However, the responsibility of converting the data to the correct type is transferred to the Storage[^2][^disable-db-level].
32
32
33
33
***Built-in `Modifier`**: Use `Modifier` to easily [encrypt](#encryption), [compress](./docs/Modifier.md#Compression) and [extend types](./docs/Modifier.md#Conversion) of your database. Sure you can do much more than these. _(See [Modifier](./docs/Modifier.md))_
34
34
35
35
***Isolation Level**: Performance or ACID? It's up to you[^isolevel].
36
36
37
37
***Atomic Write**: **A**CID!
38
38
39
-
***Batch search by IDs**: `search` method now takes an extra `doc_ids` argument (works like an additional condition)
39
+
***Batch Search By IDs**: `search` method now takes an extra `doc_ids` argument (works like an additional condition)
This subclass contains methods to convert the data to a different format.
126
127
127
-
## `ExtendedJSON`
128
+
###`ExtendedJSON`
128
129
129
130
*`events`: `write.pre`, `read.post`
130
131
*`input`: `dict`
131
132
*`output`: `dict`
132
133
133
134
This method allows JSONStorage to store more data types.
134
135
135
-
### Extended Types
136
+
#### Extended Types
137
+
136
138
*`uuid.UUID`
137
139
*`datetime.datetime`: Converted to `ISO 8601` format.
138
140
*`datetime.timestamp`
@@ -149,11 +151,11 @@ db = TinyDB("db.json")
149
151
Modifier.Converter.ExtendedJSON(db)
150
152
```
151
153
152
-
### Customise Extended Types
154
+
####Customise Extended Types
153
155
154
156
By passing `type_hooks` and `marker_hooks` arguments to the modifier, you can modifiy the behaviour of the conversion.
155
157
156
-
### To modify `type_hooks`
158
+
#####To modify `type_hooks`
157
159
158
160
```Python
159
161
type_hooks = {
@@ -162,24 +164,56 @@ type_hooks = {
162
164
}
163
165
```
164
166
165
-
The first argument is the data to be converted.
166
-
The second argument is the converter function, useful when you are dealing with `Container` types.
167
+
The first argument `x`is the data to be converted.
168
+
The second argument `c`is the converter function, useful when you are dealing with `Container` types.
167
169
168
-
In this example, we convert `int` to a `dict` with a `$int` key (i.e. `42` -> `{"$int": "42"}`), and we remove `set` from the conversion.
170
+
In this example, we convert `int` to a `dict` with a `$int` key and a `str` as the value(i.e. `42` -> `{"$int": "42"}`), and we remove `set` from the conversion.
169
171
Return value of the hook does not necessarily have to be a `dict`. It can be anything that is JSON serializable.
170
172
171
-
### To modify `marker_hooks`
173
+
#####To modify `marker_hooks`
172
174
173
175
A marker is a special key that is used to identify the type of the data.
174
-
We use a MongoDB style marker by default (starts with `'$'`), but you can change it to anything you want.
176
+
We use a MongoDB style marker by default (a `str`starts with `'$'`), but you can change it to anything you want.
175
177
176
178
```Python
177
179
marker_hooks = {
178
180
"$int": lambdax, r: int(x["$int"]),
179
181
}
180
182
```
181
183
182
-
The first argument is the data to be converted.
183
-
The second argument is the reverse converter function, useful when you are dealing with `Container` types.
184
+
The first argument `x`is the data to be converted.
185
+
The second argument `r`is the reverse converter function, useful when you are dealing with `Container` types.
184
186
185
187
In this example, we convert `dict` with the `$int` marker back to an `int` (i.e. `{"$int":"42"}` -> `42`).
188
+
189
+
##### A more complicated example
190
+
191
+
```Python
192
+
import asyncio
193
+
from asynctinydb import TinyDB, Modifier
194
+
195
+
classMyclass:
196
+
def__init__(self, value: bytes):
197
+
self.value = value
198
+
199
+
type_hooks = {
200
+
Myclass: lambdax, c: {"$Myclass": c(x.value)}
201
+
# Converts `bytes` with the converter
202
+
}
203
+
204
+
marker_hooks = {
205
+
"$Myclass": lambdax, r: Myclass(r(x["$Myclass"]))
206
+
# Converts back to `bytes` then pass it to Myclass
0 commit comments