SQL library agnostic data model framework
Project description
pydantic-db aims to be a database framework agnostic modeling library. Providing functionality to convert database result object(s) into pydantic model(s). The aim is not to provide an ORM, but to target users who prefer raw sql interactions over obfuscated ORM object built queries layers.
For those who prefer libraries like pypika to build their queries, this library can still provide a nice layer between raw query results and database models.
So long as the database library you are using returns result objects that can be converted to a dictionary, pydantic-db will ineract cleanly with your results. See unittests for examples with asyncpg, mysql-connector-python, psycopg2 and sqlite3.
Usage
All examples assumes the existence of underlying tables and data, they are not intended to run as is.
from_result
To convert a single result object into a model, use Model.from_result.
import sqlite3
from pydantic_db import Model
class User(Model):
id: int
name: str
db = sqlite3.connect(":memory:")
db.row_factory = sqlite3.Row
stmt = "SELECT * FROM my_user LIMIT 1"
cursor.execute(stmt)
r = cursor.fetchone()
user = User.from_result(r)
from_results
To convert a list of result objects into models, use Model.from_results.
import sqlite3
from pydantic_db import Model
class User(Model):
id: int
name: str
db = sqlite3.connect(":memory:")
db.row_factory = sqlite3.Row
stmt = "SELECT * FROM my_user"
cursor.execute(stmt)
results = cursor.fetchall()
users = User.from_results(results)
Nested models
For more complicated queries returning a nested object, models can be nested. To
parse them automatically prefix query fields with name__ format prefixes.
Say we have a Vehicle table with a reference to an owner (User).
import sqlite3
from pydantic_db import Model
class User(Model):
id: int
name: str
class Vehicle(Model):
id: int
name: str
owner: User
db = sqlite3.connect(":memory:")
db.row_factory = sqlite3.Row
stmt = """
SELECT
v.id,
v.name,
u.id AS owner__id,
u.name AS owner__name
FROM my_vehicle v
JOIN my_user u ON v.owner_id = u.id
"""
cursor.execute(stmt)
results = cursor.fetchall()
vehicles = Vehicle.from_results(results)
Optional nested models
When a nested model is optional i.e. user: User | None the library will check
if there is an id field by default, and if that field is empty (None), it
will nullify that field.
If your nested model contains a differently named primary key or some other
field that can be relied on to detect that a query has not successfully joined,
and so the nested model should be None. Override the _skip_prefix_fields class var.
class User(Model):
primary_key: int
name: str
class Vehicle(Model):
_skip_prefix_fields = {"owner": "primary_key"}
id: int
name: str
owner: User | None
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 pydantic_db-0.2.0.tar.gz.
File metadata
- Download URL: pydantic_db-0.2.0.tar.gz
- Upload date:
- Size: 56.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0d9066029182fc7c0920f6b673c00afcce64b17f8a3e03d959aad67223f5572
|
|
| MD5 |
0ec99230f446b02a14b3f9431e0ec813
|
|
| BLAKE2b-256 |
66d114fcb8ec7b3511f46f00df0b0d4e3cb08c6cc5aba0dff232526248cac6b9
|
File details
Details for the file pydantic_db-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pydantic_db-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6bbb573193304e8510f253b92158591a4d1b32894fe1416a2d99ac37b86381c
|
|
| MD5 |
29330c63258b2a48ae12bb03e6c9d7c6
|
|
| BLAKE2b-256 |
3d26baec6367510a1fdbd59162fbfa0683fdb04903ab19a888402e5c27a7e815
|