A Pydantic-powered ODM (object-document mapper) for TinyDB
Project description
tinydantic
tinydantic is a simple Python object-document mapper (ODM) for the TinyDB document database. It uses Pydantic—the most widely used data validation library—for document model definition and validation.
[!NOTE]
tinydanticis under active development. Releases follow the SemVer versioning spec:Major version zero (0.y.z) is for initial development. Anything MAY change at any time. The public API SHOULD NOT be considered stable.
Minor releases may include breaking changes until v1.0 — check the changelog when upgrading. Feedback is welcome!
Full documentation is available at tinydantic.dev.
Table of Contents
Introduction
tinydantic is a wrapper library around TinyDB that enables using Pydantic models to manage documents stored in a TinyDB database. By specifying Python type annotations for each field in a document model, data validation is handled automatically by Pydantic when instantiating a model from the database.
Installation
tinydantic is available on PyPI and can be installed with pip. Easy peasy lemon squeezy 🍋
pip install tinydantic
Basic Example
Here's a basic example showing how to create, insert, and query for a document using tinydantic.
Using tinydantic models
First, create a TinyDB database where the documents will be stored. In this example, we're using an in-memory database, but TinyDB supports other storage types which can store the database persistently as JSON, YAML, etc.
>>> from tinydb import TinyDB
>>> from tinydb.storages import MemoryStorage
>>> db = TinyDB(storage=MemoryStorage)
Create a User document model, configuring it to store documents in the users table of the db database.
[!TIP]
Since
Useris a subclass of [TinydanticModel][tinydantic.TinydanticModel] (which itself is a subclass of [pydantic.BaseModel][]),Useris also a Pydantic model! You have all the power of Pydantic models when creating atinydanticdocument model.
>>> from pydantic import EmailStr
>>> from tinydantic import TinydanticModel
>>> class User(TinydanticModel, database=db, table_name='users'):
... name: str
... email: EmailStr
Create a new User instance for a user named "Alice".
>>> alice = User(name='Alice', email='alice@example.com')
Insert alice into the database.
>>> alice.insert()
User(id=1, name='Alice', email='alice@example.com')
Query the database for users with the name "Alice". Notice that this returns a User instance that was automatically validated by Pydantic!
>>> User.get(User.name == 'Alice')
User(id=1, name='Alice', email='alice@example.com')
Comparison to TinyDB
Since tinydantic is built on top of TinyDB, you can still use tinydb to interact with the database directly if needed.
For comparison, let's try to accomplish the same task as shown above using only TinyDB without tinydantic. For this example, we'll continue using the same database (db) we created earlier.
>>> users_table = db.table('users')
>>> users_table.insert({'name': 'Bob', 'email': 'bob@example.com'})
2
>>> from tinydb import where
>>> users_table.get(where('name') == 'Bob')
{'name': 'Bob', 'email': 'bob@example.com'}
Notice, that TinyDB does not impose any restrictions on the document we insert into the database table. Additionally, there is no automatic parsing or validation of the data returned from the database.
Pydantic validation in action
What happens if an invalid document is somehow returned from the database?
Let's set up a contrived example to test the automatic Pydantic validation.
First, we'll manually insert a document that is missing the email field from the User model we created earlier.
>>> users_table.insert({'name': 'Carol'})
3
Next, we'll query for users named "Bob" using the tinydantic model we created earlier.
>>> User.get(User.name == 'Carol')
Traceback (most recent call last):
...
pydantic_core._pydantic_core.ValidationError: 1 validation error for User
email
Field required [type=missing, input_value={'name': 'Carol'}, input_type=Document]
As you can see, Pydantic produced a ValidationError because the document returned from the database is missing the email field required by the User model.
License
See the LICENSE file for copyright & license information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tinydantic-0.2.0.tar.gz.
File metadata
- Download URL: tinydantic-0.2.0.tar.gz
- Upload date:
- Size: 26.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3964c31f6d171214e60dec2efabfef2caf8cdf2a140a249396549192fd38b7b4
|
|
| MD5 |
05d2a78218b4bb863c80fdddae3bf60a
|
|
| BLAKE2b-256 |
f0bf18cc10af4570bca77a22b476514818df7cb239010ec01731974ea1afd81a
|
Provenance
The following attestation bundles were made for tinydantic-0.2.0.tar.gz:
Publisher:
publish-python-package.yaml on tinydantic/tinydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tinydantic-0.2.0.tar.gz -
Subject digest:
3964c31f6d171214e60dec2efabfef2caf8cdf2a140a249396549192fd38b7b4 - Sigstore transparency entry: 2088365955
- Sigstore integration time:
-
Permalink:
tinydantic/tinydantic@3b55de34baff5d34e2ad17c8f17119752aed56e3 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/tinydantic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python-package.yaml@3b55de34baff5d34e2ad17c8f17119752aed56e3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file tinydantic-0.2.0-py3-none-any.whl.
File metadata
- Download URL: tinydantic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 28.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11639c39678f78c8758797881754a824eb251074b7e9f5924e50446e822b0b34
|
|
| MD5 |
027e4ce33358c38f9d47d4398d4ec7de
|
|
| BLAKE2b-256 |
fdcbc8e8cb819aaca4a7586397d0182f0ba9100c0c9180ea7f18aa7f07207070
|
Provenance
The following attestation bundles were made for tinydantic-0.2.0-py3-none-any.whl:
Publisher:
publish-python-package.yaml on tinydantic/tinydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tinydantic-0.2.0-py3-none-any.whl -
Subject digest:
11639c39678f78c8758797881754a824eb251074b7e9f5924e50446e822b0b34 - Sigstore transparency entry: 2088366090
- Sigstore integration time:
-
Permalink:
tinydantic/tinydantic@3b55de34baff5d34e2ad17c8f17119752aed56e3 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/tinydantic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python-package.yaml@3b55de34baff5d34e2ad17c8f17119752aed56e3 -
Trigger Event:
push
-
Statement type: