Skip to main content

A Pydantic-powered ODM (object-document mapper) for TinyDB

Project description

tinydantic

PyPI - Version PyPI - Python Version Pydantic v2

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] tinydantic is 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 User is a subclass of [TinydanticModel][tinydantic.TinydanticModel] (which itself is a subclass of [pydantic.BaseModel][]), User is also a Pydantic model! You have all the power of Pydantic models when creating a tinydantic document 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tinydantic-0.2.0.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tinydantic-0.2.0-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

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

Hashes for tinydantic-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3964c31f6d171214e60dec2efabfef2caf8cdf2a140a249396549192fd38b7b4
MD5 05d2a78218b4bb863c80fdddae3bf60a
BLAKE2b-256 f0bf18cc10af4570bca77a22b476514818df7cb239010ec01731974ea1afd81a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tinydantic-0.2.0.tar.gz:

Publisher: publish-python-package.yaml on tinydantic/tinydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for tinydantic-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11639c39678f78c8758797881754a824eb251074b7e9f5924e50446e822b0b34
MD5 027e4ce33358c38f9d47d4398d4ec7de
BLAKE2b-256 fdcbc8e8cb819aaca4a7586397d0182f0ba9100c0c9180ea7f18aa7f07207070

See more details on using hashes here.

Provenance

The following attestation bundles were made for tinydantic-0.2.0-py3-none-any.whl:

Publisher: publish-python-package.yaml on tinydantic/tinydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page