Skip to main content

Automatic Creation of ORM Models from Python Dataclasses.

Project description

Welcome to ORMatic

ORMatic is a python package that automatically converts python dataclasses to sqlalchemy tables. This is done using the imperative mapping.

When designing the dataclasses 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 inheritance instead.
  • Iterables are never optional and never nested. If you want an optional iterable, use an empty iterable as default factory instead.
  • No multiple inheritance.
  • No forward referenced classes.

Features:

  • Automatic conversion of dataclasses to sqlalchemy tables.

  • Automatic application of relationships.

  • Automatic generation of ORM interface.

  • ORM interface only affects your code if it is imported.

  • 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 orm_interface.py. Example usage of the ORM interface is found in integration.py.

The following script generates the bindings in orm_interface.py.

import sqlacodegen.generators
from sqlalchemy import create_engine
from sqlalchemy.orm import registry, Session

from example import *
from ormatic.ormatic import ORMatic


def main():
    mapper_registry = registry()

    engine = create_engine('sqlite:///:memory:')

    classes = [Position, Orientation, Pose, Position4D, Positions, EnumContainer, Node]
    ormatic = ORMatic(classes, mapper_registry)
    ormatic.make_all_tables()
    mapper_registry.metadata.create_all(engine)

    generator = sqlacodegen.generators.TablesGenerator(mapper_registry.metadata, engine, [])

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


if __name__ == '__main__':
    main()

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.0.9.tar.gz (17.0 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.0.9-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ormatic-1.0.9.tar.gz
Algorithm Hash digest
SHA256 f91a475adb28a16fc6b39ba2108e1d016e75cb75a0df0d12efa199ff0c361fa2
MD5 622f97dd3672fc26e91e6dc7584b50ec
BLAKE2b-256 abd3d77d1c49c87ce8954c203cbb58f334b6aa954a62fc00ff007403add92517

See more details on using hashes here.

Provenance

The following attestation bundles were made for ormatic-1.0.9.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.0.9-py3-none-any.whl.

File metadata

  • Download URL: ormatic-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 10.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.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 7f8c9d981ab35bc4b4d4e79ad38afdd724df955e1524f96fa1eafff0438bfa10
MD5 988ef49520b6d0467e05a1d3de9d6ee0
BLAKE2b-256 6c2f09070bb9aa09b91c526060f78da743f2bfe0bfc68b066a7dd48f0cd5c3b4

See more details on using hashes here.

Provenance

The following attestation bundles were made for ormatic-1.0.9-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