API Documentation¶
tinydb.database¶
- class tinydb.database.TinyDB(*args, **kwargs)[source]¶
The main class of TinyDB.
The
TinyDBclass is responsible for creating the storage class instance that will store this database’s documents, managing the database tables as well as providing access to the default table.For table management, a simple
dictis used that stores the table class instances accessible using their table name.Default table access is provided by forwarding all unknown method calls and property access operations to the default table by implementing
__getattr__.When creating a new instance, all arguments and keyword arguments (except for
storage) will be passed to the storage class that is provided. If no storage class is specified,JSONStoragewill be used.Customization
For customization, the following class variables can be set:
table_classdefines the class that is used to create tables,default_table_namedefines the name of the default table, anddefault_storage_classwill define the class that will be used to create storage instances if no other storage is passed.
Added in version 4.0.
Data Storage Model
Data is stored using a storage class that provides persistence for a
dictinstance. Thisdictcontains all tables and their data. The data is modelled like this:{ 'table1': { 0: {document...}, 1: {document...}, }, 'table2': { ... } }
Each entry in this
dictuses the table name as its key and adictof documents as its value. The documentdictcontains document IDs as keys and the documents themselves as values.- Parameters:
storage – The class of the storage to use. Will be initialized with
argsandkwargs.
- table_class¶
The class that will be used to create table instances
Added in version 4.0.
alias of
Table
- default_table_name = '_default'¶
The name of the default table
Added in version 4.0.
- default_storage_class¶
The class that will be used by default to create storage instances
Added in version 4.0.
alias of
JSONStorage
- table(name: str, **kwargs) Table[source]¶
Get access to a specific table.
If the table hasn’t been accessed yet, a new table instance will be created using the
table_classclass. Otherwise, the previously created table instance will be returned.All further options besides the name are passed to the table class which by default is
Table. Check its documentation for further parameters you can pass.- Parameters:
name – The name of the table.
kwargs – Keyword arguments to pass to the table class constructor
- tables() set[str][source]¶
Get the names of all tables in the database.
- Returns:
a set of table names
- drop_table(name: str) None[source]¶
Drop a specific table from the database. CANNOT BE REVERSED!
- Parameters:
name – The name of the table to drop.
- property storage: Storage¶
Get the storage instance used for this TinyDB instance.
- Returns:
This instance’s storage
- Return type:
- close() None[source]¶
Close the database.
This may be needed if the storage instance used for this database needs to perform cleanup operations like closing file handles.
To ensure this method is called, the TinyDB instance can be used as a context manager:
with TinyDB('data.json') as db: db.insert({'foo': 'bar'})
Upon leaving this context, the
closemethod will be called.
tinydb.table¶
- class tinydb.table.Table(storage: Storage, name: str, cache_size: int = 10, persist_empty: bool = False)[source]¶
Represents a single TinyDB table.
It provides methods for accessing and manipulating documents.
Query Cache
As an optimization, a query cache is implemented using a
LRUCache. This class mimics the interface of a normaldict, but starts to remove the least-recently used entries once a threshold is reached.The query cache is updated on every search operation. When writing data, the whole cache is discarded as the query results may have changed.
Customization
For customization, the following class variables can be set:
document_classdefines the class that is used to represent documents,document_id_classdefines the class that is used to represent document IDs,query_cache_classdefines the class that is used for the query cachedefault_query_cache_capacitydefines the default capacity of the query cache
Added in version 4.0.
- Parameters:
storage – The storage instance to use for this table
name – The table name
cache_size – Maximum capacity of query cache
persist_empty – Store new table even with no operations on it
- document_id_class¶
The class used to represent a document ID
Added in version 4.0.
alias of
int
- default_query_cache_capacity = 10¶
The default capacity of the query cache
Added in version 4.0.
- __init__(storage: Storage, name: str, cache_size: int = 10, persist_empty: bool = False)[source]¶
Create a table instance.
- property name: str¶
Get the table name.
- insert(document: Mapping) int[source]¶
Insert a new document into the table.
- Parameters:
document – the document to insert
- Returns:
the inserted document’s ID
- insert_multiple(documents: Iterable[Mapping]) list[int][source]¶
Insert multiple documents into the table.
- Parameters:
documents – an Iterable of documents to insert
- Returns:
a list containing the inserted documents’ IDs
- all() list[Document][source]¶
Get all documents stored in the table.
- Returns:
a list with all documents.
- search(cond: QueryLike) list[Document][source]¶
Search for all documents matching a ‘where’ cond.
- Parameters:
cond – the condition to check against
- Returns:
list of matching documents
- get() NoReturn[source]¶
- get(cond: QueryLike, doc_id: None = None, doc_ids: None = None) Document | None
- get(*, cond: QueryLike, doc_id: None = None, doc_ids: None = None) Document | None
- get(cond: QueryLike | None, doc_id: int, doc_ids: list | None = None) Document | None
- get(*, cond: QueryLike | None = None, doc_id: int, doc_ids: list | None = None) Document | None
- get(cond: QueryLike | None, doc_id: None, doc_ids: list) list[Document]
- get(cond: QueryLike | None, *, doc_id: None = None, doc_ids: list) list[Document]
- get(*, cond: QueryLike | None = None, doc_id: None = None, doc_ids: list) list[Document]
Get exactly one document specified by a query or a document ID. However, if multiple document IDs are given then returns all documents in a list.
Returns
Noneif the document doesn’t exist.- Parameters:
cond – the condition to check against
doc_id – the document’s ID
doc_ids – the document’s IDs(multiple)
- Returns:
the document(s) or
None
- contains(cond: QueryLike | None = None, doc_id: int | None = None) bool[source]¶
Check whether the database contains a document matching a query or an ID.
If
doc_idis set, it checks if the db contains the specified ID.- Parameters:
cond – the condition use
doc_id – the document ID to look for
- update(fields: Mapping | Callable[[Mapping], None], cond: QueryLike | None = None, doc_ids: Iterable[int] | None = None) list[int][source]¶
Update all matching documents to have a given set of fields.
When
doc_idsis given, IDs that don’t refer to an existing document are silently skipped, and the returned list only contains the IDs that were actually updated.- Parameters:
fields – the fields that the matching documents will have or a method that will update the documents
cond – which documents to update
doc_ids – a list of document IDs
- Returns:
a list containing the updated document’s ID
- update_multiple(updates: Iterable[tuple[Mapping | Callable[[Mapping], None], QueryLike]]) list[int][source]¶
Update all matching documents to have a given set of fields.
- Returns:
a list containing the updated document’s ID
- upsert(document: Mapping, cond: QueryLike | None = None) list[int][source]¶
Update documents, if they exist, insert them otherwise.
Note: This will update all documents matching the query. For example, if the query matches 3 documents, all 3 will be updated with the new data. If no documents match, a new one is inserted.
Document argument can be a tinydb.table.Document object if you want to specify a doc_id.
- Parameters:
document – the document to insert or the fields to update
cond – which document to look for, optional if you’ve passed a
Document with a doc_id :returns: a list containing the updated documents’ IDs
- remove(cond: QueryLike | None = None, doc_ids: Iterable[int] | None = None) list[int][source]¶
Remove all matching documents.
When
doc_idsis given, IDs that don’t refer to an existing document are silently skipped, and the returned list only contains the IDs that were actually removed.- Parameters:
cond – the condition to check against
doc_ids – a list of document IDs
- Returns:
a list containing the removed documents’ ID
tinydb.queries¶
- class tinydb.queries.Query[source]¶
TinyDB Queries.
Allows building queries for TinyDB databases. There are two main ways of using queries:
ORM-like usage:
>>> User = Query() >>> db.search(User.name == 'John Doe') >>> db.search(User['logged-in'] == True)
Classical usage:
>>> db.search(where('value') == True)
Note that
where(...)is a shorthand forQuery(...)allowing for a more fluent syntax.Besides the methods documented here you can combine queries using the binary AND and OR operators:
>>> # Binary AND: >>> db.search((where('field1').exists()) & (where('field2') == 5)) >>> # Binary OR: >>> db.search((where('field1').exists()) | (where('field2') == 5))
Queries are executed by calling the resulting object. They expect to get the document to test as the first argument and return
TrueorFalsedepending on whether the documents match the query or not.- __eq__(rhs: Any)[source]¶
Test a dict value for equality.
>>> Query().f1 == 42
- Parameters:
rhs – The value to compare against
- __ne__(rhs: Any)[source]¶
Test a dict value for inequality.
>>> Query().f1 != 42
- Parameters:
rhs – The value to compare against
- __lt__(rhs: Any) QueryInstance[source]¶
Test a dict value for being lower than another value.
>>> Query().f1 < 42
- Parameters:
rhs – The value to compare against
- __le__(rhs: Any) QueryInstance[source]¶
Test a dict value for being lower than or equal to another value.
>>> where('f1') <= 42
- Parameters:
rhs – The value to compare against
- __gt__(rhs: Any) QueryInstance[source]¶
Test a dict value for being greater than another value.
>>> Query().f1 > 42
- Parameters:
rhs – The value to compare against
- __ge__(rhs: Any) QueryInstance[source]¶
Test a dict value for being greater than or equal to another value.
>>> Query().f1 >= 42
- Parameters:
rhs – The value to compare against
- exists() QueryInstance[source]¶
Test for a dict where a provided key exists.
>>> Query().f1.exists()
- matches(regex: str, flags: int = 0) QueryInstance[source]¶
Run a regex test against a dict value (whole string has to match).
>>> Query().f1.matches(r'^\w+$')
- Parameters:
regex – The regular expression to use for matching
flags – regex flags to pass to
re.match
- search(regex: str, flags: int = 0) QueryInstance[source]¶
Run a regex test against a dict value (only substring string has to match).
>>> Query().f1.search(r'^\w+$')
- Parameters:
regex – The regular expression to use for matching
flags – regex flags to pass to
re.match
- test(func: Callable[[Mapping], bool], *args) QueryInstance[source]¶
Run a user-defined test function against a dict value.
>>> def test_func(val): ... return val == 42 ... >>> Query().f1.test(test_func)
Warning
The test function provided needs to be deterministic (returning the same value when provided with the same arguments), otherwise this may mess up the query cache that
Tableimplements.- Parameters:
func – The function to call, passing the dict as the first argument
args – Additional arguments to pass to the test function
- any(cond: QueryInstance | list[Any]) QueryInstance[source]¶
Check if a condition is met by any document in a list, where a condition can also be a sequence (e.g. list).
>>> Query().f1.any(Query().f2 == 1)
Matches:
{'f1': [{'f2': 1}, {'f2': 0}]}
>>> Query().f1.any([1, 2, 3])
Matches:
{'f1': [1, 2]} {'f1': [3, 4, 5]}
- Parameters:
cond – Either a query that at least one document has to match or a list of which at least one document has to be contained in the tested document.
- all(cond: QueryInstance | list[Any]) QueryInstance[source]¶
Check if a condition is met by all documents in a list, where a condition can also be a sequence (e.g. list).
>>> Query().f1.all(Query().f2 == 1)
Matches:
{'f1': [{'f2': 1}, {'f2': 1}]}
>>> Query().f1.all([1, 2, 3])
Matches:
{'f1': [1, 2, 3, 4, 5]}
- Parameters:
cond – Either a query that all documents have to match or a list which has to be contained in the tested document.
- one_of(items: list[Any]) QueryInstance[source]¶
Check if the value is contained in a list or generator.
>>> Query().f1.one_of(['value 1', 'value 2'])
- Parameters:
items – The list of items to check with
- noop() QueryInstance[source]¶
Always evaluate to
True.Useful for having a base value when composing queries dynamically.
- class tinydb.queries.QueryInstance(test: Callable[[Mapping], bool], hashval: tuple | None)[source]¶
A query instance.
This is the object on which the actual query operations are performed. The
Queryclass acts like a query builder and generatesQueryInstanceobjects which will evaluate their query against a given document when called.Query instances can be combined using logical OR and AND and inverted using logical NOT.
In order to be usable in a query cache, a query needs to have a stable hash value with the same query always returning the same hash. That way a query instance can be used as a key in a dictionary.
- __call__(value: Mapping) bool[source]¶
Evaluate the query to check if it matches a specified value.
- Parameters:
value – The value to check.
- Returns:
Whether the value matches this query.
- __or__(other: QueryInstance) QueryInstance[source]¶
Return self|value.
tinydb.operations¶
A collection of update operations for TinyDB.
They are used for updates like this:
>>> db.update(delete('foo'), where('foo') == 2)
This would delete the foo field from all documents where foo equals 2.
- tinydb.operations.delete(field: str) Callable[[MutableMapping], None][source]¶
Delete a given field from the document.
- tinydb.operations.add(field: str, n: int | float) Callable[[MutableMapping], None][source]¶
Add
nto a given field in the document.
- tinydb.operations.subtract(field: str, n: int | float) Callable[[MutableMapping], None][source]¶
Subtract
nto a given field in the document.
- tinydb.operations.set(field: str, val: Any) Callable[[MutableMapping], None][source]¶
Set a given field to
val.
tinydb.storage¶
Contains the base class for storages and
implementations.
- class tinydb.storages.Storage[source]¶
The abstract base class for all Storages.
A Storage (de)serializes the current state of the database and stores it in some place (memory, file on disk, …).
- class tinydb.storages.JSONStorage(path: str, create_dirs=False, encoding=None, access_mode='r+', **kwargs)[source]¶
Store the data in a JSON file.
- __init__(path: str, create_dirs=False, encoding=None, access_mode='r+', **kwargs)[source]¶
Create a new instance.
Also creates the storage file, if it doesn’t exist and the access mode is appropriate for writing.
Note: Using an access mode other than r or r+ will probably lead to data loss or data corruption!
Note: Never pass untrusted or user-controlled code as
kwargsmembers likeclsordefaultwill be called on every write operation.- Parameters:
path – Where to store the JSON data.
access_mode (str) – mode in which the file is opened (r, r+)
tinydb.middlewares¶
Contains the base class for
middlewares and implementations.
- class tinydb.middlewares.Middleware[source]¶
The base class for all Middlewares.
Middlewares hook into the read/write process of TinyDB allowing you to extend the behaviour by adding caching, logging, …
If
read()orwrite()are not overloaded, they will be forwarded directly to the storage instance.- read()¶
Read the last stored state.
- write(data)¶
Write the current state of the database to the storage.
- close()¶
Optional: Close open file handles, etc.
- class tinydb.middlewares.CachingMiddleware(storage_cls)[source]¶
Add some caching to TinyDB.
This Middleware aims to improve the performance of TinyDB by writing only the last DB state every
WRITE_CACHE_SIZEtime and reading always from cache.- WRITE_CACHE_SIZE = 1000¶
The number of write operations to cache before writing to disc
tinydb.utils¶
- class tinydb.utils.LRUCache(capacity=None)[source]¶
A least-recently used (LRU) cache with a fixed cache size.
This class acts as a dictionary but has a limited size. If the number of entries in the cache exceeds the cache size, the least-recently accessed entry will be discarded.
This is implemented using an
OrderedDict. On every access the accessed entry is moved to the front by re-inserting it into theOrderedDict. When adding an entry and the cache size is exceeded, the last entry will be discarded.- __weakref__¶
list of weak references to the object