Skip to main content

Automatic Creation of ORM Models from Python Dataclasses.

Project description

from dataclasses import is_dataclass

Welcome to ORMatic

ORMatic is a python package that automatically converts python dataclasses to sqlalchemy tables. This is done using the declarative mapping. The package outputs a file that can be used as an SQLAlchemy interface.

When designing the dataclasses that should be mapped there are a couple of rules that need to be followed:

  • Fields that are not mapped start with an _ (underscore).
  • The only allowed union is the Optional[_T] union. Whenever you want a union of other types, use common superclasses as type instead.
  • Iterables are never optional and never nested. If you want an optional iterable, use an empty iterable as default factory instead.
  • Superclasses that are not the first mentioned superclass are not queryable via abstract queries. (Polymorphic identity)

If your dataclasses are not compatible with this pattern, there are two workarounds, the Alternative Mapping and the Type Decorator.

Features:

  • Automatic conversion of dataclasses to sqlalchemy tables.

  • Automatic application of relationships.

  • Automatic generation of ORM interface.

  • ORM interface never affects your existing code.

  • Support for inheritance.

  • Support for optional fields.

  • Support for nested dataclasses.

  • Support for many-many relationships.

  • Support for self-referencing relationships.

Example

The most common use case is to create an ORM for an existing set of dataclasses. An example for such a set of dataclasses is found in example.py. The automatically generated ORM interface is found in sqlalchemy_interface.py. Example usage of the ORM interface is found in integration.py.

The following script generates the bindings in sqlalchemy_interface.py.

from enum import Enum
import test.classes.example_classes
from ormatic.ormatic import ORMatic
from ormatic.dao import AlternativeMapping
from ormatic.utils import recursive_subclasses, classes_of_module
from dataclasses import is_dataclass


def main():
    
    # get classes that should be mapped
    classes = set(recursive_subclasses(AlternativeMapping))
    classes |= set(classes_of_module(test.classes.example_classes))
    
    # remove classes that should not be mapped
    classes -= set(recursive_subclasses(Enum))
    classes -= set([cls for cls in classes if not is_dataclass(cls)])
    
    ormatic = ORMatic(classes)
    ormatic.make_all_tables()

    with open('orm_interface.py', 'w') as f:
        ormatic.to_sqlalchemy_file(f)

        
if __name__ == '__main__':
    main()

TODO List

  • Fields that are typed as Sequence/Iterable should be stored as list
  • Check deep inheritance

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

ormatic-1.1.2.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

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

ormatic-1.1.2-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file ormatic-1.1.2.tar.gz.

File metadata

  • Download URL: ormatic-1.1.2.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ormatic-1.1.2.tar.gz
Algorithm Hash digest
SHA256 0e8186b2723ad7a61f1557f21a57e0e7cd2d99d611c650dbc31142ebd549a25e
MD5 3e99fd97fa0c0db9bdb5557ff23817e7
BLAKE2b-256 b52cdb59abef23a3a08c6e1095af29dd6d920a15512f71c38c3732f239fb3530

See more details on using hashes here.

Provenance

The following attestation bundles were made for ormatic-1.1.2.tar.gz:

Publisher: publish-to-pypi.yml on tomsch420/ormatic

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

File details

Details for the file ormatic-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: ormatic-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ormatic-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1da74e9952af00b9782bd9f9fd88efde7d4db602d98bcfe0dde48f9dfb5cad51
MD5 7fb8a20e96506fdbf2ed9d07a2e89520
BLAKE2b-256 e30d51b526a2a767a83749aca4e5f0ee424390e265cfb81d4020ad09292a476a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ormatic-1.1.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on tomsch420/ormatic

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