EncryptedBetterJSONStorage is a faster 'Storage Type' for TinyDB by taking BetterJSONStorage & TinyDB JSONStorage as base. It uses the faster Orjson library for parsing the JSON , BLOSC2 for compression and cryptography for encryption.
Parsing, compressing, and writing to the file is done by a seperate thread so reads don't get blocked by slow fileIO. Smaller filesizes result in faster reading and writing (less diskIO). Even Reading is all done from memory.
These optimizations result in much faster reading and writing without loss of functionality.
A goal for the EncryptedBetterJSONStorage project is to provide a drop in replacement for the default JSONStorage with added functionality of encryption and compression.
An example of how to implement EncryptedBetterJSONStorage can be found below. Anything else can be found in the TinyDB docs.
Install EncryptedBetterJSONStorage as follows
python setup.py install
from tinydb import TinyDB
from EncryptedBetterJSONStorage import EncryptedBetterJSONStorage
path = 'relative/path/to/file.db'
with TinyDB(path,encryption_key=b"KeY", encryption=True ,compression=True, access_mode="r+", storage=EncryptedBetterJSONStorage) as db:
db.insert({'int': 1, 'char': 'a'})
db.insert({'int': 1, 'char': 'b'})
one difference from TinyDB default JSONStorage is that BetterJSONStorage is ReadOnly by default. use access_mode='r+' or 'rb+' if you want to write as well.
All arguments except for the storage and access_mode argument are forwarded to the underlying storage. You can use this to pass additional keyword arguments to orjson.dumps(…) method.
For all options see the orjson documentation.
with TinyDB('file.db',encryption_key=b"KeY", encryption=True ,compression=True, option=orjson.OPT_NAIVE_UTC, storage=BetterEncryptedJSONStorage) as db:
You can change the encryption key of the storage by accessing the storage property of your TinyDB docs database.
db = ...
db.storage.change_encryption_key("NEW_KEY")