Skip to content

Commit

Permalink
Feat: implement direct storage access
Browse files Browse the repository at this point in the history
Closes #258
  • Loading branch information
msiemens committed Mar 16, 2019
1 parent 74b89fa commit 02ee040
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ To modify the default storage for all ``TinyDB`` instances, set the

>>> TinyDB.DEFAULT_STORAGE = MemoryStorage

In case you need to access the storage instance directly, you can use the
``storage`` property of your TinyDB instance. This may be useful to call
method directly on the storage or middleware:

>>> db = TinyDB(storage=CachingMiddleware(MemoryStorage))
<tinydb.middlewares.CachingMiddleware at 0x10991def0>
>>> db.storage.flush()

Middleware
..........

Expand Down
9 changes: 8 additions & 1 deletion tests/test_tinydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from tinydb import TinyDB, where
from tinydb.middlewares import Middleware
from tinydb.middlewares import Middleware, CachingMiddleware
from tinydb.storages import MemoryStorage


Expand Down Expand Up @@ -785,3 +785,10 @@ def test_insert_multiple_with_single_dict(db):
d = {'first': 'John', 'last': 'smith'}
db.insert_multiple(d)
db.close()


def test_access_storage():
assert isinstance(TinyDB(storage=MemoryStorage).storage,
MemoryStorage)
assert isinstance(TinyDB(storage=CachingMiddleware(MemoryStorage)).storage,
CachingMiddleware)
9 changes: 9 additions & 0 deletions tinydb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ def purge_table(self, name):
proxy = StorageProxy(self._storage, name)
proxy.purge_table()

@property
def storage(self):
"""
Access the storage used for this TinyDB instance.
:return: This instance's storage
"""
return self._storage

def close(self):
"""
Close the database.
Expand Down

0 comments on commit 02ee040

Please sign in to comment.