Skip to main content

OpenAlchemy

Code Quality Status Azure DevOps coverage Documentation Status Code Climate maintainability Code Climate technical debt LGTM Grade

Translates an OpenAPI schema to SQLAlchemy models.

Supports OpenAPI 3.0 and 3.1.

Get started with the online editor that will guide you through using your existing OpenAPI specification to define your database schema and offers installing your models using pip: Online Editor

Installation

python -m pip install OpenAlchemy
# To be able to load YAML file
python -m pip install OpenAlchemy[yaml]

Example

For example, given the following OpenAPI specification:

# ./examples/simple/example-spec.yml
openapi: "3.0.0"

info:
  title: Test Schema
  description: API to illustrate OpenAlchemy MVP.
  version: "0.1"

paths:
  /employee:
    get:
      summary: Used to retrieve all employees.
      responses:
        200:
          description: Return all employees from the database.
          content:
            application/json:
              schema:
                type: array
                items:
                  "$ref": "#/components/schemas/Employee"

components:
  schemas:
    Employee:
      description: Person that works for a company.
      type: object
      x-tablename: employee
      properties:
        id:
          type: integer
          description: Unique identifier for the employee.
          example: 0
          x-primary-key: true
          x-autoincrement: true
        name:
          type: string
          description: The name of the employee.
          example: David Andersson
          x-index: true
        division:
          type: string
          description: The part of the company the employee works in.
          example: Engineering
          x-index: true
        salary:
          type: number
          description: The amount of money the employee is paid.
          example: 1000000.00
      required:
        - id
        - name
        - division

The SQLALchemy models file then becomes:

# models.py
from open_alchemy import init_yaml

init_yaml("./examples/simple/example-spec.yml")

The Base and Employee objects can be accessed:

from open_alchemy.models import Base
from open_alchemy.models import Employee

With the models_filename parameter a file is auto generated with type hints for the SQLAlchemy models at the specified location, for example: type hinted models example. This adds support for IDE auto complete, for example for the model initialization:

autocomplete init

and for properties and methods available on an instance:

autocomplete instance

An extensive set of examples with a range of features is here:

examples for main features

An example API has been defined using connexion and Flask here:

example connexion app

Documentation

Read the Docs

Buy me a coffee

Buy Me A Coffee

Features

  • initializing from JSON,
  • initializing from YAML,
  • build a package with the models for distribution, packaged as sdist or wheel,
  • automatically generate a models file,
  • integer (32 and 64 bit),
  • number (float only),
  • boolean,
  • string,
  • password,
  • byte,
  • binary,
  • date,
  • date-time,
  • generic JSON data,
  • $ref references for columns and models,
  • remote $ref to other files on the same file system (not supported on Windows),
  • remote $ref to other files at a URL,
  • primary keys,
  • auto incrementing,
  • indexes,
  • composite indexes,
  • unique constraints,
  • composite unique constraints,
  • column nullability,
  • foreign keys,
  • default values for columns (both application and database side),
  • many to one relationships,
  • one to one relationships,
  • one to many relationships,
  • many to many relationships,
  • many to many relationships with custom association tables,
  • custom foreign keys for relationships,
  • back references for relationships,
  • allOf inheritance for columns and models,
  • joined and single table inheritance,
  • from_str model methods to construct from JSON string,
  • from_dict model methods to construct from dictionaries,
  • to_str model methods to convert instances to JSON string,
  • __str__ model methods to support the python str function,
  • __repr__ model methods to support the python repr function,
  • to_dict model methods to convert instances to dictionaries,
  • readOnly and writeOnly for influence the conversion to and from dictionaries,
  • exposing created models under open_alchemy.models removing the need for models.py files,
  • ability to mix in arbitrary classes into a model,
  • can use the short x- prefix or a namespaced x-open-alchemy- prefix for extension properties and
  • grouping models into schemas.

Contributing

Fork and checkout the repository. To install:

poetry install

To run tests:

poetry run pytest

Make your changes and raise a pull request.

Compiling Docs

poetry shell
cd docs
make html

This creates the index.html file in docs/build/html/index.html.

Release Commands

rm -r dist/*
poetry build
poetry publish

Metadata

Release files for openalchemy 2.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openalchemy 2.5.0
File Size Uploaded
OpenAlchemy-2.5.0.tar.gz 89.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openalchemy 2.5.0
File Interpreter ABI Platform
OpenAlchemy-2.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 221.5 kB

Release files / OpenAlchemy-2.5.0.tar.gz

Download URL OpenAlchemy-2.5.0.tar.gz
Size 89.4 kB
Tags Source
SHA-256 checksum
How to use checksums
23aee416e86e90ff77e122fa0999261f35628643af2955f9d89fec4b62875525
BLAKE2b-256 checksum
How to use checksums
ac2020530bc445d1fbbed62bfa8fdd1a762665c0c5fe40bf4a02b00b799adc6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release files / OpenAlchemy-2.5.0-py3-none-any.whl

Download URL OpenAlchemy-2.5.0-py3-none-any.whl
Size 132.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
108a4adc58c681bcadd1e022ef1e3e96f75610f59a6c476966f8d2b270c14776
BLAKE2b-256 checksum
How to use checksums
0001c0d3b542baa6b153bb42006c8aabf28d1e29a08a3bc38dfcd806ff2492d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release history Release notifications | RSS feed

This release

2.5.0 This release

2 release files

2.4.2

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.6.0

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.1

2 release files

0.11.0

2 release files

0.10.4

2 release files

0.10.3

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page