tinydantic 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
1
+ Metadata-Version: 2.4
2
+ Name: tinydantic
3
+ Version: 0.3.0
4
+ Summary: A Pydantic-powered ODM (object-document mapper) for TinyDB
5
+ Keywords: odm,pydantic,tinydb
6
+ Author: Chris Wilson
7
+ Author-email: Chris Wilson <christopher.david.wilson@gmail.com>
8
+ License-Expression: Apache-2.0 OR MIT
9
+ License-File: LICENSES/Apache-2.0.txt
10
+ License-File: LICENSES/CC-BY-4.0.txt
11
+ License-File: LICENSES/MIT.txt
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Framework :: Pydantic :: 2
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Programming Language :: Python :: Implementation :: CPython
23
+ Classifier: Programming Language :: Python
24
+ Classifier: Topic :: Database
25
+ Classifier: Topic :: Software Development :: Libraries
26
+ Classifier: Typing :: Typed
27
+ Requires-Dist: pyyaml>=6.0
28
+ Requires-Dist: pydantic[email]>=2.11
29
+ Requires-Dist: tinydb>=4.8
30
+ Requires-Python: >=3.10
31
+ Project-URL: Changelog, https://github.com/tinydantic/tinydantic/blob/main/CHANGELOG.md
32
+ Project-URL: Documentation, https://tinydantic.dev
33
+ Project-URL: Issues, https://github.com/tinydantic/tinydantic/issues
34
+ Project-URL: Source, https://github.com/tinydantic/tinydantic
35
+ Description-Content-Type: text/markdown
36
+
37
+ # tinydantic
38
+
39
+ <!-- overview-start -->
40
+
41
+ [![PyPI - Version](https://img.shields.io/pypi/v/tinydantic.svg)](https://pypi.org/project/tinydantic) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/tinydantic.svg)](https://pypi.org/project/tinydantic) [![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)
42
+
43
+ `tinydantic` is a simple Python object-document mapper (ODM) for the [TinyDB](https://tinydb.readthedocs.io/en/latest/) document database. It uses [Pydantic](https://docs.pydantic.dev/latest/)—the most widely used data validation library—for document model definition and validation.
44
+
45
+ <!-- prettier-ignore-start -->
46
+
47
+ > [!NOTE]
48
+ > `tinydantic` is an experimental project under active development and should not be considered stable for use in a production environment.
49
+ >
50
+ > Releases follow the [SemVer versioning spec](https://semver.org):
51
+ >
52
+ > > Major version zero (0.y.z) is for initial development. Anything MAY change at any time. The public API SHOULD NOT be considered stable.
53
+ >
54
+ > Minor releases may include breaking changes until v1.0 — check the [changelog](https://github.com/tinydantic/tinydantic/blob/main/CHANGELOG.md) when upgrading.
55
+ >
56
+ > **AI Disclaimer**
57
+ >
58
+ > I started thinking about the idea for this project after discovering TinyDB a couple years ago. However, I never had the time (or a real use case) to finish the implementation. With the release of [Claude Fable 5](https://www.anthropic.com/claude/fable), it seemed like a good opportunity to revisit this idea and try to get an initial prototype of this working with the help of AI. I'm currently experimenting with the implementation generated by Claude to understand what works well and what needs further improvement.
59
+ >
60
+ > I am *not* a NoSQL database expert and it's very possible that there are better implementations and/or interfaces than what Claude has produced in this initial prototype. If you find things that are broken, awkward, missing, or don't follow best practices, please feel free to open an issue or a PR to suggest changes. Feedback is welcome!
61
+
62
+ <!-- prettier-ignore-end -->
63
+
64
+ <!-- overview-end -->
65
+
66
+ Full documentation — including a [quickstart](https://tinydantic.dev/latest/usage/quickstart/) that walks through a complete create-read-update-delete example — is available at [tinydantic.dev](https://tinydantic.dev).
67
+
68
+ ## Table of Contents
69
+
70
+ - [Installation](#installation)
71
+ - [Introduction](#introduction)
72
+ - [License](#license)
73
+
74
+ ## Introduction
75
+
76
+ <!-- introduction-start -->
77
+
78
+ `tinydantic` is a wrapper library around [TinyDB](https://tinydb.readthedocs.io/en/latest/) that enables using [Pydantic](https://docs.pydantic.dev/) 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.
79
+
80
+ <!-- introduction-end -->
81
+
82
+ ## Installation
83
+
84
+ <!-- installation-start -->
85
+
86
+ `tinydantic` is [available on PyPI](https://pypi.org/project/tinydantic/) and can be installed with [pip](https://github.com/pypa/pip). Easy peasy lemon squeezy 🍋
87
+
88
+ ```sh
89
+ pip install tinydantic
90
+ ```
91
+
92
+ <!-- installation-end -->
93
+
94
+ ## License
95
+
96
+ See the [LICENSE](https://github.com/tinydantic/tinydantic/blob/main/LICENSE.md) file for copyright & license information.
@@ -0,0 +1,60 @@
1
+ # tinydantic
2
+
3
+ <!-- overview-start -->
4
+
5
+ [![PyPI - Version](https://img.shields.io/pypi/v/tinydantic.svg)](https://pypi.org/project/tinydantic) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/tinydantic.svg)](https://pypi.org/project/tinydantic) [![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)
6
+
7
+ `tinydantic` is a simple Python object-document mapper (ODM) for the [TinyDB](https://tinydb.readthedocs.io/en/latest/) document database. It uses [Pydantic](https://docs.pydantic.dev/latest/)—the most widely used data validation library—for document model definition and validation.
8
+
9
+ <!-- prettier-ignore-start -->
10
+
11
+ > [!NOTE]
12
+ > `tinydantic` is an experimental project under active development and should not be considered stable for use in a production environment.
13
+ >
14
+ > Releases follow the [SemVer versioning spec](https://semver.org):
15
+ >
16
+ > > Major version zero (0.y.z) is for initial development. Anything MAY change at any time. The public API SHOULD NOT be considered stable.
17
+ >
18
+ > Minor releases may include breaking changes until v1.0 — check the [changelog](https://github.com/tinydantic/tinydantic/blob/main/CHANGELOG.md) when upgrading.
19
+ >
20
+ > **AI Disclaimer**
21
+ >
22
+ > I started thinking about the idea for this project after discovering TinyDB a couple years ago. However, I never had the time (or a real use case) to finish the implementation. With the release of [Claude Fable 5](https://www.anthropic.com/claude/fable), it seemed like a good opportunity to revisit this idea and try to get an initial prototype of this working with the help of AI. I'm currently experimenting with the implementation generated by Claude to understand what works well and what needs further improvement.
23
+ >
24
+ > I am *not* a NoSQL database expert and it's very possible that there are better implementations and/or interfaces than what Claude has produced in this initial prototype. If you find things that are broken, awkward, missing, or don't follow best practices, please feel free to open an issue or a PR to suggest changes. Feedback is welcome!
25
+
26
+ <!-- prettier-ignore-end -->
27
+
28
+ <!-- overview-end -->
29
+
30
+ Full documentation — including a [quickstart](https://tinydantic.dev/latest/usage/quickstart/) that walks through a complete create-read-update-delete example — is available at [tinydantic.dev](https://tinydantic.dev).
31
+
32
+ ## Table of Contents
33
+
34
+ - [Installation](#installation)
35
+ - [Introduction](#introduction)
36
+ - [License](#license)
37
+
38
+ ## Introduction
39
+
40
+ <!-- introduction-start -->
41
+
42
+ `tinydantic` is a wrapper library around [TinyDB](https://tinydb.readthedocs.io/en/latest/) that enables using [Pydantic](https://docs.pydantic.dev/) 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.
43
+
44
+ <!-- introduction-end -->
45
+
46
+ ## Installation
47
+
48
+ <!-- installation-start -->
49
+
50
+ `tinydantic` is [available on PyPI](https://pypi.org/project/tinydantic/) and can be installed with [pip](https://github.com/pypa/pip). Easy peasy lemon squeezy 🍋
51
+
52
+ ```sh
53
+ pip install tinydantic
54
+ ```
55
+
56
+ <!-- installation-end -->
57
+
58
+ ## License
59
+
60
+ See the [LICENSE](https://github.com/tinydantic/tinydantic/blob/main/LICENSE.md) file for copyright & license information.
@@ -21,6 +21,7 @@ classifiers = [
21
21
  "Intended Audience :: Developers",
22
22
  "Operating System :: OS Independent",
23
23
  "Programming Language :: Python :: 3",
24
+ "Programming Language :: Python :: 3.10",
24
25
  "Programming Language :: Python :: 3.11",
25
26
  "Programming Language :: Python :: 3.12",
26
27
  "Programming Language :: Python :: 3.13",
@@ -48,8 +49,8 @@ license-files = [
48
49
  ]
49
50
  name = "tinydantic"
50
51
  readme = "README.md"
51
- requires-python = ">=3.11"
52
- version = "0.2.0"
52
+ requires-python = ">=3.10"
53
+ version = "0.3.0"
53
54
 
54
55
  [project.urls]
55
56
  Changelog = "https://github.com/tinydantic/tinydantic/blob/main/CHANGELOG.md"
@@ -79,7 +80,7 @@ docs = [
79
80
  "black",
80
81
  "fastapi",
81
82
  "griffe",
82
- "httpx",
83
+ "httpx2",
83
84
  "linkchecker",
84
85
  "markdown-callouts",
85
86
  "markdown-gfm-admonition",
@@ -105,7 +106,7 @@ lint = [
105
106
  ]
106
107
  test = [
107
108
  "fastapi",
108
- "httpx",
109
+ "httpx2",
109
110
  "packaging",
110
111
  "poethepoet",
111
112
  "pytest-cov",
@@ -215,11 +216,16 @@ style = "google"
215
216
  verbose = 2
216
217
 
217
218
  [tool.mypy]
218
- python_version = "3.11"
219
+ python_version = "3.10"
219
220
 
220
221
  [tool.pytest.ini_options]
221
222
  addopts = "--doctest-modules --doctest-glob '*.md' --ignore=docs/superpowers"
222
223
  doctest_optionflags = "NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL"
224
+ filterwarnings = [
225
+ # The queries.md "shadowing" section deliberately defines a field named
226
+ # `search` to demonstrate the pitfall, so pydantic's warning is expected.
227
+ 'ignore:Field name "search" in "Command" shadows an attribute in parent "TinydanticModel":UserWarning',
228
+ ]
223
229
  minversion = "6.0"
224
230
  testpaths = [
225
231
  "CONTRIBUTING.md",
@@ -28,7 +28,6 @@ from tinydantic.tinydb.operations import replace
28
28
 
29
29
  if TYPE_CHECKING:
30
30
  from collections.abc import Callable, Iterable, Mapping
31
- from typing import Self
32
31
 
33
32
  # pydantic's ModelMetaclass lives in a private module. We
34
33
  # type-check against the real class but resolve it at runtime as
@@ -37,6 +36,11 @@ if TYPE_CHECKING:
37
36
  from pydantic._internal._model_construction import ModelMetaclass
38
37
  from tinydb import TinyDB
39
38
  from tinydb.queries import QueryLike
39
+
40
+ # Self is typing_extensions-only on Python 3.10 (typing.Self is
41
+ # 3.11+). This import is TYPE_CHECKING-only, so typing_extensions
42
+ # is not a runtime dependency.
43
+ from typing_extensions import Self
40
44
  else:
41
45
  ModelMetaclass = type(BaseModel)
42
46
 
@@ -531,9 +535,9 @@ class TinydanticModel(BaseModel, metaclass=TinydanticModelMetaclass):
531
535
 
532
536
  Uses JSON-mode serialization so rich pydantic types (datetime,
533
537
  UUID, enums, nested models, ...) become JSON-safe primitives
534
- that round-trip through any TinyDB storage (spec 3.7). The
535
- ``id`` field is never embedded in the document — it maps to
536
- TinyDB's ``doc_id``.
538
+ that round-trip through any TinyDB storage. The ``id`` field is
539
+ never embedded in the document — it maps to TinyDB's
540
+ ``doc_id``.
537
541
 
538
542
  Args:
539
543
  force_dict: Return a plain dict even when ``id`` is set
tinydantic-0.2.0/PKG-INFO DELETED
@@ -1,190 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: tinydantic
3
- Version: 0.2.0
4
- Summary: A Pydantic-powered ODM (object-document mapper) for TinyDB
5
- Keywords: odm,pydantic,tinydb
6
- Author: Chris Wilson
7
- Author-email: Chris Wilson <christopher.david.wilson@gmail.com>
8
- License-Expression: Apache-2.0 OR MIT
9
- License-File: LICENSES/Apache-2.0.txt
10
- License-File: LICENSES/CC-BY-4.0.txt
11
- License-File: LICENSES/MIT.txt
12
- Classifier: Development Status :: 3 - Alpha
13
- Classifier: Framework :: Pydantic :: 2
14
- Classifier: Intended Audience :: Developers
15
- Classifier: Operating System :: OS Independent
16
- Classifier: Programming Language :: Python :: 3
17
- Classifier: Programming Language :: Python :: 3.11
18
- Classifier: Programming Language :: Python :: 3.12
19
- Classifier: Programming Language :: Python :: 3.13
20
- Classifier: Programming Language :: Python :: 3.14
21
- Classifier: Programming Language :: Python :: Implementation :: CPython
22
- Classifier: Programming Language :: Python
23
- Classifier: Topic :: Database
24
- Classifier: Topic :: Software Development :: Libraries
25
- Classifier: Typing :: Typed
26
- Requires-Dist: pyyaml>=6.0
27
- Requires-Dist: pydantic[email]>=2.11
28
- Requires-Dist: tinydb>=4.8
29
- Requires-Python: >=3.11
30
- Project-URL: Changelog, https://github.com/tinydantic/tinydantic/blob/main/CHANGELOG.md
31
- Project-URL: Documentation, https://tinydantic.dev
32
- Project-URL: Issues, https://github.com/tinydantic/tinydantic/issues
33
- Project-URL: Source, https://github.com/tinydantic/tinydantic
34
- Description-Content-Type: text/markdown
35
-
36
- # tinydantic
37
-
38
- <!-- overview-start -->
39
-
40
- [![PyPI - Version](https://img.shields.io/pypi/v/tinydantic.svg)](https://pypi.org/project/tinydantic) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/tinydantic.svg)](https://pypi.org/project/tinydantic) [![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)
41
-
42
- `tinydantic` is a simple Python object-document mapper (ODM) for the [TinyDB](https://tinydb.readthedocs.io/en/latest/) document database. It uses [Pydantic](https://docs.pydantic.dev/latest/)—the most widely used data validation library—for document model definition and validation.
43
-
44
- <!-- prettier-ignore-start -->
45
-
46
- > [!NOTE]
47
- > `tinydantic` is under active development. Releases follow the [SemVer versioning spec](https://semver.org):
48
- >
49
- > > Major version zero (0.y.z) is for initial development. Anything MAY change at any time. The public API SHOULD NOT be considered stable.
50
- >
51
- > Minor releases may include breaking changes until v1.0 — check the [changelog](https://github.com/tinydantic/tinydantic/blob/main/CHANGELOG.md) when upgrading. Feedback is welcome!
52
-
53
- <!-- prettier-ignore-end -->
54
-
55
- <!-- overview-end -->
56
-
57
- Full documentation is available at [tinydantic.dev](https://tinydantic.dev).
58
-
59
- ## Table of Contents
60
-
61
- - [Installation](#installation)
62
- - [Introduction](#introduction)
63
- - [Basic Example](#basic-example)
64
- - [License](#license)
65
-
66
- ## Introduction
67
-
68
- <!-- introduction-start -->
69
-
70
- `tinydantic` is a wrapper library around [TinyDB](https://tinydb.readthedocs.io/en/latest/) that enables using [Pydantic](https://docs.pydantic.dev/) 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.
71
-
72
- <!-- introduction-end -->
73
-
74
- ## Installation
75
-
76
- <!-- installation-start -->
77
-
78
- `tinydantic` is [available on PyPI](https://pypi.org/project/tinydantic/) and can be installed with [pip](https://github.com/pypa/pip). Easy peasy lemon squeezy 🍋
79
-
80
- ```sh
81
- pip install tinydantic
82
- ```
83
-
84
- <!-- installation-end -->
85
-
86
- ## Basic Example
87
-
88
- <!-- basic-example-start -->
89
-
90
- Here's a basic example showing how to create, insert, and query for a document using `tinydantic`.
91
-
92
- ### Using `tinydantic` models
93
-
94
- 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](https://tinydb.readthedocs.io/en/latest/usage.html#storage-types) which can store the database persistently as JSON, YAML, etc.
95
-
96
- ```pycon
97
- >>> from tinydb import TinyDB
98
- >>> from tinydb.storages import MemoryStorage
99
- >>> db = TinyDB(storage=MemoryStorage)
100
-
101
- ```
102
-
103
- Create a `User` document model, configuring it to store documents in the `users` table of the `db` database.
104
-
105
- > [!TIP]
106
- >
107
- > 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.
108
-
109
- ```pycon
110
- >>> from pydantic import EmailStr
111
- >>> from tinydantic import TinydanticModel
112
- >>> class User(TinydanticModel, database=db, table_name='users'):
113
- ... name: str
114
- ... email: EmailStr
115
-
116
- ```
117
-
118
- Create a new `User` instance for a user named "Alice".
119
-
120
- ```pycon
121
- >>> alice = User(name='Alice', email='alice@example.com')
122
-
123
- ```
124
-
125
- Insert `alice` into the database.
126
-
127
- ```pycon
128
- >>> alice.insert()
129
- User(id=1, name='Alice', email='alice@example.com')
130
-
131
- ```
132
-
133
- Query the database for users with the name "Alice". Notice that this returns a `User` instance that was automatically validated by Pydantic!
134
-
135
- ```pycon
136
- >>> User.get(User.name == 'Alice')
137
- User(id=1, name='Alice', email='alice@example.com')
138
-
139
- ```
140
-
141
- ### Comparison to TinyDB
142
-
143
- Since `tinydantic` is built on top of TinyDB, you can still use `tinydb` to interact with the database directly if needed.
144
-
145
- 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.
146
-
147
- ```pycon
148
- >>> users_table = db.table('users')
149
- >>> users_table.insert({'name': 'Bob', 'email': 'bob@example.com'})
150
- 2
151
- >>> from tinydb import where
152
- >>> users_table.get(where('name') == 'Bob')
153
- {'name': 'Bob', 'email': 'bob@example.com'}
154
-
155
- ```
156
-
157
- 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.
158
-
159
- ### Pydantic validation in action
160
-
161
- _What happens if an invalid document is somehow returned from the database?_
162
-
163
- Let's set up a contrived example to test the automatic Pydantic validation.
164
-
165
- First, we'll manually insert a document that is missing the `email` field from the `User` model we created earlier.
166
-
167
- ```pycon
168
- >>> users_table.insert({'name': 'Carol'})
169
- 3
170
-
171
- ```
172
-
173
- Next, we'll query for users named "Bob" using the tinydantic model we created earlier.
174
-
175
- ```pycon
176
- >>> User.get(User.name == 'Carol')
177
- Traceback (most recent call last):
178
- ...
179
- pydantic_core._pydantic_core.ValidationError: 1 validation error for User
180
- email
181
- Field required [type=missing, input_value={'name': 'Carol'}, input_type=Document]
182
- ```
183
-
184
- 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.
185
-
186
- <!-- basic-example-end -->
187
-
188
- ## License
189
-
190
- See the [LICENSE](https://github.com/tinydantic/tinydantic/blob/main/LICENSE.md) file for copyright & license information.
@@ -1,155 +0,0 @@
1
- # tinydantic
2
-
3
- <!-- overview-start -->
4
-
5
- [![PyPI - Version](https://img.shields.io/pypi/v/tinydantic.svg)](https://pypi.org/project/tinydantic) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/tinydantic.svg)](https://pypi.org/project/tinydantic) [![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)
6
-
7
- `tinydantic` is a simple Python object-document mapper (ODM) for the [TinyDB](https://tinydb.readthedocs.io/en/latest/) document database. It uses [Pydantic](https://docs.pydantic.dev/latest/)—the most widely used data validation library—for document model definition and validation.
8
-
9
- <!-- prettier-ignore-start -->
10
-
11
- > [!NOTE]
12
- > `tinydantic` is under active development. Releases follow the [SemVer versioning spec](https://semver.org):
13
- >
14
- > > Major version zero (0.y.z) is for initial development. Anything MAY change at any time. The public API SHOULD NOT be considered stable.
15
- >
16
- > Minor releases may include breaking changes until v1.0 — check the [changelog](https://github.com/tinydantic/tinydantic/blob/main/CHANGELOG.md) when upgrading. Feedback is welcome!
17
-
18
- <!-- prettier-ignore-end -->
19
-
20
- <!-- overview-end -->
21
-
22
- Full documentation is available at [tinydantic.dev](https://tinydantic.dev).
23
-
24
- ## Table of Contents
25
-
26
- - [Installation](#installation)
27
- - [Introduction](#introduction)
28
- - [Basic Example](#basic-example)
29
- - [License](#license)
30
-
31
- ## Introduction
32
-
33
- <!-- introduction-start -->
34
-
35
- `tinydantic` is a wrapper library around [TinyDB](https://tinydb.readthedocs.io/en/latest/) that enables using [Pydantic](https://docs.pydantic.dev/) 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.
36
-
37
- <!-- introduction-end -->
38
-
39
- ## Installation
40
-
41
- <!-- installation-start -->
42
-
43
- `tinydantic` is [available on PyPI](https://pypi.org/project/tinydantic/) and can be installed with [pip](https://github.com/pypa/pip). Easy peasy lemon squeezy 🍋
44
-
45
- ```sh
46
- pip install tinydantic
47
- ```
48
-
49
- <!-- installation-end -->
50
-
51
- ## Basic Example
52
-
53
- <!-- basic-example-start -->
54
-
55
- Here's a basic example showing how to create, insert, and query for a document using `tinydantic`.
56
-
57
- ### Using `tinydantic` models
58
-
59
- 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](https://tinydb.readthedocs.io/en/latest/usage.html#storage-types) which can store the database persistently as JSON, YAML, etc.
60
-
61
- ```pycon
62
- >>> from tinydb import TinyDB
63
- >>> from tinydb.storages import MemoryStorage
64
- >>> db = TinyDB(storage=MemoryStorage)
65
-
66
- ```
67
-
68
- Create a `User` document model, configuring it to store documents in the `users` table of the `db` database.
69
-
70
- > [!TIP]
71
- >
72
- > 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.
73
-
74
- ```pycon
75
- >>> from pydantic import EmailStr
76
- >>> from tinydantic import TinydanticModel
77
- >>> class User(TinydanticModel, database=db, table_name='users'):
78
- ... name: str
79
- ... email: EmailStr
80
-
81
- ```
82
-
83
- Create a new `User` instance for a user named "Alice".
84
-
85
- ```pycon
86
- >>> alice = User(name='Alice', email='alice@example.com')
87
-
88
- ```
89
-
90
- Insert `alice` into the database.
91
-
92
- ```pycon
93
- >>> alice.insert()
94
- User(id=1, name='Alice', email='alice@example.com')
95
-
96
- ```
97
-
98
- Query the database for users with the name "Alice". Notice that this returns a `User` instance that was automatically validated by Pydantic!
99
-
100
- ```pycon
101
- >>> User.get(User.name == 'Alice')
102
- User(id=1, name='Alice', email='alice@example.com')
103
-
104
- ```
105
-
106
- ### Comparison to TinyDB
107
-
108
- Since `tinydantic` is built on top of TinyDB, you can still use `tinydb` to interact with the database directly if needed.
109
-
110
- 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.
111
-
112
- ```pycon
113
- >>> users_table = db.table('users')
114
- >>> users_table.insert({'name': 'Bob', 'email': 'bob@example.com'})
115
- 2
116
- >>> from tinydb import where
117
- >>> users_table.get(where('name') == 'Bob')
118
- {'name': 'Bob', 'email': 'bob@example.com'}
119
-
120
- ```
121
-
122
- 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.
123
-
124
- ### Pydantic validation in action
125
-
126
- _What happens if an invalid document is somehow returned from the database?_
127
-
128
- Let's set up a contrived example to test the automatic Pydantic validation.
129
-
130
- First, we'll manually insert a document that is missing the `email` field from the `User` model we created earlier.
131
-
132
- ```pycon
133
- >>> users_table.insert({'name': 'Carol'})
134
- 3
135
-
136
- ```
137
-
138
- Next, we'll query for users named "Bob" using the tinydantic model we created earlier.
139
-
140
- ```pycon
141
- >>> User.get(User.name == 'Carol')
142
- Traceback (most recent call last):
143
- ...
144
- pydantic_core._pydantic_core.ValidationError: 1 validation error for User
145
- email
146
- Field required [type=missing, input_value={'name': 'Carol'}, input_type=Document]
147
- ```
148
-
149
- 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.
150
-
151
- <!-- basic-example-end -->
152
-
153
- ## License
154
-
155
- See the [LICENSE](https://github.com/tinydantic/tinydantic/blob/main/LICENSE.md) file for copyright & license information.
File without changes