Skip to main content

Database migration tool for asyncpg

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 framework you are using returns result objects that can be converted to a dictionary, pydantic-db will ineract cleanly with your results.

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)

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

pydantic_db-0.1.0.tar.gz (54.5 kB view details)

Uploaded Source

Built Distribution

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

pydantic_db-0.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_db-0.1.0.tar.gz.

File metadata

  • Download URL: pydantic_db-0.1.0.tar.gz
  • Upload date:
  • Size: 54.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for pydantic_db-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e571e3a42a40e91223fe1c5aa6218f150a27de3e1f5cbf83c8e2cd5c80075978
MD5 640f2d406446f6ca5b1f8bd6a8546124
BLAKE2b-256 0f4f04ee009ce1af1d0d7cccec9a6d459aa8ae28adcaac8a9d1c08a860a9c07d

See more details on using hashes here.

File details

Details for the file pydantic_db-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_db-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for pydantic_db-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53cdc0baca57d3f5b5b4e4d186d80e89d32cf64e2d9848be576daf8596d1e2d3
MD5 a9baa5c8ea401c782c6b874302c4f47d
BLAKE2b-256 e5f2afccfa332c5fc51a7da11cacca04682bdfff787b2a35a044f353d9494159

See more details on using hashes here.

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