jdkandersson / OpenAlchemy

Define SQLAlchemy models using the OpenAPI specification.
https://openapi-sqlalchemy.readthedocs.io/en/latest/
Apache License 2.0
47 stars 13 forks source link
openapi openapi-specification openapi3 python python3 sqlalchemy sqlalchemy-models sqlalchemy-python

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.

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 _modelsfilename 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

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