Giter Club home page Giter Club logo

wonderbeyond / sqlalchemy-nested-mutable Goto Github PK

View Code? Open in Web Editor NEW
15.0 1.0 7.0 32 KB

An advanced SQLAlchemy column type factory that helps map complex Python types (e.g. List, Dict, Pydantic Model and their hybrids) to database types (e.g. ARRAY, JSONB), And keep track of mutations in deeply nested data structures so that SQLAlchemy can emit proper UPDATE statements.

License: MIT License

Python 100.00%
database jsonb orm pydantic python sqlalchemy

sqlalchemy-nested-mutable's Introduction

SQLAlchemy-Nested-Mutable

An advanced SQLAlchemy column type factory that helps map compound Python types (e.g. list, dict, Pydantic Model and their hybrids) to database types (e.g. ARRAY, JSONB), And keep track of mutations in deeply nested data structures so that SQLAlchemy can emit proper UPDATE statements.

SQLAlchemy-Nested-Mutable is highly inspired by SQLAlchemy-JSON[0][1]. However, it does not limit the mapped Python type to be dict or list.


Why this package?

  • By default, SQLAlchemy does not track in-place mutations for non-scalar data types such as list and dict (which are usually mapped with ARRAY and JSON/JSONB).

  • Even though SQLAlchemy provides an extension to track mutations on compound objects, it's too shallow, i.e. it only tracks mutations on the first level of the compound object.

  • There exists the SQLAlchemy-JSON package to help track mutations on nested dict or list data structures. However, the db type is limited to JSON(B).

  • Also, I would like the mapped Python types can be subclasses of the Pydantic BaseModelModel, which have strong schemas, with the db type be schema-less JSON.

Installation

pip install sqlalchemy-nested-mutable

Usage

NOTE the example below is first updated in examples/user-addresses.py and then updated here.

from typing import Optional, List

import pydantic
import sqlalchemy as sa
from sqlalchemy.orm import Session, DeclarativeBase, Mapped, mapped_column
from sqlalchemy_nested_mutable import MutablePydanticBaseModel


class Base(DeclarativeBase):
    pass


class Addresses(MutablePydanticBaseModel):
    """A container for storing various addresses of users.

    NOTE: for working with pydantic model, use a subclass of `MutablePydanticBaseModel` for column mapping.
    However, the nested models (e.g. `AddressItem` below) should be direct subclasses of `pydantic.BaseModel`.
    """

    class AddressItem(pydantic.BaseModel):
        street: str
        city: str
        area: Optional[str]

    preferred: AddressItem
    work: Optional[AddressItem]
    home: Optional[AddressItem]
    others: List[AddressItem] = []


class User(Base):
    __tablename__ = "user_account"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(sa.String(30))
    addresses: Mapped[Addresses] = mapped_column(Addresses.as_mutable(), nullable=True)


engine = sa.create_engine("sqlite://")
Base.metadata.create_all(engine)

with Session(engine) as s:
    s.add(u := User(name="foo", addresses={"preferred": {"street": "bar", "city": "baz"}}))
    assert isinstance(u.addresses, MutablePydanticBaseModel)
    s.commit()

    u.addresses.preferred.street = "bar2"
    s.commit()
    assert u.addresses.preferred.street == "bar2"

    u.addresses.others.append(Addresses.AddressItem.parse_obj({"street": "bar3", "city": "baz3"}))
    s.commit()
    assert isinstance(u.addresses.others[0], Addresses.AddressItem)

    print(u.addresses.dict())

For more usage, please refer to the following test files:

  • tests/test_mutable_list.py
  • tests/test_mutable_dict.py
  • tests/test_mutable_pydantic_type.py

sqlalchemy-nested-mutable's People

Contributors

atugushev avatar wonderbeyond avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

sqlalchemy-nested-mutable's Issues

Is it possible provide an option to use either JSON or JSONB?

Hi,
Thanks for this library which gives clean examples with SQLAlchemy 2.0 mapped_column syntax. Looks like for Postgresql, Pydantic type defaults to JSONB. Is it possible to give the user an option to choose between two, similar to SQLAlchemy?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.