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 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
Nothing
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 ormatic-1.1.12.tar.gz.
File metadata
- Download URL: ormatic-1.1.12.tar.gz
- Upload date:
- Size: 26.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
758dc9b60e208dcdc828a21ae8124f191367f180eff962bda44957e383536099
|
|
| MD5 |
6b31229329183fc5a4c0b0349b82cb29
|
|
| BLAKE2b-256 |
0bc4b32ba02a4676ec2fa475eafa22017f26e7a123d72588b7dcd4892226c8da
|
Provenance
The following attestation bundles were made for ormatic-1.1.12.tar.gz:
Publisher:
publish-to-pypi.yml on tomsch420/ormatic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ormatic-1.1.12.tar.gz -
Subject digest:
758dc9b60e208dcdc828a21ae8124f191367f180eff962bda44957e383536099 - Sigstore transparency entry: 380160754
- Sigstore integration time:
-
Permalink:
tomsch420/ormatic@354177dcc7ecf04e73e3951ba59fd1d8d6986ef0 -
Branch / Tag:
refs/tags/v1.1.12 - Owner: https://github.com/tomsch420
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@354177dcc7ecf04e73e3951ba59fd1d8d6986ef0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ormatic-1.1.12-py3-none-any.whl.
File metadata
- Download URL: ormatic-1.1.12-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a4d7748340ad82aab67331f4d6f3dc8ba9d8881a2b800ecf00a255fcae21261
|
|
| MD5 |
1cd3f571795692a3404fea8d2957fe27
|
|
| BLAKE2b-256 |
2a23c54202129c53925bf875c25d307ec5eaf4b42930f5d95280eda170324907
|
Provenance
The following attestation bundles were made for ormatic-1.1.12-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on tomsch420/ormatic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ormatic-1.1.12-py3-none-any.whl -
Subject digest:
6a4d7748340ad82aab67331f4d6f3dc8ba9d8881a2b800ecf00a255fcae21261 - Sigstore transparency entry: 380160788
- Sigstore integration time:
-
Permalink:
tomsch420/ormatic@354177dcc7ecf04e73e3951ba59fd1d8d6986ef0 -
Branch / Tag:
refs/tags/v1.1.12 - Owner: https://github.com/tomsch420
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@354177dcc7ecf04e73e3951ba59fd1d8d6986ef0 -
Trigger Event:
push
-
Statement type: