Skip to main content

The simplest way to implement autogenerated strawberry types for columns and relationships in SQLAlchemy models

Project description

strawberry-sqlalchemy-mapper

Strawberry-sqlalchemy-mapper is the simplest way to implement autogenerated strawberry types for columns and relationships in SQLAlchemy models.

  • Instead of manually listing every column and relationship in a SQLAlchemy model, strawberry-sqlalchemy-mapper lets you decorate a class declaration and it will automatically generate the necessary strawberry fields for all columns and relationships in the given model.

  • Native support for most of SQLAlchemy's most common types.

  • Extensible to arbitrary custom SQLAlchemy types.

  • Support for SQLAlchemy >=1.4.x

  • Lightweight and fast.

Getting Started

First, define your sqlalchemy model:

# models.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Employee(Base):
    __tablename__ = 'employee'
    id = Column(UUID, primary_key=True)
    name = Column(String, nullable=False)

Next, decorate a type with strawberry_sqlalchemy_mapper.type() to register it as a strawberry type for the given SQLAlchemy model. This will automatically add fields for the model's columns, relationships, association proxies, and hybrid properties. For example:

# elsewhere
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
@strawberry_sqlalchemy_mapper.type(models.Employee)
class Employee:
    pass

Several examples to help you get started are provided in the examples directory.

Limitations

Natively supports the following SQLAlchemy types:

Integer: int,
Float: float,
BigInteger: int,
Numeric: Decimal,
DateTime: datetime,
Date: date,
Time: time,
String: str,
Text: str,
Boolean: bool,
Unicode: str,
UnicodeText: str,
SmallInteger: int,
SQLAlchemyUUID: uuid.UUID,
VARCHAR: str,
ARRAY[T]: List[T] # PostgreSQL array
Enum: (the Python enum it is mapped to, which should be @strawberry.enum-decorated)

Additional types can be supported by passing extra_sqlalchemy_type_to_strawberry_type_map, although support for TypeDecorator types is untested.

Association proxies are expected to be of the form association_proxy('relationship1', 'relationship2'), i.e., both properties are expected to be relationships.

Contributing

We encourage you to contribute to strawberry-sqlalchemy-mapper! Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature)
  3. Commit your Changes (git commit -m 'Add some feature')
  4. Push to the Branch (git push origin feature)
  5. Open a Pull Request

Prerequisites

This project uses pre-commit_, please make sure to install it before making any changes::

pip install pre-commit
cd strawberry-sqlalchemy-mapper
pre-commit install

It is a good idea to update the hooks to the latest version::

pre-commit autoupdate

Don't forget to tell your contributors to also install and use pre-commit.

Installation

pip install -r requirements.txt

Test

pytest

⚖️ LICENSE

MIT © strawberry-sqlalchemy-mapper

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

strawberry-sqlalchemy-mapper-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

File details

Details for the file strawberry-sqlalchemy-mapper-0.1.0.tar.gz.

File metadata

  • Download URL: strawberry-sqlalchemy-mapper-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for strawberry-sqlalchemy-mapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf35299a768f68235b5b5ff2e5158b5f1d8cb0c326ab0824d3194652580b9061
MD5 61fa0cb0016a9ff3290c5032c1e1ea65
BLAKE2b-256 b407d2e3c5680c460bc237e360e3bd79f144f6a4360b0bee4ab4645ecca23621

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page