Introduction to TinyDB
masterdict). It is optimized for simplicity and ease of use, making it a suitable alternative to SQL databases or external database servers for smaller projects.repository·master·Indexed 27 days ago
https://github.com/msiemens/tinydbTinyDB is a lightweight, pure-Python, document-oriented database designed for small applications. It stores data as Python dictionaries and provides a clean API for querying and managing data without external dependencies. It includes built-in JSONStorage and MemoryStorage, and supports extensibility through custom Storage classes and Middlewares. Version 4.8.2.
dict). It is optimized for simplicity and ease of use, making it a suitable alternative to SQL databases or external database servers for smaller projects.TinyDB is a document-oriented, pure-Python database designed for simplicity and ease of use. It stores documents as Python dict objects and requires no external server or PyPI dependencies.
If your requirements include the features above, consider alternatives like SQLite, MongoDB, Buzhug, or CodernityDB.
Several extensions are available to improve the performance of TinyDB:
orjson library for parsing and BLOSC for compression. (Status: stable)Query class, and perform basic operations like insert and search.upsert operation updates documents that match a query, or inserts the document if no match is found. You can also use a Document with a doc_id to perform an upsert based on the ID.You can combine or negate queries using logical operators.
CRITICAL: Due to Python's operator precedence, you must wrap individual conditions in parentheses when using & (AND) or | (OR), and you must wrap the query you wish to negate in parentheses when using ~ (NOT).
Note: Comparisons only support literal values on the right-hand side. For field-to-field comparisons, use a lambda predicate.
You can install TinyDB from PyPI using pip, or install the latest development version directly from GitHub after downloading and unpacking the repository.
$ pip install tinydbUpdate documents using either a dictionary of fields or an operation function. To update all documents in the database, omit the query argument. For complex updates (like deleting a key or incrementing a value), pass an operation function instead of a dictionary.
# Update all documents
db.update({'foo': 'bar'})
# Update matching documents using an operation (e.g., delete a key)
from tinydb.operations import delete
db.update(delete('key1'), User.name == 'John')
# Perform multiple different updates at once
db.update_multiple([
({'int': 2}, where('char') == 'a'),
({'int': 4}, where('char') == 'b'),
])
# Mix dictionary updates with operations in update_multiple
db.update_multiple([
({'int': 2}, where('char') == 'a'),
({delete('int'), where('char') == 'b}),
])To use TinyDB, import TinyDB and initialize it with a file path. You can then use the .insert() method to add documents (dictionaries) to the database.
from tinydb import TinyDB, Query
db = TinyDB('/path/to/db.json')
db.insert({'int': 1, 'char': 'a'})
db.insert({'int': 1, 'char': 'b'})You can construct queries using the Query object, which allows for attribute-style access to fields. This syntax supports nested fields and dictionary-style access for field names that are not valid Python identifiers (e.g., containing hyphens).
To handle complex data types that JSON cannot serialize, you can implement a custom storage class using libraries like pickle or PyYAML.
from tinydb import Query
User = Query()
# Basic field access
db.search(User.name == 'John')
# Nested field access
db.search(User.birthday.year == 1990)
# Dictionary-style access for invalid Python identifiers
db.search(User['country-code'] == 'foo')
# Using a transform function on a field
from unidecode import unidecode
db.search(User.name.map(unidecode) == 'Jose')asyncio-aware contexts without the overhead of slow synchronous IO, use aiotinydb. It provides an asyncio compatibility shim for TinyDB. (Status: stable)